-
Notifications
You must be signed in to change notification settings - Fork 0
/
faces.py
60 lines (43 loc) · 1.69 KB
/
faces.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
from setup import load_labels, save_labels, init_labels, get_dlib_components
from settings import ANONYMOUS_UPN
from scipy.spatial import distance
import cv2
class FaceRecognition:
def __init__(self, unpickle=False):
self.detector, self.frm, self.sp = get_dlib_components()
self.threshold = 0.55
if unpickle:
self.labels = load_labels()
else:
self.labels = init_labels()
self.alert_ready()
@staticmethod
def alert_ready():
print('Recognizer is ready')
def face_recognition(self):
cap = cv2.VideoCapture(0)
_, frame = cap.read()
cap.release()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
dets = self.detector(gray)
if len(dets) == 0:
return [ANONYMOUS_UPN]
else:
result = []
for _, d in enumerate(dets):
shape = self.sp(gray, d)
face_descriptor = self.frm.compute_face_descriptor(gray, shape)
min_distance = 1000
our_label = ''
for labelDictKey in self.labels:
for vectorDict in self.labels[labelDictKey]:
calc_distance = distance.euclidean(vectorDict, face_descriptor)
if calc_distance < min_distance:
min_distance = calc_distance
our_label = labelDictKey
if min_distance < self.threshold:
predict_name = our_label
result.append(predict_name)
self.labels[our_label].append(face_descriptor)
save_labels(self.labels)
return result