File size: 2,732 Bytes
74d7b02
01a8440
74d7b02
 
 
bdabcee
74d7b02
 
 
 
 
 
 
 
 
 
 
e2adb40
74d7b02
 
e2adb40
 
 
 
74d7b02
 
 
 
 
 
 
 
 
 
 
 
 
e2adb40
74d7b02
 
e2adb40
74d7b02
 
 
 
 
 
 
 
 
 
 
 
e2adb40
bdabcee
74d7b02
e2adb40
74d7b02
 
 
 
 
 
 
 
 
 
 
 
e2adb40
74d7b02
bdabcee
 
e2adb40
 
4ce1e33
e2adb40
bdabcee
 
74d7b02
 
 
 
 
e2adb40
74d7b02
 
 
bdabcee
 
 
 
 
 
 
 
 
 
 
e2adb40
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import gradio as gr

import torch
from ultralyticsplus import YOLO, render_model_output

# Define the model names
model_names = [
    "yolov8n-seg.pt",
    "yolov8s-seg.pt",
    "yolov8m-seg.pt",
    "yolov8l-seg.pt",
    "yolov8x-seg.pt",
]

current_model_name = "yolov8m-seg.pt"
model = YOLO(current_model_name)


def yolov8_inference(
    image: gr.inputs.Image = None,
    model_name: gr.inputs.Dropdown = None,
    image_size: gr.inputs.Slider = 640,
    conf_threshold: gr.inputs.Slider = 0.25,
    iou_threshold: gr.inputs.Slider = 0.45,
):
    """
    YOLOv8 inference function
    Args:
        image: Input image
        model_name: Name of the model
        image_size: Image size
        conf_threshold: Confidence threshold
        iou_threshold: IOU threshold
    Returns:
        Rendered image
    """
    global model
    global current_model_name
    if model_name != current_model_name:
        model = YOLO(model_name)
        current_model_name = model_name
    model.overrides["conf"] = conf_threshold
    model.overrides["iou"] = iou_threshold
    results = model.predict(image, imgsz=image_size, return_outputs=True)
    renders = []
    for image_results in model.predict(image, imgsz=image_size, return_outputs=True):
        render = render_model_output(
            model=model, image=image, model_output=image_results
        )
        renders.append(render)

    return renders[0]


# Define the input and output components
inputs = [
    gr.Image(type="filepath", label="Input Image"),
    gr.Dropdown(
        model_names,
        value=current_model_name,
        label="Model type",
    ),
    gr.Slider(minimum=320, maximum=1280, value=640, step=32, label="Image Size"),
    gr.Slider(
        minimum=0.0, maximum=1.0, value=0.25, step=0.05, label="Confidence Threshold"
    ),
    gr.Slider(minimum=0.0, maximum=1.0, value=0.45, step=0.05, label="IOU Threshold"),
]

outputs = gr.Image(type="filepath", label="Output Image")

# Define the title and examples
title = "Ultralytics YOLOv8 Segmentation Demo"
examples = [
    ["ikun.jpg", "yolov8m-seg.pt", 640, 0.6, 0.45],
    ["people.png", "yolov8m-seg.pt", 640, 0.25, 0.45],
]

# Create the Gradio interface
demo_app = gr.Interface(
    fn=yolov8_inference,
    inputs=inputs,
    outputs=outputs,
    title=title,
    examples=examples,
    cache_examples=True,
    theme="default",
)

# Set the custom CSS style with background image
demo_app.css = """
body {
    background-image: url('backbone.jpg'); /* URL of your background image */
    background-size: cover; /* Cover the entire interface */
    background-position: center; /* Center the background image */
}
"""

# Launch the interface
demo_app.launch(debug=True, enable_queue=True)