[36ab12]: / ViTPose / tools / webcam / webcam_apis / utils / pose.py

Download this file

227 lines (189 with data), 7.9 kB

  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
# Copyright (c) OpenMMLab. All rights reserved.
from typing import List, Tuple
from mmcv import Config
from mmpose.datasets.dataset_info import DatasetInfo
def get_eye_keypoint_ids(model_cfg: Config) -> Tuple[int, int]:
"""A helpfer function to get the keypoint indices of left and right eyes
from the model config.
Args:
model_cfg (Config): pose model config.
Returns:
int: left eye keypoint index.
int: right eye keypoint index.
"""
left_eye_idx = None
right_eye_idx = None
# try obtaining eye point ids from dataset_info
try:
dataset_info = DatasetInfo(model_cfg.data.test.dataset_info)
left_eye_idx = dataset_info.keypoint_name2id.get('left_eye', None)
right_eye_idx = dataset_info.keypoint_name2id.get('right_eye', None)
except AttributeError:
left_eye_idx = None
right_eye_idx = None
if left_eye_idx is None or right_eye_idx is None:
# Fall back to hard coded keypoint id
dataset_name = model_cfg.data.test.type
if dataset_name in {
'TopDownCocoDataset', 'TopDownCocoWholeBodyDataset'
}:
left_eye_idx = 1
right_eye_idx = 2
elif dataset_name in {'AnimalPoseDataset', 'AnimalAP10KDataset'}:
left_eye_idx = 0
right_eye_idx = 1
else:
raise ValueError('Can not determine the eye keypoint id of '
f'{dataset_name}')
return left_eye_idx, right_eye_idx
def get_face_keypoint_ids(model_cfg: Config) -> Tuple[int, int]:
"""A helpfer function to get the keypoint indices of the face from the
model config.
Args:
model_cfg (Config): pose model config.
Returns:
list[int]: face keypoint index.
"""
face_indices = None
# try obtaining nose point ids from dataset_info
try:
dataset_info = DatasetInfo(model_cfg.data.test.dataset_info)
for id in range(68):
face_indices.append(
dataset_info.keypoint_name2id.get(f'face_{id}', None))
except AttributeError:
face_indices = None
if face_indices is None:
# Fall back to hard coded keypoint id
dataset_name = model_cfg.data.test.type
if dataset_name in {'TopDownCocoWholeBodyDataset'}:
face_indices = list(range(23, 91))
else:
raise ValueError('Can not determine the face id of '
f'{dataset_name}')
return face_indices
def get_wrist_keypoint_ids(model_cfg: Config) -> Tuple[int, int]:
"""A helpfer function to get the keypoint indices of left and right wrist
from the model config.
Args:
model_cfg (Config): pose model config.
Returns:
int: left wrist keypoint index.
int: right wrist keypoint index.
"""
# try obtaining eye point ids from dataset_info
try:
dataset_info = DatasetInfo(model_cfg.data.test.dataset_info)
left_wrist_idx = dataset_info.keypoint_name2id.get('left_wrist', None)
right_wrist_idx = dataset_info.keypoint_name2id.get(
'right_wrist', None)
except AttributeError:
left_wrist_idx = None
right_wrist_idx = None
if left_wrist_idx is None or right_wrist_idx is None:
# Fall back to hard coded keypoint id
dataset_name = model_cfg.data.test.type
if dataset_name in {
'TopDownCocoDataset', 'TopDownCocoWholeBodyDataset'
}:
left_wrist_idx = 9
right_wrist_idx = 10
elif dataset_name == 'AnimalPoseDataset':
left_wrist_idx = 16
right_wrist_idx = 17
elif dataset_name == 'AnimalAP10KDataset':
left_wrist_idx = 7
right_wrist_idx = 10
else:
raise ValueError('Can not determine the eye keypoint id of '
f'{dataset_name}')
return left_wrist_idx, right_wrist_idx
def get_mouth_keypoint_ids(model_cfg: Config) -> Tuple[int, int]:
"""A helpfer function to get the keypoint indices of the left and right
part of mouth from the model config.
Args:
model_cfg (Config): pose model config.
Returns:
int: left-part mouth keypoint index.
int: right-part mouth keypoint index.
"""
# try obtaining mouth point ids from dataset_info
try:
dataset_info = DatasetInfo(model_cfg.data.test.dataset_info)
mouth_index = dataset_info.keypoint_name2id.get('face-62', None)
except AttributeError:
mouth_index = None
if mouth_index is None:
# Fall back to hard coded keypoint id
dataset_name = model_cfg.data.test.type
if dataset_name == 'TopDownCocoWholeBodyDataset':
mouth_index = 85
else:
raise ValueError('Can not determine the eye keypoint id of '
f'{dataset_name}')
return mouth_index
def get_hand_keypoint_ids(model_cfg: Config) -> List[int]:
"""A helpfer function to get the keypoint indices of left and right hand
from the model config.
Args:
model_cfg (Config): pose model config.
Returns:
list[int]: hand keypoint indices.
"""
# try obtaining hand keypoint ids from dataset_info
try:
hand_indices = []
dataset_info = DatasetInfo(model_cfg.data.test.dataset_info)
hand_indices.append(
dataset_info.keypoint_name2id.get('left_hand_root', None))
for id in range(1, 5):
hand_indices.append(
dataset_info.keypoint_name2id.get(f'left_thumb{id}', None))
for id in range(1, 5):
hand_indices.append(
dataset_info.keypoint_name2id.get(f'left_forefinger{id}',
None))
for id in range(1, 5):
hand_indices.append(
dataset_info.keypoint_name2id.get(f'left_middle_finger{id}',
None))
for id in range(1, 5):
hand_indices.append(
dataset_info.keypoint_name2id.get(f'left_ring_finger{id}',
None))
for id in range(1, 5):
hand_indices.append(
dataset_info.keypoint_name2id.get(f'left_pinky_finger{id}',
None))
hand_indices.append(
dataset_info.keypoint_name2id.get('right_hand_root', None))
for id in range(1, 5):
hand_indices.append(
dataset_info.keypoint_name2id.get(f'right_thumb{id}', None))
for id in range(1, 5):
hand_indices.append(
dataset_info.keypoint_name2id.get(f'right_forefinger{id}',
None))
for id in range(1, 5):
hand_indices.append(
dataset_info.keypoint_name2id.get(f'right_middle_finger{id}',
None))
for id in range(1, 5):
hand_indices.append(
dataset_info.keypoint_name2id.get(f'right_ring_finger{id}',
None))
for id in range(1, 5):
hand_indices.append(
dataset_info.keypoint_name2id.get(f'right_pinky_finger{id}',
None))
except AttributeError:
hand_indices = None
if hand_indices is None:
# Fall back to hard coded keypoint id
dataset_name = model_cfg.data.test.type
if dataset_name in {'TopDownCocoWholeBodyDataset'}:
hand_indices = list(range(91, 133))
else:
raise ValueError('Can not determine the hand id of '
f'{dataset_name}')
return hand_indices