reab5555 commited on
Commit
8047571
·
verified ·
1 Parent(s): 08d4d4c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -72
app.py CHANGED
@@ -1,58 +1,3 @@
1
- import gradio as gr
2
- import time
3
- from video_processing import process_video
4
- from PIL import Image
5
- import matplotlib
6
-
7
- matplotlib.rcParams['figure.dpi'] = 300
8
- matplotlib.rcParams['savefig.dpi'] = 300
9
-
10
- def process_and_show_completion(video_input_path, anomaly_threshold_input, fps, progress=gr.Progress()):
11
- try:
12
- print("Starting video processing...")
13
- results = process_video(video_input_path, anomaly_threshold_input, fps, progress=progress)
14
- print("Video processing completed.")
15
-
16
- if isinstance(results[0], str) and results[0].startswith("Error"):
17
- print(f"Error occurred: {results[0]}")
18
- return [results[0]] + [None] * 23
19
-
20
- exec_time, results_summary, df, mse_embeddings, mse_posture, mse_voice, \
21
- mse_plot_embeddings, mse_plot_posture, mse_plot_voice, \
22
- mse_histogram_embeddings, mse_histogram_posture, mse_histogram_voice, \
23
- mse_heatmap_embeddings, mse_heatmap_posture, mse_heatmap_voice, \
24
- face_samples_frequent, \
25
- anomaly_faces_embeddings, anomaly_frames_posture_images, \
26
- aligned_faces_folder, frames_folder, \
27
- heatmap_video_path = results
28
-
29
- anomaly_faces_embeddings_pil = [Image.fromarray(face) for face in anomaly_faces_embeddings] if anomaly_faces_embeddings is not None else []
30
- anomaly_frames_posture_pil = [Image.fromarray(frame) for frame in anomaly_frames_posture_images] if anomaly_frames_posture_images is not None else []
31
-
32
- face_samples_frequent = [Image.open(path) for path in face_samples_frequent] if face_samples_frequent is not None else []
33
-
34
- output = [
35
- exec_time, results_summary,
36
- df, mse_embeddings, mse_posture, mse_voice,
37
- mse_plot_embeddings, mse_plot_posture, mse_plot_voice,
38
- mse_histogram_embeddings, mse_histogram_posture, mse_histogram_voice,
39
- mse_heatmap_embeddings, mse_heatmap_posture, mse_heatmap_voice,
40
- anomaly_faces_embeddings_pil, anomaly_frames_posture_pil,
41
- face_samples_frequent,
42
- aligned_faces_folder, frames_folder,
43
- mse_embeddings, mse_posture, mse_voice,
44
- heatmap_video_path
45
- ]
46
-
47
- return output
48
-
49
- except Exception as e:
50
- error_message = f"An error occurred: {str(e)}"
51
- print(error_message)
52
- import traceback
53
- traceback.print_exc()
54
- return [error_message] + [None] * 23
55
-
56
  with gr.Blocks() as iface:
57
  gr.Markdown("""
58
  # Multimodal Behavioral Anomalies Detection
@@ -73,26 +18,27 @@ with gr.Blocks() as iface:
73
  with gr.Group(visible=False) as results_group:
74
  results_text = gr.TextArea(label="Anomaly Detection Results", lines=4)
75
 
76
- with gr.Tab("Facial Features"):
77
- mse_features_plot = gr.Plot(label="MSE: Facial Features")
78
- mse_features_hist = gr.Plot(label="MSE Distribution: Facial Features")
79
- mse_features_heatmap = gr.Plot(label="MSE Heatmap: Facial Features")
80
- anomaly_frames_features = gr.Gallery(label="Anomaly Frames (Facial Features)", columns=6, rows=2, height="auto")
81
- face_samples_most_frequent = gr.Gallery(label="Most Frequent Person Samples", columns=10, rows=2, height="auto")
 
82
 
83
- with gr.Tab("Body Posture"):
84
- mse_posture_plot = gr.Plot(label="MSE: Body Posture")
85
- mse_posture_hist = gr.Plot(label="MSE Distribution: Body Posture")
86
- mse_posture_heatmap = gr.Plot(label="MSE Heatmap: Body Posture")
87
- anomaly_frames_posture = gr.Gallery(label="Anomaly Frames (Body Posture)", columns=6, rows=2, height="auto")
88
 
89
- with gr.Tab("Voice"):
90
- mse_voice_plot = gr.Plot(label="MSE: Voice")
91
- mse_voice_hist = gr.Plot(label="MSE Distribution: Voice")
92
- mse_voice_heatmap = gr.Plot(label="MSE Heatmap: Voice")
93
 
94
- with gr.Tab("Video with Heatmap"):
95
- heatmap_video = gr.Video(label="Video with Anomaly Heatmap")
96
 
97
  df_store = gr.State()
98
  mse_features_store = gr.State()
@@ -104,6 +50,9 @@ with gr.Blocks() as iface:
104
  mse_heatmap_posture_store = gr.State()
105
  mse_heatmap_voice_store = gr.State()
106
 
 
 
 
107
  process_btn.click(
108
  process_and_show_completion,
109
  inputs=[video_input, anomaly_threshold, fps_slider],
@@ -119,6 +68,10 @@ with gr.Blocks() as iface:
119
  mse_heatmap_embeddings_store, mse_heatmap_posture_store, mse_heatmap_voice_store,
120
  heatmap_video
121
  ]
 
 
 
 
122
  )
123
 
124
  if __name__ == "__main__":
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  with gr.Blocks() as iface:
2
  gr.Markdown("""
3
  # Multimodal Behavioral Anomalies Detection
 
18
  with gr.Group(visible=False) as results_group:
19
  results_text = gr.TextArea(label="Anomaly Detection Results", lines=4)
20
 
21
+ with gr.Tabs():
22
+ with gr.TabItem("Facial Features"):
23
+ mse_features_plot = gr.Plot(label="MSE: Facial Features")
24
+ mse_features_hist = gr.Plot(label="MSE Distribution: Facial Features")
25
+ mse_features_heatmap = gr.Plot(label="MSE Heatmap: Facial Features")
26
+ anomaly_frames_features = gr.Gallery(label="Anomaly Frames (Facial Features)", columns=6, rows=2, height="auto")
27
+ face_samples_most_frequent = gr.Gallery(label="Most Frequent Person Samples", columns=10, rows=2, height="auto")
28
 
29
+ with gr.TabItem("Body Posture"):
30
+ mse_posture_plot = gr.Plot(label="MSE: Body Posture")
31
+ mse_posture_hist = gr.Plot(label="MSE Distribution: Body Posture")
32
+ mse_posture_heatmap = gr.Plot(label="MSE Heatmap: Body Posture")
33
+ anomaly_frames_posture = gr.Gallery(label="Anomaly Frames (Body Posture)", columns=6, rows=2, height="auto")
34
 
35
+ with gr.TabItem("Voice"):
36
+ mse_voice_plot = gr.Plot(label="MSE: Voice")
37
+ mse_voice_hist = gr.Plot(label="MSE Distribution: Voice")
38
+ mse_voice_heatmap = gr.Plot(label="MSE Heatmap: Voice")
39
 
40
+ with gr.TabItem("Video with Heatmap"):
41
+ heatmap_video = gr.Video(label="Video with Anomaly Heatmap")
42
 
43
  df_store = gr.State()
44
  mse_features_store = gr.State()
 
50
  mse_heatmap_posture_store = gr.State()
51
  mse_heatmap_voice_store = gr.State()
52
 
53
+ def show_results(outputs):
54
+ return gr.Group(visible=True)
55
+
56
  process_btn.click(
57
  process_and_show_completion,
58
  inputs=[video_input, anomaly_threshold, fps_slider],
 
68
  mse_heatmap_embeddings_store, mse_heatmap_posture_store, mse_heatmap_voice_store,
69
  heatmap_video
70
  ]
71
+ ).then(
72
+ show_results,
73
+ inputs=None,
74
+ outputs=results_group
75
  )
76
 
77
  if __name__ == "__main__":