Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
import supervision as sv
|
| 5 |
+
from ultralytics import YOLO
|
| 6 |
+
import os
|
| 7 |
+
from collections import defaultdict
|
| 8 |
+
|
| 9 |
+
def ensure_dir(file_path):
|
| 10 |
+
if not os.path.exists(file_path):
|
| 11 |
+
os.makedirs(file_path)
|
| 12 |
+
|
| 13 |
+
def is_point_in_polygon(point, polygon):
|
| 14 |
+
return cv2.pointPolygonTest(polygon, (int(point[0]), int(point[1])), False) > 0
|
| 15 |
+
|
| 16 |
+
def process_video(input_video_path, output_video_path, polygons):
|
| 17 |
+
model = YOLO('yolov8x-seg.pt')
|
| 18 |
+
cap = cv2.VideoCapture(input_video_path)
|
| 19 |
+
intruder_ids = set()
|
| 20 |
+
|
| 21 |
+
ensure_dir(os.path.dirname(output_video_path))
|
| 22 |
+
|
| 23 |
+
if cap.isOpened():
|
| 24 |
+
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 25 |
+
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 26 |
+
fourcc = cv2.VideoWriter_fourcc(*'avc1')
|
| 27 |
+
out = cv2.VideoWriter(output_video_path, fourcc, 20.0, (width, height), True)
|
| 28 |
+
|
| 29 |
+
poly_arrays = [np.array(polygon, np.int32) for polygon in polygons]
|
| 30 |
+
while cap.isOpened():
|
| 31 |
+
success, frame = cap.read()
|
| 32 |
+
if not success:
|
| 33 |
+
break
|
| 34 |
+
|
| 35 |
+
results = model.track(frame, conf=0.6, classes=None, persist=True, save=True, tracker="bytetrack.yaml")
|
| 36 |
+
boxes = results[0].boxes.xywh.cpu()
|
| 37 |
+
track_ids = results[0].boxes.id.int().cpu().tolist()
|
| 38 |
+
classes = results[0].boxes.cls.int().cpu().numpy()
|
| 39 |
+
|
| 40 |
+
annotated_frame = results[0].plot() if hasattr(results[0], 'plot') else frame
|
| 41 |
+
|
| 42 |
+
for poly_array in poly_arrays:
|
| 43 |
+
cv2.polylines(annotated_frame, [poly_array], True, (0, 255, 0), 3)
|
| 44 |
+
|
| 45 |
+
for box, track_id, cls in zip(boxes, track_ids, classes):
|
| 46 |
+
x, y, w, h = box
|
| 47 |
+
centroid = (x + w / 2, y + h / 2)
|
| 48 |
+
|
| 49 |
+
if cls != 19:
|
| 50 |
+
for poly_array in poly_arrays:
|
| 51 |
+
if is_point_in_polygon(centroid, poly_array):
|
| 52 |
+
if track_id not in intruder_ids:
|
| 53 |
+
intruder_ids.add(track_id)
|
| 54 |
+
cv2.rectangle(annotated_frame, (int(x), int(y)), (int(x + w), int(y + h)), (0, 0, 255), 2)
|
| 55 |
+
cv2.putText(annotated_frame, f"Intruder ID: {track_id}", (int(x), int(y) - 10), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
|
| 56 |
+
break
|
| 57 |
+
|
| 58 |
+
intrusion_text = f"Total Intruders Detected: {len(intruder_ids)}"
|
| 59 |
+
cv2.putText(annotated_frame, intrusion_text, (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2)
|
| 60 |
+
|
| 61 |
+
out.write(annotated_frame)
|
| 62 |
+
|
| 63 |
+
cap.release()
|
| 64 |
+
out.release()
|
| 65 |
+
|
| 66 |
+
def main():
|
| 67 |
+
st.title("Video Processing for Intrusion Detection")
|
| 68 |
+
video_file = st.file_uploader("Upload a video", type=['mp4', 'avi'])
|
| 69 |
+
|
| 70 |
+
if video_file is not None:
|
| 71 |
+
output_folder = "output_videos"
|
| 72 |
+
ensure_dir(output_folder)
|
| 73 |
+
file_path = os.path.join(output_folder, "uploaded_video.mp4")
|
| 74 |
+
file_name = os.path.join(output_folder, "processed_video.mp4")
|
| 75 |
+
|
| 76 |
+
with open(file_path, "wb") as f:
|
| 77 |
+
f.write(video_file.getbuffer())
|
| 78 |
+
|
| 79 |
+
polygons = [
|
| 80 |
+
|
| 81 |
+
np.array([
|
| 82 |
+
[110, 70],[1754, 62],[1754, 1062],[138, 1066],[110, 70]
|
| 83 |
+
])
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
]
|
| 87 |
+
|
| 88 |
+
if st.button("Process Video"):
|
| 89 |
+
process_video(file_path, file_name, polygons)
|
| 90 |
+
st.video(file_name)
|
| 91 |
+
|
| 92 |
+
with open(file_name, "rb") as file:
|
| 93 |
+
st.download_button(
|
| 94 |
+
label="Download processed video",
|
| 95 |
+
data=file,
|
| 96 |
+
file_name="processed_video.mp4",
|
| 97 |
+
mime="video/mp4"
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
if __name__ == "__main__":
|
| 101 |
+
main()
|