-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFacial_Recognition_Inference.py
119 lines (90 loc) · 3.61 KB
/
Facial_Recognition_Inference.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import os, sys, time
import dlib
import cv2
import numpy as np
import DBHelper
import Upload_Thief
def inference():
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'
SKIP_FRAMES = 1
THRESHOLD = 0.4
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')
cam = cv2.VideoCapture(0)
count = 0
x1 = x2 = y1 = y2 = 0
cond = False
thief = False
label = 'unknown'
start = time.time()
while DBHelper.get_power() == "on":
success, im = cam.read()
if not success:
print('cannot capture input from camera')
break
if (count % SKIP_FRAMES) == 0:
img = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
faces = faceDetector(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
minDistance = 0.0
for face in faces:
shape = shapePredictor(cv2.cvtColor(img, cv2.COLOR_BGR2RGB), face)
x1 = face.left()
y1 = face.top()
x2 = face.right()
y2 = face.bottom()
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, :]
# 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
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.imshow('img', im)
k = cv2.waitKey(1) & 0xff
if k == 27:
break
count += 1
if cond:
DBHelper.set_motor("on")
DBHelper.set_alarm("off")
start = time.time()
elif not cond:
DBHelper.set_motor("off")
DBHelper.set_alarm("on")
end = time.time()
if end - start >= 30:
thief = True
DBHelper.set_power("off")
DBHelper.set_alarm("off")
DBHelper.set_motor("off")
cv2.destroyAllWindows()
if thief:
Upload_Thief.upload_thief_face()
if __name__ == "__main__":
inference()