-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecognize_with_antispoof.py
78 lines (61 loc) · 2.62 KB
/
recognize_with_antispoof.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
import face_recognition
import cv2
import numpy as np
from known_faces import known_face_encodings, known_face_names
from spoof_detection import check_spoof
video_capture = cv2.VideoCapture(0)
# FOR THE DEMO - Define the codec and create VideoWriter object
# video_save = cv2.VideoWriter('demo_antispoof.mp4', cv2.VideoWriter_fourcc(*'MJPG'), 10, (640,480))
# Initialize some variables
face_locations = []
face_encodings = []
face_names = []
process_this_frame = True
while True:
# Grab a single frame of video
ret, frame = video_capture.read()
# Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
rgb_frame = frame[:, :, ::-1]
# Only process every other frame of video to save time
if process_this_frame:
#verify the liveness of the face (not a photo)
(face_location, spoof) = check_spoof(frame)
if not spoof:
name = "Unknown"
# Get the detected face's encoding
face_encoding = face_recognition.face_encodings(rgb_frame, [face_location])
# See if the face is a match for the known face(s)
matches = face_recognition.compare_faces(known_face_encodings, face_encoding[0]) #[0] because it returns an array
# Use the known face with the smallest distance to the new face
face_distances = face_recognition.face_distance(known_face_encodings, face_encoding[0])
best_match_index = np.argmin(face_distances)
if matches[best_match_index]:
name = known_face_names[best_match_index]
else:
name = 'Fake'
process_this_frame = not process_this_frame
# DISPLAY THE RESULTS
(top, right, bottom, left) = face_location
# Draw a box around the face
if name=="Fake":
color = (0, 0, 0) # black
elif name=="Unknown":
color = (0, 0, 255) # red
else:
color = (0, 255, 0) # green
cv2.rectangle(frame, (left, top), (right, bottom), color, 2)
# Draw a label with a name below the face
cv2.rectangle(frame, (left, bottom - 35), (right, bottom), color, cv2.FILLED)
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
# Display the resulting image
cv2.imshow('Video', frame)
# Save frame for the demo
# video_save.write(frame)
# Hit 'q' on the keyboard to quit!
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release handle to the webcam
video_capture.release()
# video_save.release()
cv2.destroyAllWindows()