forked from axinc-ai/ailia-models
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblazepose-fullbody.py
384 lines (301 loc) · 12.7 KB
/
blazepose-fullbody.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
import sys
import time
import cv2
import numpy as np
import ailia
import blazepose_utils as but
sys.path.append('../../util')
from arg_utils import get_base_parser, update_parser, get_savepath # noqa: E402
from model_utils import check_and_download_models # noqa: E402
from detector_utils import load_image # noqa: E402C
import webcamera_utils # noqa: E402
# logger
from logging import getLogger # noqa: E402
logger = getLogger(__name__)
# ======================
# Parameters
# ======================
MODEL_LIST = ['lite', 'full', 'heavy']
WEIGHT_LITE_PATH = 'pose_landmark_lite.onnx'
MODEL_LITE_PATH = 'pose_landmark_lite.onnx.prototxt'
WEIGHT_FULL_PATH = 'pose_landmark_full.onnx'
MODEL_FULL_PATH = 'pose_landmark_full.onnx.prototxt'
WEIGHT_HEAVY_PATH = 'pose_landmark_heavy.onnx'
MODEL_HEAVY_PATH = 'pose_landmark_heavy.onnx.prototxt'
WEIGHT_DETECTOR_PATH = 'pose_detection.onnx'
MODEL_DETECTOR_PATH = 'pose_detection.onnx.prototxt'
REMOTE_PATH = \
'https://storage.googleapis.com/ailia-models/blazepose-fullbody/'
IMAGE_PATH = 'girl-5204299_640.jpg'
SAVE_IMAGE_PATH = 'output.png'
IMAGE_SIZE = 256
# ======================
# Argument Parser Config
# ======================
parser = get_base_parser(
'BlazePose, an on-device real-time body pose tracking.',
IMAGE_PATH,
SAVE_IMAGE_PATH,
)
parser.add_argument(
'-m', '--model', metavar='ARCH',
default='heavy', choices=MODEL_LIST,
help='Set model architecture: ' + ' | '.join(MODEL_LIST)
)
parser.add_argument(
'-th', '--threshold',
default=0.5, type=float,
help='The detection threshold'
)
args = update_parser(parser)
# ======================
# Utils
# ======================
def preprocess(img):
img = cv2.resize(img, (IMAGE_SIZE, IMAGE_SIZE), interpolation=cv2.INTER_LINEAR)
img = img.astype(np.float32) / 255
img = np.expand_dims(img, axis=0)
return img
def sigmoid(x):
return 1.0 / (1.0 + np.exp(-x))
def postprocess(landmarks):
num = len(landmarks)
normalized_landmarks = np.zeros((num, 33, 4))
for i in range(num):
xx = landmarks[i]
for j in range(33):
x = xx[j * 5] / IMAGE_SIZE
y = xx[j * 5 + 1] / IMAGE_SIZE
z = xx[j * 5 + 2] / IMAGE_SIZE
visibility = xx[j * 5 + 3]
presence = xx[j * 5 + 4]
normalized_landmarks[i, j] = (x, y, z, sigmoid(min(visibility, presence)))
return normalized_landmarks
def pose_estimate(net, det_net, img):
h, w = img.shape[:2]
src_img = img
logger.debug(f'input image shape: {img.shape}')
_, img224, scale, pad = but.resize_pad(img)
img224 = img224.astype('float32') / 255.
img224 = np.expand_dims(img224, axis=0)
detector_out = det_net.predict([img224])
detections = but.detector_postprocess(detector_out)
count = len(detections) if detections[0].size != 0 else 0
# Pose estimation
imgs = []
if 0 < count:
imgs, affine, _ = but.estimator_preprocess(
src_img, detections, scale, pad
)
flags = []
landmarks = []
for i, img in enumerate(imgs):
img = np.expand_dims(img, axis=0)
output = net.predict([img])
normalized_landmarks, f, _, _, _ = output
normalized_landmarks = postprocess(normalized_landmarks)
flags.append(f[0])
landmarks.append(normalized_landmarks[0])
if len(imgs)>=1:
landmarks = np.stack(landmarks)
landmarks = but.denormalize_landmarks(landmarks, affine)
return flags, landmarks
def hsv_to_rgb(h, s, v):
bgr = cv2.cvtColor(
np.array([[[h, s, v]]], dtype=np.uint8), cv2.COLOR_HSV2BGR
)[0][0]
return (int(bgr[2]), int(bgr[1]), int(bgr[0]))
def line(input_img, landmarks, flags, point1, point2):
threshold = args.threshold
for i in range(len(flags)):
landmark, flag = landmarks[i], flags[i]
conf1 = landmark[point1, 3]
conf2 = landmark[point2, 3]
if flag >= threshold and conf1 >= threshold and conf2 >= threshold:
color = hsv_to_rgb(255 * point1 / but.BLAZEPOSE_KEYPOINT_CNT, 255, 255)
line_width = 5
x1 = int(landmark[point1, 0])
y1 = int(landmark[point1, 1])
x2 = int(landmark[point2, 0])
y2 = int(landmark[point2, 1])
cv2.line(input_img, (x1, y1), (x2, y2), color, line_width)
def circle(input_img, landmarks, flags):
threshold = args.threshold
for i in range(len(flags)):
for point1 in range(11,33):
landmark, flag = landmarks[i], flags[i]
conf1 = landmark[point1, 3]
if flag >= threshold and conf1 >= threshold:
color = hsv_to_rgb(255 * point1 / but.BLAZEPOSE_KEYPOINT_CNT, 255, 255)
base_line_width = 5
line_width = landmark[point1, 2]
line_width = base_line_width - line_width/2 * 100
line_width = max(int(line_width),1)
x1 = int(landmark[point1, 0])
y1 = int(landmark[point1, 1])
cv2.circle(input_img, (x1, y1), line_width, color, thickness=2, lineType=cv2.LINE_8, shift=0)
def display_result(img, landmarks, flags):
circle(img, landmarks, flags)
line(img, landmarks, flags, but.BLAZEPOSE_KEYPOINT_NOSE,
but.BLAZEPOSE_KEYPOINT_EYE_LEFT_INNER)
line(img, landmarks, flags, but.BLAZEPOSE_KEYPOINT_EYE_LEFT_INNER,
but.BLAZEPOSE_KEYPOINT_EYE_LEFT)
line(img, landmarks, flags, but.BLAZEPOSE_KEYPOINT_EYE_LEFT,
but.BLAZEPOSE_KEYPOINT_EYE_LEFT_OUTER)
line(img, landmarks, flags, but.BLAZEPOSE_KEYPOINT_EYE_LEFT_OUTER,
but.BLAZEPOSE_KEYPOINT_EAR_LEFT)
line(img, landmarks, flags, but.BLAZEPOSE_KEYPOINT_NOSE,
but.BLAZEPOSE_KEYPOINT_EYE_RIGHT_INNER)
line(img, landmarks, flags, but.BLAZEPOSE_KEYPOINT_EYE_RIGHT_INNER,
but.BLAZEPOSE_KEYPOINT_EYE_RIGHT)
line(img, landmarks, flags, but.BLAZEPOSE_KEYPOINT_EYE_RIGHT,
but.BLAZEPOSE_KEYPOINT_EYE_RIGHT_OUTER)
line(img, landmarks, flags, but.BLAZEPOSE_KEYPOINT_EYE_RIGHT_OUTER,
but.BLAZEPOSE_KEYPOINT_EAR_RIGHT)
line(img, landmarks, flags, but.BLAZEPOSE_KEYPOINT_MOUTH_LEFT,
but.BLAZEPOSE_KEYPOINT_MOUTH_RIGHT)
line(img, landmarks, flags, but.BLAZEPOSE_KEYPOINT_SHOULDER_LEFT,
but.BLAZEPOSE_KEYPOINT_SHOULDER_RIGHT)
line(img, landmarks, flags, but.BLAZEPOSE_KEYPOINT_SHOULDER_LEFT,
but.BLAZEPOSE_KEYPOINT_ELBOW_LEFT)
line(img, landmarks, flags, but.BLAZEPOSE_KEYPOINT_ELBOW_LEFT,
but.BLAZEPOSE_KEYPOINT_WRIST_LEFT)
line(img, landmarks, flags, but.BLAZEPOSE_KEYPOINT_SHOULDER_RIGHT,
but.BLAZEPOSE_KEYPOINT_ELBOW_RIGHT)
line(img, landmarks, flags, but.BLAZEPOSE_KEYPOINT_ELBOW_RIGHT,
but.BLAZEPOSE_KEYPOINT_WRIST_RIGHT)
line(img, landmarks, flags, but.BLAZEPOSE_KEYPOINT_WRIST_LEFT,
but.BLAZEPOSE_KEYPOINT_PINKY_LEFT_KNUCKLE1)
line(img, landmarks, flags, but.BLAZEPOSE_KEYPOINT_PINKY_LEFT_KNUCKLE1,
but.BLAZEPOSE_KEYPOINT_INDEX_LEFT_KNUCKLE1)
line(img, landmarks, flags, but.BLAZEPOSE_KEYPOINT_WRIST_LEFT,
but.BLAZEPOSE_KEYPOINT_INDEX_LEFT_KNUCKLE1)
line(img, landmarks, flags, but.BLAZEPOSE_KEYPOINT_WRIST_LEFT,
but.BLAZEPOSE_KEYPOINT_THUMB_LEFT_KNUCKLE2)
line(img, landmarks, flags, but.BLAZEPOSE_KEYPOINT_WRIST_RIGHT,
but.BLAZEPOSE_KEYPOINT_PINKY_RIGHT_KNUCKLE1)
line(img, landmarks, flags, but.BLAZEPOSE_KEYPOINT_PINKY_RIGHT_KNUCKLE1,
but.BLAZEPOSE_KEYPOINT_INDEX_RIGHT_KNUCKLE1)
line(img, landmarks, flags, but.BLAZEPOSE_KEYPOINT_WRIST_RIGHT,
but.BLAZEPOSE_KEYPOINT_INDEX_RIGHT_KNUCKLE1)
line(img, landmarks, flags, but.BLAZEPOSE_KEYPOINT_WRIST_RIGHT,
but.BLAZEPOSE_KEYPOINT_THUMB_RIGHT_KNUCKLE2)
line(img, landmarks, flags, but.BLAZEPOSE_KEYPOINT_SHOULDER_LEFT,
but.BLAZEPOSE_KEYPOINT_HIP_LEFT)
line(img, landmarks, flags, but.BLAZEPOSE_KEYPOINT_SHOULDER_RIGHT,
but.BLAZEPOSE_KEYPOINT_HIP_RIGHT)
line(img, landmarks, flags, but.BLAZEPOSE_KEYPOINT_HIP_LEFT,
but.BLAZEPOSE_KEYPOINT_HIP_RIGHT)
# Upper body: stop here
line(img, landmarks, flags, but.BLAZEPOSE_KEYPOINT_HIP_LEFT,
but.BLAZEPOSE_KEYPOINT_KNEE_LEFT)
line(img, landmarks, flags, but.BLAZEPOSE_KEYPOINT_KNEE_LEFT,
but.BLAZEPOSE_KEYPOINT_ANKLE_LEFT)
line(img, landmarks, flags, but.BLAZEPOSE_KEYPOINT_HIP_RIGHT,
but.BLAZEPOSE_KEYPOINT_KNEE_RIGHT)
line(img, landmarks, flags, but.BLAZEPOSE_KEYPOINT_KNEE_RIGHT,
but.BLAZEPOSE_KEYPOINT_ANKLE_RIGHT)
line(img, landmarks, flags, but.BLAZEPOSE_KEYPOINT_ANKLE_LEFT,
but.BLAZEPOSE_KEYPOINT_HEEL_LEFT)
line(img, landmarks, flags, but.BLAZEPOSE_KEYPOINT_HEEL_LEFT,
but.BLAZEPOSE_KEYPOINT_FOOT_LEFT_INDEX)
line(img, landmarks, flags, but.BLAZEPOSE_KEYPOINT_ANKLE_LEFT,
but.BLAZEPOSE_KEYPOINT_FOOT_LEFT_INDEX)
line(img, landmarks, flags, but.BLAZEPOSE_KEYPOINT_ANKLE_RIGHT,
but.BLAZEPOSE_KEYPOINT_HEEL_RIGHT)
line(img, landmarks, flags, but.BLAZEPOSE_KEYPOINT_HEEL_RIGHT,
but.BLAZEPOSE_KEYPOINT_FOOT_RIGHT_INDEX)
line(img, landmarks, flags, but.BLAZEPOSE_KEYPOINT_ANKLE_RIGHT,
but.BLAZEPOSE_KEYPOINT_FOOT_RIGHT_INDEX)
# ======================
# Main functions
# ======================
def recognize_from_image(net, det_net):
# input image loop
for image_path in args.input:
# prepare input data
logger.info(image_path)
img = src_img = load_image(image_path)
img = cv2.cvtColor(img, cv2.COLOR_BGRA2RGB)
# inference
logger.info('Start inference...')
if args.benchmark:
logger.info('BENCHMARK mode')
total_time_estimation = 0
for i in range(args.benchmark_count):
# Pose estimation
start = int(round(time.time() * 1000))
flags, landmarks = pose_estimate(net, det_net, img)
end = int(round(time.time() * 1000))
estimation_time = (end - start)
# Loggin
logger.info(f'\tailia processing estimation time {estimation_time} ms')
if i != 0:
total_time_estimation = total_time_estimation + estimation_time
logger.info(f'\taverage time estimation {total_time_estimation / (args.benchmark_count - 1)} ms')
else:
# inference
flags, landmarks = pose_estimate(net, det_net, img)
# plot result
src_img = cv2.cvtColor(src_img, cv2.COLOR_BGRA2BGR)
display_result(src_img, landmarks, flags)
# save results
savepath = get_savepath(args.savepath, image_path)
logger.info(f'saved at : {savepath}')
cv2.imwrite(savepath, src_img)
logger.info('Script finished successfully.')
def recognize_from_video(net, det_net):
capture = webcamera_utils.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 = webcamera_utils.get_writer(args.savepath, f_h, f_w)
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
# inference
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
flags, landmarks = pose_estimate(net, det_net, frame_rgb)
# plot result
display_result(frame, landmarks, flags)
cv2.imshow('frame', frame)
frame_shown = True
# save results
if writer is not None:
writer.write(frame)
capture.release()
cv2.destroyAllWindows()
if writer is not None:
writer.release()
logger.info('Script finished successfully.')
def main():
# model files check and download
logger.info('=== detector model ===')
check_and_download_models(WEIGHT_DETECTOR_PATH, MODEL_DETECTOR_PATH, REMOTE_PATH)
logger.info('=== blazepose model ===')
info = {
'lite': (WEIGHT_LITE_PATH, MODEL_LITE_PATH),
'full': (WEIGHT_FULL_PATH, MODEL_FULL_PATH),
'heavy': (WEIGHT_HEAVY_PATH, MODEL_HEAVY_PATH),
}
weight_path, model_path = info[args.model]
check_and_download_models(weight_path, model_path, REMOTE_PATH)
env_id = args.env_id
# initialize
det_net = ailia.Net(MODEL_DETECTOR_PATH, WEIGHT_DETECTOR_PATH, env_id=env_id)
net = ailia.Net(model_path, weight_path, env_id=env_id)
if args.video is not None:
# video mode
recognize_from_video(net, det_net)
else:
# image mode
recognize_from_image(net, det_net)
if __name__ == '__main__':
main()