Mounika256 commited on
Commit
23e13f8
·
verified ·
1 Parent(s): 1845a9a

Update visualise/rendering.py

Browse files
Files changed (1) hide show
  1. visualise/rendering.py +33 -161
visualise/rendering.py CHANGED
@@ -1,97 +1,54 @@
1
  import random
2
-
3
  import cv2
4
  import os
5
-
6
  import tempfile
7
  import threading
8
  from subprocess import call
9
 
10
  import numpy as np
11
  from scipy.io import wavfile
12
- import pyrender
13
 
14
- import librosa
 
 
15
 
 
16
  from tqdm import tqdm
17
 
18
- # import open3d as o3d
19
  from data_utils.utils import load_wav_old
20
  from voca.rendering import render_mesh_helper
21
 
22
-
23
  class Struct(object):
24
  def __init__(self, **kwargs):
25
  for key, val in kwargs.items():
26
  setattr(self, key, val)
27
 
28
-
29
  def get_sen(i, num_video, i_frame, pos):
30
  if num_video == 1:
31
  sen = 'GT'
32
  elif num_video == 2:
33
  if i == 0:
34
- if pos == 1:
35
- sen = 'A'
36
- elif pos == 2:
37
- sen = 'B'
38
- else:
39
- sen = 'GT'
40
  else:
41
- if pos == 1:
42
- sen = 'B'
43
- elif pos == 2:
44
- sen = 'A'
45
- else:
46
- sen = 'result'
47
  elif num_video == 3:
48
- if i == 0:
49
- sen = 'sample1'
50
- elif i == 1:
51
- sen = 'interpolation'
52
- else:
53
- sen = 'sample2'
54
  elif num_video == 9 or num_video == 16:
55
- if i == 0:
56
- sen = 'frame '+str(i_frame)
57
- else:
58
- sen = 'sample' + str(i)
59
  elif num_video == 12:
60
- if i == 0:
61
- sen = 'sample1'
62
- elif i < 11:
63
- sen = 'interpolation' + str(i)
64
- else:
65
- sen = 'sample2'
66
-
67
  return sen
68
 
69
-
70
  def add_image_text(img, text, color=(0,0,255), w=800, h=800):
71
  font = cv2.FONT_HERSHEY_SIMPLEX
72
- textsize = cv2.getTextSize(text, font, 8, 2)[0]
73
- textX = (img.shape[1] - textsize[0]) // 2
74
- textY = textsize[1] + 10
75
- # img = img.copy()
76
- # a = img * 255
77
- # img = a.transpose(1, 2, 0).astype(np.uint8).copy()
78
- # cv2.putText(img, '%s' % (text), (textX, textY), font, 1, (0, 0, 0), 2, cv2.LINE_AA)
79
-
80
- # w = int(text)
81
-
82
- # img = img.transpose(1, 2, 0)
83
  img = np.require(img, dtype='f4', requirements=['O', 'W'])
84
  img.flags.writeable = True
85
  img1 = img.copy()
86
  img1 = cv2.putText(img1, '%s' % (text), (100, 100), font, 4, color, 2, 1)
87
- img1 = cv2.rectangle(img1, (0, 0), (w, h), color, thickness=3, )
88
-
89
- # img1 = img1.transpose(2, 0, 1)
90
-
91
  return img1
92
 
93
-
94
- class RenderTool():
95
  def __init__(self, out_path):
96
  path = os.path.join(os.getcwd(), 'visualise/smplx/SMPLX_NEUTRAL.npz')
97
  model_data = np.load(path, allow_pickle=True)
@@ -102,21 +59,13 @@ class RenderTool():
102
  os.makedirs(self.out_path)
103
 
104
  def _render_sequences(self, cur_wav_file, v_list, j=-1, stand=False, face=False, whole_body=False, run_in_parallel=False, transcript=None):
105
- # import sys
106
- # if sys.platform == 'win32':
107
  symbol = '/'
108
- # else:
109
- # symbol = '\\'
110
- print("Render {} {} sequence.".format(cur_wav_file.split(symbol)[-2],cur_wav_file.split(symbol)[-1]))
111
  if run_in_parallel:
112
  thread = threading.Thread(target=self._render_helper, args=(cur_wav_file, v_list))
113
  thread.start()
114
  thread.join()
115
  else:
116
- # directory = os.path.join(self.out_path, cur_wav_file.split(symbol)[-2])
117
- # if not os.path.exists(directory):
118
- # os.makedirs(directory)
119
- # video_fname = os.path.join(directory, '%s.mp4' % cur_wav_file.split(symbol)[-1].split('.')[-2])
120
  directory = os.path.join(self.out_path, cur_wav_file.split(symbol)[2].split(symbol)[0])
121
  if not os.path.exists(directory):
122
  os.makedirs(directory)
@@ -131,119 +80,53 @@ class RenderTool():
131
  def _render_sequences_helper(self, video_fname, cur_wav_file, v_list, stand, face, whole_body, transcript):
132
  num_frames = v_list[0].shape[0]
133
 
134
- # dataset is inverse
135
  for v in v_list:
136
  v = v.reshape(v.shape[0], -1, 3)
137
  v[:, :, 1] = -v[:, :, 1]
138
  v[:, :, 2] = -v[:, :, 2]
 
139
  viewport_height = 800
140
  z_offset = 1.0
141
  num_video = len(v_list)
142
- assert num_video in [1, 2, 3, 9, 12, 16, 18]
143
- if num_video == 1:
144
- width, height = 800, 800
145
- elif num_video == 2:
146
- width, height = 1600, 800
147
- elif num_video == 3:
148
- width, height = 2400, 800
149
- elif num_video == 9:
150
- width, height = 2400, 2400
151
- elif num_video == 12:
152
- width, height = 3200, 2400
153
- elif num_video == 16:
154
- width, height = 3200, 3200
155
- elif num_video == 18:
156
- width, height = 4800, 2400
157
 
158
  if whole_body:
159
  width, height = 800, 1440
160
  viewport_height = 1440
161
  z_offset = 1.8
162
 
163
- sr = 22000
164
  audio, sr = librosa.load(cur_wav_file, sr=16000)
165
  tmp_audio_file = tempfile.NamedTemporaryFile('w', suffix='.wav', dir=os.path.dirname(video_fname))
166
  tmp_audio_file.close()
167
  wavfile.write(tmp_audio_file.name, sr, audio)
168
  tmp_video_file = tempfile.NamedTemporaryFile('w', suffix='.mp4', dir=os.path.dirname(video_fname))
169
  tmp_video_file.close()
170
- if int(cv2.__version__[0]) < 3:
171
- print('cv2 < 3')
172
- writer = cv2.VideoWriter(tmp_video_file.name, cv2.cv.CV_FOURCC(*'mp4v'), 30, (width, height), True)
173
- else:
174
- print('cv2 >= 3')
175
- writer = cv2.VideoWriter(tmp_video_file.name, cv2.VideoWriter_fourcc(*'mp4v'), 30, (width, height), True)
176
 
177
- center = np.mean(v_list[0][0], axis=0)
178
 
179
- r = pyrender.OffscreenRenderer(viewport_width=800, viewport_height=viewport_height)
180
 
181
- # random exchange the position of our method and SG3D
182
- # pos = random.randint(1, 2)
183
- # video_fname = list(video_fname)
184
- # video_fname.insert(-4, str(pos))
185
- # video_fname = ''.join(video_fname)
186
- pos = 1
187
 
188
  for i_frame in tqdm(range(num_frames)):
189
- # pyrender.Viewer(scene)
190
  cur_img = []
191
  for i in range(len(v_list)):
192
- if face:
193
- img = render_mesh_helper((v_list[i][i_frame], self.f), center,
194
- r=r, xmag=0.15, y=1, z=1.0, camera='o')
195
- else:
196
- img = render_mesh_helper((v_list[i][i_frame], self.f), center, camera='o', r=r, y=0.7, z_offset=z_offset)
197
- # sen = get_sen(i, num_video, i_frame, pos)
198
- # if transcript is not None:
199
- # sen = str(int(transcript[i_frame].item()))
200
- # else:
201
- # sen = ' '
202
- # img = add_image_text(img, sen)
203
  cur_img.append(img)
204
 
205
- if num_video == 1:
206
- final_img = cur_img[0].astype(np.uint8)
207
- elif num_video == 2:
208
- final_img = np.hstack((cur_img[0], cur_img[1])).astype(np.uint8)
209
- elif num_video == 3:
210
- final_img = np.hstack((cur_img[0], cur_img[1], cur_img[2])).astype(np.uint8)
211
- elif num_video == 9:
212
- img_vert_0 = np.hstack((cur_img[0], cur_img[1], cur_img[2])).astype(np.uint8)
213
- img_vert_1 = np.hstack((cur_img[3], cur_img[4], cur_img[5])).astype(np.uint8)
214
- img_vert_2 = np.hstack((cur_img[6], cur_img[7], cur_img[8])).astype(np.uint8)
215
- final_img = np.vstack((img_vert_0, img_vert_1, img_vert_2)).astype(np.uint8)
216
- elif num_video == 12:
217
- img_vert_0 = np.hstack((cur_img[0], cur_img[1], cur_img[2], cur_img[3])).astype(np.uint8)
218
- img_vert_1 = np.hstack((cur_img[4], cur_img[5], cur_img[6], cur_img[7])).astype(np.uint8)
219
- img_vert_2 = np.hstack((cur_img[8], cur_img[9], cur_img[10], cur_img[11])).astype(np.uint8)
220
- final_img = np.vstack((img_vert_0, img_vert_1, img_vert_2)).astype(np.uint8)
221
- elif num_video == 16:
222
- img_vert_0 = np.hstack((cur_img[0], cur_img[1], cur_img[2], cur_img[3])).astype(np.uint8)
223
- img_vert_1 = np.hstack((cur_img[4], cur_img[5], cur_img[6], cur_img[7])).astype(np.uint8)
224
- img_vert_2 = np.hstack((cur_img[8], cur_img[9], cur_img[10], cur_img[11])).astype(np.uint8)
225
- img_vert_3 = np.hstack((cur_img[12], cur_img[13], cur_img[14], cur_img[15])).astype(np.uint8)
226
- final_img = np.vstack((img_vert_0, img_vert_1, img_vert_2, img_vert_3)).astype(np.uint8)
227
- elif num_video == 18:
228
- img_vert_0 = np.hstack((cur_img[0], cur_img[1], cur_img[2], cur_img[3], cur_img[4], cur_img[5])).astype(np.uint8)
229
- img_vert_1 = np.hstack((cur_img[6], cur_img[7], cur_img[8], cur_img[9], cur_img[10], cur_img[11])).astype(np.uint8)
230
- img_vert_2 = np.hstack((cur_img[12], cur_img[13], cur_img[14], cur_img[15], cur_img[16], cur_img[17])).astype(
231
- np.uint8)
232
- final_img = np.vstack((img_vert_0, img_vert_1, img_vert_2)).astype(np.uint8)
233
- # final_img = add_image_text(final_img, 'frame'+str(i_frame), w=width, h=height)
234
  writer.write(final_img)
235
  writer.release()
236
 
237
- cmd = ('ffmpeg' + ' -i {0} -i {1} -vcodec h264 -ac 2 -channel_layout stereo -pix_fmt yuv420p {2}'.format(
238
  tmp_audio_file.name, tmp_video_file.name, video_fname)).split()
239
- # cmd = ('ffmpeg' + '-i {0} -vcodec h264 -ac 2 -channel_layout stereo -pix_fmt yuv420p {1}'.format(
240
- # tmp_video_file.name, video_fname)).split()
241
  call(cmd)
242
  os.remove(tmp_audio_file.name)
243
  os.remove(tmp_video_file.name)
244
 
245
  def _render_continuity(self, cur_wav_file, pred, frame, run_in_parallel=False):
246
- print("Render {} {} sequence.".format(cur_wav_file.split(symbol)[-2],cur_wav_file.split(symbol)[-1]))
 
247
  if run_in_parallel:
248
  thread = threading.Thread(target=self._render_helper, args=(cur_wav_file, pred))
249
  thread.start()
@@ -252,53 +135,42 @@ class RenderTool():
252
  self._render_helper2(cur_wav_file, pred, frame)
253
 
254
  def _render_helper2(self, cur_wav_file, pred, frame):
255
- directory = os.path.join(self.out_path, cur_wav_file.split('/')[2].split(symbol)[0])
 
256
  if not os.path.exists(directory):
257
  os.makedirs(directory)
258
- video_fname = os.path.join(directory, '%s.mp4' % cur_wav_file.split(symbol)[-1].split('.')[-2].split('/')[-1])
259
  self._render_sequences_helper2(video_fname, cur_wav_file, pred, frame)
260
 
261
  def _render_sequences_helper2(self, video_fname, cur_wav_file, pred, frame):
262
-
263
  num_frames = pred.shape[0]
264
  pred = pred.reshape(pred.shape[0], -1, 3)
265
 
266
  pred[:, :, 1] = -pred[:, :, 1]
267
  pred[:, :, 2] = -pred[:, :, 2]
268
 
269
- sr = 22000
270
- audio, sr = load_wav_old(cur_wav_file, sr=sr)
271
  tmp_audio_file = tempfile.NamedTemporaryFile('w', suffix='.wav', dir=os.path.dirname(video_fname))
272
  tmp_audio_file.close()
273
  wavfile.write(tmp_audio_file.name, sr, audio)
274
  tmp_video_file = tempfile.NamedTemporaryFile('w', suffix='.mp4', dir=os.path.dirname(video_fname))
275
  tmp_video_file.close()
276
- if int(cv2.__version__[0]) < 3:
277
- print('cv2 < 3')
278
- writer = cv2.VideoWriter(tmp_video_file.name, cv2.cv.CV_FOURCC(*'mp4v'), 15, (190, 800), True)
279
- else:
280
- print('cv2 >= 3')
281
- writer = cv2.VideoWriter(tmp_video_file.name, cv2.VideoWriter_fourcc(*'mp4v'), 30, (800, 800), True)
282
 
283
  center = np.mean(pred[0], axis=0)
284
 
285
- r = pyrender.OffscreenRenderer(viewport_width=800, viewport_height=800)
286
 
287
  for i_frame in tqdm(range(num_frames)):
288
- if i_frame < frame:
289
- sen = 'sequence 1'
290
- color = (0,255,0)
291
- else:
292
- sen = 'sequence 2'
293
- color = (0, 0, 255)
294
- pred_img = render_mesh_helper(Mesh(pred[i_frame], self.template_mesh.f), center, camera='o',r=r, y=0.7)
295
- pred_img = add_image_text(pred_img, sen, color)
296
  pred_img = pred_img.astype(np.uint8)
297
  writer.write(pred_img)
298
  writer.release()
299
 
300
- cmd = ('ffmpeg' + ' -i {0} -i {1} -vcodec h264 -ac 2 -channel_layout stereo -pix_fmt yuv420p {2}'.format(
301
  tmp_audio_file.name, tmp_video_file.name, video_fname)).split()
302
  call(cmd)
303
  os.remove(tmp_audio_file.name)
304
- os.remove(tmp_video_file.name)
 
1
  import random
 
2
  import cv2
3
  import os
 
4
  import tempfile
5
  import threading
6
  from subprocess import call
7
 
8
  import numpy as np
9
  from scipy.io import wavfile
 
10
 
11
+ from pyrender import OffscreenRenderer, DirectionalLight, Scene, Mesh
12
+ import trimesh
13
+ from PIL import Image
14
 
15
+ import librosa
16
  from tqdm import tqdm
17
 
 
18
  from data_utils.utils import load_wav_old
19
  from voca.rendering import render_mesh_helper
20
 
 
21
  class Struct(object):
22
  def __init__(self, **kwargs):
23
  for key, val in kwargs.items():
24
  setattr(self, key, val)
25
 
 
26
  def get_sen(i, num_video, i_frame, pos):
27
  if num_video == 1:
28
  sen = 'GT'
29
  elif num_video == 2:
30
  if i == 0:
31
+ sen = 'A' if pos == 1 else 'B'
 
 
 
 
 
32
  else:
33
+ sen = 'B' if pos == 1 else 'A'
 
 
 
 
 
34
  elif num_video == 3:
35
+ sen = ['sample1', 'interpolation', 'sample2'][i]
 
 
 
 
 
36
  elif num_video == 9 or num_video == 16:
37
+ sen = 'frame ' + str(i_frame) if i == 0 else 'sample' + str(i)
 
 
 
38
  elif num_video == 12:
39
+ sen = 'sample1' if i == 0 else 'sample2' if i == 11 else 'interpolation' + str(i)
 
 
 
 
 
 
40
  return sen
41
 
 
42
  def add_image_text(img, text, color=(0,0,255), w=800, h=800):
43
  font = cv2.FONT_HERSHEY_SIMPLEX
 
 
 
 
 
 
 
 
 
 
 
44
  img = np.require(img, dtype='f4', requirements=['O', 'W'])
45
  img.flags.writeable = True
46
  img1 = img.copy()
47
  img1 = cv2.putText(img1, '%s' % (text), (100, 100), font, 4, color, 2, 1)
48
+ img1 = cv2.rectangle(img1, (0, 0), (w, h), color, thickness=3)
 
 
 
49
  return img1
50
 
51
+ class RenderTool:
 
52
  def __init__(self, out_path):
53
  path = os.path.join(os.getcwd(), 'visualise/smplx/SMPLX_NEUTRAL.npz')
54
  model_data = np.load(path, allow_pickle=True)
 
59
  os.makedirs(self.out_path)
60
 
61
  def _render_sequences(self, cur_wav_file, v_list, j=-1, stand=False, face=False, whole_body=False, run_in_parallel=False, transcript=None):
 
 
62
  symbol = '/'
63
+ print("Render {} {} sequence.".format(cur_wav_file.split(symbol)[-2], cur_wav_file.split(symbol)[-1]))
 
 
64
  if run_in_parallel:
65
  thread = threading.Thread(target=self._render_helper, args=(cur_wav_file, v_list))
66
  thread.start()
67
  thread.join()
68
  else:
 
 
 
 
69
  directory = os.path.join(self.out_path, cur_wav_file.split(symbol)[2].split(symbol)[0])
70
  if not os.path.exists(directory):
71
  os.makedirs(directory)
 
80
  def _render_sequences_helper(self, video_fname, cur_wav_file, v_list, stand, face, whole_body, transcript):
81
  num_frames = v_list[0].shape[0]
82
 
 
83
  for v in v_list:
84
  v = v.reshape(v.shape[0], -1, 3)
85
  v[:, :, 1] = -v[:, :, 1]
86
  v[:, :, 2] = -v[:, :, 2]
87
+
88
  viewport_height = 800
89
  z_offset = 1.0
90
  num_video = len(v_list)
91
+ width, height = (800, 800) if num_video == 1 else (1600, 800) if num_video == 2 else (2400, 800)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
 
93
  if whole_body:
94
  width, height = 800, 1440
95
  viewport_height = 1440
96
  z_offset = 1.8
97
 
 
98
  audio, sr = librosa.load(cur_wav_file, sr=16000)
99
  tmp_audio_file = tempfile.NamedTemporaryFile('w', suffix='.wav', dir=os.path.dirname(video_fname))
100
  tmp_audio_file.close()
101
  wavfile.write(tmp_audio_file.name, sr, audio)
102
  tmp_video_file = tempfile.NamedTemporaryFile('w', suffix='.mp4', dir=os.path.dirname(video_fname))
103
  tmp_video_file.close()
 
 
 
 
 
 
104
 
105
+ writer = cv2.VideoWriter(tmp_video_file.name, cv2.VideoWriter_fourcc(*'mp4v'), 30, (width, height), True)
106
 
107
+ center = np.mean(v_list[0][0], axis=0)
108
 
109
+ r = OffscreenRenderer(viewport_width=800, viewport_height=viewport_height)
 
 
 
 
 
110
 
111
  for i_frame in tqdm(range(num_frames)):
 
112
  cur_img = []
113
  for i in range(len(v_list)):
114
+ img = render_mesh_helper((v_list[i][i_frame], self.f), center, r=r, camera='o', y=0.7, z_offset=z_offset)
 
 
 
 
 
 
 
 
 
 
115
  cur_img.append(img)
116
 
117
+ final_img = np.hstack(cur_img).astype(np.uint8) if num_video in [2, 3] else cur_img[0].astype(np.uint8)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118
  writer.write(final_img)
119
  writer.release()
120
 
121
+ cmd = ('ffmpeg -i {0} -i {1} -vcodec h264 -ac 2 -channel_layout stereo -pix_fmt yuv420p {2}'.format(
122
  tmp_audio_file.name, tmp_video_file.name, video_fname)).split()
 
 
123
  call(cmd)
124
  os.remove(tmp_audio_file.name)
125
  os.remove(tmp_video_file.name)
126
 
127
  def _render_continuity(self, cur_wav_file, pred, frame, run_in_parallel=False):
128
+ symbol = '/'
129
+ print("Render {} {} sequence.".format(cur_wav_file.split(symbol)[-2], cur_wav_file.split(symbol)[-1]))
130
  if run_in_parallel:
131
  thread = threading.Thread(target=self._render_helper, args=(cur_wav_file, pred))
132
  thread.start()
 
135
  self._render_helper2(cur_wav_file, pred, frame)
136
 
137
  def _render_helper2(self, cur_wav_file, pred, frame):
138
+ symbol = '/'
139
+ directory = os.path.join(self.out_path, cur_wav_file.split(symbol)[2].split(symbol)[0])
140
  if not os.path.exists(directory):
141
  os.makedirs(directory)
142
+ video_fname = os.path.join(directory, '%s.mp4' % cur_wav_file.split(symbol)[-1].split('.')[-2].split(symbol)[-1])
143
  self._render_sequences_helper2(video_fname, cur_wav_file, pred, frame)
144
 
145
  def _render_sequences_helper2(self, video_fname, cur_wav_file, pred, frame):
 
146
  num_frames = pred.shape[0]
147
  pred = pred.reshape(pred.shape[0], -1, 3)
148
 
149
  pred[:, :, 1] = -pred[:, :, 1]
150
  pred[:, :, 2] = -pred[:, :, 2]
151
 
152
+ audio, sr = load_wav_old(cur_wav_file, sr=22000)
 
153
  tmp_audio_file = tempfile.NamedTemporaryFile('w', suffix='.wav', dir=os.path.dirname(video_fname))
154
  tmp_audio_file.close()
155
  wavfile.write(tmp_audio_file.name, sr, audio)
156
  tmp_video_file = tempfile.NamedTemporaryFile('w', suffix='.mp4', dir=os.path.dirname(video_fname))
157
  tmp_video_file.close()
158
+
159
+ writer = cv2.VideoWriter(tmp_video_file.name, cv2.VideoWriter_fourcc(*'mp4v'), 30, (800, 800), True)
 
 
 
 
160
 
161
  center = np.mean(pred[0], axis=0)
162
 
163
+ r = OffscreenRenderer(viewport_width=800, viewport_height=800)
164
 
165
  for i_frame in tqdm(range(num_frames)):
166
+ pred_img = render_mesh_helper((pred[i_frame], self.f), center, camera='o', r=r, y=0.7)
167
+ pred_img = add_image_text(pred_img, 'frame{}'.format(i_frame))
 
 
 
 
 
 
168
  pred_img = pred_img.astype(np.uint8)
169
  writer.write(pred_img)
170
  writer.release()
171
 
172
+ cmd = ('ffmpeg -i {0} -i {1} -vcodec h264 -ac 2 -channel_layout stereo -pix_fmt yuv420p {2}'.format(
173
  tmp_audio_file.name, tmp_video_file.name, video_fname)).split()
174
  call(cmd)
175
  os.remove(tmp_audio_file.name)
176
+ os.remove(tmp_video_file.name)