File size: 2,479 Bytes
bbe81e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import cv2
import torch
import gradio as gr
from ultralytics import YOLO
import os

# Load YOLOv8 Model
model = YOLO("yolov8n.pt")

def detect_vehicles(input_video):
    input_video_path = "input_video.mp4"
    output_video_path = "output_video.mp4"

    # Save uploaded video
    with open(input_video_path, "wb") as f:
        f.write(input_video)

    # Open input video
    cap = cv2.VideoCapture(input_video_path)

    # Get video properties
    frame_width = int(cap.get(3))
    frame_height = int(cap.get(4))
    fps = int(cap.get(cv2.CAP_PROP_FPS))

    # Define VideoWriter
    out = cv2.VideoWriter(output_video_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (frame_width, frame_height))

    while cap.isOpened():
        ret, frame = cap.read()
        if not ret:
            break  

        #Run YOLOv8 inference
        results = model(frame)

        #Draw bounding boxes
        for result in results:
            for box in result.boxes:
                x1, y1, x2, y2 = map(int, box.xyxy[0])  
                conf = box.conf[0]  
                cls = int(box.cls[0])  
                label = model.names[cls]

                #Filter vehicles only
                if label in ["car", "truck", "bus", "motorcycle"]:
                    cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
                    cv2.putText(frame, f"{label} {conf:.2f}", (x1, y1 - 10), 
                                cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)

        #Write to output video
        out.write(frame)

    # Release resources
    cap.release()
    out.release()

    return output_video_path

# Clear function
def clear():
    return None, None  

#Gradio UI
with gr.Blocks() as demo:
    gr.Markdown("## ๐Ÿš— Vehicle Detection with YOLOv8")
    
    with gr.Row():
        input_video = gr.File(label="๐Ÿ“‚ Upload Video", type="binary")
        output_video = gr.Video(label="๐Ÿ“น Processed Video")

    with gr.Row():
        process_button = gr.Button("Detect Vehicles", elem_id="process_button")
        clear_button = gr.Button("Clear", elem_id="clear_button")

    demo.css = """
    #process_button {background-color: #90EE90; color: black; font-weight: bold;}
    #clear_button {background-color: #FF7F7F; color: white; font-weight: bold;}
    """

    process_button.click(fn=detect_vehicles, inputs=input_video, outputs=output_video)
    clear_button.click(fn=clear, inputs=[], outputs=[input_video, output_video])

#Launch Gradio
demo.launch()