diff --git a/Facial_Recognition_Enrollment.py b/Facial_Recognition_Enrollment.py index 4899d5d65..126030380 100644 --- a/Facial_Recognition_Enrollment.py +++ b/Facial_Recognition_Enrollment.py @@ -5,74 +5,71 @@ import sys import numpy as np try: - import cPickle # Python2. + import cPickle # Python2. except ImportError: - import _pickle as cPickle # Python3. + import _pickle as cPickle # Python3. + def enroll_face_dataset(): - 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' + 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) + faceDetector = dlib.get_frontal_face_detector() + shapePredictor = dlib.shape_predictor(PREDICTOR_PATH) + faceRecognizer = dlib.face_recognition_model_v1(FACE_RECOGNITION_MODEL_PATH) + faceDatasetFolder = pwd + '/Facial_images/face_rec/train/' - faceDatasetFolder = pwd + '/Facial_images/face_rec/train/' + subfolders = [] + for x in os.listdir(faceDatasetFolder): + xpath = os.path.join(faceDatasetFolder, x) + if os.path.isdir(xpath): + subfolders.append(xpath) - subfolders = [] - for x in os.listdir(faceDatasetFolder): - xpath = os.path.join(faceDatasetFolder, x) - if os.path.isdir(xpath): - subfolders.append(xpath) + nameLabelMap = {} + labels = [] + imagePaths = [] + for i, subfolder in enumerate(subfolders): + for x in os.listdir(subfolder): + xpath = os.path.join(subfolder, x) + if x.endswith('jpg'): + imagePaths.append(xpath) + labels.append(i) + nameLabelMap[xpath] = subfolder.split('/')[-1] + index = {} + i = 0 + faceDescriptors = None + for imagePath in imagePaths: + # print("processing: {}".format(imagePath)) + img = cv2.imread(imagePath) - nameLabelMap = {} - labels = [] - imagePaths = [] - for i, subfolder in enumerate(subfolders): - for x in os.listdir(subfolder): - xpath = os.path.join(subfolder, x) - if x.endswith('jpg'): - imagePaths.append(xpath) - labels.append(i) - nameLabelMap[xpath] = subfolder.split('/')[-1] + faces = faceDetector(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) - index = {} - i = 0 - faceDescriptors = None - for imagePath in imagePaths: - #print("processing: {}".format(imagePath)) - img = cv2.imread(imagePath) + # print("{} Face(s) found".format(len(faces))) - faces = faceDetector(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) + for k, face in enumerate(faces): - #print("{} Face(s) found".format(len(faces))) + shape = shapePredictor(cv2.cvtColor(img, cv2.COLOR_BGR2RGB), face) - for k, face in enumerate(faces): + landmarks = [(p.x, p.y) for p in shape.parts()] - shape = shapePredictor(cv2.cvtColor(img, cv2.COLOR_BGR2RGB), face) + faceDescriptor = faceRecognizer.compute_face_descriptor(img, shape) - landmarks = [(p.x, p.y) for p in shape.parts()] + faceDescriptorList = [x for x in faceDescriptor] + faceDescriptorNdarray = np.asarray(faceDescriptorList, dtype=np.float64) + faceDescriptorNdarray = faceDescriptorNdarray[np.newaxis, :] - faceDescriptor = faceRecognizer.compute_face_descriptor(img, shape) + if faceDescriptors is None: + faceDescriptors = faceDescriptorNdarray + else: + faceDescriptors = np.concatenate((faceDescriptors, faceDescriptorNdarray), axis=0) - - faceDescriptorList = [x for x in faceDescriptor] - faceDescriptorNdarray = np.asarray(faceDescriptorList, dtype=np.float64) - faceDescriptorNdarray = faceDescriptorNdarray[np.newaxis, :] + index[i] = nameLabelMap[imagePath] + i += 1 - - if faceDescriptors is None: - faceDescriptors = faceDescriptorNdarray - else: - faceDescriptors = np.concatenate((faceDescriptors, faceDescriptorNdarray), axis=0) - - index[i] = nameLabelMap[imagePath] - i += 1 - - # Write descriors and index to disk - np.save(pwd+'/Facial_models/descriptors.npy', faceDescriptors) - with open(pwd+'/Facial_models/index.pkl', 'wb') as f: - cPickle.dump(index, f) + # Write descriors and index to disk + np.save(pwd + '/Facial_models/descriptors.npy', faceDescriptors) + with open(pwd + '/Facial_models/index.pkl', 'wb') as f: + cPickle.dump(index, f) diff --git a/Facial_Recognition_Inference.py b/Facial_Recognition_Inference.py index a0fb56965..271a89510 100644 --- a/Facial_Recognition_Inference.py +++ b/Facial_Recognition_Inference.py @@ -1,13 +1,13 @@ -import os,sys,time +import os, sys, time import dlib import cv2 import numpy as np +import DBHelper try: - import cPickle # Python 2 + import cPickle # Python 2 except ImportError: - import _pickle as cPickle # Python 3 - + import _pickle as cPickle # Python 3 pwd = sys.path[0] PREDICTOR_PATH = pwd + '/Facial_models/shape_predictor_68_face_landmarks.dat' @@ -20,75 +20,83 @@ faceDetector = dlib.get_frontal_face_detector() shapePredictor = dlib.shape_predictor(PREDICTOR_PATH) faceRecognizer = dlib.face_recognition_model_v1(FACE_RECOGNITION_MODEL_PATH) -index = np.load(pwd+'/Facial_models/index.pkl', allow_pickle=True) -faceDescriptorsEnrolled = np.load(pwd+'/Facial_models/descriptors.npy') +index = np.load(pwd + '/Facial_models/index.pkl', allow_pickle=True) +faceDescriptorsEnrolled = np.load(pwd + '/Facial_models/descriptors.npy') - -cam = cv2.VideoCapture(1) +cam = cv2.VideoCapture(0) count = 0 x1 = x2 = y1 = y2 = 0 -while True: - 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 not success: + print('cannot capture input from camera') + break - if (count % SKIP_FRAMES) == 0: + if (count % SKIP_FRAMES) == 0: - img = cv2.cvtColor(im, cv2.COLOR_BGR2RGB) - faces = faceDetector(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) + img = cv2.cvtColor(im, cv2.COLOR_BGR2RGB) + faces = faceDetector(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) - for face in faces: + for face in faces: - shape = shapePredictor(cv2.cvtColor(img, cv2.COLOR_BGR2RGB), face) + shape = shapePredictor(cv2.cvtColor(img, cv2.COLOR_BGR2RGB), face) - x1 = face.left() - y1 = face.top() - x2 = face.right() - y2 = face.bottom() + x1 = face.left() + y1 = face.top() + x2 = face.right() + y2 = face.bottom() - faceDescriptor = faceRecognizer.compute_face_descriptor(img, shape) + faceDescriptor = faceRecognizer.compute_face_descriptor(img, shape) - # dlib format to list - faceDescriptorList = [m for m in faceDescriptor] - # to numpy array - faceDescriptorNdarray = np.asarray(faceDescriptorList, dtype=np.float64) - faceDescriptorNdarray = faceDescriptorNdarray[np.newaxis, :] + # dlib format to list + faceDescriptorList = [m for m in faceDescriptor] + # to numpy array + faceDescriptorNdarray = np.asarray(faceDescriptorList, dtype=np.float64) + faceDescriptorNdarray = faceDescriptorNdarray[np.newaxis, :] - # Euclidean distances - distances = np.linalg.norm(faceDescriptorsEnrolled - faceDescriptorNdarray, axis=1) - - # 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 = index[argmin] - else: - label = 'unknown' + # 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 + # print("time taken = {:.3f} seconds".format(time.time() - t)) - 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) + 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) - - cv2.imshow('img', im) + cv2.imshow('img', im) - k = cv2.waitKey(1) & 0xff - if k == 27: - break + k = cv2.waitKey(1) & 0xff + if k == 27: + break - count += 1 + 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() diff --git a/Facial_Recognition_Registration.py b/Facial_Recognition_Registration.py index 1f2559a93..7bde7603d 100644 --- a/Facial_Recognition_Registration.py +++ b/Facial_Recognition_Registration.py @@ -4,6 +4,7 @@ import math import cv2 import Facial_Recognition_Enrollment + def register_your_face(label): num_cap = 50 @@ -37,6 +38,6 @@ if __name__ == "__main__": register_your_face(label) print("Data saved! Starting enrollment...") print() - Facial_Recognition_Enrollment.enroll_face_dataset() #Need discuss and modify after intergrate with database. + Facial_Recognition_Enrollment.enroll_face_dataset() # Need discuss and modify after intergrate with database. print("Face registration completed!") print() diff --git a/Upload_Face.py b/Upload_Face.py index 75f343e63..1ac68f96a 100644 --- a/Upload_Face.py +++ b/Upload_Face.py @@ -1,5 +1,6 @@ import DBHelper import Facial_Recognition_Registration +import Facial_Recognition_Enrollment def upload_your_face(firstname, lastname, email, phone): @@ -13,6 +14,7 @@ def upload_your_face(firstname, lastname, email, phone): count += 1 DBHelper.upload_data("User_" + str(count), firstname, lastname, email, phone) Facial_Recognition_Registration.register_your_face("User_" + str(count)) + Facial_Recognition_Enrollment.enroll_face_dataset() for i in range(20): DBHelper.upload_user_photo("User_" + str(count) + "/" + str(i) + ".jpg") except: diff --git a/__pycache__/DBHelper.cpython-36.pyc b/__pycache__/DBHelper.cpython-36.pyc index dcf011971..8e7fd07be 100644 Binary files a/__pycache__/DBHelper.cpython-36.pyc and b/__pycache__/DBHelper.cpython-36.pyc differ diff --git a/start_engine.py b/start_engine.py index e5470039c..a0f0c81bd 100644 --- a/start_engine.py +++ b/start_engine.py @@ -1,6 +1,6 @@ import os import DBHelper -import Facial_Recognition_Wrapper +import Facial_Recognition_Inference def start(): @@ -32,8 +32,7 @@ def start(): print("Success.") except: print("No Thieves are registered.") - Facial_Recognition_Wrapper.training_recognizer("LBPH") - Facial_Recognition_Wrapper.face_recognition_inference("LBPH") + Facial_Recognition_Inference if __name__ == "__main__":