2020-11-08 21:57:59 +00:00
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import math
|
|
|
|
import cv2
|
|
|
|
import dlib
|
|
|
|
import numpy as np
|
|
|
|
import Facial_Recognition_Render as fr
|
|
|
|
import _pickle as cPickle
|
|
|
|
import glob
|
2020-11-18 02:56:47 +00:00
|
|
|
import DBHelper
|
2020-11-08 21:57:59 +00:00
|
|
|
|
|
|
|
faceWidth = 320
|
|
|
|
faceHeight = 320
|
|
|
|
SKIP_FRAMES = 1
|
|
|
|
|
2020-11-14 21:38:19 +00:00
|
|
|
|
2020-11-08 21:57:59 +00:00
|
|
|
def alignFace(imFace, landmarks):
|
|
|
|
l_x = landmarks[39][0]
|
|
|
|
l_y = landmarks[39][1]
|
|
|
|
r_x = landmarks[42][0]
|
|
|
|
r_y = landmarks[42][1]
|
|
|
|
dy = r_y - l_y
|
|
|
|
dx = r_x - l_x
|
|
|
|
# Convert from radians to degrees
|
2020-11-14 21:38:19 +00:00
|
|
|
angle = math.atan2(dy, dx) * 180.0 / math.pi
|
2020-11-08 21:57:59 +00:00
|
|
|
|
2020-11-14 21:38:19 +00:00
|
|
|
eyesCenter = ((l_x + r_x) * 0.5, (l_y + r_y) * 0.5)
|
2020-11-08 21:57:59 +00:00
|
|
|
rotMatrix = cv2.getRotationMatrix2D(eyesCenter, angle, 1)
|
|
|
|
alignedImFace = np.zeros(imFace.shape, dtype=np.uint8)
|
2020-11-14 21:38:19 +00:00
|
|
|
alignedImFace = cv2.warpAffine(imFace, rotMatrix, (imFace.shape[1], imFace.shape[0]))
|
2020-11-08 21:57:59 +00:00
|
|
|
return alignedImFace
|
|
|
|
|
|
|
|
|
2020-11-14 21:38:19 +00:00
|
|
|
def face_detector_haarcascade(image):
|
|
|
|
grey = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
2020-11-08 21:57:59 +00:00
|
|
|
|
|
|
|
resize_fx = 1
|
|
|
|
resize_fy = 1
|
2020-11-14 21:38:19 +00:00
|
|
|
grey = cv2.resize(grey, dsize=None, fx=resize_fx, fy=resize_fy, interpolation=cv2.INTER_AREA)
|
2020-11-08 21:57:59 +00:00
|
|
|
|
|
|
|
pwd = sys.path[0]
|
|
|
|
classfier = cv2.CascadeClassifier(pwd + "/Facial_models/haarcascade_frontalface_alt2.xml")
|
|
|
|
|
2020-11-14 21:38:19 +00:00
|
|
|
faceRects = classfier.detectMultiScale(grey, scaleFactor=1.2, minNeighbors=1, minSize=(16, 16))
|
2020-11-08 21:57:59 +00:00
|
|
|
|
|
|
|
if len(faceRects) > 0:
|
|
|
|
for faceRect in faceRects:
|
|
|
|
x, y, w, h = faceRect
|
2020-11-14 21:38:19 +00:00
|
|
|
x = int(x / resize_fx)
|
|
|
|
y = int(y / resize_fy)
|
|
|
|
w = int(w / resize_fx)
|
|
|
|
h = int(h / resize_fy)
|
|
|
|
cv2.rectangle(image, (x - 10, y - 10), (x + w + 10, y + h + 10), (0, 255, 0), 5)
|
2020-11-08 21:57:59 +00:00
|
|
|
|
|
|
|
return image
|
|
|
|
|
|
|
|
|
2020-11-14 21:38:19 +00:00
|
|
|
def face_detector_ssd(image):
|
2020-11-08 21:57:59 +00:00
|
|
|
pwd = sys.path[0]
|
2020-11-14 21:38:19 +00:00
|
|
|
net = cv2.dnn.readNetFromCaffe(pwd + "/Facial_models/deploy.prototxt",
|
|
|
|
pwd + "/Facial_models/res10_300x300_ssd_iter_140000_fp16.caffemodel")
|
2020-11-08 21:57:59 +00:00
|
|
|
|
|
|
|
resize = (300, 300)
|
|
|
|
confidence_thres = 0.65
|
2020-11-14 21:38:19 +00:00
|
|
|
|
2020-11-08 21:57:59 +00:00
|
|
|
blob = cv2.dnn.blobFromImage(cv2.resize(image, dsize=resize), 1.0, resize, (104.0, 177.0, 123.0))
|
2020-11-14 21:38:19 +00:00
|
|
|
# blob = cv2.dnn.blobFromImage(cv2.resize(image, (300, 300)), 1.0, (300, 300), (104.0, 177.0, 123.0))
|
2020-11-08 21:57:59 +00:00
|
|
|
|
|
|
|
net.setInput(blob)
|
|
|
|
|
|
|
|
detections = net.forward()
|
|
|
|
|
2020-11-14 21:38:19 +00:00
|
|
|
h, w, c = image.shape
|
2020-11-08 21:57:59 +00:00
|
|
|
|
|
|
|
for i in range(0, detections.shape[2]):
|
|
|
|
confidence = detections[0, 0, i, 2]
|
|
|
|
if confidence > confidence_thres:
|
|
|
|
box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
|
|
|
|
(startX, startY, endX, endY) = box.astype("int")
|
|
|
|
text = "{:.2f}%".format(confidence * 100)
|
|
|
|
y = startY - 10 if startY - 10 > 10 else startY + 10
|
2020-11-14 21:38:19 +00:00
|
|
|
cv2.rectangle(image, (startX, startY), (endX, endY), (0, 255, 0), 5)
|
2020-11-08 21:57:59 +00:00
|
|
|
cv2.putText(image, text, (startX, y), cv2.FONT_HERSHEY_SIMPLEX, 1.00, (0, 255, 0), 3)
|
2020-11-14 21:38:19 +00:00
|
|
|
|
2020-11-08 21:57:59 +00:00
|
|
|
return image
|
|
|
|
|
2020-11-14 21:38:19 +00:00
|
|
|
|
2020-11-08 21:57:59 +00:00
|
|
|
def training_data_loader():
|
2020-11-14 21:38:19 +00:00
|
|
|
imagesFolder = sys.path[0] + "/Facial_images/face_rec/train/"
|
2020-11-08 21:57:59 +00:00
|
|
|
subfolders = []
|
|
|
|
|
|
|
|
for x in os.listdir(imagesFolder):
|
|
|
|
xpath = os.path.join(imagesFolder, x)
|
|
|
|
if os.path.isdir(xpath):
|
|
|
|
subfolders.append(xpath)
|
|
|
|
|
|
|
|
imagePaths = []
|
|
|
|
labels = []
|
|
|
|
labelsMap = {}
|
|
|
|
labelsMap[-1] = "unknown"
|
|
|
|
|
|
|
|
for i, subfolder in enumerate(subfolders):
|
|
|
|
labelsMap[i] = os.path.basename(subfolder)
|
|
|
|
for x in os.listdir(subfolder):
|
|
|
|
xpath = os.path.join(subfolder, x)
|
|
|
|
if x.endswith('jpg') or x.endswith('pgm'):
|
|
|
|
imagePaths.append(xpath)
|
|
|
|
labels.append(i)
|
|
|
|
|
|
|
|
imagesFaceTrain = []
|
|
|
|
labelsFaceTrain = []
|
|
|
|
|
2020-11-14 21:38:19 +00:00
|
|
|
faceDetector = dlib.get_frontal_face_detector()
|
|
|
|
landmarkDetector = dlib.shape_predictor(sys.path[0] + "/Facial_models/shape_predictor_68_face_landmarks.dat")
|
|
|
|
|
2020-11-08 21:57:59 +00:00
|
|
|
for j, imagePath in enumerate(imagePaths):
|
|
|
|
im = cv2.imread(imagePath, 0)
|
|
|
|
imHeight, imWidth = im.shape[:2]
|
|
|
|
|
|
|
|
landmarks = fr.getLandmarks(faceDetector, landmarkDetector, im)
|
|
|
|
|
|
|
|
landmarks = np.array(landmarks)
|
2020-11-14 21:38:19 +00:00
|
|
|
|
2020-11-08 21:57:59 +00:00
|
|
|
if len(landmarks) == 68:
|
|
|
|
x1Limit = landmarks[0][0] - (landmarks[36][0] - landmarks[0][0])
|
|
|
|
x2Limit = landmarks[16][0] + (landmarks[16][0] - landmarks[45][0])
|
2020-11-14 21:38:19 +00:00
|
|
|
y1Limit = landmarks[27][1] - 3 * (landmarks[30][1] - landmarks[27][1])
|
2020-11-08 21:57:59 +00:00
|
|
|
y2Limit = landmarks[8][1] + (landmarks[30][1] - landmarks[29][1])
|
|
|
|
|
2020-11-14 21:38:19 +00:00
|
|
|
x1 = max(x1Limit, 0)
|
2020-11-08 21:57:59 +00:00
|
|
|
x2 = min(x2Limit, imWidth)
|
|
|
|
y1 = max(y1Limit, 0)
|
|
|
|
y2 = min(y2Limit, imHeight)
|
|
|
|
imFace = im[y1:y2, x1:x2]
|
|
|
|
|
|
|
|
alignedFace = alignFace(imFace, landmarks)
|
|
|
|
alignedFace = cv2.resize(alignedFace, (faceHeight, faceWidth))
|
|
|
|
|
2020-11-14 21:38:19 +00:00
|
|
|
imagesFaceTrain.append(np.float32(alignedFace) / 255.0)
|
2020-11-08 21:57:59 +00:00
|
|
|
labelsFaceTrain.append(labels[j])
|
2020-11-14 21:38:19 +00:00
|
|
|
|
2020-11-08 21:57:59 +00:00
|
|
|
return imagesFaceTrain, labelsFaceTrain, labelsMap
|
|
|
|
|
2020-11-14 21:38:19 +00:00
|
|
|
|
2020-11-08 21:57:59 +00:00
|
|
|
def training_recognizer(rec_type):
|
|
|
|
imagesFaceTrain, labelsFaceTrain, labelsMap = training_data_loader()
|
|
|
|
|
2020-11-14 21:38:19 +00:00
|
|
|
if (rec_type == 'LBPH'):
|
2020-11-08 21:57:59 +00:00
|
|
|
faceRecognizer = cv2.face.LBPHFaceRecognizer_create()
|
|
|
|
print("Training using LBPH Faces")
|
2020-11-14 21:38:19 +00:00
|
|
|
elif (rec_type == 'Eigen'):
|
2020-11-08 21:57:59 +00:00
|
|
|
faceRecognizer = cv2.face.EigenFaceRecognizer_create()
|
|
|
|
print("Training using Eigen Faces")
|
2020-11-14 21:38:19 +00:00
|
|
|
elif (rec_type == 'Fisher'):
|
2020-11-08 21:57:59 +00:00
|
|
|
faceRecognizer = cv2.face.FisherFaceRecognizer_create()
|
|
|
|
print("Training using Fisher Faces")
|
|
|
|
|
|
|
|
faceRecognizer.train(imagesFaceTrain, np.array(labelsFaceTrain))
|
2020-11-14 21:38:19 +00:00
|
|
|
faceRecognizer.write(sys.path[0] + '/Facial_models/face_rec_model.yml')
|
2020-11-08 21:57:59 +00:00
|
|
|
|
2020-11-14 21:38:19 +00:00
|
|
|
with open(sys.path[0] + '/Facial_models/labels_map.pkl', 'wb') as f:
|
2020-11-08 21:57:59 +00:00
|
|
|
cPickle.dump(labelsMap, f)
|
|
|
|
|
2020-11-14 21:38:19 +00:00
|
|
|
|
2020-11-08 21:57:59 +00:00
|
|
|
def face_recognition_inference(rec_type):
|
2020-11-14 21:38:19 +00:00
|
|
|
# testFiles = glob.glob(sys.path[0]+'/Facial_test_images/face_rec/test/*.jpg')
|
|
|
|
# testFiles.sort()
|
2020-11-08 21:57:59 +00:00
|
|
|
i = 0
|
|
|
|
correct = 0
|
|
|
|
error = 0
|
|
|
|
faceDetector = dlib.get_frontal_face_detector()
|
2020-11-12 16:05:57 +00:00
|
|
|
print(sys.path[0])
|
2020-11-14 21:38:19 +00:00
|
|
|
landmarkDetector = dlib.shape_predictor(sys.path[0] + '/Facial_models/shape_predictor_68_face_landmarks.dat')
|
|
|
|
|
2020-11-15 01:38:37 +00:00
|
|
|
if rec_type == 'LBPH':
|
2020-11-08 21:57:59 +00:00
|
|
|
faceRecognizer = cv2.face.LBPHFaceRecognizer_create()
|
|
|
|
print("Test using LBPH Faces")
|
2020-11-15 01:38:37 +00:00
|
|
|
elif rec_type == 'Eigen':
|
2020-11-08 21:57:59 +00:00
|
|
|
faceRecognizer = cv2.face.EigenFaceRecognizer_create()
|
|
|
|
print("Test using Eigen Faces")
|
2020-11-15 01:38:37 +00:00
|
|
|
elif rec_type == 'Fisher':
|
2020-11-08 21:57:59 +00:00
|
|
|
faceRecognizer = cv2.face.FisherFaceRecognizer_create()
|
|
|
|
print("Test using Fisher Faces")
|
2020-11-14 21:38:19 +00:00
|
|
|
|
|
|
|
faceRecognizer.read(sys.path[0] + '/Facial_models/face_rec_model.yml')
|
|
|
|
labelsMap = np.load(sys.path[0] + '/Facial_models/labels_map.pkl', allow_pickle=True)
|
2020-11-08 21:57:59 +00:00
|
|
|
|
2020-11-12 16:05:57 +00:00
|
|
|
cam = cv2.VideoCapture(0)
|
2020-11-08 21:57:59 +00:00
|
|
|
|
2020-11-18 04:14:51 +00:00
|
|
|
while DBHelper.get_power() == "on":
|
2020-11-14 21:38:19 +00:00
|
|
|
# imagePath = testFiles[i]
|
2020-11-08 21:57:59 +00:00
|
|
|
success, original = cam.read()
|
|
|
|
im = cv2.resize(original, (640, 480))
|
|
|
|
i += 1
|
|
|
|
|
2020-11-14 21:38:19 +00:00
|
|
|
im = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
|
2020-11-08 21:57:59 +00:00
|
|
|
imHeight, imWidth = im.shape[:2]
|
|
|
|
|
|
|
|
landmarks = fr.getLandmarks(faceDetector, landmarkDetector, im)
|
|
|
|
landmarks = np.array(landmarks)
|
|
|
|
|
2020-11-19 16:58:04 +00:00
|
|
|
cond = False
|
|
|
|
|
|
|
|
if len(landmarks) == 68:
|
2020-11-08 21:57:59 +00:00
|
|
|
x1Limit = landmarks[0][0] - (landmarks[36][0] - landmarks[0][0])
|
|
|
|
x2Limit = landmarks[16][0] + (landmarks[16][0] - landmarks[45][0])
|
2020-11-14 21:38:19 +00:00
|
|
|
y1Limit = landmarks[27][1] - 3 * (landmarks[30][1] - landmarks[27][1])
|
2020-11-08 21:57:59 +00:00
|
|
|
y2Limit = landmarks[8][1] + (landmarks[30][1] - landmarks[29][1])
|
|
|
|
|
2020-11-14 21:38:19 +00:00
|
|
|
x1 = max(x1Limit, 0)
|
2020-11-08 21:57:59 +00:00
|
|
|
x2 = min(x2Limit, imWidth)
|
|
|
|
y1 = max(y1Limit, 0)
|
|
|
|
y2 = min(y2Limit, imHeight)
|
|
|
|
imFace = im[y1:y2, x1:x2]
|
|
|
|
|
|
|
|
alignedFace = alignFace(imFace, landmarks)
|
|
|
|
alignedFace = cv2.resize(alignedFace, (faceHeight, faceWidth))
|
2020-11-14 21:38:19 +00:00
|
|
|
imFaceFloat = np.float32(alignedFace) / 255.0
|
2020-11-08 21:57:59 +00:00
|
|
|
|
|
|
|
predictedLabel = -1
|
|
|
|
predictedLabel, score = faceRecognizer.predict(imFaceFloat)
|
2020-11-14 21:38:19 +00:00
|
|
|
center = (int((x1 + x2) / 2), int((y1 + y2) / 2))
|
|
|
|
radius = int((y2 - y1) / 2.0)
|
|
|
|
text = '{} {}%'.format(labelsMap[predictedLabel], round(score, 5))
|
|
|
|
cv2.rectangle(original, (x1, y1), (x2, y2), (0, 255, 0), 5)
|
|
|
|
cv2.putText(original, text, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 3)
|
2020-11-19 16:58:04 +00:00
|
|
|
cond = True
|
|
|
|
|
|
|
|
if cond:
|
2020-11-18 02:56:47 +00:00
|
|
|
DBHelper.set_motor("on")
|
2020-11-19 16:58:04 +00:00
|
|
|
DBHelper.set_alarm("off")
|
|
|
|
else:
|
|
|
|
DBHelper.set_motor("off")
|
|
|
|
DBHelper.set_alarm("on")
|
2020-11-08 21:57:59 +00:00
|
|
|
|
|
|
|
cv2.imshow('Face Recognition Demo', original)
|
|
|
|
|
|
|
|
k = cv2.waitKey(10)
|
|
|
|
|
2020-11-19 16:58:04 +00:00
|
|
|
DBHelper.set_alarm("off")
|
|
|
|
DBHelper.set_motor("off")
|
2020-11-08 21:57:59 +00:00
|
|
|
cam.release()
|
2020-11-14 21:38:19 +00:00
|
|
|
cv2.destroyAllWindows()
|
2020-11-08 21:57:59 +00:00
|
|
|
|
|
|
|
|
2020-11-14 21:38:19 +00:00
|
|
|
if __name__ == "__main__":
|
2020-11-08 21:57:59 +00:00
|
|
|
mode = 'test'
|
2020-11-19 16:58:04 +00:00
|
|
|
rec_type = 'LBPH' # 'LBPH' 'Fisher' 'Eigen'
|
2020-11-08 21:57:59 +00:00
|
|
|
|
|
|
|
if mode == 'train':
|
|
|
|
training_recognizer(rec_type)
|
2020-11-15 01:38:37 +00:00
|
|
|
elif mode == 'test':
|
2020-11-08 21:57:59 +00:00
|
|
|
face_recognition_inference(rec_type)
|
|
|
|
|
|
|
|
# video process (keep it in case if needed)
|
|
|
|
'''
|
|
|
|
cameraCapture = cv2.VideoCapture(1)
|
|
|
|
success, frame = cameraCapture.read()
|
|
|
|
|
|
|
|
while success and cv2.waitKey(1) == -1:
|
|
|
|
success, frame = cameraCapture.read()
|
|
|
|
face_detector_ssd(frame)
|
|
|
|
cv2.imshow("video", frame)
|
|
|
|
|
|
|
|
cameraCapture.release()
|
|
|
|
cv2.destroyAllWindows()
|
|
|
|
'''
|
|
|
|
|
|
|
|
# image process (keep it in case if needed)
|
|
|
|
'''
|
|
|
|
image_name = "8.jpg"
|
|
|
|
split_name = image_name.split(".")
|
|
|
|
|
|
|
|
image_read_path = sys.path[0]+"/Facial_test_images/"+image_name
|
|
|
|
image_save_path = sys.path[0]+"/Facial_test_images/output/"+split_name[0]+"_result."+split_name[1]
|
|
|
|
|
|
|
|
image = cv2.imread(image_read_path)
|
|
|
|
|
|
|
|
image = face_detector_ssd(image)
|
|
|
|
image = face_detector_haarcascade(image)
|
|
|
|
|
|
|
|
print(image_save_path)
|
|
|
|
|
|
|
|
cv2.imwrite(image_save_path, image)
|
|
|
|
cv2.imshow("result", image)
|
|
|
|
cv2.waitKey()
|
|
|
|
cv2.destroyAllWindows()
|
|
|
|
'''
|