initial commit
Browse files
app.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
from ultralytics import YOLO
|
| 5 |
+
import time
|
| 6 |
+
|
| 7 |
+
# Load YOLO model
|
| 8 |
+
model = YOLO("yolov8n.pt") # Updated to correct model name
|
| 9 |
+
|
| 10 |
+
# Define trapezoidal restricted area (top-left, top-right, bottom-right, bottom-left)
|
| 11 |
+
trapezoid_pts = np.array([[250, 150], [400, 150], [450, 300], [200, 300]], np.int32)
|
| 12 |
+
|
| 13 |
+
def is_inside_trapezoid(box, trapezoid_pts):
|
| 14 |
+
"""Check if the center of a detected object is inside the trapezoidal area."""
|
| 15 |
+
x1, y1, x2, y2 = box
|
| 16 |
+
cx, cy = int((x1 + x2) / 2), int((y1 + y2) / 2)
|
| 17 |
+
return cv2.pointPolygonTest(trapezoid_pts, (cx, cy), False) >= 0
|
| 18 |
+
|
| 19 |
+
def detect_objects(frame):
|
| 20 |
+
if frame is None:
|
| 21 |
+
return np.zeros((480, 640, 3), dtype=np.uint8), "No input frame"
|
| 22 |
+
|
| 23 |
+
results = model.predict(frame, conf=0.5)
|
| 24 |
+
annotated_frame = results[0].plot()
|
| 25 |
+
|
| 26 |
+
# Draw the trapezoid area
|
| 27 |
+
cv2.polylines(annotated_frame, [trapezoid_pts.reshape((-1, 1, 2))], isClosed=True, color=(0, 0, 255), thickness=2)
|
| 28 |
+
|
| 29 |
+
isAlert = {"alert": [False, ""], "personCount": 0}
|
| 30 |
+
classInIntrusion = ["person", "bicycle", "car", "motorcycle"]
|
| 31 |
+
|
| 32 |
+
for r in results:
|
| 33 |
+
for box, cls in zip(r.boxes.xyxy, r.boxes.cls):
|
| 34 |
+
class_id = int(cls.item())
|
| 35 |
+
if class_id == 0: # Person
|
| 36 |
+
isAlert["personCount"] += 1
|
| 37 |
+
if class_id in [0, 1, 2, 3]: # Person, bicycle, car, motorcycle
|
| 38 |
+
if is_inside_trapezoid(box.tolist(), trapezoid_pts):
|
| 39 |
+
isAlert["alert"] = [True, classInIntrusion[class_id]]
|
| 40 |
+
# Mark the intrusion with a red box
|
| 41 |
+
x1, y1, x2, y2 = map(int, box.tolist())
|
| 42 |
+
cv2.rectangle(annotated_frame, (x1, y1), (x2, y2), (0, 0, 255), 3)
|
| 43 |
+
|
| 44 |
+
# Add alert text on the frame
|
| 45 |
+
alert_text = f"Intrusion Alert: {isAlert['alert'][0]}, Object: {isAlert['alert'][1]}, Persons: {isAlert['personCount']}"
|
| 46 |
+
cv2.putText(annotated_frame, alert_text, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
|
| 47 |
+
|
| 48 |
+
return annotated_frame, alert_text
|
| 49 |
+
|
| 50 |
+
def webcam_feed():
|
| 51 |
+
cap = cv2.VideoCapture(0)
|
| 52 |
+
|
| 53 |
+
# Check if the webcam opened successfully
|
| 54 |
+
if not cap.isOpened():
|
| 55 |
+
return np.zeros((480, 640, 3), dtype=np.uint8), "Failed to open webcam"
|
| 56 |
+
|
| 57 |
+
while True:
|
| 58 |
+
ret, frame = cap.read()
|
| 59 |
+
if not ret:
|
| 60 |
+
break
|
| 61 |
+
|
| 62 |
+
# Process frame
|
| 63 |
+
result_frame, alert_message = detect_objects(frame)
|
| 64 |
+
|
| 65 |
+
# Return the processed frame and alert message
|
| 66 |
+
yield result_frame, alert_message
|
| 67 |
+
|
| 68 |
+
# Create the Gradio interface with webcam
|
| 69 |
+
demo = gr.Interface(
|
| 70 |
+
fn=webcam_feed,
|
| 71 |
+
inputs=[],
|
| 72 |
+
outputs=[
|
| 73 |
+
gr.Image(label="Detection Output"),
|
| 74 |
+
gr.Textbox(label="Alert Status")
|
| 75 |
+
],
|
| 76 |
+
live=True,
|
| 77 |
+
title="YOLO Intrusion Detection",
|
| 78 |
+
description="Real-time detection of persons and vehicles inside a restricted trapezoidal area.",
|
| 79 |
+
allow_flagging="never"
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
if __name__ == "__main__":
|
| 83 |
+
demo.queue(max_size=1).launch()
|