Update app.py
Browse files
app.py
CHANGED
|
@@ -1,67 +1,139 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from ultralytics import YOLO
|
| 3 |
from PIL import Image
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
def predict_suspicious_activity(image):
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
| 15 |
custom_css = """
|
| 16 |
#main-card {
|
| 17 |
-
background:
|
| 18 |
-
backdrop-filter: blur(
|
| 19 |
-
border-radius:
|
| 20 |
-
box-shadow: 0
|
| 21 |
-
padding:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
}
|
| 23 |
h1 {
|
| 24 |
-
font-size: 2.
|
| 25 |
-
|
|
|
|
| 26 |
-webkit-background-clip: text;
|
| 27 |
-webkit-text-fill-color: transparent;
|
| 28 |
text-align: center;
|
| 29 |
-
margin-bottom:
|
| 30 |
}
|
| 31 |
.description {
|
| 32 |
text-align: center;
|
| 33 |
-
font-size: 1.
|
| 34 |
-
margin-bottom:
|
| 35 |
-
color: #
|
| 36 |
}
|
| 37 |
.gr-button {
|
| 38 |
-
border-radius:
|
| 39 |
-
padding:
|
| 40 |
-
font-weight:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
transition: all 0.3s ease-in-out;
|
| 42 |
}
|
| 43 |
.gr-button:hover {
|
| 44 |
-
transform: scale(1.
|
| 45 |
-
box-shadow: 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
}
|
| 47 |
"""
|
| 48 |
|
| 49 |
-
# Build modern UI
|
| 50 |
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
| 51 |
with gr.Column(elem_id="main-card"):
|
| 52 |
gr.Markdown("<h1>π¨ Suspicious Activity Detection</h1>")
|
| 53 |
-
gr.Markdown(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
with gr.Row():
|
| 56 |
input_image = gr.Image(type="pil", label="Upload Image", height=350)
|
| 57 |
output_image = gr.Image(type="pil", label="Detection Result", height=350)
|
| 58 |
|
| 59 |
with gr.Row():
|
| 60 |
-
detect_btn = gr.Button("π Detect"
|
| 61 |
-
clear_btn = gr.Button("ποΈ Clear",
|
| 62 |
|
|
|
|
| 63 |
detect_btn.click(fn=predict_suspicious_activity, inputs=input_image, outputs=output_image)
|
| 64 |
clear_btn.click(fn=lambda: (None, None), inputs=None, outputs=[input_image, output_image])
|
| 65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
# Launch
|
| 67 |
demo.launch(share=True)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from ultralytics import YOLO
|
| 3 |
from PIL import Image
|
| 4 |
+
import tensorflow as tf
|
| 5 |
+
import numpy as np
|
| 6 |
|
| 7 |
+
# Global variables
|
| 8 |
+
loaded_model = None
|
| 9 |
+
selected_model = None
|
| 10 |
|
| 11 |
+
# Load model on demand
|
| 12 |
+
def load_model(choice):
|
| 13 |
+
global loaded_model, selected_model
|
| 14 |
+
selected_model = choice
|
| 15 |
+
|
| 16 |
+
if choice == "YOLO":
|
| 17 |
+
loaded_model = YOLO("Suspicious_Activities_nano.pt")
|
| 18 |
+
return "β
YOLO model loaded successfully!"
|
| 19 |
+
|
| 20 |
+
elif choice == "SlowFast":
|
| 21 |
+
loaded_model = tf.keras.models.load_model("slowfast_finalmodel.h5")
|
| 22 |
+
return "β
SlowFast model loaded successfully!"
|
| 23 |
+
|
| 24 |
+
else:
|
| 25 |
+
return "β οΈ Please select a valid model."
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
# Prediction function
|
| 29 |
def predict_suspicious_activity(image):
|
| 30 |
+
global loaded_model, selected_model
|
| 31 |
+
|
| 32 |
+
if loaded_model is None:
|
| 33 |
+
return Image.new("RGB", (400, 200), (30, 30, 30)) # blank image
|
| 34 |
+
|
| 35 |
+
if selected_model == "YOLO":
|
| 36 |
+
results = loaded_model.predict(source=image, show=False, conf=0.6)
|
| 37 |
+
results_img = results[0].plot()
|
| 38 |
+
return Image.fromarray(results_img)
|
| 39 |
+
|
| 40 |
+
elif selected_model == "SlowFast":
|
| 41 |
+
img = image.resize((224, 224))
|
| 42 |
+
arr = np.expand_dims(np.array(img) / 255.0, axis=0)
|
| 43 |
+
preds = loaded_model.predict(arr)
|
| 44 |
+
class_id = np.argmax(preds)
|
| 45 |
+
return image # placeholder β replace with visualization
|
| 46 |
|
| 47 |
+
return None
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
# π Custom Attractive CSS
|
| 51 |
custom_css = """
|
| 52 |
#main-card {
|
| 53 |
+
background: linear-gradient(135deg, rgba(30,30,30,0.8), rgba(60,60,60,0.6));
|
| 54 |
+
backdrop-filter: blur(15px);
|
| 55 |
+
border-radius: 22px;
|
| 56 |
+
box-shadow: 0 10px 40px rgba(0,0,0,0.5);
|
| 57 |
+
padding: 35px;
|
| 58 |
+
border: 1px solid rgba(255,255,255,0.15);
|
| 59 |
+
transition: all 0.4s ease-in-out;
|
| 60 |
+
}
|
| 61 |
+
#main-card:hover {
|
| 62 |
+
transform: translateY(-4px);
|
| 63 |
+
box-shadow: 0 15px 45px rgba(0,0,0,0.65);
|
| 64 |
}
|
| 65 |
h1 {
|
| 66 |
+
font-size: 2.8rem !important;
|
| 67 |
+
font-weight: bold;
|
| 68 |
+
background: linear-gradient(90deg, #ff4b1f, #ff9068, #1fddff);
|
| 69 |
-webkit-background-clip: text;
|
| 70 |
-webkit-text-fill-color: transparent;
|
| 71 |
text-align: center;
|
| 72 |
+
margin-bottom: 15px;
|
| 73 |
}
|
| 74 |
.description {
|
| 75 |
text-align: center;
|
| 76 |
+
font-size: 1.15rem;
|
| 77 |
+
margin-bottom: 28px;
|
| 78 |
+
color: #f0f0f0;
|
| 79 |
}
|
| 80 |
.gr-button {
|
| 81 |
+
border-radius: 14px !important;
|
| 82 |
+
padding: 12px 24px !important;
|
| 83 |
+
font-weight: 600 !important;
|
| 84 |
+
letter-spacing: 0.5px;
|
| 85 |
+
background: linear-gradient(135deg, #ff4b1f, #1fddff) !important;
|
| 86 |
+
color: white !important;
|
| 87 |
+
border: none !important;
|
| 88 |
transition: all 0.3s ease-in-out;
|
| 89 |
}
|
| 90 |
.gr-button:hover {
|
| 91 |
+
transform: scale(1.08) rotate(-1deg);
|
| 92 |
+
box-shadow: 0 6px 20px rgba(0,0,0,0.4);
|
| 93 |
+
}
|
| 94 |
+
.gr-button-secondary {
|
| 95 |
+
background: linear-gradient(135deg, #555, #333) !important;
|
| 96 |
+
}
|
| 97 |
+
label {
|
| 98 |
+
color: #ddd !important;
|
| 99 |
+
font-weight: 600;
|
| 100 |
}
|
| 101 |
"""
|
| 102 |
|
| 103 |
+
# π Build modern UI
|
| 104 |
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
| 105 |
with gr.Column(elem_id="main-card"):
|
| 106 |
gr.Markdown("<h1>π¨ Suspicious Activity Detection</h1>")
|
| 107 |
+
gr.Markdown(
|
| 108 |
+
"<p class='description'>Choose a model (YOLO or SlowFast), upload your image, and let AI detect activities instantly β‘</p>"
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
model_choice = gr.Dropdown(
|
| 112 |
+
choices=["YOLO", "SlowFast"], label="Select Model", value=None
|
| 113 |
+
)
|
| 114 |
+
load_status = gr.Label(label="Model Load Status")
|
| 115 |
+
load_btn = gr.Button("π₯ Load Model")
|
| 116 |
|
| 117 |
with gr.Row():
|
| 118 |
input_image = gr.Image(type="pil", label="Upload Image", height=350)
|
| 119 |
output_image = gr.Image(type="pil", label="Detection Result", height=350)
|
| 120 |
|
| 121 |
with gr.Row():
|
| 122 |
+
detect_btn = gr.Button("π Detect")
|
| 123 |
+
clear_btn = gr.Button("ποΈ Clear", elem_classes="gr-button-secondary")
|
| 124 |
|
| 125 |
+
load_btn.click(fn=load_model, inputs=model_choice, outputs=load_status)
|
| 126 |
detect_btn.click(fn=predict_suspicious_activity, inputs=input_image, outputs=output_image)
|
| 127 |
clear_btn.click(fn=lambda: (None, None), inputs=None, outputs=[input_image, output_image])
|
| 128 |
|
| 129 |
+
gr.Markdown(
|
| 130 |
+
"""
|
| 131 |
+
---
|
| 132 |
+
π **Dataset Reference:**
|
| 133 |
+
[DCSASS Dataset on Kaggle](https://www.kaggle.com/mateohervas/dcsass-dataset)
|
| 134 |
+
*(Used for training the SlowFast model)*
|
| 135 |
+
"""
|
| 136 |
+
)
|
| 137 |
+
|
| 138 |
# Launch
|
| 139 |
demo.launch(share=True)
|