Spaces:
Runtime error
Runtime error
Update visualization.py
Browse files- visualization.py +16 -18
visualization.py
CHANGED
|
@@ -212,20 +212,17 @@ def plot_posture(df, posture_scores, color='blue', anomaly_threshold=3):
|
|
| 212 |
return fig
|
| 213 |
|
| 214 |
|
| 215 |
-
def create_video_with_heatmap(video_path, df, mse_embeddings, mse_posture, mse_voice,
|
| 216 |
-
print(f"Creating heatmap video. Output
|
| 217 |
-
# Filter the DataFrame to only include frames from the largest cluster
|
| 218 |
-
df_largest_cluster = df[df['Cluster'] == largest_cluster]
|
| 219 |
|
| 220 |
-
#
|
| 221 |
-
|
| 222 |
-
x_new = np.linspace(0, len(mse_voice)-1, len(df))
|
| 223 |
-
f = interpolate.interp1d(x_voice, mse_voice)
|
| 224 |
-
mse_voice_interpolated = f(x_new)
|
| 225 |
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
|
|
|
|
|
|
| 229 |
|
| 230 |
cap = cv2.VideoCapture(video_path)
|
| 231 |
original_fps = cap.get(cv2.CAP_PROP_FPS)
|
|
@@ -234,7 +231,7 @@ def create_video_with_heatmap(video_path, df, mse_embeddings, mse_posture, mse_v
|
|
| 234 |
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 235 |
|
| 236 |
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 237 |
-
out = cv2.VideoWriter(
|
| 238 |
print(f"VideoWriter initialized. FPS: {original_fps}, Size: {(width, height + 200)}")
|
| 239 |
|
| 240 |
# Ensure all MSE arrays have the same length as total_frames
|
|
@@ -242,12 +239,12 @@ def create_video_with_heatmap(video_path, df, mse_embeddings, mse_posture, mse_v
|
|
| 242 |
np.arange(len(mse_embeddings)), mse_embeddings)
|
| 243 |
mse_posture = np.interp(np.linspace(0, len(mse_posture) - 1, total_frames),
|
| 244 |
np.arange(len(mse_posture)), mse_posture)
|
| 245 |
-
|
| 246 |
-
|
| 247 |
|
| 248 |
mse_embeddings_norm = (mse_embeddings - np.min(mse_embeddings)) / (np.max(mse_embeddings) - np.min(mse_embeddings))
|
| 249 |
mse_posture_norm = (mse_posture - np.min(mse_posture)) / (np.max(mse_posture) - np.min(mse_posture))
|
| 250 |
-
mse_voice_norm = (
|
| 251 |
|
| 252 |
combined_mse = np.zeros((3, total_frames))
|
| 253 |
combined_mse[0] = mse_embeddings_norm
|
|
@@ -307,5 +304,6 @@ def create_video_with_heatmap(video_path, df, mse_embeddings, mse_posture, mse_v
|
|
| 307 |
cap.release()
|
| 308 |
out.release()
|
| 309 |
plt.close(fig)
|
| 310 |
-
|
| 311 |
-
|
|
|
|
|
|
| 212 |
return fig
|
| 213 |
|
| 214 |
|
| 215 |
+
def create_video_with_heatmap(video_path, df, mse_embeddings, mse_posture, mse_voice, output_folder, desired_fps, largest_cluster, progress=None):
|
| 216 |
+
print(f"Creating heatmap video. Output folder: {output_folder}")
|
|
|
|
|
|
|
| 217 |
|
| 218 |
+
# Create output folder if it doesn't exist
|
| 219 |
+
os.makedirs(output_folder, exist_ok=True)
|
|
|
|
|
|
|
|
|
|
| 220 |
|
| 221 |
+
# Define output path
|
| 222 |
+
output_filename = os.path.basename(video_path).rsplit('.', 1)[0] + '_heatmap.mp4'
|
| 223 |
+
heatmap_video_path = os.path.join(output_folder, output_filename)
|
| 224 |
+
|
| 225 |
+
print(f"Heatmap video will be saved at: {heatmap_video_path}")
|
| 226 |
|
| 227 |
cap = cv2.VideoCapture(video_path)
|
| 228 |
original_fps = cap.get(cv2.CAP_PROP_FPS)
|
|
|
|
| 231 |
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 232 |
|
| 233 |
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 234 |
+
out = cv2.VideoWriter(heatmap_video_path, fourcc, original_fps, (width, height + 200))
|
| 235 |
print(f"VideoWriter initialized. FPS: {original_fps}, Size: {(width, height + 200)}")
|
| 236 |
|
| 237 |
# Ensure all MSE arrays have the same length as total_frames
|
|
|
|
| 239 |
np.arange(len(mse_embeddings)), mse_embeddings)
|
| 240 |
mse_posture = np.interp(np.linspace(0, len(mse_posture) - 1, total_frames),
|
| 241 |
np.arange(len(mse_posture)), mse_posture)
|
| 242 |
+
mse_voice = np.interp(np.linspace(0, len(mse_voice) - 1, total_frames),
|
| 243 |
+
np.arange(len(mse_voice)), mse_voice)
|
| 244 |
|
| 245 |
mse_embeddings_norm = (mse_embeddings - np.min(mse_embeddings)) / (np.max(mse_embeddings) - np.min(mse_embeddings))
|
| 246 |
mse_posture_norm = (mse_posture - np.min(mse_posture)) / (np.max(mse_posture) - np.min(mse_posture))
|
| 247 |
+
mse_voice_norm = (mse_voice - np.min(mse_voice)) / (np.max(mse_voice) - np.min(mse_voice))
|
| 248 |
|
| 249 |
combined_mse = np.zeros((3, total_frames))
|
| 250 |
combined_mse[0] = mse_embeddings_norm
|
|
|
|
| 304 |
cap.release()
|
| 305 |
out.release()
|
| 306 |
plt.close(fig)
|
| 307 |
+
|
| 308 |
+
print(f"Heatmap video created at: {heatmap_video_path}")
|
| 309 |
+
return heatmap_video_path
|