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

Update visualization.py

Browse files
Files changed (1) hide show
  1. visualization.py +18 -16
visualization.py CHANGED
@@ -210,7 +210,7 @@ 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)
216
  width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
@@ -222,35 +222,37 @@ def create_video_with_heatmap(video_path, df, mse_embeddings, mse_posture, outpu
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
253
- frame_interval = max(1, int(original_fps / desired_fps))
254
 
255
  for frame_count in range(0, total_frames, frame_interval):
256
  cap.set(cv2.CAP_PROP_POS_FRAMES, frame_count)
 
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, largest_cluster):
214
  cap = cv2.VideoCapture(video_path)
215
  original_fps = cap.get(cv2.CAP_PROP_FPS)
216
  width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
 
222
 
223
  # Create custom colormap
224
  cmap = mcolors.LinearSegmentedColormap.from_list("custom",
225
+ [(1, 1, 1), (0, 0, 1), (0.5, 0, 0.5)], N=256)
226
+
227
+ # Filter data for the most frequent person
228
+ df_most_frequent = df[df['Cluster'] == largest_cluster]
229
 
230
  # Ensure heatmap data covers all frames
231
+ full_range = np.arange(total_frames)
232
+ mse_embeddings_full = np.zeros(total_frames)
233
+ mse_posture_full = np.zeros(total_frames)
234
+
235
+ mse_embeddings_full[df_most_frequent['Frame']] = mse_embeddings
236
+ mse_posture_full[df_most_frequent['Frame']] = mse_posture
237
 
238
  # Normalize MSE values
239
+ mse_embeddings_norm = (mse_embeddings_full - np.min(mse_embeddings_full)) / (np.max(mse_embeddings_full) - np.min(mse_embeddings_full))
240
+ mse_posture_norm = (mse_posture_full - np.min(mse_posture_full)) / (np.max(mse_posture_full) - np.min(mse_posture_full))
241
 
242
  # Combine MSEs
243
+ combined_mse = np.zeros((2, total_frames, 3))
244
+ combined_mse[0] = np.array([1 - mse_embeddings_norm, 1 - mse_embeddings_norm, mse_embeddings_norm]).T # RGB for facial
245
+ combined_mse[1] = np.array([1 - mse_posture_norm, mse_posture_norm, 1 - mse_posture_norm]).T # RGB for posture
246
 
247
  fig, ax = plt.subplots(figsize=(width/100, 2))
248
+ im = ax.imshow(combined_mse, aspect='auto', extent=[0, total_frames, 0, 2])
249
  ax.set_yticks([0.5, 1.5])
250
  ax.set_yticklabels(['Facial', 'Posture'])
251
  ax.set_xticks([])
 
 
 
252
  plt.tight_layout()
253
 
254
  line = None
255
+ frame_interval = int(original_fps / desired_fps)
256
 
257
  for frame_count in range(0, total_frames, frame_interval):
258
  cap.set(cv2.CAP_PROP_POS_FRAMES, frame_count)