-
Notifications
You must be signed in to change notification settings - Fork 10
/
DARTset_utils.py
181 lines (152 loc) · 5.96 KB
/
DARTset_utils.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
from typing import Union
import cv2
import numpy as np
import torch
from pytorch3d.transforms import (axis_angle_to_matrix, matrix_to_quaternion, quaternion_to_axis_angle)
class Compose:
def __init__(self, transforms: list):
"""Composes several transforms together. This transform does not
support torchscript.
Args:
transforms (list): (list of transform functions)
"""
self.transforms = transforms
def __call__(self, rotation: Union[torch.Tensor, np.ndarray], convention: str = 'xyz', **kwargs):
convention = convention.lower()
if not (set(convention) == set('xyz') and len(convention) == 3):
raise ValueError(f'Invalid convention {convention}.')
if isinstance(rotation, np.ndarray):
data_type = 'numpy'
rotation = torch.FloatTensor(rotation)
elif isinstance(rotation, torch.Tensor):
data_type = 'tensor'
else:
raise TypeError('Type of rotation should be torch.Tensor or numpy.ndarray')
for t in self.transforms:
if 'convention' in t.__code__.co_varnames:
rotation = t(rotation, convention.upper(), **kwargs)
else:
rotation = t(rotation, **kwargs)
if data_type == 'numpy':
rotation = rotation.detach().cpu().numpy()
return rotation
def aa_to_rotmat(axis_angle: Union[torch.Tensor, np.ndarray]) -> Union[torch.Tensor, np.ndarray]:
"""
Convert axis_angle to rotation matrixs.
Args:
axis_angle (Union[torch.Tensor, numpy.ndarray]): input shape
should be (..., 3). ndim of input is unlimited.
Returns:
Union[torch.Tensor, numpy.ndarray]: shape would be (..., 3, 3).
"""
if axis_angle.shape[-1] != 3:
raise ValueError(f'Invalid input axis angles shape f{axis_angle.shape}.')
t = Compose([axis_angle_to_matrix])
return t(axis_angle)
def rotmat_to_aa(matrix: Union[torch.Tensor, np.ndarray]) -> Union[torch.Tensor, np.ndarray]:
"""Convert rotation matrixs to axis angles.
Args:
matrix (Union[torch.Tensor, numpy.ndarray]): input shape
should be (..., 3, 3). ndim of input is unlimited.
convention (str, optional): Convention string of three letters
from {“x”, “y”, and “z”}. Defaults to 'xyz'.
Returns:
Union[torch.Tensor, numpy.ndarray]: shape would be (..., 3).
"""
if matrix.shape[-1] != 3 or matrix.shape[-2] != 3:
raise ValueError(f'Invalid rotation matrix shape f{matrix.shape}.')
t = Compose([matrix_to_quaternion, quaternion_to_axis_angle])
return t(matrix)
def fit_ortho_param(joints3d: np.ndarray, joints2d: np.ndarray) -> np.ndarray:
joints3d_xy = joints3d[:, :2] # (21, 2)
joints3d_xy = joints3d_xy.reshape(-1)[:, np.newaxis]
joints2d = joints2d.reshape(-1)[:, np.newaxis]
pad2 = np.array(range(joints2d.shape[0]))
pad2 = (pad2 % 2)[:, np.newaxis]
pad1 = 1 - pad2
jM = np.concatenate([joints3d_xy, pad1, pad2], axis=1) # (42, 3)
jMT = jM.transpose() # (3, 42)
jMTjM = np.matmul(jMT, jM)
jMTb = np.matmul(jMT, joints2d)
ortho_param = np.matmul(np.linalg.inv(jMTjM), jMTb)
ortho_param = ortho_param.reshape(-1)
return ortho_param # [f, tx, ty]
def ortho_project(points3d, ortho_cam):
x, y = points3d[:, 0], points3d[:, 1]
u = ortho_cam[0] * x + ortho_cam[1]
v = ortho_cam[0] * y + ortho_cam[2]
u_, v_ = u[:, np.newaxis], v[:, np.newaxis]
return np.concatenate([u_, v_], axis=1)
class COLOR_CONST():
colors = {
"colors": [228 / 255, 178 / 255, 148 / 255],
"light_pink": [0.9, 0.7, 0.7], # This is used to do no-3d
"light_blue": [102 / 255, 209 / 255, 243 / 255],
}
color_hand_joints = [
[1.0, 0.0, 0.0],
[0.0, 0.4, 0.0],
[0.0, 0.6, 0.0],
[0.0, 0.8, 0.0],
[0.0, 1.0, 0.0], # thumb
[0.0, 0.0, 0.6],
[0.0, 0.0, 1.0],
[0.2, 0.2, 1.0],
[0.4, 0.4, 1.0], # index
[0.0, 0.4, 0.4],
[0.0, 0.6, 0.6],
[0.0, 0.8, 0.8],
[0.0, 1.0, 1.0], # middle
[0.4, 0.4, 0.0],
[0.6, 0.6, 0.0],
[0.8, 0.8, 0.0],
[1.0, 1.0, 0.0], # ring
[0.4, 0.0, 0.4],
[0.6, 0.0, 0.6],
[0.8, 0.0, 0.8],
[1.0, 0.0, 1.0],
] # little
def plot_hand(image, coords_hw, vis=None, linewidth=3):
"""Plots a hand stick figure into a matplotlib figure."""
colors = np.array(COLOR_CONST.color_hand_joints)
colors = colors[:, ::-1]
# define connections and colors of the bones
bones = [
((0, 1), colors[1, :]),
((1, 2), colors[2, :]),
((2, 3), colors[3, :]),
((3, 4), colors[4, :]),
((0, 5), colors[5, :]),
((5, 6), colors[6, :]),
((6, 7), colors[7, :]),
((7, 8), colors[8, :]),
((0, 9), colors[9, :]),
((9, 10), colors[10, :]),
((10, 11), colors[11, :]),
((11, 12), colors[12, :]),
((0, 13), colors[13, :]),
((13, 14), colors[14, :]),
((14, 15), colors[15, :]),
((15, 16), colors[16, :]),
((0, 17), colors[17, :]),
((17, 18), colors[18, :]),
((18, 19), colors[19, :]),
((19, 20), colors[20, :]),
]
if vis is None:
vis = np.ones_like(coords_hw[:, 0]) == 1.0
for connection, color in bones:
if (vis[connection[0]] == False) or (vis[connection[1]] == False):
continue
coord1 = coords_hw[connection[0], :]
coord2 = coords_hw[connection[1], :]
c1x = int(coord1[0])
c1y = int(coord1[1])
c2x = int(coord2[0])
c2y = int(coord2[1])
cv2.line(image, (c1x, c1y), (c2x, c2y), color=color * 255, thickness=linewidth)
for i in range(coords_hw.shape[0]):
cx = int(coords_hw[i, 0])
cy = int(coords_hw[i, 1])
cv2.circle(image, (cx, cy), radius=2 * linewidth, thickness=-1, color=colors[i, :] * 255)
return image