-
Notifications
You must be signed in to change notification settings - Fork 31
/
run_with_webcam.py
212 lines (166 loc) · 7.31 KB
/
run_with_webcam.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
from typing import List, Optional
import torch
from torch.nn import DataParallel
from models.eyenet import EyeNet
import os
import numpy as np
import cv2
import dlib
import imutils
import util.gaze
from imutils import face_utils
from util.eye_prediction import EyePrediction
from util.eye_sample import EyeSample
torch.backends.cudnn.enabled = True
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
print(device)
webcam = cv2.VideoCapture(0)
webcam.set(cv2.CAP_PROP_FRAME_WIDTH, 960)
webcam.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
webcam.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc(*'MJPG'))
webcam.set(cv2.CAP_PROP_FPS, 60)
dirname = os.path.dirname(__file__)
face_cascade = cv2.CascadeClassifier(os.path.join(dirname, 'lbpcascade_frontalface_improved.xml'))
landmarks_detector = dlib.shape_predictor(os.path.join(dirname, 'shape_predictor_5_face_landmarks.dat'))
checkpoint = torch.load('checkpoint.pt', map_location=device)
nstack = checkpoint['nstack']
nfeatures = checkpoint['nfeatures']
nlandmarks = checkpoint['nlandmarks']
eyenet = EyeNet(nstack=nstack, nfeatures=nfeatures, nlandmarks=nlandmarks).to(device)
eyenet.load_state_dict(checkpoint['model_state_dict'])
def main():
current_face = None
landmarks = None
alpha = 0.95
left_eye = None
right_eye = None
while True:
_, frame_bgr = webcam.read()
orig_frame = frame_bgr.copy()
frame = cv2.cvtColor(frame_bgr, cv2.COLOR_BGR2RGB)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray)
if len(faces):
next_face = faces[0]
if current_face is not None:
current_face = alpha * next_face + (1 - alpha) * current_face
else:
current_face = next_face
if current_face is not None:
#draw_cascade_face(current_face, orig_frame)
next_landmarks = detect_landmarks(current_face, gray)
if landmarks is not None:
landmarks = next_landmarks * alpha + (1 - alpha) * landmarks
else:
landmarks = next_landmarks
#draw_landmarks(landmarks, orig_frame)
if landmarks is not None:
eye_samples = segment_eyes(gray, landmarks)
eye_preds = run_eyenet(eye_samples)
left_eyes = list(filter(lambda x: x.eye_sample.is_left, eye_preds))
right_eyes = list(filter(lambda x: not x.eye_sample.is_left, eye_preds))
if left_eyes:
left_eye = smooth_eye_landmarks(left_eyes[0], left_eye, smoothing=0.1)
if right_eyes:
right_eye = smooth_eye_landmarks(right_eyes[0], right_eye, smoothing=0.1)
for ep in [left_eye, right_eye]:
for (x, y) in ep.landmarks[16:33]:
color = (0, 255, 0)
if ep.eye_sample.is_left:
color = (255, 0, 0)
cv2.circle(orig_frame,
(int(round(x)), int(round(y))), 1, color, -1, lineType=cv2.LINE_AA)
gaze = ep.gaze.copy()
if ep.eye_sample.is_left:
gaze[1] = -gaze[1]
util.gaze.draw_gaze(orig_frame, ep.landmarks[-2], gaze, length=60.0, thickness=2)
cv2.imshow("Webcam", orig_frame)
cv2.waitKey(1)
def detect_landmarks(face, frame, scale_x=0, scale_y=0):
(x, y, w, h) = (int(e) for e in face)
rectangle = dlib.rectangle(x, y, x + w, y + h)
face_landmarks = landmarks_detector(frame, rectangle)
return face_utils.shape_to_np(face_landmarks)
def draw_cascade_face(face, frame):
(x, y, w, h) = (int(e) for e in face)
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2)
def draw_landmarks(landmarks, frame):
for (x, y) in landmarks:
cv2.circle(frame, (int(x), int(y)), 2, (0, 255, 0), -1, lineType=cv2.LINE_AA)
def segment_eyes(frame, landmarks, ow=160, oh=96):
eyes = []
# Segment eyes
for corner1, corner2, is_left in [(2, 3, True), (0, 1, False)]:
x1, y1 = landmarks[corner1, :]
x2, y2 = landmarks[corner2, :]
eye_width = 1.5 * np.linalg.norm(landmarks[corner1, :] - landmarks[corner2, :])
if eye_width == 0.0:
return eyes
cx, cy = 0.5 * (x1 + x2), 0.5 * (y1 + y2)
# center image on middle of eye
translate_mat = np.asmatrix(np.eye(3))
translate_mat[:2, 2] = [[-cx], [-cy]]
inv_translate_mat = np.asmatrix(np.eye(3))
inv_translate_mat[:2, 2] = -translate_mat[:2, 2]
# Scale
scale = ow / eye_width
scale_mat = np.asmatrix(np.eye(3))
scale_mat[0, 0] = scale_mat[1, 1] = scale
inv_scale = 1.0 / scale
inv_scale_mat = np.asmatrix(np.eye(3))
inv_scale_mat[0, 0] = inv_scale_mat[1, 1] = inv_scale
estimated_radius = 0.5 * eye_width * scale
# center image
center_mat = np.asmatrix(np.eye(3))
center_mat[:2, 2] = [[0.5 * ow], [0.5 * oh]]
inv_center_mat = np.asmatrix(np.eye(3))
inv_center_mat[:2, 2] = -center_mat[:2, 2]
# Get rotated and scaled, and segmented image
transform_mat = center_mat * scale_mat * translate_mat
inv_transform_mat = (inv_translate_mat * inv_scale_mat * inv_center_mat)
eye_image = cv2.warpAffine(frame, transform_mat[:2, :], (ow, oh))
eye_image = cv2.equalizeHist(eye_image)
if is_left:
eye_image = np.fliplr(eye_image)
cv2.imshow('left eye image', eye_image)
else:
cv2.imshow('right eye image', eye_image)
eyes.append(EyeSample(orig_img=frame.copy(),
img=eye_image,
transform_inv=inv_transform_mat,
is_left=is_left,
estimated_radius=estimated_radius))
return eyes
def smooth_eye_landmarks(eye: EyePrediction, prev_eye: Optional[EyePrediction], smoothing=0.2, gaze_smoothing=0.4):
if prev_eye is None:
return eye
return EyePrediction(
eye_sample=eye.eye_sample,
landmarks=smoothing * prev_eye.landmarks + (1 - smoothing) * eye.landmarks,
gaze=gaze_smoothing * prev_eye.gaze + (1 - gaze_smoothing) * eye.gaze)
def run_eyenet(eyes: List[EyeSample], ow=160, oh=96) -> List[EyePrediction]:
result = []
for eye in eyes:
with torch.no_grad():
x = torch.tensor([eye.img], dtype=torch.float32).to(device)
_, landmarks, gaze = eyenet.forward(x)
landmarks = np.asarray(landmarks.cpu().numpy()[0])
gaze = np.asarray(gaze.cpu().numpy()[0])
assert gaze.shape == (2,)
assert landmarks.shape == (34, 2)
landmarks = landmarks * np.array([oh/48, ow/80])
temp = np.zeros((34, 3))
if eye.is_left:
temp[:, 0] = ow - landmarks[:, 1]
else:
temp[:, 0] = landmarks[:, 1]
temp[:, 1] = landmarks[:, 0]
temp[:, 2] = 1.0
landmarks = temp
assert landmarks.shape == (34, 3)
landmarks = np.asarray(np.matmul(landmarks, eye.transform_inv.T))[:, :2]
assert landmarks.shape == (34, 2)
result.append(EyePrediction(eye_sample=eye, landmarks=landmarks, gaze=gaze))
return result
if __name__ == '__main__':
main()