reab5555 commited on
Commit
f0f1a1c
·
verified ·
1 Parent(s): edbec6e

Update visualization.py

Browse files
Files changed (1) hide show
  1. visualization.py +25 -15
visualization.py CHANGED
@@ -204,6 +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)
@@ -215,27 +221,32 @@ def create_video_with_heatmap(video_path, df, mse_embeddings, mse_posture, outpu
215
  out = cv2.VideoWriter(output_path, fourcc, desired_fps, (width, height + 200))
216
 
217
  # Create custom colormap
218
- colors = ['navy', 'white', 'purple'] # Changed order: blue for facial, purple for posture
219
- n_bins = 100
220
- cmap = mcolors.LinearSegmentedColormap.from_list('custom', colors, N=n_bins)
221
 
222
- # Ensure heatmap data covers all frames and normalize
223
- mse_embeddings = np.pad(mse_embeddings, (0, max(0, total_frames - len(mse_embeddings))), 'constant', constant_values=np.nan)
224
- mse_posture = np.pad(mse_posture, (0, max(0, total_frames - len(mse_posture))), 'constant', constant_values=np.nan)
 
 
225
 
226
- mse_embeddings_norm = (mse_embeddings - np.nanmin(mse_embeddings)) / (np.nanmax(mse_embeddings) - np.nanmin(mse_embeddings))
227
- mse_posture_norm = (mse_posture - np.nanmin(mse_posture)) / (np.nanmax(mse_posture) - np.nanmin(mse_posture))
 
228
 
229
- # Combine MSEs: negative for facial features (blue), positive for body posture (purple)
230
- combined_mse = mse_posture_norm - mse_embeddings_norm
 
 
231
 
232
  fig, ax = plt.subplots(figsize=(width/100, 2))
233
- im = ax.imshow(combined_mse.reshape(1, -1), aspect='auto', cmap=cmap, extent=[0, total_frames, 0, 1], vmin=-1, vmax=1)
234
- ax.set_yticks([])
 
235
  ax.set_xticks([])
236
  cbar = plt.colorbar(im, ax=ax, orientation='horizontal', pad=0.1, aspect=20, shrink=0.5)
237
- cbar.set_ticks([-1, 0, 1])
238
- cbar.set_ticklabels(['High Facial MSE', 'Neutral', 'High Posture MSE'])
239
  plt.tight_layout()
240
 
241
  line = None
@@ -259,7 +270,6 @@ def create_video_with_heatmap(video_path, df, mse_embeddings, mse_posture, outpu
259
 
260
  combined_frame = np.vstack((frame, heatmap_img))
261
 
262
- # Use frame_count to get timecode, not df index
263
  seconds = frame_count / original_fps
264
  timecode = f"{int(seconds//3600):02d}:{int((seconds%3600)//60):02d}:{int(seconds%60):02d}"
265
  cv2.putText(combined_frame, f"Time: {timecode}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
 
204
  return fig
205
 
206
 
207
+ import matplotlib.pyplot as plt
208
+ from matplotlib.backends.backend_agg import FigureCanvasAgg
209
+ import matplotlib.colors as mcolors
210
+ import numpy as np
211
+ import cv2
212
+
213
  def create_video_with_heatmap(video_path, df, mse_embeddings, mse_posture, output_path, desired_fps):
214
  cap = cv2.VideoCapture(video_path)
215
  original_fps = cap.get(cv2.CAP_PROP_FPS)
 
221
  out = cv2.VideoWriter(output_path, fourcc, desired_fps, (width, height + 200))
222
 
223
  # Create custom colormap
224
+ cmap = mcolors.LinearSegmentedColormap.from_list("custom",
225
+ [(0, 0, 1), (1, 1, 1), (0.5, 0, 0.5)], N=256)
 
226
 
227
+ # Ensure heatmap data covers all frames
228
+ mse_embeddings = np.interp(np.linspace(0, len(mse_embeddings) - 1, total_frames),
229
+ np.arange(len(mse_embeddings)), mse_embeddings)
230
+ mse_posture = np.interp(np.linspace(0, len(mse_posture) - 1, total_frames),
231
+ np.arange(len(mse_posture)), mse_posture)
232
 
233
+ # Normalize MSE values
234
+ mse_embeddings_norm = (mse_embeddings - np.min(mse_embeddings)) / (np.max(mse_embeddings) - np.min(mse_embeddings))
235
+ mse_posture_norm = (mse_posture - np.min(mse_posture)) / (np.max(mse_posture) - np.min(mse_posture))
236
 
237
+ # Combine MSEs
238
+ combined_mse = np.zeros((2, total_frames))
239
+ combined_mse[0] = mse_embeddings_norm
240
+ combined_mse[1] = mse_posture_norm
241
 
242
  fig, ax = plt.subplots(figsize=(width/100, 2))
243
+ im = ax.imshow(combined_mse, aspect='auto', cmap=cmap, extent=[0, total_frames, 0, 2], vmin=0, vmax=1)
244
+ ax.set_yticks([0.5, 1.5])
245
+ ax.set_yticklabels(['Facial', 'Posture'])
246
  ax.set_xticks([])
247
  cbar = plt.colorbar(im, ax=ax, orientation='horizontal', pad=0.1, aspect=20, shrink=0.5)
248
+ cbar.set_ticks([0, 0.5, 1])
249
+ cbar.set_ticklabels(['Low MSE', 'Medium MSE', 'High MSE'])
250
  plt.tight_layout()
251
 
252
  line = None
 
270
 
271
  combined_frame = np.vstack((frame, heatmap_img))
272
 
 
273
  seconds = frame_count / original_fps
274
  timecode = f"{int(seconds//3600):02d}:{int((seconds%3600)//60):02d}:{int(seconds%60):02d}"
275
  cv2.putText(combined_frame, f"Time: {timecode}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)