Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import cv2
|
| 3 |
import tempfile
|
|
|
|
| 4 |
import smtplib
|
| 5 |
from email.mime.text import MIMEText
|
| 6 |
from ultralytics import YOLO
|
|
@@ -14,14 +15,18 @@ model_path = hf_hub_download(
|
|
| 14 |
|
| 15 |
model = YOLO(model_path)
|
| 16 |
|
| 17 |
-
# -------- EMAIL ALERT --------
|
| 18 |
def send_alert():
|
| 19 |
-
sender =
|
| 20 |
-
password = "
|
| 21 |
-
receiver =
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
msg = MIMEText("⚠️ Alert: Person detected!")
|
| 24 |
-
msg["Subject"] = "YOLO Alert"
|
| 25 |
msg["From"] = sender
|
| 26 |
msg["To"] = receiver
|
| 27 |
|
|
@@ -47,7 +52,7 @@ def detect_image(image):
|
|
| 47 |
|
| 48 |
return annotated
|
| 49 |
|
| 50 |
-
# -------- VIDEO DETECTION --------
|
| 51 |
def detect_video(video):
|
| 52 |
cap = cv2.VideoCapture(video)
|
| 53 |
|
|
@@ -61,6 +66,8 @@ def detect_video(video):
|
|
| 61 |
|
| 62 |
out = cv2.VideoWriter(output_path, fourcc, fps, (w, h))
|
| 63 |
|
|
|
|
|
|
|
| 64 |
while cap.isOpened():
|
| 65 |
ret, frame = cap.read()
|
| 66 |
if not ret:
|
|
@@ -69,11 +76,13 @@ def detect_video(video):
|
|
| 69 |
results = model.track(frame, persist=True)
|
| 70 |
annotated = results[0].plot()
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
| 77 |
|
| 78 |
out.write(annotated)
|
| 79 |
|
|
@@ -82,9 +91,9 @@ def detect_video(video):
|
|
| 82 |
|
| 83 |
return output_path
|
| 84 |
|
| 85 |
-
# -------- WEBCAM --------
|
| 86 |
-
def webcam_detect(
|
| 87 |
-
results = model
|
| 88 |
annotated = results[0].plot()
|
| 89 |
|
| 90 |
for box in results[0].boxes:
|
|
@@ -97,23 +106,27 @@ def webcam_detect(frame):
|
|
| 97 |
|
| 98 |
# -------- UI --------
|
| 99 |
with gr.Blocks() as demo:
|
| 100 |
-
gr.Markdown("# 🔐
|
| 101 |
|
| 102 |
-
|
|
|
|
| 103 |
img_in = gr.Image(type="numpy")
|
| 104 |
img_out = gr.Image()
|
| 105 |
-
btn1 = gr.Button("
|
| 106 |
btn1.click(detect_image, inputs=img_in, outputs=img_out)
|
| 107 |
|
| 108 |
-
|
|
|
|
| 109 |
vid_in = gr.Video()
|
| 110 |
vid_out = gr.Video()
|
| 111 |
-
btn2 = gr.Button("
|
| 112 |
btn2.click(detect_video, inputs=vid_in, outputs=vid_out)
|
| 113 |
|
| 114 |
-
|
| 115 |
-
|
|
|
|
| 116 |
cam_out = gr.Image()
|
| 117 |
-
|
|
|
|
| 118 |
|
| 119 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import cv2
|
| 3 |
import tempfile
|
| 4 |
+
import os
|
| 5 |
import smtplib
|
| 6 |
from email.mime.text import MIMEText
|
| 7 |
from ultralytics import YOLO
|
|
|
|
| 15 |
|
| 16 |
model = YOLO(model_path)
|
| 17 |
|
| 18 |
+
# -------- EMAIL ALERT (SECURE) --------
|
| 19 |
def send_alert():
|
| 20 |
+
sender = os.getenv("EMAIL_USER")
|
| 21 |
+
password = os.getenv("EMAIL_PASS")
|
| 22 |
+
receiver = os.getenv("EMAIL_TO")
|
| 23 |
+
|
| 24 |
+
if not sender or not password or not receiver:
|
| 25 |
+
print("Email credentials not set.")
|
| 26 |
+
return
|
| 27 |
|
| 28 |
msg = MIMEText("⚠️ Alert: Person detected!")
|
| 29 |
+
msg["Subject"] = "YOLO Surveillance Alert"
|
| 30 |
msg["From"] = sender
|
| 31 |
msg["To"] = receiver
|
| 32 |
|
|
|
|
| 52 |
|
| 53 |
return annotated
|
| 54 |
|
| 55 |
+
# -------- VIDEO DETECTION + TRACKING --------
|
| 56 |
def detect_video(video):
|
| 57 |
cap = cv2.VideoCapture(video)
|
| 58 |
|
|
|
|
| 66 |
|
| 67 |
out = cv2.VideoWriter(output_path, fourcc, fps, (w, h))
|
| 68 |
|
| 69 |
+
alert_sent = False
|
| 70 |
+
|
| 71 |
while cap.isOpened():
|
| 72 |
ret, frame = cap.read()
|
| 73 |
if not ret:
|
|
|
|
| 76 |
results = model.track(frame, persist=True)
|
| 77 |
annotated = results[0].plot()
|
| 78 |
|
| 79 |
+
if not alert_sent:
|
| 80 |
+
for box in results[0].boxes:
|
| 81 |
+
cls = int(box.cls[0])
|
| 82 |
+
if model.names[cls] == "person":
|
| 83 |
+
send_alert()
|
| 84 |
+
alert_sent = True
|
| 85 |
+
break
|
| 86 |
|
| 87 |
out.write(annotated)
|
| 88 |
|
|
|
|
| 91 |
|
| 92 |
return output_path
|
| 93 |
|
| 94 |
+
# -------- WEBCAM (SAFE VERSION FOR SPACES) --------
|
| 95 |
+
def webcam_detect(image):
|
| 96 |
+
results = model(image)
|
| 97 |
annotated = results[0].plot()
|
| 98 |
|
| 99 |
for box in results[0].boxes:
|
|
|
|
| 106 |
|
| 107 |
# -------- UI --------
|
| 108 |
with gr.Blocks() as demo:
|
| 109 |
+
gr.Markdown("# 🔐 Smart Surveillance System (YOLO + Alerts)")
|
| 110 |
|
| 111 |
+
# IMAGE TAB
|
| 112 |
+
with gr.Tab("📷 Image"):
|
| 113 |
img_in = gr.Image(type="numpy")
|
| 114 |
img_out = gr.Image()
|
| 115 |
+
btn1 = gr.Button("Run Detection")
|
| 116 |
btn1.click(detect_image, inputs=img_in, outputs=img_out)
|
| 117 |
|
| 118 |
+
# VIDEO TAB
|
| 119 |
+
with gr.Tab("🎥 Video"):
|
| 120 |
vid_in = gr.Video()
|
| 121 |
vid_out = gr.Video()
|
| 122 |
+
btn2 = gr.Button("Run Detection + Tracking")
|
| 123 |
btn2.click(detect_video, inputs=vid_in, outputs=vid_out)
|
| 124 |
|
| 125 |
+
# WEBCAM TAB (Stable Version)
|
| 126 |
+
with gr.Tab("📡 Webcam"):
|
| 127 |
+
cam_in = gr.Image(sources=["webcam"], type="numpy")
|
| 128 |
cam_out = gr.Image()
|
| 129 |
+
btn3 = gr.Button("Capture & Detect")
|
| 130 |
+
btn3.click(webcam_detect, inputs=cam_in, outputs=cam_out)
|
| 131 |
|
| 132 |
demo.launch()
|