-
Notifications
You must be signed in to change notification settings - Fork 88
/
sample_pose_landmark_detection.py
516 lines (454 loc) · 16.9 KB
/
sample_pose_landmark_detection.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import copy
import argparse
from typing import List, Any, Dict, Tuple, Union
import cv2
import numpy as np
import mediapipe as mp # type:ignore
from mediapipe.tasks import python # type:ignore
from mediapipe.tasks.python import vision # type:ignore
from utils import CvFpsCalc
from utils.download_file import download_file
def get_args() -> argparse.Namespace:
parser = argparse.ArgumentParser()
parser.add_argument("--device", type=int, default=0)
parser.add_argument("--video", type=str, default=None)
parser.add_argument("--width", help='cap width', type=int, default=960)
parser.add_argument("--height", help='cap height', type=int, default=540)
parser.add_argument('--unuse_mirror', action='store_true')
parser.add_argument(
"--model",
type=int,
choices=[0, 1, 2],
default=0,
help='''
0:Pose landmarker(lite)
1:Pose landmarker(Full)
2:Pose landmarker(Heavy)
''',
)
parser.add_argument('--use_output_segmentation_masks', action='store_true')
parser.add_argument('--use_world_landmark', action='store_true')
args = parser.parse_args()
return args
def main() -> None:
# 引数解析
args = get_args()
cap_device: Union[int, str] = args.device
cap_width: int = args.width
cap_height: int = args.height
unuse_mirror: bool = args.unuse_mirror
use_world_landmark: bool = args.use_world_landmark
model: int = args.model
use_output_segmentation_masks: bool = args.use_output_segmentation_masks
if args.video is not None:
cap_device = args.video
model_url: List[str] = [
'https://storage.googleapis.com/mediapipe-models/pose_landmarker/pose_landmarker_lite/float16/latest/pose_landmarker_lite.task',
'https://storage.googleapis.com/mediapipe-models/pose_landmarker/pose_landmarker_full/float16/latest/pose_landmarker_full.task',
'https://storage.googleapis.com/mediapipe-models/pose_landmarker/pose_landmarker_heavy/float16/latest/pose_landmarker_heavy.task',
]
# ダウンロードファイル名生成
model_name: str = model_url[model].split('/')[-1]
quantize_type: str = model_url[model].split('/')[-3]
split_name: List[str] = model_name.split('.')
model_name = split_name[0] + '_' + quantize_type + '.' + split_name[1]
# 重みファイルダウンロード
model_path: str = os.path.join('model', model_name)
if not os.path.exists(model_path):
download_file(url=model_url[model], save_path=model_path)
# カメラ準備
cap: cv2.VideoCapture = cv2.VideoCapture(cap_device)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, cap_width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, cap_height)
# PoseLandmarker生成
base_options: python.BaseOptions = python.BaseOptions(
model_asset_path=model_path)
options: vision.PoseLandmarkerOptions = vision.PoseLandmarkerOptions(
base_options=base_options,
output_segmentation_masks=use_output_segmentation_masks,
)
detector: vision.PoseLandmarker = vision.PoseLandmarker.create_from_options(
options) # type:ignore
# FPS計測モジュール
cvFpsCalc: CvFpsCalc = CvFpsCalc(buffer_len=10)
# World座標プロット
if use_world_landmark:
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111, projection="3d") # type:ignore
fig.subplots_adjust(left=0.0, right=1, bottom=0, top=1)
while True:
display_fps: float = cvFpsCalc.get()
# カメラキャプチャ
ret: bool
frame: np.ndarray
ret, frame = cap.read()
if not ret:
break
if not unuse_mirror:
frame = cv2.flip(frame, 1) # ミラー表示
# 推論実施
rgb_frame: mp.Image = mp.Image(
image_format=mp.ImageFormat.SRGBA,
data=cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA),
)
detection_result: vision.HandLandmarkerResult = detector.detect(
rgb_frame)
# 外接矩形計算
bboxes: List[List[int]] = calc_bounding_rect(frame, detection_result)
# 描画
debug_image: np.ndarray = copy.deepcopy(frame)
debug_image = draw_debug(
debug_image,
detection_result,
bboxes,
display_fps,
)
# 画面反映
cv2.imshow('MediaPipe Pose Landmarks Detection Demo', debug_image)
# 描画(ワールド座標)
if use_world_landmark:
draw_world_landmarks(
plt,
ax,
detection_result,
)
# キー処理(ESC:終了)
key: int = cv2.waitKey(1)
if key == 27: # ESC
break
cap.release()
cv2.destroyAllWindows()
def calc_bounding_rect(
image: np.ndarray,
detection_result: vision.HandLandmarkerResult) -> List[List[int]]:
image_width, image_height = image.shape[1], image.shape[0]
bboxes: List[List[int]] = []
for pose_landmarks in detection_result.pose_landmarks:
landmark_array: np.ndarray = np.empty((0, 2), int)
for landmark in pose_landmarks:
landmark_x: int = min(int(landmark.x * image_width),
image_width - 1)
landmark_y: int = min(int(landmark.y * image_height),
image_height - 1)
landmark_point: np.ndarray = np.array((landmark_x, landmark_y))
landmark_array = np.append(landmark_array, [landmark_point],
axis=0)
x, y, w, h = cv2.boundingRect(landmark_array)
bboxes.append([x, y, x + w, y + h])
return bboxes
def draw_debug(
image: np.ndarray,
detection_result: vision.HandLandmarkerResult, # type:ignore
bboxes: List[List[int]],
display_fps: float,
) -> np.ndarray:
image_width, image_height = image.shape[1], image.shape[0]
landmark_draw_info: Dict[
int,
Dict[str, Union[str, Tuple[int, int, int]]],
] = {
0: { # 鼻
'name': 'NOSE',
'color': (0, 255, 0) # 緑
},
1: { # 左目(内側)
'name': 'LEFT_EYE_INNER',
'color': (255, 0, 0) # 赤
},
2: { # 左目
'name': 'LEFT_EYE',
'color': (0, 0, 255) # 青
},
3: { # 左目(外側)
'name': 'LEFT_EYE_OUTER',
'color': (255, 255, 0) # 黄
},
4: { # 右目(内側)
'name': 'RIGHT_EYE_INNER',
'color': (0, 255, 255) # シアン
},
5: { # 右目
'name': 'RIGHT_EYE',
'color': (255, 0, 255) # マゼンタ
},
6: { # 右目(外側)
'name': 'RIGHT_EYE_OUTER',
'color': (128, 128, 128) # グレー
},
7: { # 左耳
'name': 'LEFT_EAR',
'color': (255, 128, 0) # オレンジ
},
8: { # 右耳
'name': 'RIGHT_EAR',
'color': (128, 0, 255) # 紫
},
9: { # 口(左)
'name': 'MOUTH_LEFT',
'color': (0, 128, 255) # ライトブルー
},
10: { # 口(右)
'name': 'MOUTH_RIGHT',
'color': (128, 255, 0) # ライム
},
11: { # 左肩
'name': 'LEFT_SHOULDER',
'color': (255, 128, 128) # ライトレッド
},
12: { # 右肩
'name': 'RIGHT_SHOULDER',
'color': (128, 128, 0) # オリーブ
},
13: { # 左肘
'name': 'LEFT_ELBOW',
'color': (0, 128, 128) # ティール
},
14: { # 右肘
'name': 'RIGHT_ELBOW',
'color': (128, 0, 128) # マルーン
},
15: { # 左手首
'name': 'LEFT_WRIST',
'color': (64, 64, 64) # ダークグレー
},
16: { # 右手首
'name': 'RIGHT_WRIST',
'color': (192, 192, 192) # シルバー
},
17: { # 左小指
'name': 'LEFT_PINKY',
'color': (255, 69, 0) # レッドオレンジ
},
18: { # 右小指
'name': 'RIGHT_PINKY',
'color': (75, 0, 130) # インディゴ
},
19: { # 左人差し指
'name': 'LEFT_INDEX',
'color': (173, 255, 47) # グリーンイエロー
},
20: { # 右人差し指
'name': 'RIGHT_INDEX',
'color': (220, 20, 60) # クリムゾン
},
21: { # 左親指
'name': 'LEFT_THUMB',
'color': (255, 0, 0) # 赤
},
22: { # 右親指
'name': 'RIGHT_THUMB',
'color': (0, 0, 255) # 青
},
23: { # 左腰
'name': 'LEFT_HIP',
'color': (0, 255, 0) # 緑
},
24: { # 右腰
'name': 'RIGHT_HIP',
'color': (255, 255, 0) # 黄
},
25: { # 左膝
'name': 'LEFT_KNEE',
'color': (0, 255, 255) # シアン
},
26: { # 右膝
'name': 'RIGHT_KNEE',
'color': (255, 0, 255) # マゼンタ
},
27: { # 左足首
'name': 'LEFT_ANKLE',
'color': (128, 128, 128) # グレー
},
28: { # 右足首
'name': 'RIGHT_ANKLE',
'color': (255, 128, 0) # オレンジ
},
29: { # 左かかと
'name': 'LEFT_HEEL',
'color': (128, 0, 255) # 紫
},
30: { # 右かかと
'name': 'RIGHT_HEEL',
'color': (0, 128, 255) # ライトブルー
},
31: { # 左足指先
'name': 'LEFT_FOOT_INDEX',
'color': (128, 255, 0) # ライム
},
32: { # 右足指先
'name': 'RIGHT_FOOT_INDEX',
'color': (255, 128, 128) # ライトレッド
}
}
line_info_list: List[List[int]] = [
[0, 1], # 鼻から左目(内側)
[1, 2], # 左目(内側)から左目
[2, 3], # 左目から左目(外側)
[3, 7], # 左目(外側)から左耳
[0, 4], # 鼻から右目(内側)
[4, 5], # 右目(内側)から右目
[5, 6], # 右目から右目(外側)
[6, 8], # 右目(外側)から右耳
[9, 10], # 口(左)から口(右)
[11, 12], # 左肩から右肩
[11, 13], # 左肩から左肘
[13, 15], # 左肘から左手首
[15, 17], # 左手首から左小指
[15, 19], # 左手首から左人差し指
[15, 21], # 左手首から左親指
[12, 14], # 右肩から右肘
[14, 16], # 右肘から右手首
[16, 18], # 右手首から右小指
[16, 20], # 右手首から右人差し指
[16, 22], # 右手首から右親指
[23, 24], # 左腰から右腰
[23, 25], # 左腰から左膝
[25, 27], # 左膝から左足首
[27, 29], # 左足首から左かかと
[29, 31], # 左かかとから左足指先
[24, 26], # 右腰から右膝
[26, 28], # 右膝から右足首
[28, 30], # 右足首から右かかと
[30, 32], # 右かかとから右足指先
[11, 23], # 左肩から左腰
[12, 24] # 右肩から右腰
]
# セグメンテーション
if detection_result.segmentation_masks is not None:
segmentation_mask = detection_result.segmentation_masks[0].numpy_view()
mask = np.stack((segmentation_mask, ) * 3, axis=-1) > 0.5
bg_resize_image = np.zeros(image.shape, dtype=np.uint8)
bg_resize_image[:] = (0, 255, 0)
image = np.where(mask, image, bg_resize_image)
for pose_landmarks, _, bbox in zip(
detection_result.pose_landmarks,
detection_result.pose_world_landmarks,
bboxes,
):
# 各ランドマーク情報整理
landmark_dict: Dict[int, List[Union[int, float]]] = {}
for index, landmark in enumerate(pose_landmarks):
if landmark.visibility < 0 or landmark.presence < 0:
continue
landmark_x: int = min(int(landmark.x * image_width),
image_width - 1)
landmark_y: int = min(int(landmark.y * image_height),
image_height - 1)
landmark_dict[index] = [landmark_x, landmark_y, landmark.z]
# 接続線描画
for line_info in line_info_list:
cv2.line(image, tuple(landmark_dict[line_info[0]][:2]),
tuple(landmark_dict[line_info[1]][:2]), (220, 220, 220),
3, cv2.LINE_AA) # type:ignore
# 各ランドマーク描画
for index, landmark in landmark_dict.items():
cv2.circle(image, (landmark[0], landmark[1]), 5,
landmark_draw_info[index]['color'], -1,
cv2.LINE_AA) # type:ignore
# 外接矩形描画
cv2.rectangle(image, (bbox[0], bbox[1]), (bbox[2], bbox[3]),
(255, 255, 255), 2)
# FPS
if detection_result.segmentation_masks is None:
color = (0, 255, 0)
else:
color = (255, 255, 255)
cv2.putText(
image,
"FPS:" + str(display_fps),
(10, 30),
cv2.FONT_HERSHEY_SIMPLEX,
1.0,
color,
2,
cv2.LINE_AA,
)
return image
def draw_world_landmarks(
plt: Any,
ax: Any,
detection_result: vision.HandLandmarkerResult,
) -> None:
for _, pose_world_landmarks in zip(
detection_result.pose_landmarks,
detection_result.pose_world_landmarks,
):
# 各ランドマーク情報整理
landmark_dict: Dict[int, List[float]] = {}
for index, landmark in enumerate(pose_world_landmarks):
landmark_dict[index] = [landmark.x, landmark.y, landmark.z]
face_index_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
right_arm_index_list = [11, 13, 15, 17, 19, 21]
left_arm_index_list = [12, 14, 16, 18, 20, 22]
right_body_side_index_list = [11, 23, 25, 27, 29, 31]
left_body_side_index_list = [12, 24, 26, 28, 30, 32]
shoulder_index_list = [11, 12]
waist_index_list = [23, 24]
# 顔
face_x, face_y, face_z = [], [], []
for index in face_index_list:
point = landmark_dict[index]
face_x.append(point[0])
face_y.append(point[2])
face_z.append(point[1] * (-1))
# 右腕
right_arm_x, right_arm_y, right_arm_z = [], [], []
for index in right_arm_index_list:
point = landmark_dict[index]
right_arm_x.append(point[0])
right_arm_y.append(point[2])
right_arm_z.append(point[1] * (-1))
# 左腕
left_arm_x, left_arm_y, left_arm_z = [], [], []
for index in left_arm_index_list:
point = landmark_dict[index]
left_arm_x.append(point[0])
left_arm_y.append(point[2])
left_arm_z.append(point[1] * (-1))
# 右半身
right_body_side_x, right_body_side_y, right_body_side_z = [], [], []
for index in right_body_side_index_list:
point = landmark_dict[index]
right_body_side_x.append(point[0])
right_body_side_y.append(point[2])
right_body_side_z.append(point[1] * (-1))
# 左半身
left_body_side_x, left_body_side_y, left_body_side_z = [], [], []
for index in left_body_side_index_list:
point = landmark_dict[index]
left_body_side_x.append(point[0])
left_body_side_y.append(point[2])
left_body_side_z.append(point[1] * (-1))
# 肩
shoulder_x, shoulder_y, shoulder_z = [], [], []
for index in shoulder_index_list:
point = landmark_dict[index]
shoulder_x.append(point[0])
shoulder_y.append(point[2])
shoulder_z.append(point[1] * (-1))
# 腰
waist_x, waist_y, waist_z = [], [], []
for index in waist_index_list:
point = landmark_dict[index]
waist_x.append(point[0])
waist_y.append(point[2])
waist_z.append(point[1] * (-1))
ax.cla()
ax.set_xlim3d(-1.0, 1.0)
ax.set_ylim3d(-1.0, 1.0)
ax.set_zlim3d(-1.0, 1.0)
ax.scatter(face_x, face_y, face_z)
ax.plot(right_arm_x, right_arm_y, right_arm_z)
ax.plot(left_arm_x, left_arm_y, left_arm_z)
ax.plot(right_body_side_x, right_body_side_y, right_body_side_z)
ax.plot(left_body_side_x, left_body_side_y, left_body_side_z)
ax.plot(shoulder_x, shoulder_y, shoulder_z)
ax.plot(waist_x, waist_y, waist_z)
plt.pause(.001)
return
if __name__ == '__main__':
main()