Spaces:
Runtime error
Runtime error
Rename main.py to app.py
Browse files- main.py → app.py +154 -151
main.py → app.py
RENAMED
|
@@ -1,152 +1,155 @@
|
|
| 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['
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
output += "
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
|
|
|
|
|
|
|
|
|
| 152 |
iface.launch()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import time
|
| 3 |
+
from video_processing import process_video
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import matplotlib
|
| 6 |
+
import spaces
|
| 7 |
+
matplotlib.rcParams['figure.dpi'] = 500
|
| 8 |
+
matplotlib.rcParams['savefig.dpi'] = 500
|
| 9 |
+
|
| 10 |
+
@spaces.GPU(duration=300)
|
| 11 |
+
|
| 12 |
+
def process_and_show_completion(video_input_path, anomaly_threshold_input, fps, progress=gr.Progress()):
|
| 13 |
+
try:
|
| 14 |
+
print("Starting video processing...")
|
| 15 |
+
results = process_video(video_input_path, anomaly_threshold_input, fps, progress=progress)
|
| 16 |
+
print("Video processing completed.")
|
| 17 |
+
|
| 18 |
+
if isinstance(results[0], str) and results[0].startswith("Error"):
|
| 19 |
+
print(f"Error occurred: {results[0]}")
|
| 20 |
+
return [results[0]] + [None] * 18
|
| 21 |
+
|
| 22 |
+
exec_time, results_summary, df, mse_embeddings, mse_posture, \
|
| 23 |
+
mse_plot_embeddings, mse_histogram_embeddings, \
|
| 24 |
+
mse_plot_posture, mse_histogram_posture, \
|
| 25 |
+
mse_heatmap_embeddings, mse_heatmap_posture, \
|
| 26 |
+
face_samples_frequent, face_samples_other, \
|
| 27 |
+
anomaly_faces_embeddings, anomaly_frames_posture_images, \
|
| 28 |
+
aligned_faces_folder, frames_folder, \
|
| 29 |
+
anomaly_sentences_features, anomaly_sentences_posture = results
|
| 30 |
+
|
| 31 |
+
anomaly_faces_embeddings_pil = [Image.fromarray(face) for face in anomaly_faces_embeddings]
|
| 32 |
+
anomaly_frames_posture_pil = [Image.fromarray(frame) for frame in anomaly_frames_posture_images]
|
| 33 |
+
|
| 34 |
+
face_samples_frequent = [Image.open(path) for path in face_samples_frequent]
|
| 35 |
+
face_samples_other = [Image.open(path) for path in face_samples_other]
|
| 36 |
+
|
| 37 |
+
anomaly_sentences_features, anomaly_sentences_posture = results[-2:]
|
| 38 |
+
|
| 39 |
+
# Format anomaly sentences output
|
| 40 |
+
sentences_features_output = format_anomaly_sentences(anomaly_sentences_features, "Facial Features")
|
| 41 |
+
sentences_posture_output = format_anomaly_sentences(anomaly_sentences_posture, "Body Posture")
|
| 42 |
+
|
| 43 |
+
output = [
|
| 44 |
+
exec_time, results_summary,
|
| 45 |
+
df, mse_embeddings, mse_posture,
|
| 46 |
+
mse_plot_embeddings, mse_plot_posture,
|
| 47 |
+
mse_histogram_embeddings, mse_histogram_posture,
|
| 48 |
+
mse_heatmap_embeddings, mse_heatmap_posture,
|
| 49 |
+
anomaly_faces_embeddings_pil, anomaly_frames_posture_pil,
|
| 50 |
+
face_samples_frequent, face_samples_other,
|
| 51 |
+
aligned_faces_folder, frames_folder,
|
| 52 |
+
mse_embeddings, mse_posture,
|
| 53 |
+
sentences_features_output, sentences_posture_output
|
| 54 |
+
]
|
| 55 |
+
|
| 56 |
+
return output
|
| 57 |
+
|
| 58 |
+
except Exception as e:
|
| 59 |
+
error_message = f"An error occurred: {str(e)}"
|
| 60 |
+
print(error_message)
|
| 61 |
+
import traceback
|
| 62 |
+
traceback.print_exc()
|
| 63 |
+
return [error_message] + [None] * 20
|
| 64 |
+
|
| 65 |
+
with gr.Blocks() as iface:
|
| 66 |
+
gr.Markdown("""
|
| 67 |
+
# Facial Expression and Body Language Anomaly Detection
|
| 68 |
+
|
| 69 |
+
This application analyzes videos to detect anomalies in facial features and body language.
|
| 70 |
+
It processes the video frames to extract facial embeddings and body posture,
|
| 71 |
+
then uses machine learning techniques to identify unusual patterns or deviations from the norm.
|
| 72 |
+
|
| 73 |
+
For more information, visit: [https://github.com/reab5555/Facial-Expression-Anomaly-Detection](https://github.com/reab5555/Facial-Expression-Anomaly-Detection)
|
| 74 |
+
""")
|
| 75 |
+
|
| 76 |
+
with gr.Row():
|
| 77 |
+
video_input = gr.Video()
|
| 78 |
+
|
| 79 |
+
anomaly_threshold = gr.Slider(minimum=1, maximum=5, step=0.1, value=3, label="Anomaly Detection Threshold")
|
| 80 |
+
fps_slider = gr.Slider(minimum=5, maximum=20, step=1, value=10, label="Frames Per Second")
|
| 81 |
+
process_btn = gr.Button("Detect Anomalies")
|
| 82 |
+
progress_bar = gr.Progress()
|
| 83 |
+
execution_time = gr.Number(label="Execution Time (seconds)")
|
| 84 |
+
|
| 85 |
+
with gr.Group(visible=False) as results_group:
|
| 86 |
+
results_text = gr.TextArea(label="Anomaly Detection Results", lines=4)
|
| 87 |
+
|
| 88 |
+
with gr.Tab("Facial Features"):
|
| 89 |
+
mse_features_plot = gr.Plot(label="MSE: Facial Features")
|
| 90 |
+
mse_features_hist = gr.Plot(label="MSE Distribution: Facial Features")
|
| 91 |
+
mse_features_heatmap = gr.Plot(label="MSE Heatmap: Facial Features")
|
| 92 |
+
anomaly_frames_features = gr.Gallery(label="Anomaly Frames (Facial Features)", columns=6, rows=2, height="auto")
|
| 93 |
+
|
| 94 |
+
with gr.Tab("Body Posture"):
|
| 95 |
+
mse_posture_plot = gr.Plot(label="MSE: Body Posture")
|
| 96 |
+
mse_posture_hist = gr.Plot(label="MSE Distribution: Body Posture")
|
| 97 |
+
mse_posture_heatmap = gr.Plot(label="MSE Heatmap: Body Posture")
|
| 98 |
+
anomaly_frames_posture = gr.Gallery(label="Anomaly Frames (Body Posture)", columns=6, rows=2, height="auto")
|
| 99 |
+
|
| 100 |
+
with gr.Tab("Sentences"):
|
| 101 |
+
with gr.Row():
|
| 102 |
+
anomaly_sentences_features_output = gr.Textbox(label="Sentences before Facial Feature Anomalies",
|
| 103 |
+
lines=10)
|
| 104 |
+
anomaly_frames_features = gr.Gallery(label="Anomaly Frames (Facial Features)", columns=6, rows=2,
|
| 105 |
+
height="auto")
|
| 106 |
+
|
| 107 |
+
with gr.Row():
|
| 108 |
+
anomaly_sentences_posture_output = gr.Textbox(label="Sentences before Body Posture Anomalies", lines=10)
|
| 109 |
+
anomaly_frames_posture = gr.Gallery(label="Anomaly Frames (Body Posture)", columns=6, rows=2,
|
| 110 |
+
height="auto")
|
| 111 |
+
|
| 112 |
+
with gr.Tab("Face Samples"):
|
| 113 |
+
face_samples_most_frequent = gr.Gallery(label="Most Frequent Person Samples (Target)", columns=6, rows=2, height="auto")
|
| 114 |
+
face_samples_others = gr.Gallery(label="Other Persons Samples", columns=6, rows=1, height="auto")
|
| 115 |
+
|
| 116 |
+
df_store = gr.State()
|
| 117 |
+
mse_features_store = gr.State()
|
| 118 |
+
mse_posture_store = gr.State()
|
| 119 |
+
aligned_faces_folder_store = gr.State()
|
| 120 |
+
frames_folder_store = gr.State()
|
| 121 |
+
mse_heatmap_embeddings_store = gr.State()
|
| 122 |
+
mse_heatmap_posture_store = gr.State()
|
| 123 |
+
|
| 124 |
+
def format_anomaly_sentences(anomaly_sentences, anomaly_type):
|
| 125 |
+
output = f"Sentences before {anomaly_type} Anomalies:\n\n"
|
| 126 |
+
for anomaly_timecode, sentences in anomaly_sentences:
|
| 127 |
+
output += f"Anomaly at {anomaly_timecode}:\n"
|
| 128 |
+
for sentence_timecode, sentence in sentences:
|
| 129 |
+
output += f" [{sentence_timecode}] {sentence}\n"
|
| 130 |
+
output += "\n"
|
| 131 |
+
return output
|
| 132 |
+
|
| 133 |
+
process_btn.click(
|
| 134 |
+
process_and_show_completion,
|
| 135 |
+
inputs=[video_input, anomaly_threshold, fps_slider],
|
| 136 |
+
outputs=[
|
| 137 |
+
execution_time, results_text, df_store,
|
| 138 |
+
mse_features_store, mse_posture_store,
|
| 139 |
+
mse_features_plot, mse_posture_plot,
|
| 140 |
+
mse_features_hist, mse_posture_hist,
|
| 141 |
+
mse_features_heatmap, mse_posture_heatmap,
|
| 142 |
+
anomaly_frames_features, anomaly_frames_posture,
|
| 143 |
+
face_samples_most_frequent, face_samples_others,
|
| 144 |
+
aligned_faces_folder_store, frames_folder_store,
|
| 145 |
+
mse_heatmap_embeddings_store, mse_heatmap_posture_store,
|
| 146 |
+
anomaly_sentences_features_output, anomaly_sentences_posture_output
|
| 147 |
+
]
|
| 148 |
+
).then(
|
| 149 |
+
lambda: gr.Group(visible=True),
|
| 150 |
+
inputs=None,
|
| 151 |
+
outputs=[results_group]
|
| 152 |
+
)
|
| 153 |
+
|
| 154 |
+
if __name__ == "__main__":
|
| 155 |
iface.launch()
|