forked from sd2001/-le-medicin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
face_recog.py
45 lines (29 loc) · 1.28 KB
/
face_recog.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
import numpy as np
import face_recognition as fr
import cv2
video = cv2.VideoCapture(0)
my_image = fr.load_image_file("2.jpg")
my_face_encoding = fr.face_encodings(my_image)[0]
known_face_encondings = [my_face_encoding]
known_face_names = ["SD"]
while True:
ret, frame = video.read()
rgb_frame = frame[:, :, ::-1]
face_locations = fr.face_locations(rgb_frame)
face_encodings = fr.face_encodings(rgb_frame, face_locations)
for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
matches = fr.compare_faces(known_face_encondings, face_encoding)
name = "Unknown"
face_distances = fr.face_distance(known_face_encondings, face_encoding)
best_match_index = np.argmin(face_distances)
if matches[best_match_index]:
name = known_face_names[best_match_index]
cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
cv2.rectangle(frame, (left, bottom -35), (right, bottom), (0, 0, 255), cv2.FILLED)
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
cv2.imshow('Webcam_facerecognition', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video.release()
cv2.destroyAllWindows()