reab5555 commited on
Commit
6e79c57
·
verified ·
1 Parent(s): 9c87667

Update visualization.py

Browse files
Files changed (1) hide show
  1. visualization.py +35 -11
visualization.py CHANGED
@@ -230,7 +230,7 @@ def create_heatmap(t, mse_embeddings, mse_posture, mse_voice, video_fps, total_f
230
  combined_mse[1] = mse_posture_norm
231
  combined_mse[2] = mse_voice_norm
232
 
233
- fig, ax = plt.subplots(figsize=(video_width / 300, 0.5))
234
  ax.imshow(combined_mse, aspect='auto', cmap='Reds', vmin=0, vmax=1, extent=[0, total_frames, 0, 3])
235
  ax.set_yticks([0.5, 1.5, 2.5])
236
  ax.set_yticklabels(['Voice', 'Posture', 'Face'], fontsize=7)
@@ -264,25 +264,49 @@ def create_video_with_heatmap(video_path, df, mse_embeddings, mse_posture, mse_v
264
  width, height = video.w, video.h
265
  total_frames = int(video.duration * video.fps)
266
 
267
- def fill_with_zeros_and_values(mse_array, total_frames):
268
  result = np.zeros(total_frames)
269
  indices = np.linspace(0, total_frames - 1, len(mse_array)).astype(int)
270
  result[indices] = mse_array
 
 
 
271
  return result
272
 
273
  # Ensure all MSE arrays have the same length as total_frames
274
- mse_embeddings = fill_with_zeros_and_values(mse_embeddings, total_frames)
275
- mse_posture = fill_with_zeros_and_values(mse_posture, total_frames)
276
- mse_voice = fill_with_zeros_and_values(mse_voice, total_frames)
277
 
278
- def combine_video_and_heatmap(t):
279
- video_frame = video.get_frame(t)
280
- heatmap_frame = create_heatmap(t, mse_embeddings, mse_posture, mse_voice, video.fps, total_frames, width)
281
- heatmap_frame_resized = cv2.resize(heatmap_frame, (width, heatmap_frame.shape[0]))
282
- combined_frame = np.vstack((video_frame, heatmap_frame_resized))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
283
  return combined_frame
284
 
285
- final_clip = VideoClip(combine_video_and_heatmap, duration=video.duration)
286
  final_clip = final_clip.set_audio(video.audio)
287
 
288
  # Write the final video
 
230
  combined_mse[1] = mse_posture_norm
231
  combined_mse[2] = mse_voice_norm
232
 
233
+ fig, ax = plt.subplots(figsize=(video_width / 300, 0.4))
234
  ax.imshow(combined_mse, aspect='auto', cmap='Reds', vmin=0, vmax=1, extent=[0, total_frames, 0, 3])
235
  ax.set_yticks([0.5, 1.5, 2.5])
236
  ax.set_yticklabels(['Voice', 'Posture', 'Face'], fontsize=7)
 
264
  width, height = video.w, video.h
265
  total_frames = int(video.duration * video.fps)
266
 
267
+ def fill_with_previous_values(mse_array, total_frames):
268
  result = np.zeros(total_frames)
269
  indices = np.linspace(0, total_frames - 1, len(mse_array)).astype(int)
270
  result[indices] = mse_array
271
+ for i in range(1, total_frames):
272
+ if result[i] == 0:
273
+ result[i] = result[i - 1]
274
  return result
275
 
276
  # Ensure all MSE arrays have the same length as total_frames
277
+ mse_embeddings = fill_with_previous_values(mse_embeddings, total_frames)
278
+ mse_posture = fill_with_previous_values(mse_posture, total_frames)
279
+ mse_voice = fill_with_previous_values(mse_voice, total_frames)
280
 
281
+ def create_heatmap(t, mse_embeddings, mse_posture, mse_voice, fps, total_frames, width):
282
+ frame_index = int(t * fps)
283
+ mse_face = mse_embeddings[frame_index]
284
+ mse_body = mse_posture[frame_index]
285
+ mse_audio = mse_voice[frame_index]
286
+
287
+ max_mse_face = np.max(mse_embeddings)
288
+ max_mse_body = np.max(mse_posture)
289
+ max_mse_audio = np.max(mse_voice)
290
+
291
+ def get_heatmap_color(mse_value, max_mse_value):
292
+ normalized_value = mse_value / max_mse_value if max_mse_value != 0 else 0
293
+ color = plt.cm.hot(normalized_value)
294
+ return (color[0] * 255, color[1] * 255, color[2] * 255) # Convert to RGB
295
+
296
+ heatmap_face = np.full((height // 3, width, 3), get_heatmap_color(mse_face, max_mse_face), dtype=np.uint8)
297
+ heatmap_body = np.full((height // 3, width, 3), get_heatmap_color(mse_body, max_mse_body), dtype=np.uint8)
298
+ heatmap_audio = np.full((height // 3, width, 3), get_heatmap_color(mse_audio, max_mse_audio), dtype=np.uint8)
299
+
300
+ heatmap = np.vstack((heatmap_face, heatmap_body, heatmap_audio))
301
+ return heatmap
302
+
303
+ def combine_video_and_heatmap(get_frame, t):
304
+ video_frame = get_frame(t)
305
+ heatmap_frame = create_heatmap(t, mse_embeddings, mse_posture, mse_voice, desired_fps, total_frames, width)
306
+ combined_frame = np.vstack((video_frame, heatmap_frame))
307
  return combined_frame
308
 
309
+ final_clip = video.fl(combine_video_and_heatmap)
310
  final_clip = final_clip.set_audio(video.audio)
311
 
312
  # Write the final video