-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathface_recognition_videos.py
More file actions
61 lines (51 loc) · 2.29 KB
/
face_recognition_videos.py
File metadata and controls
61 lines (51 loc) · 2.29 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
import pickle
import cv2
from utils import face_rects
from utils import face_encodings
from utils import nb_of_matches
# load the encodings + names dictionary
with open("encodings.pickle", "rb") as f:
name_encodings_dict = pickle.load(f)
video_cap = cv2.VideoCapture(0)
while True:
_, frame = video_cap.read()
# get the 128-d face embeddings for each face in the input frame
encodings = face_encodings(frame)
# this list will contain the names of each face detected in the frame
names = []
# loop over the encodings
for encoding in encodings:
# initialize a dictionary to store the name of the
# person and the number of times it was matched
counts = {}
# loop over the known encodings
for (name, encodings) in name_encodings_dict.items():
# compute the number of matches between the current encoding and the encodings
# of the known faces and store the number of matches in the dictionary
counts[name] = nb_of_matches(encodings, encoding)
# check if all the number of matches are equal to 0
# if there is no match for any name, then we set the name to "Unknown"
if all(count == 0 for count in counts.values()):
name = "Unknown"
# otherwise, we get the name with the highest number of matches
else:
name = max(counts, key=counts.get)
# add the name to the list of names
names.append(name)
# loop over the `rectangles` of the faces in the
# input frame using the `face_rects` function
for rect, name in zip(face_rects(frame), names):
# get the bounding box for each face using the `rect` variable
x1, y1, x2, y2 = rect.left(), rect.top(), rect.right(), rect.bottom()
# draw the bounding box of the face along with the name of the person
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
cv2.putText(frame, name, (x1, y1 - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 255, 0), 2)
# show the output frame
cv2.imshow("frame", frame)
# wait for 1 milliseconde and if the q key is pressed, we break the loop
if cv2.waitKey(1) == ord('q'):
break
# release the video capture and close all windows
video_cap.release()
cv2.destroyAllWindows()