Update visualization.py
Browse files- visualization.py +11 -36
visualization.py
CHANGED
@@ -264,50 +264,25 @@ def create_video_with_heatmap(video_path, df, mse_embeddings, mse_posture, mse_v
|
|
264 |
width, height = video.w, video.h
|
265 |
total_frames = int(video.duration * video.fps)
|
266 |
|
267 |
-
def
|
268 |
result = np.zeros(total_frames)
|
269 |
indices = np.linspace(0, total_frames - 1, len(mse_array)).astype(int)
|
270 |
result[indices] = mse_array
|
271 |
-
for i in range(1, total_frames):
|
272 |
-
if result[i] == 0:
|
273 |
-
result[i] = result[i - 1]
|
274 |
return result
|
275 |
|
276 |
# Ensure all MSE arrays have the same length as total_frames
|
277 |
-
mse_embeddings =
|
278 |
-
mse_posture =
|
279 |
-
mse_voice =
|
280 |
|
281 |
-
def
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
max_mse_face = np.max(mse_embeddings)
|
288 |
-
max_mse_body = np.max(mse_posture)
|
289 |
-
max_mse_audio = np.max(mse_voice)
|
290 |
-
|
291 |
-
def get_heatmap_color(mse_value, max_mse_value):
|
292 |
-
normalized_value = mse_value / max_mse_value if max_mse_value != 0 else 0
|
293 |
-
color = plt.cm.hot(normalized_value)
|
294 |
-
return (color[0] * 255, color[1] * 255, color[2] * 255) # Convert to RGB
|
295 |
-
|
296 |
-
heatmap_face = np.full((height, width, 3), get_heatmap_color(mse_face, max_mse_face), dtype=np.uint8)
|
297 |
-
heatmap_body = np.full((height, width, 3), get_heatmap_color(mse_body, max_mse_body), dtype=np.uint8)
|
298 |
-
heatmap_audio = np.full((height, width, 3), get_heatmap_color(mse_audio, max_mse_audio), dtype=np.uint8)
|
299 |
-
|
300 |
-
# Combine heatmaps
|
301 |
-
combined_heatmap = (heatmap_face + heatmap_body + heatmap_audio) / 3
|
302 |
-
return combined_heatmap.astype(np.uint8)
|
303 |
-
|
304 |
-
def combine_video_and_heatmap(get_frame, t):
|
305 |
-
video_frame = get_frame(t)
|
306 |
-
heatmap_frame = create_heatmap(t, mse_embeddings, mse_posture, mse_voice, desired_fps, total_frames, width, height)
|
307 |
-
combined_frame = cv2.addWeighted(video_frame, 0.7, heatmap_frame, 0.3, 0)
|
308 |
return combined_frame
|
309 |
|
310 |
-
final_clip = video.
|
311 |
final_clip = final_clip.set_audio(video.audio)
|
312 |
|
313 |
# Write the final video
|
@@ -323,7 +298,7 @@ def create_video_with_heatmap(video_path, df, mse_embeddings, mse_posture, mse_v
|
|
323 |
return heatmap_video_path
|
324 |
else:
|
325 |
print(f"Failed to create heatmap video at: {heatmap_video_path}")
|
326 |
-
return None
|
327 |
|
328 |
|
329 |
# Function to create the correlation heatmap
|
|
|
264 |
width, height = video.w, video.h
|
265 |
total_frames = int(video.duration * video.fps)
|
266 |
|
267 |
+
def fill_with_zeros_and_values(mse_array, total_frames):
|
268 |
result = np.zeros(total_frames)
|
269 |
indices = np.linspace(0, total_frames - 1, len(mse_array)).astype(int)
|
270 |
result[indices] = mse_array
|
|
|
|
|
|
|
271 |
return result
|
272 |
|
273 |
# Ensure all MSE arrays have the same length as total_frames
|
274 |
+
mse_embeddings = fill_with_zeros_and_values(mse_embeddings, total_frames)
|
275 |
+
mse_posture = fill_with_zeros_and_values(mse_posture, total_frames)
|
276 |
+
mse_voice = fill_with_zeros_and_values(mse_voice, total_frames)
|
277 |
|
278 |
+
def combine_video_and_heatmap(t):
|
279 |
+
video_frame = video.get_frame(t)
|
280 |
+
heatmap_frame = create_heatmap(t, mse_embeddings, mse_posture, mse_voice, video.fps, total_frames, width)
|
281 |
+
heatmap_frame_resized = cv2.resize(heatmap_frame, (width, heatmap_frame.shape[0]))
|
282 |
+
combined_frame = np.vstack((video_frame, heatmap_frame_resized))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
283 |
return combined_frame
|
284 |
|
285 |
+
final_clip = VideoClip(combine_video_and_heatmap, duration=video.duration)
|
286 |
final_clip = final_clip.set_audio(video.audio)
|
287 |
|
288 |
# Write the final video
|
|
|
298 |
return heatmap_video_path
|
299 |
else:
|
300 |
print(f"Failed to create heatmap video at: {heatmap_video_path}")
|
301 |
+
return None]
|
302 |
|
303 |
|
304 |
# Function to create the correlation heatmap
|