-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfaceRecognition.py
More file actions
44 lines (37 loc) · 1.35 KB
/
Copy pathfaceRecognition.py
File metadata and controls
44 lines (37 loc) · 1.35 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
#coding=utf-8
import cv2
import face_recognition
import os
path = "img/face_recognition"
cap = cv2.VideoCapture(0)
total_image_name = []
total_face_encoding = []
for fn in os.listdir(path):
print(path + "/" + fn)
total_face_encoding.append(
face_recognition.face_encodings(
face_recognition.load_image_file(path +'/' + fn)
)[0]
)
fn = fn[:(len(fn) - 4)]
total_image_name.append(fn)
while(1):
ret, frame = cap.read()
face_locations = face_recognition.face_locations(frame)
face_encodings = face_recognition.face_encodings(frame, face_locations)
for(top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
for i, v in enumerate(total_face_encoding):
match = face_recognition.compare_faces([v], face_encoding, tolerance=0.5)
name = "Unknown"
if match[0]:
name = total_image_name[i]
break
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_DUPLEX
cv2.putText(frame, name, (left + 6, bottom -6), font, 1.0, (255, 255, 255), 1)
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()