-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrecognizer.py
27 lines (23 loc) · 955 Bytes
/
recognizer.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
import numpy as np
import pickle
class FaceRecognizer():
DESCRIPTORS = "face_descriptors.npy"
LABELS = "labels.pickle"
def __init__(self):
print("Retrieving recognition database...")
self.descriptors = np.load(FaceRecognizer.DESCRIPTORS)
# will be loaded as a 1D array, so needs to be
# reshaped back into a n x 128 arrary
self.descriptors = self.descriptors.reshape (-1,128)
f = open(FaceRecognizer.LABELS, 'rb')
self.labels = pickle.load(f) # in bytes
def recognize_face(self, face_descriptor, threshold = 0.7):
distances = np.linalg.norm(self.descriptors - face_descriptor, axis=1)
argmin = np.argmin(distances)
min_dist = distances[argmin]
if min_dist > (1 - threshold):
name = "unknown"
else:
name = self.labels[argmin]
print(name,"@",str(int(100.0*(1-min_dist))) + "%")
return name