reab5555 commited on
Commit
97ad667
·
verified ·
1 Parent(s): 7ec8325

Update visualization.py

Browse files
Files changed (1) hide show
  1. visualization.py +0 -131
visualization.py CHANGED
@@ -216,134 +216,3 @@ def plot_posture(df, posture_scores, color='blue', anomaly_threshold=3):
216
  plt.tight_layout()
217
  plt.close()
218
  return fig
219
-
220
- def create_heatmap(t, mse_embeddings, mse_posture, mse_voice, video_fps, total_frames, video_width):
221
- frame_count = int(t * video_fps)
222
-
223
- # Normalize MSE values
224
- mse_embeddings_norm = (mse_embeddings - np.min(mse_embeddings)) / (np.max(mse_embeddings) - np.min(mse_embeddings))
225
- mse_posture_norm = (mse_posture - np.min(mse_posture)) / (np.max(mse_posture) - np.min(mse_posture))
226
- mse_voice_norm = (mse_voice - np.min(mse_voice)) / (np.max(mse_voice) - np.min(mse_voice))
227
-
228
- combined_mse = np.zeros((3, total_frames))
229
- combined_mse[0] = mse_embeddings
230
- combined_mse[1] = mse_posture
231
- combined_mse[2] = mse_voice
232
-
233
- fig, ax = plt.subplots(figsize=(video_width / 300, 0.4))
234
- ax.imshow(combined_mse, aspect='auto', cmap='Reds', vmin=0, vmax=1, extent=[0, total_frames, 0, 3])
235
- ax.set_yticks([0.5, 1.5, 2.5])
236
- ax.set_yticklabels(['Voice', 'Posture', 'Face'], fontsize=7)
237
- ax.set_xticks([])
238
-
239
- ax.axvline(x=frame_count, color='black', linewidth=2)
240
-
241
- plt.tight_layout(pad=0.5)
242
-
243
- canvas = FigureCanvas(fig)
244
- canvas.draw()
245
- heatmap_img = np.frombuffer(canvas.tostring_rgb(), dtype='uint8')
246
- heatmap_img = heatmap_img.reshape(canvas.get_width_height()[::-1] + (3,))
247
- plt.close(fig)
248
- return heatmap_img
249
-
250
- def create_video_with_heatmap(video_path, df, mse_embeddings, mse_posture, mse_voice, output_folder, desired_fps, largest_cluster):
251
- print(f"Creating heatmap video. Output folder: {output_folder}")
252
- os.makedirs(output_folder, exist_ok=True)
253
- output_filename = os.path.basename(video_path).rsplit('.', 1)[0] + '_heatmap.mp4'
254
- heatmap_video_path = os.path.join(output_folder, output_filename)
255
- print(f"Heatmap video will be saved at: {heatmap_video_path}")
256
-
257
- # Load the original video
258
- video = VideoFileClip(video_path)
259
-
260
- # Get video properties
261
- width, height = video.w, video.h
262
- total_frames = int(video.duration * video.fps)
263
-
264
- def fill_with_previous_values(mse_array, total_frames):
265
- result = np.zeros(total_frames)
266
- indices = np.linspace(0, total_frames - 1, len(mse_array)).astype(int)
267
- result[indices] = mse_array
268
- for i in range(1, total_frames):
269
- if result[i] == 0:
270
- result[i] = result[i-1]
271
- return result
272
-
273
- # Fill gaps with previous values
274
- mse_embeddings = fill_with_previous_values(mse_embeddings, total_frames)
275
- mse_posture = fill_with_previous_values(mse_posture, total_frames)
276
- mse_voice = fill_with_previous_values(mse_voice, total_frames)
277
-
278
- # Find max values for each variable
279
- max_embeddings = np.max(mse_embeddings)
280
- max_posture = np.max(mse_posture)
281
- max_voice = np.max(mse_voice)
282
-
283
- def create_heatmap(t, mse_embeddings, mse_posture, mse_voice, fps, total_frames, width):
284
- frame_index = int(t * fps)
285
-
286
- # Create separate color maps for each variable
287
- cmap_embeddings = plt.get_cmap('Reds')
288
- cmap_posture = plt.get_cmap('Reds')
289
- cmap_voice = plt.get_cmap('Reds')
290
-
291
- # Normalize values within their own scales
292
- norm_embeddings = mse_embeddings[frame_index] / max_embeddings
293
- norm_posture = mse_posture[frame_index] / max_posture
294
- norm_voice = mse_voice[frame_index] / max_voice
295
-
296
- # Create color arrays for each variable
297
- color_embeddings = cmap_embeddings(norm_embeddings)
298
- color_posture = cmap_posture(norm_posture)
299
- color_voice = cmap_voice(norm_voice)
300
-
301
- # Create the heatmap frame
302
- heatmap_height = 100 # Adjust as needed
303
- heatmap_frame = np.zeros((heatmap_height, width, 4))
304
-
305
- # Fill the heatmap sections
306
- heatmap_frame[:heatmap_height//3, :] = color_embeddings
307
- heatmap_frame[heatmap_height//3:2*heatmap_height//3, :] = color_posture
308
- heatmap_frame[2*heatmap_height//3:, :] = color_voice
309
-
310
- return (heatmap_frame * 255).astype(np.uint8)
311
-
312
- def combine_video_and_heatmap(t):
313
- video_frame = video.get_frame(t)
314
- heatmap_frame = create_heatmap(t, mse_embeddings, mse_posture, mse_voice, video.fps, total_frames, width)
315
- heatmap_frame_resized = cv2.resize(heatmap_frame, (width, heatmap_frame.shape[0]))
316
- combined_frame = np.vstack((video_frame, heatmap_frame_resized))
317
- return combined_frame
318
-
319
- final_clip = VideoClip(combine_video_and_heatmap, duration=video.duration)
320
- final_clip = final_clip.set_audio(video.audio)
321
-
322
- # Write the final video
323
- final_clip.write_videofile(heatmap_video_path, codec='libx264', audio_codec='aac', fps=video.fps)
324
-
325
- # Close the video clips
326
- video.close()
327
- final_clip.close()
328
-
329
- if os.path.exists(heatmap_video_path):
330
- print(f"Heatmap video created at: {heatmap_video_path}")
331
- print(f"Heatmap video size: {os.path.getsize(heatmap_video_path)} bytes")
332
- return heatmap_video_path
333
- else:
334
- print(f"Failed to create heatmap video at: {heatmap_video_path}")
335
- return None
336
-
337
-
338
- # Function to create the correlation heatmap
339
- def plot_correlation_heatmap(mse_embeddings, mse_posture, mse_voice):
340
- data = np.vstack((mse_embeddings, mse_posture, mse_voice)).T
341
- df = pd.DataFrame(data, columns=["Facial Features", "Body Posture", "Voice"])
342
- corr = df.corr()
343
-
344
- plt.figure(figsize=(10, 8), dpi=300)
345
-
346
- heatmap = sns.heatmap(corr, annot=True, cmap='coolwarm', vmin=-1, vmax=1)
347
- plt.title('Correlation Heatmap of MSEs')
348
- plt.tight_layout()
349
- return plt.gcf()
 
216
  plt.tight_layout()
217
  plt.close()
218
  return fig