reab5555 commited on
Commit
9306d44
·
verified ·
1 Parent(s): 8fe0f0f

Update visualization.py

Browse files
Files changed (1) hide show
  1. visualization.py +12 -43
visualization.py CHANGED
@@ -203,7 +203,7 @@ def plot_posture(df, posture_scores, color='blue', anomaly_threshold=3):
203
  plt.close()
204
  return fig
205
 
206
- def create_video_with_heatmap(video_path, df, mse_embeddings, mse_posture, output_path, desired_fps):
207
  cap = cv2.VideoCapture(video_path)
208
  original_fps = cap.get(cv2.CAP_PROP_FPS)
209
  width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
@@ -211,63 +211,32 @@ def create_video_with_heatmap(video_path, df, mse_embeddings, mse_posture, outpu
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, desired_fps, (width, height + 200))
215
 
216
- # Create custom colormap
217
- cmap = mcolors.LinearSegmentedColormap.from_list("custom",
218
- [(1, 1, 1), (0, 0, 1), (0.5, 0, 0.5)], N=256)
219
-
220
- # Ensure heatmap data covers all frames
221
- mse_embeddings = np.interp(np.linspace(0, len(mse_embeddings) - 1, total_frames),
222
- np.arange(len(mse_embeddings)), mse_embeddings)
223
- mse_posture = np.interp(np.linspace(0, len(mse_posture) - 1, total_frames),
224
- np.arange(len(mse_posture)), mse_posture)
225
-
226
- # Normalize MSE values
227
- mse_embeddings_norm = (mse_embeddings - np.min(mse_embeddings)) / (np.max(mse_embeddings) - np.min(mse_embeddings))
228
- mse_posture_norm = (mse_posture - np.min(mse_posture)) / (np.max(mse_posture) - np.min(mse_posture))
229
-
230
- # Combine MSEs
231
- combined_mse = np.zeros((2, total_frames, 3))
232
- combined_mse[0] = np.array([1 - mse_embeddings_norm, 1 - mse_embeddings_norm, mse_embeddings_norm]).T # RGB for facial
233
- combined_mse[1] = np.array([1 - mse_posture_norm, mse_posture_norm, 1 - mse_posture_norm]).T # RGB for posture
234
-
235
- fig, ax = plt.subplots(figsize=(width/100, 2))
236
- im = ax.imshow(combined_mse, aspect='auto', extent=[0, total_frames, 0, 2])
237
- ax.set_yticks([0.5, 1.5])
238
- ax.set_yticklabels(['Face', 'Posture'])
239
- ax.set_xticks([])
240
- plt.tight_layout()
241
-
242
- line = None
243
- frame_interval = int(original_fps / desired_fps)
244
-
245
- for frame_count in range(0, total_frames, frame_interval):
246
  cap.set(cv2.CAP_PROP_POS_FRAMES, frame_count)
247
  ret, frame = cap.read()
248
  if not ret:
249
  break
250
 
251
- if line:
252
- line.remove()
253
- line = ax.axvline(x=frame_count, color='r', linewidth=2)
254
 
255
- canvas = FigureCanvasAgg(fig)
256
- canvas.draw()
257
- heatmap_img = np.frombuffer(canvas.tostring_rgb(), dtype='uint8')
258
- heatmap_img = heatmap_img.reshape(canvas.get_width_height()[::-1] + (3,))
259
- heatmap_img = cv2.resize(heatmap_img, (width, 200))
260
 
261
- combined_frame = np.vstack((frame, heatmap_img))
262
 
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)
266
 
267
  out.write(combined_frame)
268
 
269
  cap.release()
270
  out.release()
271
- plt.close(fig)
272
 
273
  return output_path
 
203
  plt.close()
204
  return fig
205
 
206
+ def create_video_with_heatmap(video_path, mse_heatmap_embeddings, mse_heatmap_posture, output_path, desired_fps):
207
  cap = cv2.VideoCapture(video_path)
208
  original_fps = cap.get(cv2.CAP_PROP_FPS)
209
  width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
 
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, desired_fps, (width, height + 400))
215
 
216
+ for frame_count in range(0, total_frames):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
217
  cap.set(cv2.CAP_PROP_POS_FRAMES, frame_count)
218
  ret, frame = cap.read()
219
  if not ret:
220
  break
221
 
222
+ heatmap_embeddings_img = np.frombuffer(mse_heatmap_embeddings, dtype='uint8')
223
+ heatmap_embeddings_img = heatmap_embeddings_img.reshape((mse_heatmap_embeddings.get_height(), mse_heatmap_embeddings.get_width(), 3))
 
224
 
225
+ heatmap_posture_img = np.frombuffer(mse_heatmap_posture, dtype='uint8')
226
+ heatmap_posture_img = heatmap_posture_img.reshape((mse_heatmap_posture.get_height(), mse_heatmap_posture.get_width(), 3))
227
+
228
+ heatmap_combined_img = np.vstack((heatmap_embeddings_img, heatmap_posture_img))
 
229
 
230
+ combined_frame = np.vstack((frame, heatmap_combined_img))
231
 
232
  seconds = frame_count / original_fps
233
  timecode = f"{int(seconds//3600):02d}:{int((seconds%3600)//60):02d}:{int(seconds%60):02d}"
234
+ cv2.putText(combined_frame, f"Time: {timecode}", (10, height + 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
235
 
236
  out.write(combined_frame)
237
 
238
  cap.release()
239
  out.release()
240
+ plt.close('all')
241
 
242
  return output_path