Update visualization.py
Browse files- visualization.py +9 -7
visualization.py
CHANGED
@@ -204,7 +204,12 @@ def plot_posture(df, posture_scores, color='blue', anomaly_threshold=3):
|
|
204 |
return fig
|
205 |
|
206 |
|
207 |
-
def create_video_with_heatmap(video_path, df, mse_embeddings, mse_posture, output_path, desired_fps):
|
|
|
|
|
|
|
|
|
|
|
208 |
cap = cv2.VideoCapture(video_path)
|
209 |
original_fps = cap.get(cv2.CAP_PROP_FPS)
|
210 |
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
@@ -214,24 +219,20 @@ def create_video_with_heatmap(video_path, df, mse_embeddings, mse_posture, outpu
|
|
214 |
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
215 |
out = cv2.VideoWriter(output_path, fourcc, desired_fps, (width, height + 200))
|
216 |
|
217 |
-
# Create custom colormap
|
218 |
cmap = mcolors.LinearSegmentedColormap.from_list("custom",
|
219 |
[(1, 1, 1), (0, 0, 1), (0.5, 0, 0.5)], N=256)
|
220 |
|
221 |
-
# Ensure heatmap data covers all frames
|
222 |
mse_embeddings = np.interp(np.linspace(0, len(mse_embeddings) - 1, total_frames),
|
223 |
np.arange(len(mse_embeddings)), mse_embeddings)
|
224 |
mse_posture = np.interp(np.linspace(0, len(mse_posture) - 1, total_frames),
|
225 |
np.arange(len(mse_posture)), mse_posture)
|
226 |
|
227 |
-
# Normalize MSE values
|
228 |
mse_embeddings_norm = (mse_embeddings - np.min(mse_embeddings)) / (np.max(mse_embeddings) - np.min(mse_embeddings))
|
229 |
mse_posture_norm = (mse_posture - np.min(mse_posture)) / (np.max(mse_posture) - np.min(mse_posture))
|
230 |
|
231 |
-
# Combine MSEs
|
232 |
combined_mse = np.zeros((2, total_frames, 3))
|
233 |
-
combined_mse[0] = np.array([1 - mse_embeddings_norm, 1 - mse_embeddings_norm, mse_embeddings_norm]).T
|
234 |
-
combined_mse[1] = np.array([1 - mse_posture_norm, mse_posture_norm, 1 - mse_posture_norm]).T
|
235 |
|
236 |
fig, ax = plt.subplots(figsize=(width/100, 2))
|
237 |
im = ax.imshow(combined_mse, aspect='auto', extent=[0, total_frames, 0, 2])
|
@@ -272,3 +273,4 @@ def create_video_with_heatmap(video_path, df, mse_embeddings, mse_posture, outpu
|
|
272 |
plt.close(fig)
|
273 |
|
274 |
return output_path
|
|
|
|
204 |
return fig
|
205 |
|
206 |
|
207 |
+
def create_video_with_heatmap(video_path, df, mse_embeddings, mse_posture, output_path, desired_fps, largest_cluster):
|
208 |
+
# Filter the DataFrame to only include frames from the largest cluster
|
209 |
+
df_largest_cluster = df[df['Cluster'] == largest_cluster]
|
210 |
+
mse_embeddings = mse_embeddings[df['Cluster'] == largest_cluster]
|
211 |
+
mse_posture = mse_posture[df['Cluster'] == largest_cluster]
|
212 |
+
|
213 |
cap = cv2.VideoCapture(video_path)
|
214 |
original_fps = cap.get(cv2.CAP_PROP_FPS)
|
215 |
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
|
|
219 |
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
220 |
out = cv2.VideoWriter(output_path, fourcc, desired_fps, (width, height + 200))
|
221 |
|
|
|
222 |
cmap = mcolors.LinearSegmentedColormap.from_list("custom",
|
223 |
[(1, 1, 1), (0, 0, 1), (0.5, 0, 0.5)], N=256)
|
224 |
|
|
|
225 |
mse_embeddings = np.interp(np.linspace(0, len(mse_embeddings) - 1, total_frames),
|
226 |
np.arange(len(mse_embeddings)), mse_embeddings)
|
227 |
mse_posture = np.interp(np.linspace(0, len(mse_posture) - 1, total_frames),
|
228 |
np.arange(len(mse_posture)), mse_posture)
|
229 |
|
|
|
230 |
mse_embeddings_norm = (mse_embeddings - np.min(mse_embeddings)) / (np.max(mse_embeddings) - np.min(mse_embeddings))
|
231 |
mse_posture_norm = (mse_posture - np.min(mse_posture)) / (np.max(mse_posture) - np.min(mse_posture))
|
232 |
|
|
|
233 |
combined_mse = np.zeros((2, total_frames, 3))
|
234 |
+
combined_mse[0] = np.array([1 - mse_embeddings_norm, 1 - mse_embeddings_norm, mse_embeddings_norm]).T
|
235 |
+
combined_mse[1] = np.array([1 - mse_posture_norm, mse_posture_norm, 1 - mse_posture_norm]).T
|
236 |
|
237 |
fig, ax = plt.subplots(figsize=(width/100, 2))
|
238 |
im = ax.imshow(combined_mse, aspect='auto', extent=[0, total_frames, 0, 2])
|
|
|
273 |
plt.close(fig)
|
274 |
|
275 |
return output_path
|
276 |
+
|