Advertisement
tqtics

Error Deepface

May 29th, 2020
553
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.22 KB | None | 0 0
  1. import cv2
  2. import face_recognition
  3. import os
  4. import time
  5. import telegram
  6. from telegram.ext import Updater
  7. from telegram.ext import CommandHandler
  8. from telegram.ext import MessageHandler, Filters
  9. from deepface import DeepFace
  10.  
  11.  
  12. os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
  13. secret = 'heregoesmytoken'
  14. bot = telegram.Bot(token=secret)
  15. startt = time.time()
  16. #model = load_model("model_v6_23.hdf5")
  17. print('[BOT] ' + str(bot.get_me()))
  18.  
  19. updater = Updater(token=secret, use_context=True)
  20. dispatcher = updater.dispatcher
  21.  
  22.  
  23. def start(update, context):
  24.     context.bot.send_message(chat_id=update.effective_chat.id, text="Im functioning well!")
  25.  
  26.  
  27. def unknown(update, context):
  28.     context.bot.send_message(chat_id=update.effective_chat.id, text="Sorry, I didn't understand that command.")
  29.  
  30.  
  31. def filedownloader(update, context):
  32.     file = bot.getFile(update.message.photo[-1].file_id)
  33.     file.download('./dwnimages/face.jpg')
  34.     context.bot.send_message(chat_id=update.effective_chat.id, text="Processing picture...")
  35.  
  36.  
  37.     try:
  38.         face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
  39.         img = cv2.imread("dwnimages/face.jpg")
  40.         gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  41.         faces = face_cascade.detectMultiScale(gray, 1.1, 4)
  42.  
  43.         print('[INFO] FACES: ' + str(len(faces)))
  44.  
  45.         if len(faces) == 0:
  46.             bot.send_message(chat_id=update.effective_chat.id, text='I could not find any faces :/')
  47.         elif len(faces) == 1:
  48.  
  49.             # START DRAW
  50.             try:
  51.                 for (x, y, w, h) in faces:
  52.                     cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
  53.  
  54.                 cv2.imwrite('imgsend/face.png', img)
  55.                 bot.send_photo(chat_id=update.effective_chat.id, photo=open('imgsend/face.png', 'rb'))
  56.                 str1 = "I guess this picture contains 1 face!".format(len(faces))
  57.                 bot.send_message(chat_id=update.effective_chat.id, text=str1)
  58.             except Exception as e:
  59.                 print(str(e) + ' [FACE DETECTION (1)]')
  60.                 bot.send_message(chat_id=update.effective_chat.id, text='Something went wrong :/ [ERROR 1]')
  61.  
  62.             # END DRAW
  63.             # START EMOTION
  64.             try:
  65.  
  66.                 faceinfo = DeepFace.analyze("images/happy.jpeg")
  67.                 print('[INFO] Emotion:', faceinfo["dominant_emotion"])
  68.                 print('[INFO] Age:', faceinfo["age"])
  69.                 print('[INFO] Gender:', faceinfo["gender"])
  70.                 print('[INFO] Race:', faceinfo["dominant_race"])
  71.  
  72.  
  73.             except Exception as e:
  74.                 print(str(e) + ' [FACE EMOTION]')
  75.                 bot.send_message(chat_id=update.effective_chat.id, text='Something went wrong :/ [ERROR 2]')
  76.  
  77.             #END EMOTION
  78.             #START RECOGNITION
  79.  
  80.             try:
  81.                 picture_of_me = face_recognition.load_image_file("images/me.jpeg")
  82.                 my_face_encoding = face_recognition.face_encodings(picture_of_me)[0]
  83.                 encodings = face_recognition.face_encodings(img)
  84.                 unknown_encoding = encodings[0]
  85.                 results = face_recognition.compare_faces([my_face_encoding], unknown_encoding)
  86.  
  87.                 if results[0] == True:
  88.                     bot.send_message(chat_id=update.effective_chat.id, text='I also guess thats a picture of me!')
  89.                 else:
  90.                     bot.send_message(chat_id=update.effective_chat.id, text="I don't think im in that picture!")
  91.  
  92.             except Exception as e:
  93.                 print(str(e) + ' [FACE EMOTION]')
  94.                 bot.send_message(chat_id=update.effective_chat.id, text='Something went wrong :/ [ERROR 3]')
  95.  
  96.             #END EMOTION
  97.  
  98.         elif len(faces) > 1:
  99.             # START DRAW
  100.             try:
  101.                 for (x, y, w, h) in faces:
  102.                     cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
  103.  
  104.                 cv2.imwrite('imgsend/face.png', img)
  105.                 bot.send_photo(chat_id=update.effective_chat.id, photo=open('imgsend/face.png', 'rb'))
  106.                 str1 = "I guess this picture contains {0} faces!".format(len(faces))
  107.                 bot.send_message(chat_id=update.effective_chat.id, text=str1)
  108.             except Exception as e:
  109.                 print(str(e) + ' [FACE DETECTION (2)]')
  110.                 bot.send_message(chat_id=update.effective_chat.id, text='Something went wrong :/ [ERROR 5]')
  111.  
  112.             # END DRAW
  113.  
  114.         else:
  115.             bot.send_message(chat_id=update.effective_chat.id, text='It looks like something went down :/ [ERROR 6]')
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.     except Exception as e:
  124.         print(e)
  125.         bot.send_message(chat_id=update.effective_chat.id, text='It looks like something went down :/ [ERROR 7]')
  126.  
  127.  
  128.  
  129. def onready():
  130.     start_handler = CommandHandler('online', start)
  131.     dispatcher.add_handler(start_handler)
  132.     unknown_handler = MessageHandler(Filters.command, unknown)
  133.     phothandler = MessageHandler(Filters.photo, filedownloader)
  134.     dispatcher.add_handler(unknown_handler)
  135.     dispatcher.add_handler(phothandler)
  136.     updater.start_polling()
  137.  
  138.  
  139. onready()
  140. end = time.time()
  141. print('[INFO] It took ' + str(round(end - startt,2)) + ' seconds to load everything up')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement