Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,6 +9,7 @@ import cv2
|
|
| 9 |
import constants as const
|
| 10 |
from get_drive_model import ensure_model_download
|
| 11 |
import atexit # Import atexit module
|
|
|
|
| 12 |
# Define the path for the directory
|
| 13 |
FRAMES_FOLDER = 'static/frames'
|
| 14 |
|
|
@@ -26,11 +27,6 @@ second_model = tf.keras.models.load_model('anomaly_classification_model.h5', cus
|
|
| 26 |
UPLOAD_FOLDER = 'uploads'
|
| 27 |
FRAMES_FOLDER = 'static/frames'
|
| 28 |
|
| 29 |
-
# Preloaded video paths for different types of detection
|
| 30 |
-
VIOLENT_DETECTION_VIDEO = 'uploads/violent_detection_video.mp4'
|
| 31 |
-
EXPLOSION_DETECTION_VIDEO = 'uploads/explosion_detection_video.mp4'
|
| 32 |
-
NORMAL_DETECTION_VIDEO = 'uploads/normal_detection_video.mp4'
|
| 33 |
-
|
| 34 |
def process_video(filepath):
|
| 35 |
print(f"Processing video: {filepath}")
|
| 36 |
|
|
@@ -58,12 +54,6 @@ def process_video(filepath):
|
|
| 58 |
frame_paths = save_frames_to_filesystem(frames_for_display)
|
| 59 |
return prediction_label, frame_paths
|
| 60 |
|
| 61 |
-
def read_video(filepath):
|
| 62 |
-
frames_for_prediction, frames_for_display = preprocess(filepath)
|
| 63 |
-
print(frames_for_prediction.shape)
|
| 64 |
-
print(frames_for_display.shape)
|
| 65 |
-
return frames_for_prediction, frames_for_display
|
| 66 |
-
|
| 67 |
def save_frames_to_filesystem(frames):
|
| 68 |
frame_paths = []
|
| 69 |
for i, frame in enumerate(frames):
|
|
@@ -75,18 +65,9 @@ def save_frames_to_filesystem(frames):
|
|
| 75 |
return frame_paths
|
| 76 |
|
| 77 |
def cleanup_uploads_folder():
|
| 78 |
-
# List of files to keep (make exceptions for these files)
|
| 79 |
-
exceptions = {
|
| 80 |
-
'violent_detection_video.mp4',
|
| 81 |
-
'explosion_detection_video.mp4',
|
| 82 |
-
'normal_detection_video.mp4'
|
| 83 |
-
}
|
| 84 |
-
|
| 85 |
# Clean up the UPLOAD_FOLDER
|
| 86 |
if os.path.exists(UPLOAD_FOLDER):
|
| 87 |
for filename in os.listdir(UPLOAD_FOLDER):
|
| 88 |
-
if filename in exceptions:
|
| 89 |
-
continue # Skip the files that are in the exceptions list
|
| 90 |
file_path = os.path.join(UPLOAD_FOLDER, filename)
|
| 91 |
try:
|
| 92 |
if os.path.isfile(file_path) or os.path.islink(file_path):
|
|
@@ -114,26 +95,16 @@ def cleanup_uploads_folder():
|
|
| 114 |
# Register the cleanup function
|
| 115 |
atexit.register(cleanup_uploads_folder)
|
| 116 |
|
| 117 |
-
#
|
| 118 |
-
def detect_violent():
|
| 119 |
-
return process_video(VIOLENT_DETECTION_VIDEO)
|
| 120 |
-
|
| 121 |
-
def detect_explosion():
|
| 122 |
-
return process_video(EXPLOSION_DETECTION_VIDEO)
|
| 123 |
-
|
| 124 |
-
def detect_normal():
|
| 125 |
-
return process_video(NORMAL_DETECTION_VIDEO)
|
| 126 |
-
|
| 127 |
-
# Create Gradio Interface with buttons for specific detections
|
| 128 |
iface = gr.Interface(
|
| 129 |
fn=process_video,
|
| 130 |
-
inputs=gr.File(type="filepath"),
|
| 131 |
outputs=[
|
| 132 |
gr.Textbox(label="Prediction", elem_id="prediction-box"),
|
| 133 |
gr.Gallery(label="Video Frames", elem_id="frame-gallery", columns=5, rows=10)
|
| 134 |
],
|
| 135 |
title="Anomaly Detection in Videos",
|
| 136 |
-
description="Upload a video file
|
| 137 |
theme="default",
|
| 138 |
css="""
|
| 139 |
body {
|
|
@@ -176,10 +147,11 @@ iface = gr.Interface(
|
|
| 176 |
"""
|
| 177 |
)
|
| 178 |
|
| 179 |
-
#
|
|
|
|
| 180 |
violent_iface = gr.Interface(
|
| 181 |
-
fn=
|
| 182 |
-
inputs=
|
| 183 |
outputs=[
|
| 184 |
gr.Textbox(label="Prediction"),
|
| 185 |
gr.Gallery(label="Video Frames", columns=5, rows=10)
|
|
@@ -188,8 +160,8 @@ violent_iface = gr.Interface(
|
|
| 188 |
)
|
| 189 |
|
| 190 |
explosion_iface = gr.Interface(
|
| 191 |
-
fn=
|
| 192 |
-
inputs=
|
| 193 |
outputs=[
|
| 194 |
gr.Textbox(label="Prediction"),
|
| 195 |
gr.Gallery(label="Video Frames", columns=5, rows=10)
|
|
@@ -198,8 +170,8 @@ explosion_iface = gr.Interface(
|
|
| 198 |
)
|
| 199 |
|
| 200 |
normal_iface = gr.Interface(
|
| 201 |
-
fn=
|
| 202 |
-
inputs=
|
| 203 |
outputs=[
|
| 204 |
gr.Textbox(label="Prediction"),
|
| 205 |
gr.Gallery(label="Video Frames", columns=5, rows=10)
|
|
@@ -213,3 +185,5 @@ combined_iface = gr.TabbedInterface([iface, violent_iface, explosion_iface, norm
|
|
| 213 |
|
| 214 |
if __name__ == "__main__":
|
| 215 |
combined_iface.launch()
|
|
|
|
|
|
|
|
|
| 9 |
import constants as const
|
| 10 |
from get_drive_model import ensure_model_download
|
| 11 |
import atexit # Import atexit module
|
| 12 |
+
|
| 13 |
# Define the path for the directory
|
| 14 |
FRAMES_FOLDER = 'static/frames'
|
| 15 |
|
|
|
|
| 27 |
UPLOAD_FOLDER = 'uploads'
|
| 28 |
FRAMES_FOLDER = 'static/frames'
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
def process_video(filepath):
|
| 31 |
print(f"Processing video: {filepath}")
|
| 32 |
|
|
|
|
| 54 |
frame_paths = save_frames_to_filesystem(frames_for_display)
|
| 55 |
return prediction_label, frame_paths
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
def save_frames_to_filesystem(frames):
|
| 58 |
frame_paths = []
|
| 59 |
for i, frame in enumerate(frames):
|
|
|
|
| 65 |
return frame_paths
|
| 66 |
|
| 67 |
def cleanup_uploads_folder():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
# Clean up the UPLOAD_FOLDER
|
| 69 |
if os.path.exists(UPLOAD_FOLDER):
|
| 70 |
for filename in os.listdir(UPLOAD_FOLDER):
|
|
|
|
|
|
|
| 71 |
file_path = os.path.join(UPLOAD_FOLDER, filename)
|
| 72 |
try:
|
| 73 |
if os.path.isfile(file_path) or os.path.islink(file_path):
|
|
|
|
| 95 |
# Register the cleanup function
|
| 96 |
atexit.register(cleanup_uploads_folder)
|
| 97 |
|
| 98 |
+
# Create Gradio Interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
iface = gr.Interface(
|
| 100 |
fn=process_video,
|
| 101 |
+
inputs=gr.File(type="filepath"), # Add file upload for all detections
|
| 102 |
outputs=[
|
| 103 |
gr.Textbox(label="Prediction", elem_id="prediction-box"),
|
| 104 |
gr.Gallery(label="Video Frames", elem_id="frame-gallery", columns=5, rows=10)
|
| 105 |
],
|
| 106 |
title="Anomaly Detection in Videos",
|
| 107 |
+
description="Upload a video file and detect anomalies, violent activity, or explosions.",
|
| 108 |
theme="default",
|
| 109 |
css="""
|
| 110 |
body {
|
|
|
|
| 147 |
"""
|
| 148 |
)
|
| 149 |
|
| 150 |
+
# Additional interfaces for individual triggers (with file upload option)
|
| 151 |
+
|
| 152 |
violent_iface = gr.Interface(
|
| 153 |
+
fn=process_video, # Use the same process_video for Violent Detection
|
| 154 |
+
inputs=gr.File(type="filepath"), # File upload for violent detection
|
| 155 |
outputs=[
|
| 156 |
gr.Textbox(label="Prediction"),
|
| 157 |
gr.Gallery(label="Video Frames", columns=5, rows=10)
|
|
|
|
| 160 |
)
|
| 161 |
|
| 162 |
explosion_iface = gr.Interface(
|
| 163 |
+
fn=process_video, # Use the same process_video for Explosion Detection
|
| 164 |
+
inputs=gr.File(type="filepath"), # File upload for explosion detection
|
| 165 |
outputs=[
|
| 166 |
gr.Textbox(label="Prediction"),
|
| 167 |
gr.Gallery(label="Video Frames", columns=5, rows=10)
|
|
|
|
| 170 |
)
|
| 171 |
|
| 172 |
normal_iface = gr.Interface(
|
| 173 |
+
fn=process_video, # Use the same process_video for Normal Detection
|
| 174 |
+
inputs=gr.File(type="filepath"), # File upload for normal detection
|
| 175 |
outputs=[
|
| 176 |
gr.Textbox(label="Prediction"),
|
| 177 |
gr.Gallery(label="Video Frames", columns=5, rows=10)
|
|
|
|
| 185 |
|
| 186 |
if __name__ == "__main__":
|
| 187 |
combined_iface.launch()
|
| 188 |
+
|
| 189 |
+
|