-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathchallenge_response.py
165 lines (116 loc) · 4.87 KB
/
challenge_response.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import random
import cv2 as cv
import numpy as np
import torch
from facenet.models.mtcnn import MTCNN
from liveness_detection.blink_detection import *
from liveness_detection.emotion_prediction import *
from liveness_detection.face_orientation import *
from utils.functions import extract_face
def random_challenge():
return random.choice(["smile", "surprise", "blink eyes", "right", "left"])
def get_question(challenge):
"""
Generate a question or instruction based on the challenge.
Parameters:
challenge (str): The current challenge, which can be 'smile', 'surprise', 'right', 'left', 'front', or 'blink eyes'.
Returns:
str or list: A question or instruction related to the challenge.
If the challenge is 'blink eyes', returns a list containing the instruction and the required number of blinks.
"""
if challenge in ["smile", "surprise"]:
return "Please put on a {} expression".format(challenge)
elif challenge in ["right", "left", "front"]:
return "Please turn your face to the {}".format(challenge)
elif challenge == "blink eyes":
num = random.randint(2, 4)
return ["Blink your eyes {} times".format(num), num]
def get_challenge_and_question():
challenge = random_challenge()
question = get_question(challenge)
return challenge, question
def blink_response(image, box, question, model: BlinkDetector):
thresh = question[1]
blink_success = model.eye_blink(image, box, thresh)
return blink_success
def face_response(challenge: str, landmarks: list, model: FaceOrientationDetector):
orientation = model.detect(landmarks)
return orientation == challenge
def emotion_response(face, challenge: str, model: EmotionPredictor):
emotion = model.predict(face)
return emotion == challenge
def result_challenge_response(
frame: np.ndarray, challenge: str, question, model: list, mtcnn: MTCNN
):
"""
Process the response to a challenge based on the input frame.
Parameters:
frame (np.ndarray): RGB color image.
challenge (str): The current challenge, which can be 'smile', 'surprise', 'right', 'left', 'front', or 'blink eyes'.
question: A question or instruction related to the challenge.
model (list): List of models used, including [blink_model, face_orientation_model, emotion_model].
mtcnn (MTCNN): MTCNN object used for face extraction.
Returns:
bool: The result of the challenge (True if correct, False if incorrect).
"""
face, box, landmarks = extract_face(frame, mtcnn, padding=10)
if box is not None:
if challenge in ["smile", "surprise"]:
isCorrect = emotion_response(face, challenge, model[2])
elif challenge in ["right", "left", "front"]:
isCorrect = face_response(challenge, landmarks, model[1])
elif challenge == "blink eyes":
isCorrect = blink_response(frame, box, question, model[0])
return isCorrect
return False
if __name__ == "__main__":
video = cv.VideoCapture(0)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
mtcnn = MTCNN()
blink_detector = BlinkDetector()
emotion_predictor = EmotionPredictor()
face_orientation_detector = FaceOrientationDetector()
model = [blink_detector, face_orientation_detector, emotion_predictor]
challenge, question = get_challenge_and_question()
challengeIsCorrect = False
count = 0
while True:
ret, frame = video.read()
if ret:
frame = cv.flip(frame, 1)
if challengeIsCorrect is False:
rgb_frame = cv.cvtColor(frame, cv.COLOR_BGR2RGB)
challengeIsCorrect = result_challenge_response(
rgb_frame, challenge, question, model, mtcnn
)
if isinstance(question, list):
cv.putText(
frame,
"Question: {}".format(question[0]),
(20, 20),
cv.FONT_HERSHEY_COMPLEX,
0.5,
(0, 0, 255),
1,
)
else:
cv.putText(
frame,
"Question: {}".format(question),
(20, 20),
cv.FONT_HERSHEY_COMPLEX,
0.5,
(0, 0, 255),
1,
)
cv.imshow("", frame)
if cv.waitKey(1) & 0xFF == ord("q"):
break
count += 1
if challengeIsCorrect is True and count >= 100:
challenge, question = get_challenge_and_question()
print(question)
challengeIsCorrect = False
count = 0
else:
break