reab5555 commited on
Commit
ee488cb
·
verified ·
1 Parent(s): 8778ec7

Update visualization.py

Browse files
Files changed (1) hide show
  1. visualization.py +66 -25
visualization.py CHANGED
@@ -217,6 +217,72 @@ def plot_posture(df, posture_scores, color='blue', anomaly_threshold=3):
217
  plt.close()
218
  return fig
219
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
220
  def create_heatmap(t, mse_embeddings, mse_posture, mse_voice, video_fps, total_frames, video_width):
221
  frame_count = int(t * video_fps)
222
 
@@ -246,31 +312,6 @@ def create_heatmap(t, mse_embeddings, mse_posture, mse_voice, video_fps, total_f
246
  heatmap_img = heatmap_img.reshape(canvas.get_width_height()[::-1] + (3,))
247
  plt.close(fig)
248
  return heatmap_img
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
- # Ensure all MSE arrays have the same length as total_frames
268
- mse_embeddings = np.interp(np.linspace(0, len(mse_embeddings) - 1, total_frames),
269
- np.arange(len(mse_embeddings)), mse_embeddings)
270
- mse_posture = np.interp(np.linspace(0, len(mse_posture) - 1, total_frames),
271
- np.arange(len(mse_posture)), mse_posture)
272
- mse_voice = np.interp(np.linspace(0, len(mse_voice) - 1, total_frames),
273
- np.arange(len(mse_voice)), mse_voice)
274
 
275
  def combine_video_and_heatmap(t):
276
  video_frame = video.get_frame(t)
 
217
  plt.close()
218
  return fig
219
 
220
+
221
+ def filter_mse_for_most_frequent_person(df, mse_embeddings, mse_posture, mse_voice, most_frequent_person_frames):
222
+ # Create a mask for the most frequent person frames
223
+ mask = df['Frame'].isin(most_frequent_person_frames)
224
+
225
+ # Apply the mask to filter the MSE arrays
226
+ mse_embeddings_filtered = np.where(mask, mse_embeddings, 0)
227
+ mse_posture_filtered = np.where(mask, mse_posture, 0)
228
+ mse_voice_filtered = np.where(mask, mse_voice, 0)
229
+
230
+ return mse_embeddings_filtered, mse_posture_filtered, mse_voice_filtered
231
+
232
+ def create_video_with_heatmap(video_path, df, mse_embeddings, mse_posture, mse_voice, output_folder, desired_fps, most_frequent_person_frames):
233
+ print(f"Creating heatmap video. Output folder: {output_folder}")
234
+
235
+ os.makedirs(output_folder, exist_ok=True)
236
+
237
+ output_filename = os.path.basename(video_path).rsplit('.', 1)[0] + '_heatmap.mp4'
238
+ heatmap_video_path = os.path.join(output_folder, output_filename)
239
+
240
+ print(f"Heatmap video will be saved at: {heatmap_video_path}")
241
+
242
+ # Load the original video
243
+ video = VideoFileClip(video_path)
244
+
245
+ # Get video properties
246
+ width, height = video.w, video.h
247
+ total_frames = int(video.duration * video.fps)
248
+
249
+ # Ensure all MSE arrays have the same length as total_frames
250
+ mse_embeddings = np.interp(np.linspace(0, len(mse_embeddings) - 1, total_frames),
251
+ np.arange(len(mse_embeddings)), mse_embeddings)
252
+ mse_posture = np.interp(np.linspace(0, len(mse_posture) - 1, total_frames),
253
+ np.arange(len(mse_posture)), mse_posture)
254
+ mse_voice = np.interp(np.linspace(0, len(mse_voice) - 1, total_frames),
255
+ np.arange(len(mse_voice)), mse_voice)
256
+
257
+ # Filter MSE arrays for the most frequent person frames
258
+ mse_embeddings_filtered, mse_posture_filtered, mse_voice_filtered = filter_mse_for_most_frequent_person(df, mse_embeddings, mse_posture, mse_voice, most_frequent_person_frames)
259
+
260
+ def combine_video_and_heatmap(t):
261
+ video_frame = video.get_frame(t)
262
+ heatmap_frame = create_heatmap(t, mse_embeddings_filtered, mse_posture_filtered, mse_voice_filtered, video.fps, total_frames, width)
263
+ heatmap_frame_resized = cv2.resize(heatmap_frame, (width, heatmap_frame.shape[0]))
264
+ combined_frame = np.vstack((video_frame, heatmap_frame_resized))
265
+ return combined_frame
266
+
267
+ final_clip = VideoClip(combine_video_and_heatmap, duration=video.duration)
268
+ final_clip = final_clip.set_audio(video.audio)
269
+
270
+ # Write the final video
271
+ final_clip.write_videofile(heatmap_video_path, codec='libx264', audio_codec='aac', fps=video.fps)
272
+
273
+ # Close the video clips
274
+ video.close()
275
+ final_clip.close()
276
+
277
+ if os.path.exists(heatmap_video_path):
278
+ print(f"Heatmap video created at: {heatmap_video_path}")
279
+ print(f"Heatmap video size: {os.path.getsize(heatmap_video_path)} bytes")
280
+ return heatmap_video_path
281
+ else:
282
+ print(f"Failed to create heatmap video at: {heatmap_video_path}")
283
+ return None
284
+
285
+ # Define the create_heatmap function
286
  def create_heatmap(t, mse_embeddings, mse_posture, mse_voice, video_fps, total_frames, video_width):
287
  frame_count = int(t * video_fps)
288
 
 
312
  heatmap_img = heatmap_img.reshape(canvas.get_width_height()[::-1] + (3,))
313
  plt.close(fig)
314
  return heatmap_img
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
315
 
316
  def combine_video_and_heatmap(t):
317
  video_frame = video.get_frame(t)