File size: 1,834 Bytes
14e965c
 
 
 
 
 
 
 
 
 
 
 
9d9c22f
14e965c
 
 
 
 
 
 
9d9c22f
14e965c
 
 
 
 
 
 
 
0717065
14e965c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9d9c22f
14e965c
 
 
 
9d9c22f
14e965c
 
 
 
 
 
9d9c22f
14e965c
 
9d9c22f
14e965c
9d9c22f
 
14e965c
 
 
 
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
import gradio as gr
import cv2
from ultralytics import YOLO
import tempfile

# Load your 3 YOLO models
models = [
    YOLO("yolo_trained_model.pt"),
    YOLO("car_person_best.pt"),
    YOLO("license best.pt")
]

# Function to detect objects in an image
def detect_on_image(image):
    result_frame = image.copy()
    for model in models:
        results = model(result_frame)
        result_frame = results[0].plot()
    return result_frame

# Function to detect objects in a video
def detect_on_video(video):
    temp_out = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
    cap = cv2.VideoCapture(video)

    fps = cap.get(cv2.CAP_PROP_FPS)
    width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

    out = cv2.VideoWriter(temp_out.name, cv2.VideoWriter_fourcc(*'mp4v'), fps, (width, height))

    while cap.isOpened():
        ret, frame = cap.read()
        if not ret:
            break
        result_frame = frame.copy()
        for model in models:
            results = model(result_frame)
            result_frame = results[0].plot()
        out.write(result_frame)

    cap.release()
    out.release()
    return temp_out.name

# Gradio interfaces
image_interface = gr.Interface(
    fn=detect_on_image,
    inputs=gr.Image(type="numpy", label="Upload an Image"),
    outputs=gr.Image(type="numpy", label="Annotated Image"),
    title="YOLO Image Detection"
)

video_interface = gr.Interface(
    fn=detect_on_video,
    inputs=gr.Video(label="Upload a Video"),
    outputs=gr.Video(label="Processed Video"),
    title="YOLO Video Detection"
)

# Combine both interfaces into tabs
demo = gr.TabbedInterface(
    interface_list=[image_interface, video_interface],
    tab_names=["Image Detection", "Video Detection"]
)

if __name__ == "__main__":
    demo.launch()