From 9ae0db2b604e8c2fa09beed42d8befd642a77311 Mon Sep 17 00:00:00 2001 From: KaranSurana Date: Thu, 26 Aug 2021 15:35:57 +0530 Subject: [PATCH] Added Facial Recognition Feature --- bridges/python/addface.py | 11 +++++++++++ bridges/python/facerecog.py | 33 +++++++++++++++++++++++++++++++++ bridges/python/main.py | 2 ++ 3 files changed, 46 insertions(+) create mode 100644 bridges/python/addface.py create mode 100644 bridges/python/facerecog.py diff --git a/bridges/python/addface.py b/bridges/python/addface.py new file mode 100644 index 000000000..5e90999d2 --- /dev/null +++ b/bridges/python/addface.py @@ -0,0 +1,11 @@ + +import cv2 +import os +import smtplib +import datetime +name=input("Name: ") +camera = cv2.VideoCapture(0) +return_value, image = camera.read() +date_string = datetime.datetime.now().strftime("%d%m%Y%H%M%S") +cv2.imwrite(os.path.join('/' , name+'.jpg'), image) +print(os.path.join('/' , name+'.jpg')) diff --git a/bridges/python/facerecog.py b/bridges/python/facerecog.py new file mode 100644 index 000000000..d169fb993 --- /dev/null +++ b/bridges/python/facerecog.py @@ -0,0 +1,33 @@ +import face_recognition +import numpy as np +import cv2 +import os +import smtplib +try: + for file in os.listdir("D:\Leon"): + filename = os.fsdecode(file) + if filename.endswith(".jpg"): + print(filename) + imgloaded=face_recognition.load_image_file(filename) + imgloaded=cv2.cvtColor(imgloaded,cv2.COLOR_BGR2RGB) + camera = cv2.VideoCapture(0) + return_value, image = camera.read() + cv2.imwrite(os.path.join('/' , 'testimage.jpg'), image) + imgtest=face_recognition.load_image_file('/testimage.jpg') + imgtest=cv2.cvtColor(imgtest,cv2.COLOR_BGR2RGB) + faceloc=face_recognition.face_locations(imgloaded)[0] + encodeloaded=face_recognition.face_encodings(imgloaded)[0] + cv2.rectangle(imgloaded,(faceloc[3],faceloc[0]),(faceloc[1],faceloc[2]),(255,0,255),2) + faceloctest=face_recognition.face_locations(imgtest)[0] + encodetest=face_recognition.face_encodings(imgtest)[0] + cv2.rectangle(imgtest,(faceloc[3],faceloc[0]),(faceloc[1],faceloc[2]),(255,0,255),2) + results=face_recognition.compare_faces([encodeloaded],encodetest) + if(results[0]): + print(filename.replace(".jpg","")," Verified") + else: + print("Verification Failed") + + +except OSError as e: + print(e) + print("Error In Loading File") diff --git a/bridges/python/main.py b/bridges/python/main.py index 0c48ad65e..8329ea48f 100644 --- a/bridges/python/main.py +++ b/bridges/python/main.py @@ -6,6 +6,7 @@ from json import dumps, loads from importlib import import_module + def main(): """Dynamically import modules related to the args and print the ouput""" @@ -17,4 +18,5 @@ def main(): return getattr(m, queryobj['action'])(queryobj['query'], queryobj['entities']) if __name__ == '__main__': + #Appropriately adding Face recognition modules in the main python file. main()