Update app.py
Browse files
app.py
CHANGED
|
@@ -1,145 +1,81 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from ultralytics import YOLO
|
| 3 |
from PIL import Image
|
| 4 |
-
import
|
| 5 |
-
import
|
| 6 |
-
|
| 7 |
-
|
| 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 |
-
border-radius: 22px;
|
| 60 |
-
box-shadow: 0 10px 40px rgba(0,0,0,0.5);
|
| 61 |
-
padding: 35px;
|
| 62 |
-
border: 1px solid rgba(255,255,255,0.15);
|
| 63 |
-
transition: all 0.4s ease-in-out;
|
| 64 |
-
}
|
| 65 |
-
#main-card:hover {
|
| 66 |
-
transform: translateY(-4px);
|
| 67 |
-
box-shadow: 0 15px 45px rgba(0,0,0,0.65);
|
| 68 |
-
}
|
| 69 |
-
h1 {
|
| 70 |
-
font-size: 2.8rem !important;
|
| 71 |
-
font-weight: bold;
|
| 72 |
-
background: linear-gradient(90deg, #ff4b1f, #ff9068, #1fddff);
|
| 73 |
-
-webkit-background-clip: text;
|
| 74 |
-
-webkit-text-fill-color: transparent;
|
| 75 |
-
text-align: center;
|
| 76 |
-
margin-bottom: 15px;
|
| 77 |
-
}
|
| 78 |
-
.description {
|
| 79 |
-
text-align: center;
|
| 80 |
-
font-size: 1.15rem;
|
| 81 |
-
margin-bottom: 28px;
|
| 82 |
-
color: #f0f0f0;
|
| 83 |
-
}
|
| 84 |
-
.gr-button {
|
| 85 |
-
border-radius: 14px !important;
|
| 86 |
-
padding: 12px 24px !important;
|
| 87 |
-
font-weight: 600 !important;
|
| 88 |
-
letter-spacing: 0.5px;
|
| 89 |
-
background: linear-gradient(135deg, #ff4b1f, #1fddff) !important;
|
| 90 |
-
color: white !important;
|
| 91 |
-
border: none !important;
|
| 92 |
-
transition: all 0.3s ease-in-out;
|
| 93 |
-
}
|
| 94 |
-
.gr-button:hover {
|
| 95 |
-
transform: scale(1.08) rotate(-1deg);
|
| 96 |
-
box-shadow: 0 6px 20px rgba(0,0,0,0.4);
|
| 97 |
-
}
|
| 98 |
-
.gr-button-secondary {
|
| 99 |
-
background: linear-gradient(135deg, #555, #333) !important;
|
| 100 |
-
}
|
| 101 |
-
label {
|
| 102 |
-
color: #ddd !important;
|
| 103 |
-
font-weight: 600;
|
| 104 |
-
}
|
| 105 |
-
"""
|
| 106 |
-
|
| 107 |
-
# π Build UI
|
| 108 |
-
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
| 109 |
-
with gr.Column(elem_id="main-card"):
|
| 110 |
-
gr.Markdown("<h1>π¨ Suspicious Activity Detection</h1>")
|
| 111 |
-
gr.Markdown(
|
| 112 |
-
"<p class='description'>Choose a model (YOLO or SlowFast), upload your image, and let AI detect activities instantly β‘</p>"
|
| 113 |
-
)
|
| 114 |
-
|
| 115 |
-
model_choice = gr.Dropdown(
|
| 116 |
-
choices=["YOLO", "SlowFast"], label="Select Model", value=None
|
| 117 |
-
)
|
| 118 |
-
load_status = gr.Label(label="Model Load Status")
|
| 119 |
-
load_btn = gr.Button("π₯ Load Model")
|
| 120 |
-
|
| 121 |
with gr.Row():
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
with gr.Row():
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
"""
|
| 140 |
-
)
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
# β
Hugging Face needs this
|
| 144 |
-
if __name__ == "__main__":
|
| 145 |
-
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from ultralytics import YOLO
|
| 3 |
from PIL import Image
|
| 4 |
+
import tempfile
|
| 5 |
+
import cv2
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
# ---------------------------
|
| 9 |
+
# Load models EARLY
|
| 10 |
+
# ---------------------------
|
| 11 |
+
yolo_model = YOLO("Suspicious_Activities_nano.pt")
|
| 12 |
+
|
| 13 |
+
# ---------------------------
|
| 14 |
+
# Prediction for IMAGE
|
| 15 |
+
# ---------------------------
|
| 16 |
+
def predict_image(image):
|
| 17 |
+
results = yolo_model.predict(source=image, show=False, conf=0.6)
|
| 18 |
+
results_img = results[0].plot()
|
| 19 |
+
return Image.fromarray(results_img)
|
| 20 |
+
|
| 21 |
+
# ---------------------------
|
| 22 |
+
# Prediction for VIDEO
|
| 23 |
+
# ---------------------------
|
| 24 |
+
def predict_video(video):
|
| 25 |
+
temp_out = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
|
| 26 |
+
cap = cv2.VideoCapture(video)
|
| 27 |
+
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
|
| 28 |
+
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
| 29 |
+
width, height = int(cap.get(3)), int(cap.get(4))
|
| 30 |
+
out = cv2.VideoWriter(temp_out.name, fourcc, fps, (width, height))
|
| 31 |
+
|
| 32 |
+
while cap.isOpened():
|
| 33 |
+
ret, frame = cap.read()
|
| 34 |
+
if not ret:
|
| 35 |
+
break
|
| 36 |
+
results = yolo_model.predict(source=frame, conf=0.6, verbose=False)
|
| 37 |
+
annotated_frame = results[0].plot()
|
| 38 |
+
out.write(annotated_frame)
|
| 39 |
+
|
| 40 |
+
cap.release()
|
| 41 |
+
out.release()
|
| 42 |
+
return temp_out.name
|
| 43 |
+
|
| 44 |
+
# ---------------------------
|
| 45 |
+
# Gradio UI
|
| 46 |
+
# ---------------------------
|
| 47 |
+
with gr.Blocks(css="""
|
| 48 |
+
body {background: linear-gradient(135deg, #1f1c2c, #928DAB);}
|
| 49 |
+
.gradio-container {font-family: 'Segoe UI', sans-serif;}
|
| 50 |
+
h1 {text-align: center; color: white; padding: 20px; font-size: 2.5em;}
|
| 51 |
+
.tabs {margin-top: 20px;}
|
| 52 |
+
.footer {text-align:center; color:#eee; font-size:14px; margin-top:25px;}
|
| 53 |
+
.gr-button {border-radius:12px; font-weight:bold; padding:10px 18px;}
|
| 54 |
+
""") as demo:
|
| 55 |
+
|
| 56 |
+
gr.HTML("<h1>π¨ Suspicious Activity Detection</h1>")
|
| 57 |
+
|
| 58 |
+
with gr.Tab("π· Image Detection"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
with gr.Row():
|
| 60 |
+
with gr.Column(scale=1):
|
| 61 |
+
img_input = gr.Image(type="pil", label="Upload Image")
|
| 62 |
+
img_btn = gr.Button("π Detect Suspicious Activity")
|
| 63 |
+
with gr.Column(scale=1):
|
| 64 |
+
img_output = gr.Image(type="pil", label="Detection Result")
|
| 65 |
+
img_btn.click(predict_image, inputs=img_input, outputs=img_output)
|
| 66 |
+
|
| 67 |
+
with gr.Tab("π₯ Video Detection"):
|
| 68 |
with gr.Row():
|
| 69 |
+
with gr.Column(scale=1):
|
| 70 |
+
vid_input = gr.Video(label="Upload Video")
|
| 71 |
+
vid_btn = gr.Button("π¬ Detect in Video")
|
| 72 |
+
with gr.Column(scale=1):
|
| 73 |
+
vid_output = gr.Video(label="Processed Video")
|
| 74 |
+
vid_btn.click(predict_video, inputs=vid_input, outputs=vid_output)
|
| 75 |
+
|
| 76 |
+
gr.HTML("<div class='footer'>Made with β€οΈ using YOLO + Gradio</div>")
|
| 77 |
+
|
| 78 |
+
# ---------------------------
|
| 79 |
+
# Launch App
|
| 80 |
+
# ---------------------------
|
| 81 |
+
demo.launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|