reab5555 commited on
Commit
8ed31f9
·
verified ·
1 Parent(s): 0c0a9ed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -85
app.py CHANGED
@@ -3,16 +3,11 @@ import time
3
  from video_processing import process_video
4
  from PIL import Image
5
  import matplotlib
6
- import threading
7
 
8
  matplotlib.rcParams['figure.dpi'] = 300
9
  matplotlib.rcParams['savefig.dpi'] = 300
10
 
11
  def process_and_show_completion(video_input_path, anomaly_threshold_input, fps, progress=gr.Progress()):
12
- global processing
13
- processing = True
14
- start_time = time.time()
15
-
16
  try:
17
  print("Starting video processing...")
18
  results = process_video(video_input_path, anomaly_threshold_input, fps, progress=progress)
@@ -20,7 +15,6 @@ def process_and_show_completion(video_input_path, anomaly_threshold_input, fps,
20
 
21
  if isinstance(results[0], str) and results[0].startswith("Error"):
22
  print(f"Error occurred: {results[0]}")
23
- processing = False
24
  return [results[0]] + [None] * 27
25
 
26
  exec_time, results_summary, df, mse_embeddings, mse_posture, mse_voice, \
@@ -37,11 +31,8 @@ def process_and_show_completion(video_input_path, anomaly_threshold_input, fps,
37
 
38
  face_samples_frequent = [Image.open(path) for path in face_samples_frequent] if face_samples_frequent is not None else []
39
 
40
- end_time = time.time()
41
- total_exec_time = end_time - start_time
42
-
43
  output = [
44
- f"Total execution time: {total_exec_time:.2f} seconds", results_summary,
45
  df, mse_embeddings, mse_posture, mse_voice,
46
  mse_plot_embeddings, mse_plot_posture, mse_plot_voice,
47
  mse_histogram_embeddings, mse_histogram_posture, mse_histogram_voice,
@@ -53,7 +44,6 @@ def process_and_show_completion(video_input_path, anomaly_threshold_input, fps,
53
  heatmap_video_path, combined_mse_plot, correlation_heatmap
54
  ]
55
 
56
- processing = False
57
  return output
58
 
59
  except Exception as e:
@@ -61,69 +51,53 @@ def process_and_show_completion(video_input_path, anomaly_threshold_input, fps,
61
  print(error_message)
62
  import traceback
63
  traceback.print_exc()
64
- processing = False
65
  return [error_message] + [None] * 27
66
 
67
- def show_results():
68
- return [gr.update(visible=True) for _ in range(4)]
69
 
70
- def start_execution_timer():
71
- return gr.update(visible=True), gr.update(visible=False)
72
-
73
- def update_execution_time():
74
- current_time = time.time() - start_time
75
- return f"Execution time: {current_time:.2f} seconds"
76
 
77
- processing = False
78
- start_time = 0
 
79
 
80
- with gr.Blocks() as iface:
81
  with gr.Row():
82
- video_input = gr.Video(label="Input Video")
83
 
84
  anomaly_threshold = gr.Slider(minimum=1, maximum=5, step=0.1, value=3, label="Anomaly Detection Threshold (Standard deviation)")
85
  fps_slider = gr.Slider(minimum=5, maximum=20, step=1, value=10, label="Frames Per Second (FPS)")
86
  process_btn = gr.Button("Detect Anomalies")
87
  progress_bar = gr.Progress()
88
-
89
- with gr.Tabs() as all_tabs:
90
- description_tab = gr.Tab("Description")
91
- with description_tab:
92
- description_md = gr.Markdown("""
93
- # Multimodal Behavioral Anomalies Detection
94
-
95
- This tool detects anomalies in facial expressions, body language, and voice over the timeline of a video.
96
- It extracts faces, postures, and voice from video frames, and analyzes them to identify anomalies using time series analysis and a variational autoencoder (VAE) approach.
97
- """)
98
- execution_time_md = gr.Markdown(visible=False)
99
-
100
- facial_features_tab = gr.Tab("Facial Features", visible=False)
101
- with facial_features_tab:
102
- results_text = gr.TextArea(label="Faces Breakdown", lines=5)
103
- mse_features_plot = gr.Plot(label="MSE: Facial Features")
104
- mse_features_hist = gr.Plot(label="MSE Distribution: Facial Features")
105
- mse_features_heatmap = gr.Plot(label="MSE Heatmap: Facial Features")
106
- anomaly_frames_features = gr.Gallery(label="Anomaly Frames (Facial Features)", columns=6, rows=2, height="auto")
107
- face_samples_most_frequent = gr.Gallery(label="Most Frequent Person Samples", columns=10, rows=2, height="auto")
108
-
109
- body_posture_tab = gr.Tab("Body Posture", visible=False)
110
- with body_posture_tab:
111
- mse_posture_plot = gr.Plot(label="MSE: Body Posture")
112
- mse_posture_hist = gr.Plot(label="MSE Distribution: Body Posture")
113
- mse_posture_heatmap = gr.Plot(label="MSE Heatmap: Body Posture")
114
- anomaly_frames_posture = gr.Gallery(label="Anomaly Frames (Body Posture)", columns=6, rows=2, height="auto")
115
-
116
- voice_tab = gr.Tab("Voice", visible=False)
117
- with voice_tab:
118
- mse_voice_plot = gr.Plot(label="MSE: Voice")
119
- mse_voice_hist = gr.Plot(label="MSE Distribution: Voice")
120
- mse_voice_heatmap = gr.Plot(label="MSE Heatmap: Voice")
121
-
122
- combined_tab = gr.Tab("Combined", visible=False)
123
- with combined_tab:
124
- heatmap_video = gr.Video(label="Video with Anomaly Heatmap")
125
- combined_mse_plot = gr.Plot(label="Combined MSE Plot")
126
- correlation_heatmap_plot = gr.Plot(label="Correlation Heatmap")
127
 
128
  df_store = gr.State()
129
  mse_features_store = gr.State()
@@ -135,24 +109,11 @@ with gr.Blocks() as iface:
135
  mse_heatmap_posture_store = gr.State()
136
  mse_heatmap_voice_store = gr.State()
137
 
138
- def start_processing():
139
- global start_time, processing
140
- start_time = time.time()
141
- processing = True
142
-
143
  process_btn.click(
144
- start_processing,
145
- inputs=None,
146
- outputs=None
147
- ).then(
148
- start_execution_timer,
149
- inputs=None,
150
- outputs=[execution_time_md, description_md]
151
- ).then(
152
  process_and_show_completion,
153
  inputs=[video_input, anomaly_threshold, fps_slider],
154
  outputs=[
155
- execution_time_md, results_text, df_store,
156
  mse_features_store, mse_posture_store, mse_voice_store,
157
  mse_features_plot, mse_posture_plot, mse_voice_plot,
158
  mse_features_hist, mse_posture_hist, mse_voice_hist,
@@ -166,14 +127,7 @@ with gr.Blocks() as iface:
166
  ).then(
167
  show_results,
168
  inputs=None,
169
- outputs=all_tabs.children[1:]
170
- )
171
-
172
- execution_time_md.change(
173
- update_execution_time,
174
- inputs=None,
175
- outputs=execution_time_md,
176
- every=0.1
177
  )
178
 
179
  if __name__ == "__main__":
 
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)
 
15
 
16
  if isinstance(results[0], str) and results[0].startswith("Error"):
17
  print(f"Error occurred: {results[0]}")
 
18
  return [results[0]] + [None] * 27
19
 
20
  exec_time, results_summary, df, mse_embeddings, mse_posture, mse_voice, \
 
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,
 
44
  heatmap_video_path, combined_mse_plot, correlation_heatmap
45
  ]
46
 
 
47
  return output
48
 
49
  except Exception as e:
 
51
  print(error_message)
52
  import traceback
53
  traceback.print_exc()
 
54
  return [error_message] + [None] * 27
55
 
56
+ def show_results(outputs):
57
+ return gr.Group(visible=True)
58
 
59
+ with gr.Blocks() as iface:
60
+ gr.Markdown("""
61
+ # Multimodal Behavioral Anomalies Detection
 
 
 
62
 
63
+ This tool detects anomalies in facial expressions, body language, and voice over the timeline of a video.
64
+ It extracts faces, postures, and voice from video frames, and analyzes them to identify anomalies using time series analysis and a variational autoencoder (VAE) approach.
65
+ """)
66
 
 
67
  with gr.Row():
68
+ video_input = gr.Video()
69
 
70
  anomaly_threshold = gr.Slider(minimum=1, maximum=5, step=0.1, value=3, label="Anomaly Detection Threshold (Standard deviation)")
71
  fps_slider = gr.Slider(minimum=5, maximum=20, step=1, value=10, label="Frames Per Second (FPS)")
72
  process_btn = gr.Button("Detect Anomalies")
73
  progress_bar = gr.Progress()
74
+ execution_time = gr.Number(label="Execution Time (seconds)")
75
+
76
+ with gr.Group(visible=False) as results_group:
77
+ with gr.Tabs():
78
+ with gr.TabItem("Facial Features"):
79
+ results_text = gr.TextArea(label="Faces Breakdown", lines=5)
80
+ mse_features_plot = gr.Plot(label="MSE: Facial Features")
81
+ mse_features_hist = gr.Plot(label="MSE Distribution: Facial Features")
82
+ mse_features_heatmap = gr.Plot(label="MSE Heatmap: Facial Features")
83
+ anomaly_frames_features = gr.Gallery(label="Anomaly Frames (Facial Features)", columns=6, rows=2, height="auto")
84
+ face_samples_most_frequent = gr.Gallery(label="Most Frequent Person Samples", columns=10, rows=2, height="auto")
85
+
86
+ with gr.TabItem("Body Posture"):
87
+ mse_posture_plot = gr.Plot(label="MSE: Body Posture")
88
+ mse_posture_hist = gr.Plot(label="MSE Distribution: Body Posture")
89
+ mse_posture_heatmap = gr.Plot(label="MSE Heatmap: Body Posture")
90
+ anomaly_frames_posture = gr.Gallery(label="Anomaly Frames (Body Posture)", columns=6, rows=2, height="auto")
91
+
92
+ with gr.TabItem("Voice"):
93
+ mse_voice_plot = gr.Plot(label="MSE: Voice")
94
+ mse_voice_hist = gr.Plot(label="MSE Distribution: Voice")
95
+ mse_voice_heatmap = gr.Plot(label="MSE Heatmap: Voice")
96
+
97
+ with gr.TabItem("Combined"):
98
+ heatmap_video = gr.Video(label="Video with Anomaly Heatmap")
99
+ combined_mse_plot = gr.Plot(label="Combined MSE Plot")
100
+ correlation_heatmap_plot = gr.Plot(label="Correlation Heatmap")
 
 
 
 
 
 
 
 
 
 
 
 
101
 
102
  df_store = gr.State()
103
  mse_features_store = gr.State()
 
109
  mse_heatmap_posture_store = gr.State()
110
  mse_heatmap_voice_store = gr.State()
111
 
 
 
 
 
 
112
  process_btn.click(
 
 
 
 
 
 
 
 
113
  process_and_show_completion,
114
  inputs=[video_input, anomaly_threshold, fps_slider],
115
  outputs=[
116
+ execution_time, results_text, df_store,
117
  mse_features_store, mse_posture_store, mse_voice_store,
118
  mse_features_plot, mse_posture_plot, mse_voice_plot,
119
  mse_features_hist, mse_posture_hist, mse_voice_hist,
 
127
  ).then(
128
  show_results,
129
  inputs=None,
130
+ outputs=results_group
 
 
 
 
 
 
 
131
  )
132
 
133
  if __name__ == "__main__":