-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrealtime_face_detection.py
51 lines (39 loc) · 1.81 KB
/
realtime_face_detection.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
import cv2
import face_recognition
webcam_video_stream = cv2.VideoCapture(0) # ou flux vidéo
raphael_image = face_recognition.load_image_file("images/raphael.jpg")
raphael_face_encodings = face_recognition.face_encodings(raphael_image)[0]
known_face_encodings = [raphael_face_encodings]
known_face_names = ["Adorable Chaton"]
all_face_locations = []
all_face_encodings = []
all_face_names = []
while True:
ret, current_frame = webcam_video_stream.read()
current_frame_small = cv2.resize(current_frame, (0, 0), fx=0.25, fy=0.25)
all_face_locations = face_recognition.face_locations(
current_frame_small, number_of_times_to_upsample=1, model="hog")
all_face_encodings = face_recognition.face_encodings(
current_frame_small, all_face_locations)
for current_face_location, current_face_encoding in zip(all_face_locations, all_face_encodings):
top_pos, right_pos, bottom_pos, left_pos = current_face_location
top_pos = top_pos * 4
right_pos = right_pos * 4
bottom_pos = bottom_pos * 4
left_pos = left_pos * 4
all_matches = face_recognition.compare_faces(
known_face_encodings, current_face_encoding)
name_of_person = "Unkown face"
if True in all_matches:
first_match_index = all_matches.index(True)
name_of_person = known_face_names[first_match_index]
cv2.rectangle(current_frame, (left_pos, top_pos),
(right_pos, bottom_pos), (255, 0, 0), 2)
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(current_frame, name_of_person, (left_pos,
bottom_pos), font, 0.5, (255, 255, 255), 1)
cv2.imshow("Webcam Video", current_frame)
if cv2.waitKey(1) & 0xFF == ord("q"):
break
webcam_video_stream.release()
cv2.destroyAllWindows()