Update visualization.py
Browse files- visualization.py +43 -1
visualization.py
CHANGED
@@ -9,6 +9,7 @@ import pandas as pd
|
|
9 |
import cv2
|
10 |
from moviepy.editor import VideoFileClip, AudioFileClip, CompositeVideoClip, ImageClip, VideoClip, concatenate_videoclips
|
11 |
from moviepy.video.fx.all import resize
|
|
|
12 |
from PIL import Image, ImageDraw, ImageFont
|
13 |
from matplotlib.patches import Rectangle
|
14 |
from utils import seconds_to_timecode
|
@@ -267,4 +268,45 @@ def plot_stacked_mse_heatmaps(mse_face, mse_posture, mse_voice, df, title="Combi
|
|
267 |
plt.suptitle(title)
|
268 |
plt.tight_layout()
|
269 |
plt.close()
|
270 |
-
return fig
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
import cv2
|
10 |
from moviepy.editor import VideoFileClip, AudioFileClip, CompositeVideoClip, ImageClip, VideoClip, concatenate_videoclips
|
11 |
from moviepy.video.fx.all import resize
|
12 |
+
from moviepy.video.io.bindings import mplfig_to_npimage
|
13 |
from PIL import Image, ImageDraw, ImageFont
|
14 |
from matplotlib.patches import Rectangle
|
15 |
from utils import seconds_to_timecode
|
|
|
268 |
plt.suptitle(title)
|
269 |
plt.tight_layout()
|
270 |
plt.close()
|
271 |
+
return fig
|
272 |
+
|
273 |
+
|
274 |
+
def create_video_with_heatmap(video_path, mse_face, mse_posture, mse_voice, df, output_path):
|
275 |
+
# Load the original video
|
276 |
+
video = VideoFileClip(video_path)
|
277 |
+
|
278 |
+
# Create the stacked heatmap
|
279 |
+
fig = plot_stacked_mse_heatmaps(mse_face, mse_posture, mse_voice, df)
|
280 |
+
heatmap_img = mplfig_to_npimage(fig)
|
281 |
+
|
282 |
+
# Resize heatmap to match video width
|
283 |
+
heatmap_height = int(video.h * 0.2) # 20% of video height
|
284 |
+
heatmap_resized = cv2.resize(heatmap_img, (video.w, heatmap_height))
|
285 |
+
|
286 |
+
def make_frame(t):
|
287 |
+
# Get the current frame from the original video
|
288 |
+
frame = video.get_frame(t)
|
289 |
+
|
290 |
+
# Calculate the position of the vertical line
|
291 |
+
line_pos = int(t / video.duration * video.w)
|
292 |
+
|
293 |
+
# Add the vertical line to the heatmap
|
294 |
+
heatmap_with_line = heatmap_resized.copy()
|
295 |
+
cv2.line(heatmap_with_line, (line_pos, 0), (line_pos, heatmap_height), (0, 0, 0), 2)
|
296 |
+
|
297 |
+
# Combine the original frame with the heatmap
|
298 |
+
combined_frame = np.vstack((frame, heatmap_with_line))
|
299 |
+
|
300 |
+
return combined_frame
|
301 |
+
|
302 |
+
# Create a new video clip with the combined frames
|
303 |
+
final_clip = VideoClip(make_frame, duration=video.duration)
|
304 |
+
|
305 |
+
# Write the final video
|
306 |
+
final_clip.write_videofile(output_path, codec='libx264', fps=video.fps)
|
307 |
+
|
308 |
+
# Close the clips
|
309 |
+
video.close()
|
310 |
+
final_clip.close()
|
311 |
+
|
312 |
+
return output_path
|