reab5555 commited on
Commit
eac3780
·
verified ·
1 Parent(s): 09770fc

Update visualization.py

Browse files
Files changed (1) hide show
  1. visualization.py +15 -20
visualization.py CHANGED
@@ -208,7 +208,7 @@ def plot_posture(df, posture_scores, color='blue', anomaly_threshold=3):
208
  return fig
209
 
210
 
211
- def create_video_with_heatmap(video_path, df, mse_embeddings, mse_posture, mse_voice, output_path, desired_fps, largest_cluster):
212
  # Filter the DataFrame to only include frames from the largest cluster
213
  df_largest_cluster = df[df['Cluster'] == largest_cluster]
214
 
@@ -247,32 +247,30 @@ def create_video_with_heatmap(video_path, df, mse_embeddings, mse_posture, mse_v
247
  combined_mse[0] = mse_embeddings_norm
248
  combined_mse[1] = mse_posture_norm
249
  combined_mse[2] = mse_voice_norm
250
-
251
  # Custom colormap definition
252
  cdict = {
253
- 'red': [(0.0, 0.5, 0.5), # Low MSE: 50% red (gray)
254
- (1.0, 1.0, 1.0)], # High MSE: Full red
255
- 'green': [(0.0, 0.5, 0.5), # Low MSE: 50% green (gray)
256
- (1.0, 0.0, 0.0)], # High MSE: No green
257
- 'blue': [(0.0, 0.5, 0.5), # Low MSE: 50% blue (gray)
258
- (1.0, 0.0, 0.0)] # High MSE: No blue
259
  }
260
 
261
  custom_cmap = LinearSegmentedColormap('custom_cmap', segmentdata=cdict, N=256)
262
 
263
  fig, ax = plt.subplots(figsize=(width/100, 2))
264
- # Use the custom colormap in the heatmap generation
265
  im = ax.imshow(combined_mse, aspect='auto', cmap=custom_cmap, extent=[0, total_frames, 0, 3])
266
  ax.set_yticks([0.5, 1.5, 2.5])
267
  ax.set_yticklabels(['Face', 'Posture', 'Voice'])
268
  ax.set_xticks([])
269
  plt.tight_layout()
270
 
 
 
 
271
  line = None
272
-
273
- # Add progress tracking
274
- progress(0.9, desc="Generating video with heatmap")
275
-
276
  for frame_count in range(total_frames):
277
  cap.set(cv2.CAP_PROP_POS_FRAMES, frame_count)
278
  ret, frame = cap.read()
@@ -281,7 +279,7 @@ def create_video_with_heatmap(video_path, df, mse_embeddings, mse_posture, mse_v
281
 
282
  if line:
283
  line.remove()
284
- line = ax.axvline(x=frame_count, color='blue', linewidth=3)
285
 
286
  canvas = FigureCanvasAgg(fig)
287
  canvas.draw()
@@ -289,9 +287,6 @@ def create_video_with_heatmap(video_path, df, mse_embeddings, mse_posture, mse_v
289
  heatmap_img = heatmap_img.reshape(canvas.get_width_height()[::-1] + (3,))
290
  heatmap_img = cv2.resize(heatmap_img, (width, 200))
291
 
292
- # Convert heatmap_img from RGB to BGR
293
- heatmap_img = cv2.cvtColor(heatmap_img, cv2.COLOR_RGB2BGR)
294
-
295
  combined_frame = np.vstack((frame, heatmap_img))
296
 
297
  seconds = frame_count / original_fps
@@ -299,9 +294,9 @@ def create_video_with_heatmap(video_path, df, mse_embeddings, mse_posture, mse_v
299
  cv2.putText(combined_frame, f"Time: {timecode}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
300
 
301
  out.write(combined_frame)
302
-
303
- # Update progress
304
- progress(0.9 + (0.1 * (frame_count + 1) / total_frames), desc="Generating video with heatmap")
305
 
306
  cap.release()
307
  out.release()
 
208
  return fig
209
 
210
 
211
+ def create_video_with_heatmap(video_path, df, mse_embeddings, mse_posture, mse_voice, output_path, desired_fps, largest_cluster, progress=None):
212
  # Filter the DataFrame to only include frames from the largest cluster
213
  df_largest_cluster = df[df['Cluster'] == largest_cluster]
214
 
 
247
  combined_mse[0] = mse_embeddings_norm
248
  combined_mse[1] = mse_posture_norm
249
  combined_mse[2] = mse_voice_norm
250
+
251
  # Custom colormap definition
252
  cdict = {
253
+ 'red': [(0.0, 0.0, 0.0),
254
+ (1.0, 1.0, 1.0)],
255
+ 'green': [(0.0, 1.0, 1.0),
256
+ (1.0, 0.0, 0.0)],
257
+ 'blue': [(0.0, 1.0, 1.0),
258
+ (1.0, 0.0, 0.0)]
259
  }
260
 
261
  custom_cmap = LinearSegmentedColormap('custom_cmap', segmentdata=cdict, N=256)
262
 
263
  fig, ax = plt.subplots(figsize=(width/100, 2))
 
264
  im = ax.imshow(combined_mse, aspect='auto', cmap=custom_cmap, extent=[0, total_frames, 0, 3])
265
  ax.set_yticks([0.5, 1.5, 2.5])
266
  ax.set_yticklabels(['Face', 'Posture', 'Voice'])
267
  ax.set_xticks([])
268
  plt.tight_layout()
269
 
270
+ if progress:
271
+ progress(0, desc="Generating video with heatmap")
272
+
273
  line = None
 
 
 
 
274
  for frame_count in range(total_frames):
275
  cap.set(cv2.CAP_PROP_POS_FRAMES, frame_count)
276
  ret, frame = cap.read()
 
279
 
280
  if line:
281
  line.remove()
282
+ line = ax.axvline(x=frame_count, color='r', linewidth=2)
283
 
284
  canvas = FigureCanvasAgg(fig)
285
  canvas.draw()
 
287
  heatmap_img = heatmap_img.reshape(canvas.get_width_height()[::-1] + (3,))
288
  heatmap_img = cv2.resize(heatmap_img, (width, 200))
289
 
 
 
 
290
  combined_frame = np.vstack((frame, heatmap_img))
291
 
292
  seconds = frame_count / original_fps
 
294
  cv2.putText(combined_frame, f"Time: {timecode}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
295
 
296
  out.write(combined_frame)
297
+
298
+ if progress:
299
+ progress((frame_count + 1) / total_frames, desc="Generating video with heatmap")
300
 
301
  cap.release()
302
  out.release()