-
Notifications
You must be signed in to change notification settings - Fork 1
/
facelist.py
51 lines (43 loc) · 1.62 KB
/
facelist.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
from recognizer import FaceRecognizer
class FaceList:
"""
List of active faces from face recognition
"""
EXPIRY = 10
MAX_DIST = 0.75
def __init__(self):
self.faces = []
self.face_recog = FaceRecognizer()
def find_face_dist(self, pos1, pos2):
# takes two positions and returns euclidean distance
y_dist = pos2['h_offset'] - pos1['h_offset']
x_dist = pos2['v_offset'] - pos1['v_offset']
z_dist = pos2['z_dist'] - pos1['z_dist']
dist = ((x_dist**2) + (y_dist**2) + (z_dist**2)) ** 0.5
return dist
def expire_faces(self):
for face in self.faces:
face['age'] += 1
if face['age'] > 10:
self.faces.remove(face)
def find_close_face(self, search_face):
for face in self.faces:
# cycle through faces and calculate Euclidean distance
# and if within MAX_DIST then call it the same face
search_pos = search_face['position']
dist = self.find_face_dist(search_pos, face['position'])
if dist > FaceList.MAX_DIST:
self.add_face(search_face)
else:
face['position'] = search_pos
def add_face(self, face_dict):
# identify face and update face_dict
# with name etc.
face_dict['age'] = 0
face_dict['name'] = self.face_recog.recognize_face(face_dict['face_descriptor'])
self.faces.append(face_dict)
def update_list(self, face_dict):
if len(self.faces) == 0:
self.add_face(face_dict)
else:
self.find_close_face(face_dict)