-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathskeleton_config.py
284 lines (234 loc) · 13.4 KB
/
skeleton_config.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
MEDIAPIPE_KEYPOINTS_WITHOUT_HANDS = [
'nose', 'left_eye_inner', 'left_eye', 'left_eye_outer', 'right_eye_inner', 'right_eye', 'right_eye_outer', 'left_ear', 'right_ear', 'mouth_left', 'mouth_right',
'left_shoulder', 'right_shoulder', 'left_elbow', 'right_elbow', 'left_wrist', 'right_wrist', 'left_pinky1', 'right_pinky1', 'left_index1', 'right_index1', 'left_thumb', 'right_thumb',
'left_hip', 'right_hip', 'left_knee', 'right_knee', 'left_ankle', 'right_ankle', 'left_heel', 'right_heel', 'left_toe', 'right_toe'
]
MEDIAPIPE_KEYPOINTS_WITH_HANDS = [
'nose', 'left_eye_inner', 'left_eye', 'left_eye_outer', 'right_eye_inner', 'right_eye', 'right_eye_outer', 'left_ear', 'right_ear', 'mouth_left', 'mouth_right',
'left_shoulder', 'right_shoulder', 'left_elbow', 'right_elbow', 'left_wrist', 'right_wrist', 'left_pinky_dummy', 'right_pinky_dummy', 'left_index_dummy', 'right_index_dummy', 'left_thumb_dummy', 'right_thumb_dummy',
'left_hip', 'right_hip', 'left_knee', 'right_knee', 'left_ankle', 'right_ankle', 'left_heel', 'right_heel', 'left_toe', 'right_toe',
"left_wrist_dummy", "left_thumb1", "left_thumb2", "left_thumb3", "left_thumb4",
"left_index1", "left_index2", "left_index3", "left_index4",
"left_middle1", "left_middle2", "left_middle3", "left_middle4",
"left_ring1", "left_ring2", "left_ring3", "left_ring4",
"left_pinky1", "left_pinky2", "left_pinky3", "left_pinky4",
"right_wrist_dummy", "right_thumb1", "right_thumb2", "right_thumb3", "right_thumb4",
"right_index1", "right_index2", "right_index3", "right_index4",
"right_middle1", "right_middle2", "right_middle3", "right_middle4",
"right_ring1", "right_ring2", "right_ring3", "right_ring4",
"right_pinky1", "right_pinky2", "right_pinky3", "right_pinky4",
]
DEFAULT_BONES = [
'left_eye', 'right_eye', 'left_ear', 'right_ear', 'head', 'neck', 'left_collar', 'right_collar', 'left_shoulder', 'right_shoulder', 'left_elbow', 'right_elbow', 'left_wrist', 'right_wrist',
'pelvis', 'spine1', 'spine2', 'spine3', 'left_hip', 'right_hip', 'left_knee', 'right_knee', 'left_ankle', 'right_ankle', 'left_toe', 'right_toe',
'left_index1', 'left_index2', 'left_index3', 'left_index4', 'left_middle1', 'left_middle2', 'left_middle3', 'left_middle4', 'left_pinky1', 'left_pinky2', 'left_pinky3', 'left_pinky4', 'left_ring1', 'left_ring2', 'left_ring3', 'left_ring4', 'left_thumb1', 'left_thumb2', 'left_thumb3', 'left_thumb4',
'right_index1', 'right_index2', 'right_index3', 'right_index4', 'right_middle1', 'right_middle2', 'right_middle3', 'right_middle4', 'right_pinky1', 'right_pinky2', 'right_pinky3', 'right_pinky4', 'right_ring1', 'right_ring2', 'right_ring3', 'right_ring4', 'right_thumb1', 'right_thumb2', 'right_thumb3', 'right_thumb4'
]
OPTIMIZABLE_BONES = [
'head', 'neck', 'left_collar', 'right_collar', 'left_shoulder', 'right_shoulder', 'left_elbow', 'right_elbow', 'left_wrist', 'right_wrist',
'pelvis', 'spine1', 'spine2', 'spine3', 'left_hip', 'right_hip', 'left_knee', 'right_knee', 'left_ankle', 'right_ankle',
'left_index1', 'left_index2', 'left_index3', 'left_middle1', 'left_middle2', 'left_middle3', 'left_ring1', 'left_ring2', 'left_ring3', 'left_pinky1', 'left_pinky2', 'left_pinky3', 'left_thumb1', 'left_thumb2', 'left_thumb3',
'right_index1', 'right_index2', 'right_index3', 'right_middle1', 'right_middle2', 'right_middle3', 'right_ring1', 'right_ring2', 'right_ring3', 'right_pinky1', 'right_pinky2', 'right_pinky3', 'right_thumb1', 'right_thumb2', 'right_thumb3',
]
ALIGN_LOCATION_WITH = ['left_shoulder', 'right_shoulder']
ALIGN_SCALE_WITH = [('left_shoulder', 'right_shoulder'), ('left_hip', 'right_hip'), ('left_shoulder', 'left_hip'), ('right_shoulder', 'right_hip')]
TARGET_KEYPOINT_PAIRS_WITH_HANDS = [
# Head
('left_ear', 'right_ear'),
# Body
('left_shoulder', 'left_hip'),
('right_shoulder', 'right_hip'),
('left_hip', 'right_hip'),
('left_shoulder', 'right_shoulder'),
# Arms
('left_elbow', 'left_shoulder'),
('left_wrist', 'left_elbow'),
('right_elbow', 'right_shoulder'),
('right_wrist', 'right_elbow'),
# Left hand
('left_wrist', 'left_pinky1'),
('left_wrist', 'left_index1'),
('left_pinky1', 'left_index1'),
# Left hand fingers
('left_thumb1', 'left_thumb2'),
('left_thumb2', 'left_thumb3'),
('left_thumb3', 'left_thumb4'),
('left_index1', 'left_index2'),
('left_index2', 'left_index3'),
('left_index3', 'left_index4'),
('left_middle1', 'left_middle2'),
('left_middle2', 'left_middle3'),
('left_middle3', 'left_middle4'),
('left_ring1', 'left_ring2'),
('left_ring2', 'left_ring3'),
('left_ring3', 'left_ring4'),
('left_pinky1', 'left_pinky2'),
('left_pinky2', 'left_pinky3'),
('left_pinky3', 'left_pinky4'),
# Right Hand
('right_wrist', 'right_pinky1'),
('right_wrist', 'right_index1'),
('right_pinky1', 'right_index1'),
# Right hand fingers
('right_thumb1', 'right_thumb2'),
('right_thumb2', 'right_thumb3'),
('right_thumb3', 'right_thumb4'),
('right_index1', 'right_index2'),
('right_index2', 'right_index3'),
('right_index3', 'right_index4'),
('right_middle1', 'right_middle2'),
('right_middle2', 'right_middle3'),
('right_middle3', 'right_middle4'),
('right_ring1', 'right_ring2'),
('right_ring2', 'right_ring3'),
('right_ring3', 'right_ring4'),
('right_pinky1', 'right_pinky2'),
('right_pinky2', 'right_pinky3'),
('right_pinky3', 'right_pinky4'),
# Legs
('left_hip', 'left_knee'),
('left_knee', 'left_ankle'),
('right_hip', 'right_knee'),
('right_knee', 'right_ankle'),
# hands to hips
('left_wrist', 'left_hip'),
('right_wrist', 'right_hip'),
# hand to hand
('left_wrist', 'right_wrist'),
]
TARGET_KEYPOINT_PAIRS_WITHOUT_HANDS = [
# Head
('left_ear', 'right_ear'),
# Body
('left_shoulder', 'left_hip'),
('right_shoulder', 'right_hip'),
('left_hip', 'right_hip'),
('left_shoulder', 'right_shoulder'),
# Arms
('left_elbow', 'left_shoulder'),
('left_wrist', 'left_elbow'),
('right_elbow', 'right_shoulder'),
('right_wrist', 'right_elbow'),
# Left hand
('left_wrist', 'left_pinky'),
('left_wrist', 'left_index'),
('left_pinky', 'left_index'),
# Right Hand
('right_wrist', 'right_pinky'),
('right_wrist', 'right_index'),
('right_pinky', 'right_index'),
# Legs
('left_hip', 'left_knee'),
('left_knee', 'left_ankle'),
('right_hip', 'right_knee'),
('right_knee', 'right_ankle'),
# hands to hips
('left_wrist', 'left_hip'),
('right_wrist', 'right_hip'),
# hand to hand
('left_wrist', 'right_wrist'),
]
BONE_CONSTRAINTS = {
# Euler angles in world space, where human stands z up, -y back
'head': ((-30, 30), (-30, 30), (-45, 45)),
'neck': ((-30, 30), (-30, 30), (-45, 45)),
'left_collar': ((-15, 15), (-15, 15), (-15, 15)),
'right_collar': ((-15, 15), (-15, 15), (-15, 15)),
'left_shoulder': ((-45, 45), (-60, 75), (-135, 45)),
'right_shoulder': ((-45, 45), (-75, 60), (-45, 135)),
'left_elbow': ((-150, 90), (-5, 5), (-135, 5)),
'right_elbow': ((-90, 150), (-5, 5), (-5, 135)),
'left_wrist': ((-5, 5), (-75, 75), (-30, 30)),
'right_wrist': ((-5, 5), (-75, 75), (-30, 30)),
'spine1': ((-5, 15), (-20, 20), (-20, 20)),
'spine2': ((-5, 15), (-20, 20), (-20, 20)),
'spine3': ((-5, 15), (-20, 20), (-20, 20)),
'left_hip': ((-90, 45), (-45, 60), (-45, 45)),
'right_hip': ((-90, 45), (-60, 45), (-45, 45)),
'left_knee': ((-5, 135), (-15, 15), (-5, 5)),
'right_knee': ((-5, 135), (-15, 15), (-5, 5)),
'left_ankle': ((-45, 90), (-15, 15), (-45, 45)),
'right_ankle': ((-45, 90), (-15, 15), (-45, 45)),
'left_index1': ((-3, 3), (-45, 135), (-45, 45)), 'left_index2': ((-3, 3), (-45, 135), (-3, 3)), 'left_index3': ((-3, 3), (-45, 120), (-3, 3)),
'left_middle1': ((-3, 3), (-45, 135), (-45, 45)), 'left_middle2': ((-3, 3), (-45, 135), (-3, 3)), 'left_middle3': ((-3, 3), (-45, 120), (-3, 3)),
'left_ring1': ((-3, 3), (-45, 135), (-45, 45)), 'left_ring2': ((-3, 3), (-45, 135), (-3, 3)), 'left_ring3': ((-3, 3), (-45, 120), (-3, 3)),
'left_pinky1': ((-3, 3), (-45, 135), (-45, 45)), 'left_pinky2': ((-3, 3), (-45, 135), (-3, 3)), 'left_pinky3': ((-3, 3), (-45, 120), (-3, 3)),
'left_thumb1': ((-3, 3), (-45, 135), (-45, 45)), 'left_thumb2': ((-3, 3), (-45, 135), (-3, 3)), 'left_thumb3': ((-3, 3), (-45, 120), (-3, 3)),
'right_index1': ((-3, 3), (-135, 45), (-45, 45)), 'right_index2': ((-3, 3), (-135, 45), (-3, 3)), 'right_index3': ((-3, 3), (-120, 45), (-3, 3)),
'right_middle1': ((-3, 3), (-135, 45), (-45, 45)), 'right_middle2': ((-3, 3), (-135, 45), (-3, 3)), 'right_middle3': ((-3, 3), (-120, 45), (-3, 3)),
'right_ring1': ((-3, 3), (-135, 45), (-45, 45)), 'right_ring2': ((-3, 3), (-135, 45), (-3, 3)), 'right_ring3': ((-3, 3), (-120, 45), (-3, 3)),
'right_pinky1': ((-3, 3), (-135, 45), (-45, 45)), 'right_pinky2': ((-3, 3), (-135, 45), (-3, 3)), 'right_pinky3': ((-3, 3), (-120, 45), (-3, 3)),
'right_thumb1': ((-3, 3), (-135, 45), (-45, 45)), 'right_thumb2': ((-3, 3), (-135, 45), (-3, 3)), 'right_thumb3': ((-3, 3), (-120, 45), (-3, 3)),
}
import os
import json
import numpy as np
import itertools
from typing import List, Tuple, Dict, Union, Optional
import torch
def load_skeleton_data(path: str):
with open(os.path.join(path, 'skeleton.json'), 'r') as f:
skeleton = json.load(f)
bone_names = skeleton['bone_names']
bone_parents = skeleton['bone_parents']
bone_matrix_world_rest = np.load(os.path.join(path, skeleton['bone_matrix_world']))
bone_matrix = np.load(os.path.join(path, skeleton['bone_matrix_rel']))
skeleton_remap = skeleton['bone_remap']
skeleton_remap = {k: v for k, v in skeleton_remap.items() if v is not None}
skeleton_remap.update({k: k for k in DEFAULT_BONES if k in bone_names})
return bone_names, bone_parents, bone_matrix_world_rest, bone_matrix, skeleton_remap
def get_optimization_target(bone_parents: Dict[str, str], skeleton_remap: Dict[str, str], track_hand: bool):
# bones to optimize
optimizable_bones = [skeleton_remap[b] for b in OPTIMIZABLE_BONES if b in skeleton_remap]
# target pairs
if track_hand:
kpt_pairs = [(a, b) for a, b in TARGET_KEYPOINT_PAIRS_WITH_HANDS if a in skeleton_remap and b in skeleton_remap]
else:
kpt_pairs = [(a, b) for a, b in TARGET_KEYPOINT_PAIRS_WITHOUT_HANDS if a in skeleton_remap and b in skeleton_remap]
joint_pairs = [(skeleton_remap[a], skeleton_remap[b]) for a, b in kpt_pairs]
# Find bones that has target bones as children
bone_subset = []
for t in itertools.chain(*joint_pairs):
bone_chain = [t]
while bone_parents[t] is not None:
t = bone_parents[t]
bone_chain.append(t)
for b in reversed(bone_chain):
if b not in bone_subset:
bone_subset.append(b)
if track_hand:
kpt_pairs_id = torch.tensor([(MEDIAPIPE_KEYPOINTS_WITH_HANDS.index(a), MEDIAPIPE_KEYPOINTS_WITH_HANDS.index(b)) for a, b in kpt_pairs], dtype=torch.long)
else:
kpt_pairs_id = torch.tensor([(MEDIAPIPE_KEYPOINTS_WITHOUT_HANDS.index(a), MEDIAPIPE_KEYPOINTS_WITHOUT_HANDS.index(b)) for a, b in kpt_pairs], dtype=torch.long)
joint_pairs_id = torch.tensor([(bone_subset.index(a), bone_subset.index(b)) for a, b in joint_pairs], dtype=torch.long)
return bone_subset, optimizable_bones, kpt_pairs_id, joint_pairs_id
def get_constraints(bone_names: List[str], bone_matrix_world_rest: np.ndarray, optimizable_bones: List[str], skeleton_remap: Dict[str, str]):
# Get constraints
joint_constraints_id = []
joint_constraints = []
for k, c in BONE_CONSTRAINTS.items():
if not (k in skeleton_remap and skeleton_remap[k] in optimizable_bones):
continue
b = skeleton_remap[k]
constraint = []
rest_mat = bone_matrix_world_rest[bone_names.index(b)]
# Get local -> world axis
for i in range(3):
world_axis = np.argmax(np.abs(rest_mat[:3, i]))
constr = c[world_axis]
if rest_mat[world_axis, i] < 0:
constr = -constr[1], -constr[0]
constraint.append(constr)
joint_constraints_id.append(optimizable_bones.index(b))
joint_constraints.append(constraint)
joint_constraints_id = torch.tensor(joint_constraints_id, dtype=torch.long)
joint_constraints = torch.tensor(joint_constraints, dtype=torch.float32)
return joint_constraints_id, torch.deg2rad(joint_constraints)
def get_align_location(bone_names: List[str], skeleton_remap: Dict[str, str]):
align_location_kpts = torch.tensor([MEDIAPIPE_KEYPOINTS_WITHOUT_HANDS.index(k) for k in ALIGN_LOCATION_WITH], dtype=torch.long)
align_location_joints = torch.tensor([bone_names.index(skeleton_remap[k]) for k in ALIGN_LOCATION_WITH], dtype=torch.long)
return align_location_kpts, align_location_joints
def get_align_scale(bone_names: List[str], skeleton_remap: Dict[str, str]):
align_scale_pairs_kpts = torch.tensor([(MEDIAPIPE_KEYPOINTS_WITHOUT_HANDS.index(a), MEDIAPIPE_KEYPOINTS_WITHOUT_HANDS.index(b)) for a, b in ALIGN_SCALE_WITH], dtype=torch.long)
align_scale_pairs_joints = torch.tensor([(bone_names.index(skeleton_remap[a]), bone_names.index(skeleton_remap[b])) for a, b in ALIGN_SCALE_WITH], dtype=torch.long)
return align_scale_pairs_kpts, align_scale_pairs_joints