reab5555 commited on
Commit
eccecf0
·
verified ·
1 Parent(s): 38dadc8

Update visualization.py

Browse files
Files changed (1) hide show
  1. visualization.py +48 -14
visualization.py CHANGED
@@ -249,32 +249,66 @@ def create_heatmap(t, mse_embeddings, mse_posture, mse_voice, video_fps, total_f
249
 
250
  def create_video_with_heatmap(video_path, df, mse_embeddings, mse_posture, mse_voice, output_folder, desired_fps, largest_cluster):
251
  print(f"Creating heatmap video. Output folder: {output_folder}")
252
-
253
  os.makedirs(output_folder, exist_ok=True)
254
-
255
  output_filename = os.path.basename(video_path).rsplit('.', 1)[0] + '_heatmap.mp4'
256
  heatmap_video_path = os.path.join(output_folder, output_filename)
257
-
258
  print(f"Heatmap video will be saved at: {heatmap_video_path}")
259
 
260
  # Load the original video
261
  video = VideoFileClip(video_path)
262
-
263
  # Get video properties
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)
@@ -287,18 +321,18 @@ def create_video_with_heatmap(video_path, df, mse_embeddings, mse_posture, mse_v
287
 
288
  # Write the final video
289
  final_clip.write_videofile(heatmap_video_path, codec='libx264', audio_codec='aac', fps=video.fps)
290
-
291
  # Close the video clips
292
  video.close()
293
  final_clip.close()
294
-
295
  if os.path.exists(heatmap_video_path):
296
  print(f"Heatmap video created at: {heatmap_video_path}")
297
  print(f"Heatmap video size: {os.path.getsize(heatmap_video_path)} bytes")
298
  return heatmap_video_path
299
  else:
300
  print(f"Failed to create heatmap video at: {heatmap_video_path}")
301
- return None]
302
 
303
 
304
  # Function to create the correlation heatmap
 
249
 
250
  def create_video_with_heatmap(video_path, df, mse_embeddings, mse_posture, mse_voice, output_folder, desired_fps, largest_cluster):
251
  print(f"Creating heatmap video. Output folder: {output_folder}")
 
252
  os.makedirs(output_folder, exist_ok=True)
 
253
  output_filename = os.path.basename(video_path).rsplit('.', 1)[0] + '_heatmap.mp4'
254
  heatmap_video_path = os.path.join(output_folder, output_filename)
 
255
  print(f"Heatmap video will be saved at: {heatmap_video_path}")
256
 
257
  # Load the original video
258
  video = VideoFileClip(video_path)
259
+
260
  # Get video properties
261
  width, height = video.w, video.h
262
  total_frames = int(video.duration * video.fps)
263
+
264
+ def fill_with_previous_values(mse_array, total_frames):
265
  result = np.zeros(total_frames)
266
  indices = np.linspace(0, total_frames - 1, len(mse_array)).astype(int)
267
  result[indices] = mse_array
268
+ for i in range(1, total_frames):
269
+ if result[i] == 0:
270
+ result[i] = result[i-1]
271
  return result
272
 
273
+ # Fill gaps with previous values
274
+ mse_embeddings = fill_with_previous_values(mse_embeddings, total_frames)
275
+ mse_posture = fill_with_previous_values(mse_posture, total_frames)
276
+ mse_voice = fill_with_previous_values(mse_voice, total_frames)
277
+
278
+ # Find max values for each variable
279
+ max_embeddings = np.max(mse_embeddings)
280
+ max_posture = np.max(mse_posture)
281
+ max_voice = np.max(mse_voice)
282
+
283
+ def create_heatmap(t, mse_embeddings, mse_posture, mse_voice, fps, total_frames, width):
284
+ frame_index = int(t * fps)
285
+
286
+ # Create separate color maps for each variable
287
+ cmap_embeddings = plt.get_cmap('Reds')
288
+ cmap_posture = plt.get_cmap('Reds')
289
+ cmap_voice = plt.get_cmap('Reds')
290
+
291
+ # Normalize values within their own scales
292
+ norm_embeddings = mse_embeddings[frame_index] / max_embeddings
293
+ norm_posture = mse_posture[frame_index] / max_posture
294
+ norm_voice = mse_voice[frame_index] / max_voice
295
+
296
+ # Create color arrays for each variable
297
+ color_embeddings = cmap_embeddings(norm_embeddings)
298
+ color_posture = cmap_posture(norm_posture)
299
+ color_voice = cmap_voice(norm_voice)
300
+
301
+ # Create the heatmap frame
302
+ heatmap_height = 100 # Adjust as needed
303
+ heatmap_frame = np.zeros((heatmap_height, width, 4))
304
+
305
+ # Fill the heatmap sections
306
+ heatmap_frame[:heatmap_height//3, :] = color_embeddings
307
+ heatmap_frame[heatmap_height//3:2*heatmap_height//3, :] = color_posture
308
+ heatmap_frame[2*heatmap_height//3:, :] = color_voice
309
+
310
+ return (heatmap_frame * 255).astype(np.uint8)
311
+
312
  def combine_video_and_heatmap(t):
313
  video_frame = video.get_frame(t)
314
  heatmap_frame = create_heatmap(t, mse_embeddings, mse_posture, mse_voice, video.fps, total_frames, width)
 
321
 
322
  # Write the final video
323
  final_clip.write_videofile(heatmap_video_path, codec='libx264', audio_codec='aac', fps=video.fps)
324
+
325
  # Close the video clips
326
  video.close()
327
  final_clip.close()
328
+
329
  if os.path.exists(heatmap_video_path):
330
  print(f"Heatmap video created at: {heatmap_video_path}")
331
  print(f"Heatmap video size: {os.path.getsize(heatmap_video_path)} bytes")
332
  return heatmap_video_path
333
  else:
334
  print(f"Failed to create heatmap video at: {heatmap_video_path}")
335
+ return None
336
 
337
 
338
  # Function to create the correlation heatmap