-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
51 lines (36 loc) · 1.44 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import io
import logging as log
import os
import cv2 as cv
import numpy as np
from aiogram import Bot, Dispatcher, executor, types
import models
TOKEN = os.environ.get('AGE_RECOGNITION_BOT_TOKEN')
log.basicConfig(
level=log.INFO,
format='[%(levelname)s] %(message)s',
handlers=[log.FileHandler('log.txt', encoding='utf-8'), log.StreamHandler()],
)
bot = Bot(TOKEN)
dp = Dispatcher(bot)
@dp.message_handler(commands=['start', 'help'])
async def info(message: types.Message):
await message.reply("Hi!\nI'm AgeRecognitionBot!\nPowered by aiogram and OpenVINO.")
@dp.message_handler()
async def handle_text(message: types.Message):
log.info(f'{message.date}, {message.from_user.username}, {message.from_user.full_name}, text = {message.text}')
await message.answer('Send me a photo with a face.')
@dp.message_handler(content_types=['photo'])
async def handle_photo(message: types.Message):
log.info(f'{message.date}, {message.from_user.username}, {message.from_user.full_name}, photo')
image = await get_image(message.photo[-1])
models.predict_and_answer(image, message.chat.id)
async def get_image(photo: types.PhotoSize):
image_bytes = io.BytesIO()
await photo.download(destination_file=image_bytes)
image = np.array(bytearray(image_bytes.getvalue()), np.uint8)
image = cv.imdecode(image, cv.IMREAD_COLOR)
image_bytes.close()
return image
if __name__ == '__main__':
executor.start_polling(dp)