diff --git a/Facial_Recognition_Inference.py b/Facial_Recognition_Inference.py index 271a89510..78615670a 100644 --- a/Facial_Recognition_Inference.py +++ b/Facial_Recognition_Inference.py @@ -4,99 +4,105 @@ import cv2 import numpy as np import DBHelper -try: - import cPickle # Python 2 -except ImportError: - import _pickle as cPickle # Python 3 -pwd = sys.path[0] -PREDICTOR_PATH = pwd + '/Facial_models/shape_predictor_68_face_landmarks.dat' -FACE_RECOGNITION_MODEL_PATH = pwd + '/Facial_models/dlib_face_recognition_resnet_model_v1.dat' +def inference(): + try: + import cPickle # Python 2 + except ImportError: + import _pickle as cPickle # Python 3 -SKIP_FRAMES = 1 -THRESHOLD = 0.4 + pwd = sys.path[0] + PREDICTOR_PATH = pwd + '/Facial_models/shape_predictor_68_face_landmarks.dat' + FACE_RECOGNITION_MODEL_PATH = pwd + '/Facial_models/dlib_face_recognition_resnet_model_v1.dat' -faceDetector = dlib.get_frontal_face_detector() -shapePredictor = dlib.shape_predictor(PREDICTOR_PATH) -faceRecognizer = dlib.face_recognition_model_v1(FACE_RECOGNITION_MODEL_PATH) + SKIP_FRAMES = 1 + THRESHOLD = 0.4 -index = np.load(pwd + '/Facial_models/index.pkl', allow_pickle=True) -faceDescriptorsEnrolled = np.load(pwd + '/Facial_models/descriptors.npy') + faceDetector = dlib.get_frontal_face_detector() + shapePredictor = dlib.shape_predictor(PREDICTOR_PATH) + faceRecognizer = dlib.face_recognition_model_v1(FACE_RECOGNITION_MODEL_PATH) -cam = cv2.VideoCapture(0) -count = 0 + index = np.load(pwd + '/Facial_models/index.pkl', allow_pickle=True) + faceDescriptorsEnrolled = np.load(pwd + '/Facial_models/descriptors.npy') -x1 = x2 = y1 = y2 = 0 + cam = cv2.VideoCapture(0) + count = 0 -cond = False + x1 = x2 = y1 = y2 = 0 -while DBHelper.get_power() == "on": - t = time.time() - success, im = cam.read() + cond = False - if not success: - print('cannot capture input from camera') - break + while DBHelper.get_power() == "on": + t = time.time() + success, im = cam.read() - if (count % SKIP_FRAMES) == 0: + if not success: + print('cannot capture input from camera') + break - img = cv2.cvtColor(im, cv2.COLOR_BGR2RGB) - faces = faceDetector(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) + if (count % SKIP_FRAMES) == 0: - for face in faces: + img = cv2.cvtColor(im, cv2.COLOR_BGR2RGB) + faces = faceDetector(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) - shape = shapePredictor(cv2.cvtColor(img, cv2.COLOR_BGR2RGB), face) + for face in faces: - x1 = face.left() - y1 = face.top() - x2 = face.right() - y2 = face.bottom() + shape = shapePredictor(cv2.cvtColor(img, cv2.COLOR_BGR2RGB), face) - faceDescriptor = faceRecognizer.compute_face_descriptor(img, shape) + x1 = face.left() + y1 = face.top() + x2 = face.right() + y2 = face.bottom() - # dlib format to list - faceDescriptorList = [m for m in faceDescriptor] - # to numpy array - faceDescriptorNdarray = np.asarray(faceDescriptorList, dtype=np.float64) - faceDescriptorNdarray = faceDescriptorNdarray[np.newaxis, :] + faceDescriptor = faceRecognizer.compute_face_descriptor(img, shape) - # Euclidean distances - distances = np.linalg.norm(faceDescriptorsEnrolled - faceDescriptorNdarray, axis=1) + # dlib format to list + faceDescriptorList = [m for m in faceDescriptor] + # to numpy array + faceDescriptorNdarray = np.asarray(faceDescriptorList, dtype=np.float64) + faceDescriptorNdarray = faceDescriptorNdarray[np.newaxis, :] - # Calculate minimum distance and index of face - argmin = np.argmin(distances) # index - minDistance = distances[argmin] # minimum distance + # Euclidean distances + distances = np.linalg.norm(faceDescriptorsEnrolled - faceDescriptorNdarray, axis=1) - if minDistance <= THRESHOLD: - label = DBHelper.get_firstname(index[argmin]) + "_" + DBHelper.get_lastname(index[argmin]) - cond = True - else: - label = 'unknown' - cond = False + # Calculate minimum distance and index of face + argmin = np.argmin(distances) # index + minDistance = distances[argmin] # minimum distance - # print("time taken = {:.3f} seconds".format(time.time() - t)) + if minDistance <= THRESHOLD: + label = DBHelper.get_firstname(index[argmin]) + "_" + DBHelper.get_lastname(index[argmin]) + cond = True + else: + label = 'unknown' + cond = False - cv2.rectangle(im, (x1, y1), (x2, y2), (0, 255, 0), 2) - font_face = cv2.FONT_HERSHEY_SIMPLEX - font_scale = 0.8 - text_color = (0, 255, 0) - printLabel = '{} {:0.4f}'.format(label, minDistance) - cv2.putText(im, printLabel, (int(x1), int(y1)), font_face, font_scale, text_color, thickness=2) + # print("time taken = {:.3f} seconds".format(time.time() - t)) - cv2.imshow('img', im) + cv2.rectangle(im, (x1, y1), (x2, y2), (0, 255, 0), 2) + font_face = cv2.FONT_HERSHEY_SIMPLEX + font_scale = 0.8 + text_color = (0, 255, 0) + printLabel = '{} {:0.4f}'.format(label, minDistance) + cv2.putText(im, printLabel, (int(x1), int(y1)), font_face, font_scale, text_color, thickness=2) - k = cv2.waitKey(1) & 0xff - if k == 27: - break + cv2.imshow('img', im) - count += 1 - if cond: - DBHelper.set_motor("on") - DBHelper.set_alarm("off") - elif not cond: - DBHelper.set_motor("off") - DBHelper.set_alarm("on") + k = cv2.waitKey(1) & 0xff + if k == 27: + break -DBHelper.set_alarm("off") -DBHelper.set_motor("off") -cv2.destroyAllWindows() + count += 1 + if cond: + DBHelper.set_motor("on") + DBHelper.set_alarm("off") + elif not cond: + DBHelper.set_motor("off") + DBHelper.set_alarm("on") + + DBHelper.set_alarm("off") + DBHelper.set_motor("off") + cv2.destroyAllWindows() + + +if __name__ == "__main__": + inference() diff --git a/Facial_images/face_rec/train/User_1/0.jpg b/Facial_images/face_rec/train/User_1/0.jpg index 536e60daf..a900455b9 100644 Binary files a/Facial_images/face_rec/train/User_1/0.jpg and b/Facial_images/face_rec/train/User_1/0.jpg differ diff --git a/Facial_images/face_rec/train/User_1/1.jpg b/Facial_images/face_rec/train/User_1/1.jpg index 7ecd6f69e..a31c11cd1 100644 Binary files a/Facial_images/face_rec/train/User_1/1.jpg and b/Facial_images/face_rec/train/User_1/1.jpg differ diff --git a/Facial_images/face_rec/train/User_1/10.jpg b/Facial_images/face_rec/train/User_1/10.jpg index ffa3bf04b..560964b05 100644 Binary files a/Facial_images/face_rec/train/User_1/10.jpg and b/Facial_images/face_rec/train/User_1/10.jpg differ diff --git a/Facial_images/face_rec/train/User_1/11.jpg b/Facial_images/face_rec/train/User_1/11.jpg index 161e8f047..dd3056bd6 100644 Binary files a/Facial_images/face_rec/train/User_1/11.jpg and b/Facial_images/face_rec/train/User_1/11.jpg differ diff --git a/Facial_images/face_rec/train/User_1/12.jpg b/Facial_images/face_rec/train/User_1/12.jpg index aea40ba8c..b98ac89f2 100644 Binary files a/Facial_images/face_rec/train/User_1/12.jpg and b/Facial_images/face_rec/train/User_1/12.jpg differ diff --git a/Facial_images/face_rec/train/User_1/13.jpg b/Facial_images/face_rec/train/User_1/13.jpg index c83ba422e..482d16f41 100644 Binary files a/Facial_images/face_rec/train/User_1/13.jpg and b/Facial_images/face_rec/train/User_1/13.jpg differ diff --git a/Facial_images/face_rec/train/User_1/14.jpg b/Facial_images/face_rec/train/User_1/14.jpg index a1951d23b..bdbf9ae66 100644 Binary files a/Facial_images/face_rec/train/User_1/14.jpg and b/Facial_images/face_rec/train/User_1/14.jpg differ diff --git a/Facial_images/face_rec/train/User_1/15.jpg b/Facial_images/face_rec/train/User_1/15.jpg index cbba7b6ac..db30962f8 100644 Binary files a/Facial_images/face_rec/train/User_1/15.jpg and b/Facial_images/face_rec/train/User_1/15.jpg differ diff --git a/Facial_images/face_rec/train/User_1/16.jpg b/Facial_images/face_rec/train/User_1/16.jpg index 2f5643922..6286f69e9 100644 Binary files a/Facial_images/face_rec/train/User_1/16.jpg and b/Facial_images/face_rec/train/User_1/16.jpg differ diff --git a/Facial_images/face_rec/train/User_1/17.jpg b/Facial_images/face_rec/train/User_1/17.jpg index 7e9d9c50e..539f30aa2 100644 Binary files a/Facial_images/face_rec/train/User_1/17.jpg and b/Facial_images/face_rec/train/User_1/17.jpg differ diff --git a/Facial_images/face_rec/train/User_1/18.jpg b/Facial_images/face_rec/train/User_1/18.jpg index 953dfe2cb..9025b737c 100644 Binary files a/Facial_images/face_rec/train/User_1/18.jpg and b/Facial_images/face_rec/train/User_1/18.jpg differ diff --git a/Facial_images/face_rec/train/User_1/19.jpg b/Facial_images/face_rec/train/User_1/19.jpg index d56039dea..f189c87e9 100644 Binary files a/Facial_images/face_rec/train/User_1/19.jpg and b/Facial_images/face_rec/train/User_1/19.jpg differ diff --git a/Facial_images/face_rec/train/User_1/2.jpg b/Facial_images/face_rec/train/User_1/2.jpg index ac6e50103..a4326d4a4 100644 Binary files a/Facial_images/face_rec/train/User_1/2.jpg and b/Facial_images/face_rec/train/User_1/2.jpg differ diff --git a/Facial_images/face_rec/train/User_1/3.jpg b/Facial_images/face_rec/train/User_1/3.jpg index 28f772be2..8e486e7d9 100644 Binary files a/Facial_images/face_rec/train/User_1/3.jpg and b/Facial_images/face_rec/train/User_1/3.jpg differ diff --git a/Facial_images/face_rec/train/User_1/4.jpg b/Facial_images/face_rec/train/User_1/4.jpg index 12cbf4f3d..965c1ffc9 100644 Binary files a/Facial_images/face_rec/train/User_1/4.jpg and b/Facial_images/face_rec/train/User_1/4.jpg differ diff --git a/Facial_images/face_rec/train/User_1/5.jpg b/Facial_images/face_rec/train/User_1/5.jpg index ed8832807..146564ebd 100644 Binary files a/Facial_images/face_rec/train/User_1/5.jpg and b/Facial_images/face_rec/train/User_1/5.jpg differ diff --git a/Facial_images/face_rec/train/User_1/6.jpg b/Facial_images/face_rec/train/User_1/6.jpg index 955e50a82..063377993 100644 Binary files a/Facial_images/face_rec/train/User_1/6.jpg and b/Facial_images/face_rec/train/User_1/6.jpg differ diff --git a/Facial_images/face_rec/train/User_1/7.jpg b/Facial_images/face_rec/train/User_1/7.jpg index 2e00ee7d6..9a891b421 100644 Binary files a/Facial_images/face_rec/train/User_1/7.jpg and b/Facial_images/face_rec/train/User_1/7.jpg differ diff --git a/Facial_images/face_rec/train/User_1/8.jpg b/Facial_images/face_rec/train/User_1/8.jpg index e0cd8a9b8..3846dff25 100644 Binary files a/Facial_images/face_rec/train/User_1/8.jpg and b/Facial_images/face_rec/train/User_1/8.jpg differ diff --git a/Facial_images/face_rec/train/User_1/9.jpg b/Facial_images/face_rec/train/User_1/9.jpg index e81cd49dd..679584654 100644 Binary files a/Facial_images/face_rec/train/User_1/9.jpg and b/Facial_images/face_rec/train/User_1/9.jpg differ diff --git a/Facial_images/face_rec/train/User_2/0.jpg b/Facial_images/face_rec/train/User_2/0.jpg index 2056159cc..ba3ef8144 100644 Binary files a/Facial_images/face_rec/train/User_2/0.jpg and b/Facial_images/face_rec/train/User_2/0.jpg differ diff --git a/Facial_images/face_rec/train/User_2/1.jpg b/Facial_images/face_rec/train/User_2/1.jpg index c9e095cb5..c9aa4c3c1 100644 Binary files a/Facial_images/face_rec/train/User_2/1.jpg and b/Facial_images/face_rec/train/User_2/1.jpg differ diff --git a/Facial_images/face_rec/train/User_2/10.jpg b/Facial_images/face_rec/train/User_2/10.jpg index bc7ee227c..9c1154324 100644 Binary files a/Facial_images/face_rec/train/User_2/10.jpg and b/Facial_images/face_rec/train/User_2/10.jpg differ diff --git a/Facial_images/face_rec/train/User_2/11.jpg b/Facial_images/face_rec/train/User_2/11.jpg index 3cd0c315c..2e5e8ee13 100644 Binary files a/Facial_images/face_rec/train/User_2/11.jpg and b/Facial_images/face_rec/train/User_2/11.jpg differ diff --git a/Facial_images/face_rec/train/User_2/12.jpg b/Facial_images/face_rec/train/User_2/12.jpg index 24bca0321..c9390ec52 100644 Binary files a/Facial_images/face_rec/train/User_2/12.jpg and b/Facial_images/face_rec/train/User_2/12.jpg differ diff --git a/Facial_images/face_rec/train/User_2/13.jpg b/Facial_images/face_rec/train/User_2/13.jpg index e75338a0f..0c9800004 100644 Binary files a/Facial_images/face_rec/train/User_2/13.jpg and b/Facial_images/face_rec/train/User_2/13.jpg differ diff --git a/Facial_images/face_rec/train/User_2/14.jpg b/Facial_images/face_rec/train/User_2/14.jpg index 6a619bfd6..984e6bda4 100644 Binary files a/Facial_images/face_rec/train/User_2/14.jpg and b/Facial_images/face_rec/train/User_2/14.jpg differ diff --git a/Facial_images/face_rec/train/User_2/15.jpg b/Facial_images/face_rec/train/User_2/15.jpg index d28f9aa67..1c98f0aef 100644 Binary files a/Facial_images/face_rec/train/User_2/15.jpg and b/Facial_images/face_rec/train/User_2/15.jpg differ diff --git a/Facial_images/face_rec/train/User_2/16.jpg b/Facial_images/face_rec/train/User_2/16.jpg index 975e30774..9c53040cd 100644 Binary files a/Facial_images/face_rec/train/User_2/16.jpg and b/Facial_images/face_rec/train/User_2/16.jpg differ diff --git a/Facial_images/face_rec/train/User_2/17.jpg b/Facial_images/face_rec/train/User_2/17.jpg index 99adf490c..ba3ff8c4f 100644 Binary files a/Facial_images/face_rec/train/User_2/17.jpg and b/Facial_images/face_rec/train/User_2/17.jpg differ diff --git a/Facial_images/face_rec/train/User_2/18.jpg b/Facial_images/face_rec/train/User_2/18.jpg index 07f2fd087..21aaa7375 100644 Binary files a/Facial_images/face_rec/train/User_2/18.jpg and b/Facial_images/face_rec/train/User_2/18.jpg differ diff --git a/Facial_images/face_rec/train/User_2/19.jpg b/Facial_images/face_rec/train/User_2/19.jpg index cd0c3d61d..d7e97ba2e 100644 Binary files a/Facial_images/face_rec/train/User_2/19.jpg and b/Facial_images/face_rec/train/User_2/19.jpg differ diff --git a/Facial_images/face_rec/train/User_2/2.jpg b/Facial_images/face_rec/train/User_2/2.jpg index d8d9cd056..2cf8ab1ae 100644 Binary files a/Facial_images/face_rec/train/User_2/2.jpg and b/Facial_images/face_rec/train/User_2/2.jpg differ diff --git a/Facial_images/face_rec/train/User_2/3.jpg b/Facial_images/face_rec/train/User_2/3.jpg index 3aaa9f108..334e2ce69 100644 Binary files a/Facial_images/face_rec/train/User_2/3.jpg and b/Facial_images/face_rec/train/User_2/3.jpg differ diff --git a/Facial_images/face_rec/train/User_2/4.jpg b/Facial_images/face_rec/train/User_2/4.jpg index 651456552..84daf5540 100644 Binary files a/Facial_images/face_rec/train/User_2/4.jpg and b/Facial_images/face_rec/train/User_2/4.jpg differ diff --git a/Facial_images/face_rec/train/User_2/5.jpg b/Facial_images/face_rec/train/User_2/5.jpg index 2dfd89a84..deafb4b65 100644 Binary files a/Facial_images/face_rec/train/User_2/5.jpg and b/Facial_images/face_rec/train/User_2/5.jpg differ diff --git a/Facial_images/face_rec/train/User_2/6.jpg b/Facial_images/face_rec/train/User_2/6.jpg index bcf70f1d3..ce463871a 100644 Binary files a/Facial_images/face_rec/train/User_2/6.jpg and b/Facial_images/face_rec/train/User_2/6.jpg differ diff --git a/Facial_images/face_rec/train/User_2/7.jpg b/Facial_images/face_rec/train/User_2/7.jpg index a07357bf6..31b8ec1c4 100644 Binary files a/Facial_images/face_rec/train/User_2/7.jpg and b/Facial_images/face_rec/train/User_2/7.jpg differ diff --git a/Facial_images/face_rec/train/User_2/8.jpg b/Facial_images/face_rec/train/User_2/8.jpg index 9ec3fe53f..8c90548bc 100644 Binary files a/Facial_images/face_rec/train/User_2/8.jpg and b/Facial_images/face_rec/train/User_2/8.jpg differ diff --git a/Facial_images/face_rec/train/User_2/9.jpg b/Facial_images/face_rec/train/User_2/9.jpg index 84748de37..7fc14b47c 100644 Binary files a/Facial_images/face_rec/train/User_2/9.jpg and b/Facial_images/face_rec/train/User_2/9.jpg differ diff --git a/__pycache__/Facial_Recognition_Enrollment.cpython-36.pyc b/__pycache__/Facial_Recognition_Enrollment.cpython-36.pyc new file mode 100644 index 000000000..b758e19de Binary files /dev/null and b/__pycache__/Facial_Recognition_Enrollment.cpython-36.pyc differ diff --git a/__pycache__/Facial_Recognition_Inference.cpython-36.pyc b/__pycache__/Facial_Recognition_Inference.cpython-36.pyc new file mode 100644 index 000000000..55edd6d14 Binary files /dev/null and b/__pycache__/Facial_Recognition_Inference.cpython-36.pyc differ diff --git a/__pycache__/Facial_Recognition_Registration.cpython-36.pyc b/__pycache__/Facial_Recognition_Registration.cpython-36.pyc index 6a20504b9..e0451523e 100644 Binary files a/__pycache__/Facial_Recognition_Registration.cpython-36.pyc and b/__pycache__/Facial_Recognition_Registration.cpython-36.pyc differ diff --git a/__pycache__/Start_Engine.cpython-36.pyc b/__pycache__/Start_Engine.cpython-36.pyc index 8b7b685a8..828391fb2 100644 Binary files a/__pycache__/Start_Engine.cpython-36.pyc and b/__pycache__/Start_Engine.cpython-36.pyc differ diff --git a/__pycache__/Upload_Face.cpython-36.pyc b/__pycache__/Upload_Face.cpython-36.pyc index 1b7832a33..6e46ff254 100644 Binary files a/__pycache__/Upload_Face.cpython-36.pyc and b/__pycache__/Upload_Face.cpython-36.pyc differ diff --git a/start_engine.py b/start_engine.py index a0f0c81bd..f2fcb4606 100644 --- a/start_engine.py +++ b/start_engine.py @@ -32,7 +32,7 @@ def start(): print("Success.") except: print("No Thieves are registered.") - Facial_Recognition_Inference + Facial_Recognition_Inference.inference() if __name__ == "__main__":