-
Notifications
You must be signed in to change notification settings - Fork 7
/
render.py
393 lines (335 loc) · 15.9 KB
/
render.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
import pickle
import numpy as np
import torch
import cv2
import os
os.environ["PYOPENGL_PLATFORM"] = "osmesa"
from tqdm import tqdm
from smplx import SMPL, SMPLX, SMPLH
import pyrender
import trimesh
import subprocess
import pickle
from pytorch3d.transforms import (axis_angle_to_matrix, matrix_to_axis_angle,
matrix_to_quaternion, matrix_to_rotation_6d,
quaternion_to_matrix, rotation_6d_to_matrix)
import sys
sys.path.append('.')
import argparse
def quat_to_6v(q):
assert q.shape[-1] == 4
mat = quaternion_to_matrix(q)
mat = matrix_to_rotation_6d(mat)
return mat
def quat_from_6v(q):
assert q.shape[-1] == 6
mat = rotation_6d_to_matrix(q)
quat = matrix_to_quaternion(mat)
return quat
def ax_to_6v(q):
assert q.shape[-1] == 3
mat = axis_angle_to_matrix(q)
mat = matrix_to_rotation_6d(mat)
return mat
def ax_from_6v(q):
assert q.shape[-1] == 6
mat = rotation_6d_to_matrix(q)
ax = matrix_to_axis_angle(mat)
return ax
class MovieMaker():
def __init__(self, save_path) -> None:
self.mag = 2
self.eyes = np.array([[3,-3,2], [0,0,-2], [0,0,4], [-8,-8,1], [0,-2,4], [0,2,4]])
self.centers = np.array([[0,0,0],[0,0,0],[0,0.5,0],[0,0,-1], [0,0.5,0], [0,0.5,0]])
self.ups = np.array([[0,0,1],[0,1,0],[0,1,0],[0,0,-1], [0,1,0], [0,1,0]])
self.save_path = save_path
self.fps = args.fps
self.img_size = (1200,1200)
# SMPLH_path = "assets/smpl_model/smplh/SMPLH_MALE.pkl"
# SMPL_path = "assets/smpl_model/smpl/SMPL_MALE.pkl"
SMPLX_path = "assets/smpl_model/smplx/SMPLX_NEUTRAL.npz"
trimesh_path = 'assets/NORMAL_new.obj'
# self.smplh = SMPLH(SMPLH_path, use_pca=False, flat_hand_mean=True)
# self.smplh.to(f'cuda:{args.gpu}').eval()
# self.smpl = SMPL(SMPL_path)
# self.smpl.to(f'cuda:{args.gpu}').eval()
self.smplx = SMPLX(SMPLX_path, use_pca=False, flat_hand_mean=True).eval()
self.smplx.to(f'cuda:{args.gpu}').eval()
self.scene = pyrender.Scene()
camera = pyrender.PerspectiveCamera(yfov=np.pi / 3.0)
camera_pose = look_at(self.eyes[5], self.centers[5], self.ups[5]) # 2
self.scene.add(camera, pose=camera_pose)
light = pyrender.DirectionalLight(color=np.ones(3), intensity=3.0)
self.scene.add(light, pose=camera_pose)
self.r = pyrender.OffscreenRenderer(self.img_size[0], self.img_size[1])
self.mesh = trimesh.load(trimesh_path)
floor_mesh = pyrender.Mesh.from_trimesh(self.mesh)
floor_node = self.scene.add(floor_mesh)
def save_video(self, save_path, color_list):
# save_path = os.path.join(save_path,'move.mp4')
f = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
videowriter = cv2.VideoWriter(save_path,f,self.fps,self.img_size)
for i in range(len(color_list)):
videowriter.write(color_list[i][:,:,::-1])
videowriter.release()
def get_imgs(self, motion):
meshes = self.motion2mesh(motion)
imgs = self.render_imgs(meshes)
return np.concatenate(imgs, axis=1)
def motion2mesh(self, motion):
if args.mode == "smpl":
output = self.smpl.forward(
betas = torch.zeros([motion.shape[0], 10]).to(motion.device),
transl = motion[:,:3],
global_orient = motion[:,3:6],
body_pose = torch.cat([motion[:,6:69], motion[:,69:72], motion[:,114:117]], dim=1)
)
elif args.mode == "smplh":
output = self.smplh.forward(
betas = torch.zeros([motion.shape[0], 10]).to(motion.device),
# transl = motion[:,:3],
transl = torch.tensor([[0,0,-1]]).expand(motion.shape[0],-1).to(motion.device) ,
global_orient = motion[:,3:6],
body_pose = motion[:,6:69],
left_hand_pose = motion[:,69:114],
right_hand_pose = motion[:,114:159],
)
elif args.mode == "smplx":
output = self.smplx.forward(
betas = torch.zeros([motion.shape[0], 10]).to(motion.device),
# transl = motion[:,:3],
transl = motion[:,:3],
global_orient = motion[:,3:6],
body_pose = motion[:,6:69],
jaw_pose = torch.zeros([motion.shape[0], 3]).to(motion),
leye_pose = torch.zeros([motion.shape[0], 3]).to(motion),
reye_pose = torch.zeros([motion.shape[0], 3]).to(motion),
left_hand_pose = motion[:,69:69+45],
right_hand_pose = motion[:,69+45:],
expression= torch.zeros([motion.shape[0], 10]).to(motion),
)
meshes = []
for i in range(output.vertices.shape[0]):
if args.mode == 'smplh':
mesh = trimesh.Trimesh(output.vertices[i].cpu(), self.smplh.faces)
elif args.mode == 'smplx':
mesh = trimesh.Trimesh(output.vertices[i].cpu(), self.smplx.faces)
elif args.mode == 'smpl':
mesh = trimesh.Trimesh(output.vertices[i].cpu(), self.smpl.faces)
# mesh.export(os.path.join(self.save_path, f'{i}.obj'))
meshes.append(mesh)
return meshes
def render_multi_view(self, meshes, music_file, tab='', eyes=None, centers=None, ups=None, views=1):
if eyes and centers and ups:
assert eyes.shape == centers.shape == ups.shape
else:
eyes = self.eyes
centers = self.centers
ups = self.ups
for i in range(views):
color_list = self.render_single_view(meshes, eyes[1], centers[1], ups[1])
movie_file = os.path.join(self.save_path, tab + '-' + str(i) + '.mp4')
output_file = os.path.join(self.save_path, tab + '-' + str(i) + '-music.mp4')
self.save_video(movie_file, color_list)
if music_file is not None:
subprocess.run(['assets/ffmpeg-6.0-amd64-static/ffmpeg','-i',movie_file,'-i',music_file,'-shortest',output_file])
else:
subprocess.run(['assets/ffmpeg-6.0-amd64-static/ffmpeg','-i',movie_file,output_file])
# if music_file is not None:
# subprocess.run(['ffmpeg','-i',movie_file,'-i',music_file,'-shortest',output_file])
# else:
# subprocess.run(['ffmpeg','-i',movie_file,output_file])
os.remove(movie_file)
def render_single_view(self, meshes):
num = len(meshes)
color_list = []
for i in tqdm(range(num)):
mesh_nodes = []
for mesh in meshes[i]:
render_mesh = pyrender.Mesh.from_trimesh(mesh)
mesh_node = self.scene.add(render_mesh)
mesh_nodes.append(mesh_node)
color, _ = self.r.render(self.scene, flags=pyrender.RenderFlags.SHADOWS_DIRECTIONAL)
color = color.copy()
color_list.append(color)
for mesh_node in mesh_nodes:
self.scene.remove_node(mesh_node)
return color_list
def render_imgs(self, meshes):
colors = []
for mesh in meshes:
render_mesh = pyrender.Mesh.from_trimesh(mesh)
mesh_node = self.scene.add(render_mesh)
color, _ = self.r.render(self.scene, flags=pyrender.RenderFlags.SHADOWS_DIRECTIONAL)
colors.append(color)
self.scene.remove_node(mesh_node)
return colors
# cv2.imwrite(os.path.join(self.save_path, 'test.jpg'), color[:,:,::-1])
def run(self, seq_rot, music_file=None, tab='', save_pt=False):
if isinstance(seq_rot, np.ndarray):
seq_rot = torch.tensor(seq_rot, dtype=torch.float32, device=f'cuda:{args.gpu}')
if save_pt:
torch.save(seq_rot.detach().cpu(), os.path.join(self.save_path, tab +'_pose.pt'))
B, D = seq_rot.shape
if args.mode == "smpl":
print("using smpl!!!")
output = self.smpl.forward(
betas = torch.zeros([seq_rot.shape[0], 10]).to(seq_rot.device),
transl = seq_rot[:,:3],
global_orient = seq_rot[:,3:6],
body_pose = torch.cat([seq_rot[:,6:69], seq_rot[:,69:72], seq_rot[:,114:117]], dim=1)
)
elif args.mode == "smplh":
print("using smplh!!!")
output = self.smplh.forward(
betas = torch.zeros([seq_rot.shape[0], 10]).to(seq_rot.device),
transl = seq_rot[:,:3],
global_orient = seq_rot[:,3:6],
body_pose = seq_rot[:,6:69],
left_hand_pose = seq_rot[:,69:114], # torch.zeros([seq_rot.shape[0], 45]).to(seq_rot.device), # seq_rot[:,69:114],
right_hand_pose = seq_rot[:,114:], # torch.zeros([seq_rot.shape[0], 45]).to(seq_rot.device), #
expression = torch.zeros([seq_rot.shape[0], 10]).to(seq_rot.device),
)
elif args.mode == "smplx":
output = self.smplx.forward(
betas = torch.zeros([seq_rot.shape[0], 10]).to(seq_rot.device),
# transl = motion[:,:3],
transl = seq_rot[:,:3],
global_orient = seq_rot[:,3:6],
body_pose = seq_rot[:,6:69],
jaw_pose = torch.zeros([seq_rot.shape[0], 3]).to(seq_rot),
leye_pose = torch.zeros([seq_rot.shape[0], 3]).to(seq_rot),
reye_pose = torch.zeros([seq_rot.shape[0], 3]).to(seq_rot),
left_hand_pose = seq_rot[:,69:69+45],
right_hand_pose = seq_rot[:,69+45:],
expression= torch.zeros([seq_rot.shape[0], 10]).to(seq_rot),
)
N, V, DD = output.vertices.shape # 150, 6890, 3
vertices = output.vertices.reshape((B, -1, V, DD)) # # 150, 1, 6890, 3
meshes = []
for i in range(B):
# if int(i) > 20:
# break
view = []
for v in vertices[i]:
# vertices[:,2] *= -1
if args.mode == 'smplh':
mesh = trimesh.Trimesh(output.vertices[i].cpu(), self.smplh.faces)
elif args.mode == 'smplx':
mesh = trimesh.Trimesh(output.vertices[i].cpu(), self.smplx.faces)
elif args.mode == 'smpl':
mesh = trimesh.Trimesh(output.vertices[i].cpu(), self.smpl.faces)
# mesh.export(os.path.join(self.save_path, 'test.obj'))
view.append(mesh)
meshes.append(view)
color_list = self.render_single_view(meshes)
movie_file = os.path.join(self.save_path, tab + 'tmp.mp4')
output_file = os.path.join(self.save_path, tab + 'z.mp4')
self.save_video(movie_file, color_list)
if music_file is not None:
subprocess.run(['assets/ffmpeg-6.0-amd64-static/ffmpeg','-i',movie_file,'-i',music_file,'-shortest',output_file])
else:
subprocess.run(['assets/ffmpeg-6.0-amd64-static/ffmpeg','-i',movie_file,output_file])
# if music_file is not None:
# subprocess.run(['ffmpeg','-i',movie_file,'-i',music_file,'-shortest',output_file])
# else:
# subprocess.run(['ffmpeg','-i',movie_file,output_file])
os.remove(movie_file)
def look_at(eye, center, up):
front = eye - center
front = front / np.linalg.norm(front)
right = np.cross(up, front)
right = right/ np.linalg.norm(right)
up_new = np.cross(front, right)
camera_pose = np.eye(4)
camera_pose[:3,:3] = np.stack([right, up_new, front]).transpose()
camera_pose[:3,3] = eye
return camera_pose
def motion_data_load_process(motionfile):
if motionfile.split(".")[-1] == "pkl":
pkl_data = pickle.load(open(motionfile, "rb"))
smpl_poses = pkl_data["smpl_poses"]
modata = np.concatenate((pkl_data["smpl_trans"], smpl_poses), axis=1)
if modata.shape[1] == 69:
hand_zeros = np.zeros([modata.shape[0], 90], dtype=np.float32)
modata = np.concatenate((modata, hand_zeros), axis=1)
assert modata.shape[1] == 159
modata[:, 1] = modata[:, 1] + 1.3
return modata
elif motionfile.split(".")[-1] == "npy":
modata = np.load(motionfile)
print("modata.shape", modata.shape)
if modata.shape[-1] == 315: # first 3-dim is root translation
print("modata.shape is:", modata.shape)
rot6d = torch.from_numpy(modata[:,3:])
T,C = rot6d.shape
rot6d = rot6d.reshape(-1,6)
axis = ax_from_6v(rot6d).view(T,-1).detach().cpu().numpy()
modata = np.concatenate((modata[:,:3], axis), axis=1)
print("modata.shape is:", modata.shape)
elif modata.shape[-1] == 319:
print("modata.shape is:", modata.shape)
modata = modata[:,4:]
rot6d = torch.from_numpy(modata[:,3:])
T,C = rot6d.shape
rot6d = rot6d.reshape(-1,6)
axis = ax_from_6v(rot6d).view(T,-1).detach().cpu().numpy()
modata = np.concatenate((modata[:,:3], axis), axis=1)
print("modata.shape is:", modata.shape)
elif modata.shape[-1] == 168:
modata = np.concatenate( [modata[:,:21*3+1], modata[:,25*3:]] , axis=1)
elif modata.shape[-1] == 159:
print("modata.shape is:", modata.shape)
print("modata.shape is:", modata.shape)
elif modata.shape[-1] == 135:
print("modata.shape is:", modata.shape)
if len(modata.shape) == 3 and modata.shape[0] ==1:
modata = modata.squeeze(0)
rot6d = torch.from_numpy(modata[:,3:])
T,C = rot6d.shape
rot6d = rot6d.reshape(-1,6)
axis = ax_from_6v(rot6d).view(T,-1).detach().cpu().numpy()
hand_zeros = torch.zeros([T, 90]).to(rot6d).detach().cpu().numpy()
modata = np.concatenate((modata[:,:3], axis, hand_zeros), axis=1)
print("modata.shape is:", modata.shape)
else:
raise("shape error!")
modata[:, 1] = modata[:, 1] + 1.3
return modata
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("--gpu", type=str, default="2")
parser.add_argument("--modir", type=str, default="")
parser.add_argument("--mode", type=str, default="smplx", choices=['smpl','smplh','smplx'])
parser.add_argument("--fps", type=int, default=30)
parser.add_argument("--save_path", type=str, default=None)
args = parser.parse_args()
print(args.gpu)
motion_dir = args.modir
if args.save_path is not None:
save_path = args.save_path
if not os.path.exists(save_path):
os.makedirs(save_path)
else:
save_path = os.path.join(motion_dir, 'video')
os.makedirs(save_path, exist_ok=True)
music_dir = "experiments/DanceDiffuse_module/debug--0517_Norm_512len_315_transloss/val1640/samples_2023-05-17-20-54-05"
for file in os.listdir(motion_dir):
if file[-3:] in ["npy", "pkl"]:
# if there have exist rendered video, continue
flag = False
for exists_file in os.listdir(save_path):
if file[:-4] in exists_file:
flag = True
break
else:
flag = False
if flag:
print("exist", file)
continue
print(file)
motion_file = os.path.join(motion_dir, file)
visualizer = MovieMaker(save_path=save_path)
modata = motion_data_load_process(motion_file)
visualizer.run(modata, tab=os.path.basename(motion_file).split(".")[0], music_file=None)
print('done')