forked from axinc-ai/ailia-models
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhopenet.py
315 lines (265 loc) · 9.76 KB
/
hopenet.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import sys
import time
import ailia
import cv2
import numpy as np
import hopenet_utils as hut
sys.path.append('../../util')
# logger
from logging import getLogger # noqa: E402
from image_utils import imread # noqa: E402
from model_utils import check_and_download_models # noqa: E402
from utils import get_base_parser, get_savepath, update_parser # noqa: E402
from webcamera_utils import get_capture, get_writer # noqa: E402
logger = getLogger(__name__)
# ======================
# Parameters 1
# ======================
IMAGE_PATH = 'man.jpg'
SAVE_IMAGE_PATH = 'output.png'
IMAGE_HEIGHT = 128
IMAGE_WIDTH = 128
# ======================
# Argument Parser Config
# ======================
parser = get_base_parser(
'Hopenet, a head pose estimation.',
IMAGE_PATH,
SAVE_IMAGE_PATH,
)
parser.add_argument(
'-l', '--lite',
action='store_true',
help='With this option, a lite version of the head pose model is used.'
)
parser.add_argument(
'-n', '--normal',
action='store_true',
help='By default, the optimized model is used, but with this option, ' +
'you can switch to the normal (not optimized) model'
)
args = update_parser(parser)
# ======================
# Parameters 2
# ======================
FACE_DETECTION_MODEL_NAME = 'blazeface'
if args.lite:
HEAD_POSE_MODEL_NAME = 'hopenet_lite'
else:
HEAD_POSE_MODEL_NAME = 'hopenet_robust_alpha1'
if args.normal:
FACE_DETECTION_WEIGHT_PATH = f'{FACE_DETECTION_MODEL_NAME}.onnx'
FACE_DETECTION_MODEL_PATH = f'{FACE_DETECTION_MODEL_NAME}.onnx.prototxt'
HEAD_POSE_WEIGHT_PATH = f'{HEAD_POSE_MODEL_NAME}.onnx'
HEAD_POSE_MODEL_PATH = f'{HEAD_POSE_MODEL_NAME}.onnx.prototxt'
else:
FACE_DETECTION_WEIGHT_PATH = f'{FACE_DETECTION_MODEL_NAME}.opt.onnx'
FACE_DETECTION_MODEL_PATH = f'{FACE_DETECTION_MODEL_NAME}.opt.onnx.prototxt'
HEAD_POSE_WEIGHT_PATH = f'{HEAD_POSE_MODEL_NAME}.opt.onnx'
HEAD_POSE_MODEL_PATH = f'{HEAD_POSE_MODEL_NAME}.opt.onnx.prototxt'
FACE_DETECTION_REMOTE_PATH = f'https://storage.googleapis.com/ailia-models/{FACE_DETECTION_MODEL_NAME}/'
HEAD_POSE_REMOTE_PATH = f'https://storage.googleapis.com/ailia-models/hopenet/'
# ======================
# Utils
# ======================
class HeadPoseEstimator:
def __init__(self):
"""
Class for estimating the head pose given an image or draw the detected
head pose on the image.
"""
# net initialize
self.face_detector = ailia.Net(
FACE_DETECTION_MODEL_PATH, FACE_DETECTION_WEIGHT_PATH, env_id=args.env_id
)
self.hp_estimator = ailia.Net(
HEAD_POSE_MODEL_PATH, HEAD_POSE_WEIGHT_PATH, env_id=args.env_id
)
def predict(self, img):
"""
Estimates the head pose given an image.
Parameters
----------
img: NumPy array
The image in BGR channels.
Returns
-------
head_pose: NumPy array
Head pose(s) in radians. Roll (left+), yaw (right+), pitch (down+)
values are given in the detected person's frame of reference.
"""
# Face detection
input_face_det, scale, padding = hut.face_detector_preprocess(img)
preds_det = self.face_detector.predict([input_face_det])
detections = hut.face_detector_postprocess(preds_det)
# Head pose estimation
input_hp_est, centers, theta = hut.head_pose_preprocess(img, detections, scale, padding)
if input_hp_est.shape[0]==0:
return [], []
self.hp_estimator.set_input_shape(input_hp_est.shape)
preds_hp = self.hp_estimator.predict([input_hp_est])
head_poses = hut.head_pose_postprocess(preds_hp, theta)
return head_poses, centers
def _get_rot_mat(self, axis, angle):
"""
Creates rotation matrix from axis (x, y or z) and angle. The axes of
reference correspond to x oriented positively to the left of the image,
y oriented positively to the bottom of the image and z oriented
positively to the back of the image.
Parameters
----------
axis: str
Axis of rotation. Only x, y and z are supported.
angle: float
Angle of rotation in radians.
Returns
-------
rot_mat: NumPy array
Rotation matrix
Head pose(s) in radians. Roll (left+), yaw (right+), pitch (down+)
values are given in the detected person's frame of reference.
"""
rot_mat = np.zeros((3, 3), dtype=np.float32)
if axis == 'z':
i = 2
elif axis == 'y':
i = 1
elif axis == 'x':
i = 0
else:
raise ValueError(f'Axis {axis} is not a valid argument.')
rot_mat[i, i] = 1
rot_mat[i-1, i-1] = np.cos(angle)
rot_mat[i-1, i-2] = np.sin(angle)
rot_mat[i-2, i-1] = -np.sin(angle)
rot_mat[i-2, i-2] = np.cos(angle)
return rot_mat
def draw(self, img, head_poses, centers, horizontal_flip=False):
"""
Draws the head pose(s) on the image. (Person POV) The axes correspond to
x (blue) oriented positively to the left, y (green) oriented positively
to the bottom and z (red) oriented positively to the back.
Parameters
----------
img: NumPy array
The image to draw on (BGR channels).
head_poses: NumPy array
The head pose(s) to draw.
centers: NumPy array
The center(s) of origin of the head pose(s).
horizontal_flip: bool
Whether to consider a horizontally flipped image for drawing.
Returns
-------
new_img: NumPy array
Image with the head pose(s) drawn on it.
"""
new_img = img.copy()
if horizontal_flip:
new_img = np.ascontiguousarray(new_img[:, ::-1])
for i in range(len(head_poses)):
hp, c = head_poses[i], centers[i]
rot_mat = self._get_rot_mat('z', hp[0])
rot_mat = rot_mat @ self._get_rot_mat('y', hp[1])
rot_mat = rot_mat @ self._get_rot_mat('x', hp[2])
hp_vecs = rot_mat.T # Each row is rotated x, y, z respectively
if horizontal_flip:
hp_vecs[0, 1] *= -1
hp_vecs[1:, 0] *= -1
c[0] = new_img.shape[1] - c[0]
for i, vec in enumerate(hp_vecs):
tip = tuple((c + 100 * vec[:2]).astype(int))
color = [0, 0, 0]
color[i] = 255
cv2.arrowedLine(new_img, tuple(c.astype(int)), tip, tuple(color), thickness=2)
return new_img
def predict_and_draw(self, img):
"""
Convenient method for predicting the head pose(s) and drawing them at
once.
Parameters
----------
img: NumPy array
The image in BGR channels.
Returns
-------
new_img: NumPy array
Image with the head pose(s) drawn on it.
"""
head_poses, centers = self.predict(img)
return self.draw(img, head_poses, centers)
# ======================
# Main functions
# ======================
def recognize_from_image():
hp_estimator = HeadPoseEstimator()
# input image loop
for image_path in args.input:
logger.info(image_path)
src_img = imread(image_path)
# inference
logger.info('Start inference...')
if args.benchmark:
logger.info('BENCHMARK mode')
for _ in range(5):
start = int(round(time.time() * 1000))
img_draw = hp_estimator.predict_and_draw(src_img)
end = int(round(time.time() * 1000))
logger.info(f'\tailia processing time {end - start} ms')
else:
img_draw = hp_estimator.predict_and_draw(src_img)
savepath = get_savepath(args.savepath, image_path)
logger.info(f'saved at : {savepath}')
cv2.imwrite(savepath, img_draw)
logger.info('Script finished successfully.')
def recognize_from_video():
hp_estimator = HeadPoseEstimator()
capture = get_capture(args.video)
# create video writer if savepath is specified as video format
if args.savepath != SAVE_IMAGE_PATH:
f_h = int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT))
f_w = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH))
writer = get_writer(args.savepath, f_h, f_w, fps=capture.get(cv2.CAP_PROP_FPS))
else:
writer = None
frame_shown = False
while(True):
ret, frame = capture.read()
if (cv2.waitKey(1) & 0xFF == ord('q')) or not ret:
break
if frame_shown and cv2.getWindowProperty('frame', cv2.WND_PROP_VISIBLE) == 0:
break
preds = hp_estimator.predict(frame)
frame_draw = hp_estimator.draw(frame, *preds)
if args.video == '0': # Flip horizontally if camera
visual_img = hp_estimator.draw(frame, *preds, horizontal_flip=True)
else:
visual_img = frame_draw
cv2.imshow('frame', visual_img)
frame_shown = True
# save results
if writer is not None:
writer.write(frame_draw)
capture.release()
if writer is not None:
writer.release()
cv2.destroyAllWindows()
if writer is not None:
writer.release()
logger.info('Script finished successfully.')
def main():
# model files check and download
check_and_download_models(
FACE_DETECTION_WEIGHT_PATH, FACE_DETECTION_MODEL_PATH, FACE_DETECTION_REMOTE_PATH
)
check_and_download_models(
HEAD_POSE_WEIGHT_PATH, HEAD_POSE_MODEL_PATH, HEAD_POSE_REMOTE_PATH
)
if args.video is not None:
# video mode
recognize_from_video()
else:
# image mode
recognize_from_image()
if __name__ == '__main__':
main()