Integrated Facial Software to Database and Main Program.

This commit is contained in:
Batuhan Berk Başoğlu 2021-01-24 10:55:36 -05:00
parent 9f7adf2762
commit e1f6e307fa
6 changed files with 117 additions and 110 deletions

View file

@ -5,74 +5,71 @@ import sys
import numpy as np import numpy as np
try: try:
import cPickle # Python2. import cPickle # Python2.
except ImportError: except ImportError:
import _pickle as cPickle # Python3. import _pickle as cPickle # Python3.
def enroll_face_dataset(): def enroll_face_dataset():
pwd = sys.path[0] pwd = sys.path[0]
PREDICTOR_PATH = pwd + '/Facial_models/shape_predictor_68_face_landmarks.dat' 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' FACE_RECOGNITION_MODEL_PATH = pwd + '/Facial_models/dlib_face_recognition_resnet_model_v1.dat'
faceDetector = dlib.get_frontal_face_detector() faceDetector = dlib.get_frontal_face_detector()
shapePredictor = dlib.shape_predictor(PREDICTOR_PATH) shapePredictor = dlib.shape_predictor(PREDICTOR_PATH)
faceRecognizer = dlib.face_recognition_model_v1(FACE_RECOGNITION_MODEL_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 = [] nameLabelMap = {}
for x in os.listdir(faceDatasetFolder): labels = []
xpath = os.path.join(faceDatasetFolder, x) imagePaths = []
if os.path.isdir(xpath): for i, subfolder in enumerate(subfolders):
subfolders.append(xpath) 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 = {} faces = faceDetector(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
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 = {} # print("{} Face(s) found".format(len(faces)))
i = 0
faceDescriptors = None
for imagePath in imagePaths:
#print("processing: {}".format(imagePath))
img = cv2.imread(imagePath)
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)
index[i] = nameLabelMap[imagePath]
i += 1
faceDescriptorList = [x for x in faceDescriptor] # Write descriors and index to disk
faceDescriptorNdarray = np.asarray(faceDescriptorList, dtype=np.float64) np.save(pwd + '/Facial_models/descriptors.npy', faceDescriptors)
faceDescriptorNdarray = faceDescriptorNdarray[np.newaxis, :] with open(pwd + '/Facial_models/index.pkl', 'wb') as f:
cPickle.dump(index, f)
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)

View file

@ -1,13 +1,13 @@
import os,sys,time import os, sys, time
import dlib import dlib
import cv2 import cv2
import numpy as np import numpy as np
import DBHelper
try: try:
import cPickle # Python 2 import cPickle # Python 2
except ImportError: except ImportError:
import _pickle as cPickle # Python 3 import _pickle as cPickle # Python 3
pwd = sys.path[0] pwd = sys.path[0]
PREDICTOR_PATH = pwd + '/Facial_models/shape_predictor_68_face_landmarks.dat' 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) shapePredictor = dlib.shape_predictor(PREDICTOR_PATH)
faceRecognizer = dlib.face_recognition_model_v1(FACE_RECOGNITION_MODEL_PATH) faceRecognizer = dlib.face_recognition_model_v1(FACE_RECOGNITION_MODEL_PATH)
index = np.load(pwd+'/Facial_models/index.pkl', allow_pickle=True) index = np.load(pwd + '/Facial_models/index.pkl', allow_pickle=True)
faceDescriptorsEnrolled = np.load(pwd+'/Facial_models/descriptors.npy') faceDescriptorsEnrolled = np.load(pwd + '/Facial_models/descriptors.npy')
cam = cv2.VideoCapture(0)
cam = cv2.VideoCapture(1)
count = 0 count = 0
x1 = x2 = y1 = y2 = 0 x1 = x2 = y1 = y2 = 0
while True: cond = False
t = time.time()
success, im = cam.read()
if not success: while DBHelper.get_power() == "on":
print('cannot capture input from camera') t = time.time()
break 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) img = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
faces = faceDetector(cv2.cvtColor(img, 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() x1 = face.left()
y1 = face.top() y1 = face.top()
x2 = face.right() x2 = face.right()
y2 = face.bottom() y2 = face.bottom()
faceDescriptor = faceRecognizer.compute_face_descriptor(img, shape) faceDescriptor = faceRecognizer.compute_face_descriptor(img, shape)
# dlib format to list # dlib format to list
faceDescriptorList = [m for m in faceDescriptor] faceDescriptorList = [m for m in faceDescriptor]
# to numpy array # to numpy array
faceDescriptorNdarray = np.asarray(faceDescriptorList, dtype=np.float64) faceDescriptorNdarray = np.asarray(faceDescriptorList, dtype=np.float64)
faceDescriptorNdarray = faceDescriptorNdarray[np.newaxis, :] faceDescriptorNdarray = faceDescriptorNdarray[np.newaxis, :]
# Euclidean distances # Euclidean distances
distances = np.linalg.norm(faceDescriptorsEnrolled - faceDescriptorNdarray, axis=1) distances = np.linalg.norm(faceDescriptorsEnrolled - faceDescriptorNdarray, axis=1)
# Calculate minimum distance and index of face # Calculate minimum distance and index of face
argmin = np.argmin(distances) # index argmin = np.argmin(distances) # index
minDistance = distances[argmin] # minimum distance minDistance = distances[argmin] # minimum distance
if minDistance <= THRESHOLD:
label = DBHelper.get_firstname(index[argmin]) + "_" + DBHelper.get_lastname(index[argmin])
cond = True
else:
label = 'unknown'
cond = False
if minDistance <= THRESHOLD: # print("time taken = {:.3f} seconds".format(time.time() - t))
label = index[argmin]
else:
label = 'unknown'
#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.imshow('img', im)
cv2.rectangle(im, (x1, y1), (x2, y2), (0, 255, 0), 2) k = cv2.waitKey(1) & 0xff
font_face = cv2.FONT_HERSHEY_SIMPLEX if k == 27:
font_scale = 0.8 break
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)
count += 1
if cond:
DBHelper.set_motor("on")
DBHelper.set_alarm("off")
elif not cond:
DBHelper.set_motor("off")
DBHelper.set_alarm("on")
cv2.imshow('img', im) DBHelper.set_alarm("off")
DBHelper.set_motor("off")
k = cv2.waitKey(1) & 0xff
if k == 27:
break
count += 1
cv2.destroyAllWindows() cv2.destroyAllWindows()

View file

@ -4,6 +4,7 @@ import math
import cv2 import cv2
import Facial_Recognition_Enrollment import Facial_Recognition_Enrollment
def register_your_face(label): def register_your_face(label):
num_cap = 50 num_cap = 50
@ -37,6 +38,6 @@ if __name__ == "__main__":
register_your_face(label) register_your_face(label)
print("Data saved! Starting enrollment...") print("Data saved! Starting enrollment...")
print() 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("Face registration completed!")
print() print()

View file

@ -1,5 +1,6 @@
import DBHelper import DBHelper
import Facial_Recognition_Registration import Facial_Recognition_Registration
import Facial_Recognition_Enrollment
def upload_your_face(firstname, lastname, email, phone): def upload_your_face(firstname, lastname, email, phone):
@ -13,6 +14,7 @@ def upload_your_face(firstname, lastname, email, phone):
count += 1 count += 1
DBHelper.upload_data("User_" + str(count), firstname, lastname, email, phone) DBHelper.upload_data("User_" + str(count), firstname, lastname, email, phone)
Facial_Recognition_Registration.register_your_face("User_" + str(count)) Facial_Recognition_Registration.register_your_face("User_" + str(count))
Facial_Recognition_Enrollment.enroll_face_dataset()
for i in range(20): for i in range(20):
DBHelper.upload_user_photo("User_" + str(count) + "/" + str(i) + ".jpg") DBHelper.upload_user_photo("User_" + str(count) + "/" + str(i) + ".jpg")
except: except:

Binary file not shown.

View file

@ -1,6 +1,6 @@
import os import os
import DBHelper import DBHelper
import Facial_Recognition_Wrapper import Facial_Recognition_Inference
def start(): def start():
@ -32,8 +32,7 @@ def start():
print("Success.") print("Success.")
except: except:
print("No Thieves are registered.") print("No Thieves are registered.")
Facial_Recognition_Wrapper.training_recognizer("LBPH") Facial_Recognition_Inference
Facial_Recognition_Wrapper.face_recognition_inference("LBPH")
if __name__ == "__main__": if __name__ == "__main__":