-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfaces_recognizer.py
More file actions
74 lines (65 loc) · 2.41 KB
/
Copy pathfaces_recognizer.py
File metadata and controls
74 lines (65 loc) · 2.41 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import face_recognition
import cv2
MODEL ="hog"
TOLERANCE = 0.2
KNOWN_FACES = {}
KNOWN_FACES_ENCODINGS = []
FONT = cv2.FONT_HERSHEY_SIMPLEX
FONT_SIZE = 0.8
FONT_THIKNESS = 1
FRAME_THIKNESS = 2
SCALER = 4
def create_face_encodings (frame):
KNOWN_FACES_ENCODINGS = []
try:
if frame is not None:
locate_faces = face_recognition.face_locations(frame, model=MODEL, number_of_times_to_upsample=2)
face_encoded = face_recognition.face_encodings(frame, num_jitters=2)
KNOWN_FACES_ENCODINGS.append(face_encoded)
except:
pass
return KNOWN_FACES_ENCODINGS, len(locate_faces)
def interpret_results(state, name = None):
if state :
text = name
color = (237, 149, 100)
else:
text = "Unrecognized"
color = (122, 20, 255)
return text, color
def show_results(image, locs, name, location):
for idx, loc in enumerate(locs):
if location is not None and idx == location:
text, color = interpret_results(True, name)
else:
text, color = interpret_results(False)
top_left = (loc[3]*SCALER, loc[0]*SCALER)
bottom_right = (loc[1]*SCALER, loc[2]*SCALER)
height = (loc[1]*SCALER, max(30, SCALER*(loc[0]+int(abs(loc[2]-loc[0])/6))))
cv2.rectangle(image, top_left, bottom_right, color, FRAME_THIKNESS)
cv2.rectangle(image, top_left, height, color, cv2.FILLED)
cv2.putText(image, text,
(5+loc[3]*SCALER, SCALER*(loc[0]+int(abs(loc[1]-loc[3])/9.5))),
FONT, min(FONT_SIZE, abs(loc[3]-loc[1])*SCALER/250),
(255,255,255), FONT_THIKNESS)
return image
def identify_faces(frame):
name = None
location = None
WIDTH = frame.shape[1]
HEIGHT = frame.shape[0]
resized_image = cv2.resize(frame, (int(WIDTH/SCALER),int(HEIGHT/SCALER)))
locate_faces = face_recognition.face_locations(resized_image, model=MODEL)
if len(KNOWN_FACES.items()):
encodings = face_recognition.face_encodings(resized_image, locate_faces)
for face_encoding in encodings :
for name, faces in KNOWN_FACES.items():
result = []
for face in faces:
result += face_recognition.compare_faces(face, face_encoding, TOLERANCE)
if True in result :
location = result.index(True)
return show_results(image=frame, locs=locate_faces, name=name.capitalize(), location= location)
else:
return show_results(image=frame, locs=locate_faces, name=name, location= location)
return show_results(image=frame, locs=locate_faces, name=name, location= location)