Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2
- import face_recognition
- import os
- import time
- import telegram
- from telegram.ext import Updater
- from telegram.ext import CommandHandler
- from telegram.ext import MessageHandler, Filters
- from deepface import DeepFace
- os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
- secret = 'heregoesmytoken'
- bot = telegram.Bot(token=secret)
- startt = time.time()
- #model = load_model("model_v6_23.hdf5")
- print('[BOT] ' + str(bot.get_me()))
- updater = Updater(token=secret, use_context=True)
- dispatcher = updater.dispatcher
- def start(update, context):
- context.bot.send_message(chat_id=update.effective_chat.id, text="Im functioning well!")
- def unknown(update, context):
- context.bot.send_message(chat_id=update.effective_chat.id, text="Sorry, I didn't understand that command.")
- def filedownloader(update, context):
- file = bot.getFile(update.message.photo[-1].file_id)
- file.download('./dwnimages/face.jpg')
- context.bot.send_message(chat_id=update.effective_chat.id, text="Processing picture...")
- try:
- face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
- img = cv2.imread("dwnimages/face.jpg")
- gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
- faces = face_cascade.detectMultiScale(gray, 1.1, 4)
- print('[INFO] FACES: ' + str(len(faces)))
- if len(faces) == 0:
- bot.send_message(chat_id=update.effective_chat.id, text='I could not find any faces :/')
- elif len(faces) == 1:
- # START DRAW
- try:
- for (x, y, w, h) in faces:
- cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
- cv2.imwrite('imgsend/face.png', img)
- bot.send_photo(chat_id=update.effective_chat.id, photo=open('imgsend/face.png', 'rb'))
- str1 = "I guess this picture contains 1 face!".format(len(faces))
- bot.send_message(chat_id=update.effective_chat.id, text=str1)
- except Exception as e:
- print(str(e) + ' [FACE DETECTION (1)]')
- bot.send_message(chat_id=update.effective_chat.id, text='Something went wrong :/ [ERROR 1]')
- # END DRAW
- # START EMOTION
- try:
- faceinfo = DeepFace.analyze("images/happy.jpeg")
- print('[INFO] Emotion:', faceinfo["dominant_emotion"])
- print('[INFO] Age:', faceinfo["age"])
- print('[INFO] Gender:', faceinfo["gender"])
- print('[INFO] Race:', faceinfo["dominant_race"])
- except Exception as e:
- print(str(e) + ' [FACE EMOTION]')
- bot.send_message(chat_id=update.effective_chat.id, text='Something went wrong :/ [ERROR 2]')
- #END EMOTION
- #START RECOGNITION
- try:
- picture_of_me = face_recognition.load_image_file("images/me.jpeg")
- my_face_encoding = face_recognition.face_encodings(picture_of_me)[0]
- encodings = face_recognition.face_encodings(img)
- unknown_encoding = encodings[0]
- results = face_recognition.compare_faces([my_face_encoding], unknown_encoding)
- if results[0] == True:
- bot.send_message(chat_id=update.effective_chat.id, text='I also guess thats a picture of me!')
- else:
- bot.send_message(chat_id=update.effective_chat.id, text="I don't think im in that picture!")
- except Exception as e:
- print(str(e) + ' [FACE EMOTION]')
- bot.send_message(chat_id=update.effective_chat.id, text='Something went wrong :/ [ERROR 3]')
- #END EMOTION
- elif len(faces) > 1:
- # START DRAW
- try:
- for (x, y, w, h) in faces:
- cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
- cv2.imwrite('imgsend/face.png', img)
- bot.send_photo(chat_id=update.effective_chat.id, photo=open('imgsend/face.png', 'rb'))
- str1 = "I guess this picture contains {0} faces!".format(len(faces))
- bot.send_message(chat_id=update.effective_chat.id, text=str1)
- except Exception as e:
- print(str(e) + ' [FACE DETECTION (2)]')
- bot.send_message(chat_id=update.effective_chat.id, text='Something went wrong :/ [ERROR 5]')
- # END DRAW
- else:
- bot.send_message(chat_id=update.effective_chat.id, text='It looks like something went down :/ [ERROR 6]')
- except Exception as e:
- print(e)
- bot.send_message(chat_id=update.effective_chat.id, text='It looks like something went down :/ [ERROR 7]')
- def onready():
- start_handler = CommandHandler('online', start)
- dispatcher.add_handler(start_handler)
- unknown_handler = MessageHandler(Filters.command, unknown)
- phothandler = MessageHandler(Filters.photo, filedownloader)
- dispatcher.add_handler(unknown_handler)
- dispatcher.add_handler(phothandler)
- updater.start_polling()
- onready()
- end = time.time()
- print('[INFO] It took ' + str(round(end - startt,2)) + ' seconds to load everything up')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement