reab5555 commited on
Commit
bc2cdc0
·
verified ·
1 Parent(s): a0bb5ff

Update visualization.py

Browse files
Files changed (1) hide show
  1. visualization.py +33 -32
visualization.py CHANGED
@@ -271,39 +271,39 @@ def create_video_with_heatmap(video_path, df, mse_embeddings, mse_posture, mse_v
271
  ax.set_xticks([])
272
  plt.tight_layout()
273
 
274
- if progress:
275
- progress(0, desc="Generating video with heatmap")
276
-
277
  line = None
278
- for frame_count in range(total_frames):
279
- cap.set(cv2.CAP_PROP_POS_FRAMES, frame_count)
280
- ret, frame = cap.read()
281
- if not ret:
282
- break
283
-
284
- if line:
285
- line.remove()
286
- line = ax.axvline(x=frame_count, color='r', linewidth=2)
287
-
288
- canvas = FigureCanvasAgg(fig)
289
- canvas.draw()
290
- heatmap_img = np.frombuffer(canvas.tostring_rgb(), dtype='uint8')
291
- heatmap_img = heatmap_img.reshape(canvas.get_width_height()[::-1] + (3,))
292
- heatmap_img = cv2.resize(heatmap_img, (width, 200))
293
-
294
- combined_frame = np.vstack((frame, heatmap_img))
295
-
296
- seconds = frame_count / original_fps
297
- timecode = f"{int(seconds//3600):02d}:{int((seconds%3600)//60):02d}:{int(seconds%60):02d}"
298
- cv2.putText(combined_frame, f"Time: {timecode}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
299
-
300
- out.write(combined_frame)
301
- if progress is not None:
302
- try:
303
- progress((frame_count + 1) / total_frames, desc="Generating video with heatmap")
304
- except Exception as e:
305
- print(f"Error updating progress: {str(e)}")
306
-
 
 
 
307
  except Exception as e:
308
  print(f"Error in frame processing: {str(e)}")
309
  import traceback
@@ -315,6 +315,7 @@ def create_video_with_heatmap(video_path, df, mse_embeddings, mse_posture, mse_v
315
 
316
  if os.path.exists(heatmap_video_path):
317
  print(f"Heatmap video created at: {heatmap_video_path}")
 
318
  return heatmap_video_path
319
  else:
320
  print(f"Failed to create heatmap video at: {heatmap_video_path}")
 
271
  ax.set_xticks([])
272
  plt.tight_layout()
273
 
 
 
 
274
  line = None
275
+ try:
276
+ for frame_count in range(total_frames):
277
+ cap.set(cv2.CAP_PROP_POS_FRAMES, frame_count)
278
+ ret, frame = cap.read()
279
+ if not ret:
280
+ print(f"Failed to read frame {frame_count}")
281
+ break
282
+
283
+ if line:
284
+ line.remove()
285
+ line = ax.axvline(x=frame_count, color='r', linewidth=2)
286
+
287
+ canvas = FigureCanvasAgg(fig)
288
+ canvas.draw()
289
+ heatmap_img = np.frombuffer(canvas.tostring_rgb(), dtype='uint8')
290
+ heatmap_img = heatmap_img.reshape(canvas.get_width_height()[::-1] + (3,))
291
+ heatmap_img = cv2.resize(heatmap_img, (width, 200))
292
+
293
+ combined_frame = np.vstack((frame, heatmap_img))
294
+
295
+ seconds = frame_count / original_fps
296
+ timecode = f"{int(seconds//3600):02d}:{int((seconds%3600)//60):02d}:{int(seconds%60):02d}"
297
+ cv2.putText(combined_frame, f"Time: {timecode}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
298
+
299
+ out.write(combined_frame)
300
+
301
+ if progress is not None:
302
+ try:
303
+ progress((frame_count + 1) / total_frames, desc="Generating video with heatmap")
304
+ except Exception as e:
305
+ print(f"Error updating progress: {str(e)}")
306
+
307
  except Exception as e:
308
  print(f"Error in frame processing: {str(e)}")
309
  import traceback
 
315
 
316
  if os.path.exists(heatmap_video_path):
317
  print(f"Heatmap video created at: {heatmap_video_path}")
318
+ print(f"Heatmap video size: {os.path.getsize(heatmap_video_path)} bytes")
319
  return heatmap_video_path
320
  else:
321
  print(f"Failed to create heatmap video at: {heatmap_video_path}")