-
Notifications
You must be signed in to change notification settings - Fork 0
/
video_face_recognition.py
83 lines (64 loc) · 3.62 KB
/
video_face_recognition.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# importing the required libraries
import cv2
import face_recognition
# capture the video from default camera
webcam_video_stream = cv2.VideoCapture('modi.mp4') # MAKE VIDEO FOR THIS
# TODO : make video for this man! ^^^^
# load the sample images and get the 128 face embeddings from them
beyonce_image = face_recognition.load_image_file('/Users/dc/Desktop/portfoliocCodes/Face/lib/Images/Test/Beyonce.jpg')
beyonce_face_encodings = face_recognition.face_encodings(beyonce_image)[0]
jayz_image = face_recognition.load_image_file('/Users/dc/Desktop/portfoliocCodes/Face/lib/Images/Test/Jay-z.jpg')
jayz_face_encodings = face_recognition.face_encodings(jayz_image)[0]
daniel_image = face_recognition.load_image_file('/Users/dc/Desktop/portfoliocCodes/Face/lib/Images/Test/Daniel.jpeg')
Daniel_face_encodings = face_recognition.face_encodings(daniel_image)[0]
# save the encodings and the corresponding labels in seperate arrays in the same order
known_face_encodings = [beyonce_face_encodings, jayz_face_encodings, Daniel_face_encodings]
known_face_names = ["Beyoncé", "Jay-z", "Daniel"]
# initialize the array variable to hold all face locations, encodings and names
all_face_locations = []
all_face_encodings = []
all_face_names = []
# loop through every frame in the video
while True:
# get the current frame from the video stream as an image
ret, current_frame = webcam_video_stream.read()
# resize the current frame to 1/4 size to proces faster
current_frame_small = cv2.resize(current_frame, (0, 0), fx=0.25, fy=0.25)
# detect all faces in the image
# arguments are image,no_of_times_to_upsample, model
all_face_locations = face_recognition.face_locations(current_frame_small, number_of_times_to_upsample=1,
model='hog')
# detect face encodings for all the faces detected
all_face_encodings = face_recognition.face_encodings(current_frame_small, all_face_locations)
# looping through the face locations and the face embeddings
for current_face_location, current_face_encoding in zip(all_face_locations, all_face_encodings):
# splitting the tuple to get the four position values of current face
top_pos, right_pos, bottom_pos, left_pos = current_face_location
# change the position maginitude to fit the actual size video frame
top_pos = top_pos * 4
right_pos = right_pos * 4
bottom_pos = bottom_pos * 4
left_pos = left_pos * 4
# find all the matches and get the list of matches
all_matches = face_recognition.compare_faces(known_face_encodings, current_face_encoding)
# string to hold the label
name_of_person = 'Unknown face'
# check if the all_matches have at least one item
# if yes, get the index number of face that is located in the first index of all_matches
# get the name corresponding to the index number and save it in name_of_person
if True in all_matches:
first_match_index = all_matches.index(True)
name_of_person = known_face_names[first_match_index]
# draw rectangle around the face
cv2.rectangle(current_frame, (left_pos, top_pos), (right_pos, bottom_pos), (255, 0, 0), 2)
# display the name as text in the image
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(current_frame, name_of_person, (left_pos, bottom_pos), font, 0.5, (255, 255, 255), 1)
# display the video
cv2.imshow("Webcam Video", current_frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# release the stream and cam
# close all opencv windows open
webcam_video_stream.release()
cv2.destroyAllWindows()