-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetect.py
More file actions
56 lines (46 loc) · 1.84 KB
/
detect.py
File metadata and controls
56 lines (46 loc) · 1.84 KB
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
import face_recognition
from main.yolo_detect import YoloFaceDetection
class FaceRecognition:
def __init__(self, weights_path, config_path, labels_path):
self.__encodings = []
self.yolo_detect = YoloFaceDetection(weights_path, config_path, labels_path)
def set_encodings(self, encodings: list):
self.__encodings = encodings
def add_encoding(self, encoding: list):
self.__encodings.append(encoding)
def remove_encoding(self, encoding: list):
self.__encodings.remove(encoding)
def get_encodings(self):
return self.__encodings
@staticmethod
def detect_face(image_as_array):
return face_recognition.face_locations(image_as_array)
'''
return [{
'face_location': (x,y,a,b),
'match': true/false
}]
'''
def process_image(self, image_as_array):
matches = []
# find faces that match
face_locations = self.yolo_detect.face_detect(image_as_array)
# face_locations = self.detect_face(image_as_array)
face_encodings = face_recognition.face_encodings(image_as_array, face_locations)
for face_encoding in face_encodings:
match = face_recognition.compare_faces(self.__encodings, face_encoding)
# if face match one face in known face encodings
matches.append(True in match)
result = []
for face_location, match in zip(face_locations, matches):
result.append({
'face_location': face_location,
'match': match
})
return result, face_encodings
@staticmethod
def get_encoding(image_as_array):
encodings = face_recognition.face_encodings(image_as_array)
if len(encodings) != 1:
raise Exception("Please choose image that has one face")
return encodings[0]