Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import cv2
|
| 3 |
+
import tempfile
|
| 4 |
+
import os
|
| 5 |
+
from ultralytics import YOLO
|
| 6 |
+
|
| 7 |
+
model = YOLO("yolov8x.pt")
|
| 8 |
+
|
| 9 |
+
vehicle_classes = {
|
| 10 |
+
"bicycle": 1,
|
| 11 |
+
"car": 2,
|
| 12 |
+
"motorcycle": 3,
|
| 13 |
+
"bus": 5,
|
| 14 |
+
"truck": 7
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
def detect_vehicles_with_logs(video_input):
|
| 18 |
+
cap = cv2.VideoCapture(video_input)
|
| 19 |
+
if not cap.isOpened():
|
| 20 |
+
return None, "", "❌ Error: Could not open video."
|
| 21 |
+
|
| 22 |
+
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 23 |
+
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 24 |
+
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 25 |
+
|
| 26 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as tmpfile:
|
| 27 |
+
out_path = tmpfile.name
|
| 28 |
+
|
| 29 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 30 |
+
out_vid = cv2.VideoWriter(out_path, fourcc, fps, (width, height))
|
| 31 |
+
|
| 32 |
+
logs = ""
|
| 33 |
+
frame_index = 0
|
| 34 |
+
|
| 35 |
+
while True:
|
| 36 |
+
ret, frame = cap.read()
|
| 37 |
+
if not ret:
|
| 38 |
+
break
|
| 39 |
+
frame_index += 1
|
| 40 |
+
|
| 41 |
+
results = model(frame, verbose=False)[0]
|
| 42 |
+
detected = []
|
| 43 |
+
|
| 44 |
+
for box in results.boxes:
|
| 45 |
+
cls_id = int(box.cls.item())
|
| 46 |
+
if cls_id in vehicle_classes.values():
|
| 47 |
+
xyxy = box.xyxy[0].cpu().numpy().astype(int)
|
| 48 |
+
conf = box.conf.item()
|
| 49 |
+
label = model.names[cls_id]
|
| 50 |
+
detected.append(f"{label} ({conf:.2f})")
|
| 51 |
+
|
| 52 |
+
cv2.rectangle(frame, tuple(xyxy[:2]), tuple(xyxy[2:]), (0, 255, 0), 2)
|
| 53 |
+
cv2.putText(frame, f"{label} {conf:.2f}", (xyxy[0], xyxy[1] - 5),
|
| 54 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)
|
| 55 |
+
|
| 56 |
+
out_vid.write(frame)
|
| 57 |
+
logs += f"Frame {frame_index}: {', '.join(detected) if detected else 'No vehicles detected'}\n"
|
| 58 |
+
|
| 59 |
+
cap.release()
|
| 60 |
+
out_vid.release()
|
| 61 |
+
|
| 62 |
+
if not os.path.exists(out_path):
|
| 63 |
+
return None, "", "❌ Error: Processed video not found."
|
| 64 |
+
|
| 65 |
+
# Return three outputs:
|
| 66 |
+
# 1) path for video player to display (as video file)
|
| 67 |
+
# 2) path for download button (same video file)
|
| 68 |
+
# 3) logs text
|
| 69 |
+
return out_path, out_path, logs
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
demo = gr.Interface(
|
| 73 |
+
fn=detect_vehicles_with_logs,
|
| 74 |
+
inputs=gr.Video(label="Upload a video"),
|
| 75 |
+
outputs=[
|
| 76 |
+
gr.Video(label="Processed Video"), # show inline video
|
| 77 |
+
gr.File(label="Download Processed Video"), # download button
|
| 78 |
+
gr.Textbox(label="Detection Logs", lines=20, interactive=False)
|
| 79 |
+
],
|
| 80 |
+
title="YOLOv8x Vehicle Detection with Inline Video and Download",
|
| 81 |
+
description="Upload a video, detect vehicles, see the processed video inline, download it, and view logs."
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
if __name__ == "__main__":
|
| 85 |
+
demo.launch(share=True)
|