reab5555 commited on
Commit
88ae781
·
verified ·
1 Parent(s): 7e7733b

Update visualization.py

Browse files
Files changed (1) hide show
  1. visualization.py +25 -9
visualization.py CHANGED
@@ -1,5 +1,6 @@
1
  import matplotlib.pyplot as plt
2
  from matplotlib.backends.backend_agg import FigureCanvasAgg
 
3
  import seaborn as sns
4
  import numpy as np
5
  import pandas as pd
@@ -203,30 +204,45 @@ def plot_posture(df, posture_scores, color='blue', anomaly_threshold=3):
203
  return fig
204
 
205
 
206
- def create_video_with_heatmap(video_path, df, mse_embeddings, mse_posture, output_path):
207
  cap = cv2.VideoCapture(video_path)
208
- fps = cap.get(cv2.CAP_PROP_FPS)
209
  width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
210
  height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
211
  total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
212
 
213
  fourcc = cv2.VideoWriter_fourcc(*'mp4v')
214
- out = cv2.VideoWriter(output_path, fourcc, fps, (width, height + 200))
215
 
216
- # Ensure heatmap data covers all frames
217
- heatmap_data = np.zeros((2, total_frames))
218
- heatmap_data[0, :len(mse_embeddings)] = mse_embeddings
219
- heatmap_data[1, :len(mse_posture)] = mse_posture
 
 
 
 
 
 
 
 
 
 
220
 
221
  fig, ax = plt.subplots(figsize=(width/100, 2))
222
- im = ax.imshow(heatmap_data, aspect='auto', cmap='YlOrRd', extent=[0, total_frames, 0, 2])
223
  ax.set_yticks([])
224
  ax.set_xticks([])
 
 
 
225
  plt.tight_layout()
226
 
227
  line = None
 
228
 
229
- for frame_count in range(total_frames):
 
230
  ret, frame = cap.read()
231
  if not ret:
232
  break
 
1
  import matplotlib.pyplot as plt
2
  from matplotlib.backends.backend_agg import FigureCanvasAgg
3
+ import matplotlib.colors as mcolors
4
  import seaborn as sns
5
  import numpy as np
6
  import pandas as pd
 
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))
211
  height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
212
  total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
213
 
214
  fourcc = cv2.VideoWriter_fourcc(*'mp4v')
215
+ out = cv2.VideoWriter(output_path, fourcc, desired_fps, (width, height + 200))
216
 
217
+ # Create custom colormap
218
+ colors = ['navy', 'blue', 'white', 'purple', 'magenta']
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, positive for body posture
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
242
+ frame_interval = max(1, int(original_fps / desired_fps))
243
 
244
+ for frame_count in range(0, total_frames, frame_interval):
245
+ cap.set(cv2.CAP_PROP_POS_FRAMES, frame_count)
246
  ret, frame = cap.read()
247
  if not ret:
248
  break