Spaces:
Running
Running
File size: 3,521 Bytes
0bf6c32 99fc858 b3b0b59 0bf6c32 b3b0b59 0bf6c32 b3b0b59 0bf6c32 b3b0b59 99fc858 b3b0b59 99fc858 b3b0b59 99fc858 b3b0b59 0bf6c32 b3b0b59 0bf6c32 99fc858 0bf6c32 b3b0b59 0bf6c32 b3b0b59 0bf6c32 b3b0b59 0bf6c32 99fc858 0bf6c32 b3b0b59 0bf6c32 b3b0b59 99fc858 b3b0b59 0bf6c32 99fc858 b3b0b59 0bf6c32 99fc858 b3b0b59 99fc858 b3b0b59 99fc858 b3b0b59 99fc858 b3b0b59 99fc858 b3b0b59 99fc858 b3b0b59 99fc858 0bf6c32 | 1 2 3 4 5 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 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 128 129 130 131 132 133 | import gradio as gr
import cv2
import tempfile
import os
import smtplib
from email.mime.text import MIMEText
from ultralytics import YOLO
from huggingface_hub import hf_hub_download
# -------- LOAD MODEL FROM HUGGING FACE --------
model_path = hf_hub_download(
repo_id="Ultralytics/YOLOv8",
filename="yolov8n.pt"
)
model = YOLO(model_path)
# -------- EMAIL ALERT (SECURE) --------
def send_alert():
sender = os.getenv("EMAIL_USER")
password = os.getenv("EMAIL_PASS")
receiver = os.getenv("EMAIL_TO")
if not sender or not password or not receiver:
print("Email credentials not set.")
return
msg = MIMEText("โ ๏ธ Alert: Person detected!")
msg["Subject"] = "YOLO Surveillance Alert"
msg["From"] = sender
msg["To"] = receiver
try:
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login(sender, password)
server.sendmail(sender, receiver, msg.as_string())
server.quit()
except Exception as e:
print("Email error:", e)
# -------- IMAGE DETECTION --------
def detect_image(image):
results = model(image)
annotated = results[0].plot()
for box in results[0].boxes:
cls = int(box.cls[0])
if model.names[cls] == "person":
send_alert()
break
return annotated
# -------- VIDEO DETECTION + TRACKING --------
def detect_video(video):
cap = cv2.VideoCapture(video)
temp_out = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False)
output_path = temp_out.name
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
fps = int(cap.get(cv2.CAP_PROP_FPS)) or 20
w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
out = cv2.VideoWriter(output_path, fourcc, fps, (w, h))
alert_sent = False
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
results = model.track(frame, persist=True)
annotated = results[0].plot()
if not alert_sent:
for box in results[0].boxes:
cls = int(box.cls[0])
if model.names[cls] == "person":
send_alert()
alert_sent = True
break
out.write(annotated)
cap.release()
out.release()
return output_path
# -------- WEBCAM (SAFE VERSION FOR SPACES) --------
def webcam_detect(image):
results = model(image)
annotated = results[0].plot()
for box in results[0].boxes:
cls = int(box.cls[0])
if model.names[cls] == "person":
send_alert()
break
return annotated
# -------- UI --------
with gr.Blocks() as demo:
gr.Markdown("# ๐ Smart Surveillance System (YOLO + Alerts)")
# IMAGE TAB
with gr.Tab("๐ท Image"):
img_in = gr.Image(type="numpy")
img_out = gr.Image()
btn1 = gr.Button("Run Detection")
btn1.click(detect_image, inputs=img_in, outputs=img_out)
# VIDEO TAB
with gr.Tab("๐ฅ Video"):
vid_in = gr.Video()
vid_out = gr.Video()
btn2 = gr.Button("Run Detection + Tracking")
btn2.click(detect_video, inputs=vid_in, outputs=vid_out)
# WEBCAM TAB (Stable Version)
with gr.Tab("๐ก Webcam"):
cam_in = gr.Image(sources=["webcam"], type="numpy")
cam_out = gr.Image()
btn3 = gr.Button("Capture & Detect")
btn3.click(webcam_detect, inputs=cam_in, outputs=cam_out)
demo.launch()
|