Spaces:
Runtime error
Runtime error
Update visualization.py
Browse files- visualization.py +44 -9
visualization.py
CHANGED
|
@@ -6,6 +6,7 @@ import seaborn as sns
|
|
| 6 |
import numpy as np
|
| 7 |
import pandas as pd
|
| 8 |
import cv2
|
|
|
|
| 9 |
from matplotlib.patches import Rectangle
|
| 10 |
from utils import seconds_to_timecode
|
| 11 |
from anomaly_detection import determine_anomalies
|
|
@@ -216,11 +217,10 @@ def plot_posture(df, posture_scores, color='blue', anomaly_threshold=3):
|
|
| 216 |
def create_video_with_heatmap(video_path, df, mse_embeddings, mse_posture, mse_voice, output_folder, desired_fps, largest_cluster, progress=None):
|
| 217 |
print(f"Creating heatmap video. Output folder: {output_folder}")
|
| 218 |
|
| 219 |
-
# Create output folder if it doesn't exist
|
| 220 |
os.makedirs(output_folder, exist_ok=True)
|
| 221 |
|
| 222 |
-
# Define output path
|
| 223 |
output_filename = os.path.basename(video_path).rsplit('.', 1)[0] + '_heatmap.mp4'
|
|
|
|
| 224 |
heatmap_video_path = os.path.join(output_folder, output_filename)
|
| 225 |
|
| 226 |
print(f"Heatmap video will be saved at: {heatmap_video_path}")
|
|
@@ -231,8 +231,8 @@ def create_video_with_heatmap(video_path, df, mse_embeddings, mse_posture, mse_v
|
|
| 231 |
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 232 |
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 233 |
|
| 234 |
-
fourcc = cv2.VideoWriter_fourcc(*'
|
| 235 |
-
out = cv2.VideoWriter(
|
| 236 |
print(f"VideoWriter initialized. FPS: {original_fps}, Size: {(width, height + 200)}")
|
| 237 |
|
| 238 |
# Ensure all MSE arrays have the same length as total_frames
|
|
@@ -252,7 +252,6 @@ def create_video_with_heatmap(video_path, df, mse_embeddings, mse_posture, mse_v
|
|
| 252 |
combined_mse[1] = mse_posture_norm
|
| 253 |
combined_mse[2] = mse_voice_norm
|
| 254 |
|
| 255 |
-
# Custom colormap definition
|
| 256 |
cdict = {
|
| 257 |
'red': [(0.0, 0.0, 0.0),
|
| 258 |
(1.0, 1.0, 1.0)],
|
|
@@ -274,7 +273,6 @@ def create_video_with_heatmap(video_path, df, mse_embeddings, mse_posture, mse_v
|
|
| 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}")
|
|
@@ -290,13 +288,23 @@ def create_video_with_heatmap(video_path, df, mse_embeddings, mse_posture, mse_v
|
|
| 290 |
heatmap_img = heatmap_img.reshape(canvas.get_width_height()[::-1] + (3,))
|
| 291 |
heatmap_img = cv2.resize(heatmap_img, (width, 200))
|
| 292 |
|
| 293 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 300 |
|
| 301 |
if progress is not None:
|
| 302 |
try:
|
|
@@ -312,6 +320,33 @@ def create_video_with_heatmap(video_path, df, mse_embeddings, mse_posture, mse_v
|
|
| 312 |
cap.release()
|
| 313 |
out.release()
|
| 314 |
plt.close(fig)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 315 |
|
| 316 |
if os.path.exists(heatmap_video_path):
|
| 317 |
print(f"Heatmap video created at: {heatmap_video_path}")
|
|
|
|
| 6 |
import numpy as np
|
| 7 |
import pandas as pd
|
| 8 |
import cv2
|
| 9 |
+
from moviepy.editor import VideoFileClip, AudioFileClip, CompositeVideoClip
|
| 10 |
from matplotlib.patches import Rectangle
|
| 11 |
from utils import seconds_to_timecode
|
| 12 |
from anomaly_detection import determine_anomalies
|
|
|
|
| 217 |
def create_video_with_heatmap(video_path, df, mse_embeddings, mse_posture, mse_voice, output_folder, desired_fps, largest_cluster, progress=None):
|
| 218 |
print(f"Creating heatmap video. Output folder: {output_folder}")
|
| 219 |
|
|
|
|
| 220 |
os.makedirs(output_folder, exist_ok=True)
|
| 221 |
|
|
|
|
| 222 |
output_filename = os.path.basename(video_path).rsplit('.', 1)[0] + '_heatmap.mp4'
|
| 223 |
+
temp_video_path = os.path.join(output_folder, 'temp_' + output_filename)
|
| 224 |
heatmap_video_path = os.path.join(output_folder, output_filename)
|
| 225 |
|
| 226 |
print(f"Heatmap video will be saved at: {heatmap_video_path}")
|
|
|
|
| 231 |
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 232 |
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 233 |
|
| 234 |
+
fourcc = cv2.VideoWriter_fourcc(*'avc1')
|
| 235 |
+
out = cv2.VideoWriter(temp_video_path, fourcc, original_fps, (width, height + 200))
|
| 236 |
print(f"VideoWriter initialized. FPS: {original_fps}, Size: {(width, height + 200)}")
|
| 237 |
|
| 238 |
# Ensure all MSE arrays have the same length as total_frames
|
|
|
|
| 252 |
combined_mse[1] = mse_posture_norm
|
| 253 |
combined_mse[2] = mse_voice_norm
|
| 254 |
|
|
|
|
| 255 |
cdict = {
|
| 256 |
'red': [(0.0, 0.0, 0.0),
|
| 257 |
(1.0, 1.0, 1.0)],
|
|
|
|
| 273 |
line = None
|
| 274 |
try:
|
| 275 |
for frame_count in range(total_frames):
|
|
|
|
| 276 |
ret, frame = cap.read()
|
| 277 |
if not ret:
|
| 278 |
print(f"Failed to read frame {frame_count}")
|
|
|
|
| 288 |
heatmap_img = heatmap_img.reshape(canvas.get_width_height()[::-1] + (3,))
|
| 289 |
heatmap_img = cv2.resize(heatmap_img, (width, 200))
|
| 290 |
|
| 291 |
+
# Convert frame from BGR to RGB
|
| 292 |
+
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 293 |
+
|
| 294 |
+
# Combine RGB frame with RGB heatmap
|
| 295 |
+
combined_frame = np.vstack((frame_rgb, heatmap_img))
|
| 296 |
|
| 297 |
seconds = frame_count / original_fps
|
| 298 |
timecode = f"{int(seconds//3600):02d}:{int((seconds%3600)//60):02d}:{int(seconds%60):02d}"
|
|
|
|
| 299 |
|
| 300 |
+
# Add timecode to the combined frame (which is now in RGB)
|
| 301 |
+
combined_frame = cv2.putText(combined_frame, f"Time: {timecode}", (10, 30),
|
| 302 |
+
cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
|
| 303 |
+
|
| 304 |
+
# Convert back to BGR for OpenCV VideoWriter
|
| 305 |
+
combined_frame_bgr = cv2.cvtColor(combined_frame, cv2.COLOR_RGB2BGR)
|
| 306 |
+
|
| 307 |
+
out.write(combined_frame_bgr)
|
| 308 |
|
| 309 |
if progress is not None:
|
| 310 |
try:
|
|
|
|
| 320 |
cap.release()
|
| 321 |
out.release()
|
| 322 |
plt.close(fig)
|
| 323 |
+
|
| 324 |
+
# Add audio to the video
|
| 325 |
+
try:
|
| 326 |
+
original_video = VideoFileClip(video_path)
|
| 327 |
+
heatmap_video = VideoFileClip(temp_video_path)
|
| 328 |
+
|
| 329 |
+
# Resize the heatmap video to match the original video's duration
|
| 330 |
+
heatmap_video = heatmap_video.set_duration(original_video.duration)
|
| 331 |
+
|
| 332 |
+
# Add the audio from the original video
|
| 333 |
+
final_video = heatmap_video.set_audio(original_video.audio)
|
| 334 |
+
|
| 335 |
+
# Write the final video
|
| 336 |
+
final_video.write_videofile(heatmap_video_path, codec='libx264', audio_codec='aac')
|
| 337 |
+
|
| 338 |
+
# Close the video clips
|
| 339 |
+
original_video.close()
|
| 340 |
+
heatmap_video.close()
|
| 341 |
+
final_video.close()
|
| 342 |
+
|
| 343 |
+
# Remove the temporary video file
|
| 344 |
+
os.remove(temp_video_path)
|
| 345 |
+
|
| 346 |
+
except Exception as e:
|
| 347 |
+
print(f"Error in adding audio to video: {str(e)}")
|
| 348 |
+
import traceback
|
| 349 |
+
traceback.print_exc()
|
| 350 |
|
| 351 |
if os.path.exists(heatmap_video_path):
|
| 352 |
print(f"Heatmap video created at: {heatmap_video_path}")
|