forked from Stanford-TML/EDGE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vis.py
333 lines (295 loc) · 10.1 KB
/
vis.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
import os
from pathlib import Path
from tempfile import TemporaryDirectory
import librosa as lr
import matplotlib.animation as animation
import matplotlib.pyplot as plt
import numpy as np
import soundfile as sf
import torch
from matplotlib import cm
from matplotlib.colors import ListedColormap
from pytorch3d.transforms import (axis_angle_to_quaternion, quaternion_apply,
quaternion_multiply)
from tqdm import tqdm
smpl_joints = [
"root", # 0
"lhip", # 1
"rhip", # 2
"belly", # 3
"lknee", # 4
"rknee", # 5
"spine", # 6
"lankle",# 7
"rankle",# 8
"chest", # 9
"ltoes", # 10
"rtoes", # 11
"neck", # 12
"linshoulder", # 13
"rinshoulder", # 14
"head", # 15
"lshoulder", # 16
"rshoulder", # 17
"lelbow", # 18
"relbow", # 19
"lwrist", # 20
"rwrist", # 21
"lhand", # 22
"rhand", # 23
]
smpl_parents = [
-1,
0,
0,
0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
9,
9,
12,
13,
14,
16,
17,
18,
19,
20,
21,
]
smpl_offsets = [
[0.0, 0.0, 0.0],
[0.05858135, -0.08228004, -0.01766408],
[-0.06030973, -0.09051332, -0.01354254],
[0.00443945, 0.12440352, -0.03838522],
[0.04345142, -0.38646945, 0.008037],
[-0.04325663, -0.38368791, -0.00484304],
[0.00448844, 0.1379564, 0.02682033],
[-0.01479032, -0.42687458, -0.037428],
[0.01905555, -0.4200455, -0.03456167],
[-0.00226458, 0.05603239, 0.00285505],
[0.04105436, -0.06028581, 0.12204243],
[-0.03483987, -0.06210566, 0.13032329],
[-0.0133902, 0.21163553, -0.03346758],
[0.07170245, 0.11399969, -0.01889817],
[-0.08295366, 0.11247234, -0.02370739],
[0.01011321, 0.08893734, 0.05040987],
[0.12292141, 0.04520509, -0.019046],
[-0.11322832, 0.04685326, -0.00847207],
[0.2553319, -0.01564902, -0.02294649],
[-0.26012748, -0.01436928, -0.03126873],
[0.26570925, 0.01269811, -0.00737473],
[-0.26910836, 0.00679372, -0.00602676],
[0.08669055, -0.01063603, -0.01559429],
[-0.0887537, -0.00865157, -0.01010708],
]
def set_line_data_3d(line, x):
line.set_data(x[:, :2].T)
line.set_3d_properties(x[:, 2])
def set_scatter_data_3d(scat, x, c):
scat.set_offsets(x[:, :2])
scat.set_3d_properties(x[:, 2], "z")
scat.set_facecolors([c])
def get_axrange(poses):
pose = poses[0]
x_min = pose[:, 0].min()
x_max = pose[:, 0].max()
y_min = pose[:, 1].min()
y_max = pose[:, 1].max()
z_min = pose[:, 2].min()
z_max = pose[:, 2].max()
xdiff = x_max - x_min
ydiff = y_max - y_min
zdiff = z_max - z_min
biggestdiff = max([xdiff, ydiff, zdiff])
return biggestdiff
def plot_single_pose(num, poses, lines, ax, axrange, scat, contact):
pose = poses[num]
static = contact[num]
indices = [7, 8, 10, 11]
for i, (point, idx) in enumerate(zip(scat, indices)):
position = pose[idx : idx + 1]
color = "r" if static[i] else "g"
set_scatter_data_3d(point, position, color)
for i, (p, line) in enumerate(zip(smpl_parents, lines)):
# don't plot root
if i == 0:
continue
# stack to create a line
data = np.stack((pose[i], pose[p]), axis=0)
set_line_data_3d(line, data)
if num == 0:
if isinstance(axrange, int):
axrange = (axrange, axrange, axrange)
xcenter, ycenter, zcenter = 0, 0, 2.5
stepx, stepy, stepz = axrange[0] / 2, axrange[1] / 2, axrange[2] / 2
x_min, x_max = xcenter - stepx, xcenter + stepx
y_min, y_max = ycenter - stepy, ycenter + stepy
z_min, z_max = zcenter - stepz, zcenter + stepz
ax.set_xlim(x_min, x_max)
ax.set_ylim(y_min, y_max)
ax.set_zlim(z_min, z_max)
def skeleton_render(
poses,
epoch=0,
out="renders",
name="",
sound=True,
stitch=False,
sound_folder="ood_sliced",
contact=None,
render=True
):
if render:
# generate the pose with FK
Path(out).mkdir(parents=True, exist_ok=True)
num_steps = poses.shape[0]
fig = plt.figure()
ax = fig.add_subplot(projection="3d")
point = np.array([0, 0, 1])
normal = np.array([0, 0, 1])
d = -point.dot(normal)
xx, yy = np.meshgrid(np.linspace(-1.5, 1.5, 2), np.linspace(-1.5, 1.5, 2))
z = (-normal[0] * xx - normal[1] * yy - d) * 1.0 / normal[2]
# plot the plane
ax.plot_surface(xx, yy, z, zorder=-11, cmap=cm.twilight)
# Create lines initially without data
lines = [
ax.plot([], [], [], zorder=10, linewidth=1.5)[0]
for _ in smpl_parents
]
scat = [
ax.scatter([], [], [], zorder=10, s=0, cmap=ListedColormap(["r", "g", "b"]))
for _ in range(4)
]
axrange = 3
# create contact labels
feet = poses[:, (7, 8, 10, 11)]
feetv = np.zeros(feet.shape[:2])
feetv[:-1] = np.linalg.norm(feet[1:] - feet[:-1], axis=-1)
if contact is None:
contact = feetv < 0.01
else:
contact = contact > 0.95
# Creating the Animation object
anim = animation.FuncAnimation(
fig,
plot_single_pose,
num_steps,
fargs=(poses, lines, ax, axrange, scat, contact),
interval=1000 // 30,
)
if sound:
# make a temporary directory to save the intermediate gif in
if render:
temp_dir = TemporaryDirectory()
gifname = os.path.join(temp_dir.name, f"{epoch}.gif")
anim.save(gifname)
# stitch wavs
if stitch:
assert type(name) == list # must be a list of names to do stitching
name_ = [os.path.splitext(x)[0] + ".wav" for x in name]
audio, sr = lr.load(name_[0], sr=None)
ll, half = len(audio), len(audio) // 2
total_wav = np.zeros(ll + half * (len(name_) - 1))
total_wav[:ll] = audio
idx = ll
for n_ in name_[1:]:
audio, sr = lr.load(n_, sr=None)
total_wav[idx : idx + half] = audio[half:]
idx += half
# save a dummy spliced audio
audioname = f"{temp_dir.name}/tempsound.wav" if render else os.path.join(out, f'{epoch}_{"_".join(os.path.splitext(os.path.basename(name[0]))[0].split("_")[:-1])}.wav')
sf.write(audioname, total_wav, sr)
outname = os.path.join(
out,
f'{epoch}_{"_".join(os.path.splitext(os.path.basename(name[0]))[0].split("_")[:-1])}.mp4',
)
else:
assert type(name) == str
assert name != "", "Must provide an audio filename"
audioname = name
outname = os.path.join(
out, f"{epoch}_{os.path.splitext(os.path.basename(name))[0]}.mp4"
)
if render:
out = os.system(
f"ffmpeg -loglevel error -stream_loop 0 -y -i {gifname} -i {audioname} -shortest -c:v libx264 -crf 26 -c:a aac -q:a 4 {outname}"
)
else:
if render:
# actually save the gif
path = os.path.normpath(name)
pathparts = path.split(os.sep)
gifname = os.path.join(out, f"{pathparts[-1][:-4]}.gif")
anim.save(gifname, savefig_kwargs={"transparent": True, "facecolor": "none"},)
plt.close()
class SMPLSkeleton:
def __init__(
self, device=None,
):
offsets = smpl_offsets
parents = smpl_parents
assert len(offsets) == len(parents)
self._offsets = torch.Tensor(offsets).to(device)
self._parents = np.array(parents)
self._compute_metadata()
def _compute_metadata(self):
self._has_children = np.zeros(len(self._parents)).astype(bool)
for i, parent in enumerate(self._parents):
if parent != -1:
self._has_children[parent] = True
self._children = []
for i, parent in enumerate(self._parents):
self._children.append([])
for i, parent in enumerate(self._parents):
if parent != -1:
self._children[parent].append(i)
def forward(self, rotations, root_positions):
"""
Perform forward kinematics using the given trajectory and local rotations.
Arguments (where N = batch size, L = sequence length, J = number of joints):
-- rotations: (N, L, J, 3) tensor of axis-angle rotations describing the local rotations of each joint.
-- root_positions: (N, L, 3) tensor describing the root joint positions.
"""
assert len(rotations.shape) == 4
assert len(root_positions.shape) == 3
# transform from axis angle to quaternion
rotations = axis_angle_to_quaternion(rotations)
positions_world = []
rotations_world = []
expanded_offsets = self._offsets.expand(
rotations.shape[0],
rotations.shape[1],
self._offsets.shape[0],
self._offsets.shape[1],
)
# Parallelize along the batch and time dimensions
for i in range(self._offsets.shape[0]):
if self._parents[i] == -1:
positions_world.append(root_positions)
rotations_world.append(rotations[:, :, 0])
else:
positions_world.append(
quaternion_apply(
rotations_world[self._parents[i]], expanded_offsets[:, :, i]
)
+ positions_world[self._parents[i]]
)
if self._has_children[i]:
rotations_world.append(
quaternion_multiply(
rotations_world[self._parents[i]], rotations[:, :, i]
)
)
else:
# This joint is a terminal node -> it would be useless to compute the transformation
rotations_world.append(None)
return torch.stack(positions_world, dim=3).permute(0, 1, 3, 2)