File size: 4,314 Bytes
c254ac1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
102
103
104
105
106
107
---

comments: true
description: Learn to use Gradio and Ultralytics YOLOv8 for interactive object detection. Upload images and adjust detection parameters in real-time.
keywords: Gradio, Ultralytics YOLOv8, object detection, interactive AI, Python
---


# Interactive Object Detection: Gradio & Ultralytics YOLOv8 🚀

## Introduction to Interactive Object Detection

This Gradio interface provides an easy and interactive way to perform object detection using the [Ultralytics YOLOv8](https://github.com/ultralytics/ultralytics/) model. Users can upload images and adjust parameters like confidence threshold and intersection-over-union (IoU) threshold to get real-time detection results.

## Why Use Gradio for Object Detection?

* **User-Friendly Interface:** Gradio offers a straightforward platform for users to upload images and visualize detection results without any coding requirement.
* **Real-Time Adjustments:** Parameters such as confidence and IoU thresholds can be adjusted on the fly, allowing for immediate feedback and optimization of detection results.
* **Broad Accessibility:** The Gradio web interface can be accessed by anyone, making it an excellent tool for demonstrations, educational purposes, and quick experiments.

<p align="center">
   <img width="800" alt="Gradio example screenshot" src="https://github.com/RizwanMunawar/ultralytics/assets/26833433/52ee3cd2-ac59-4c27-9084-0fd05c6c33be">
</p>

## How to Install the Gradio

```bash

pip install gradio

```

## How to Use the Interface

1. **Upload Image:** Click on 'Upload Image' to choose an image file for object detection.
2. **Adjust Parameters:**
    * **Confidence Threshold:** Slider to set the minimum confidence level for detecting objects.
    * **IoU Threshold:** Slider to set the IoU threshold for distinguishing different objects.
3. **View Results:** The processed image with detected objects and their labels will be displayed.

## Example Use Cases

* **Sample Image 1:** Bus detection with default thresholds.
* **Sample Image 2:** Detection on a sports image with default thresholds.

## Usage Example

This section provides the Python code used to create the Gradio interface with the Ultralytics YOLOv8 model. Supports classification tasks, detection tasks, segmentation tasks, and key point tasks.

```python

import PIL.Image as Image

import gradio as gr



from ultralytics import ASSETS, YOLO



model = YOLO("yolov8n.pt")





def predict_image(img, conf_threshold, iou_threshold):

    results = model.predict(

        source=img,

        conf=conf_threshold,

        iou=iou_threshold,

        show_labels=True,

        show_conf=True,

        imgsz=640,

    )



    for r in results:

        im_array = r.plot()

        im = Image.fromarray(im_array[..., ::-1])



    return im





iface = gr.Interface(

    fn=predict_image,

    inputs=[

        gr.Image(type="pil", label="Upload Image"),

        gr.Slider(minimum=0, maximum=1, value=0.25, label="Confidence threshold"),

        gr.Slider(minimum=0, maximum=1, value=0.45, label="IoU threshold")

    ],

    outputs=gr.Image(type="pil", label="Result"),

    title="Ultralytics Gradio",

    description="Upload images for inference. The Ultralytics YOLOv8n model is used by default.",

    examples=[

        [ASSETS / "bus.jpg", 0.25, 0.45],

        [ASSETS / "zidane.jpg", 0.25, 0.45],

    ]

)



if __name__ == '__main__':

    iface.launch()

```

## Parameters Explanation

| Parameter Name   | Type    | Description                                              |
|------------------|---------|----------------------------------------------------------|
| `img`            | `Image` | The image on which object detection will be performed.   |
| `conf_threshold` | `float` | Confidence threshold for detecting objects.              |
| `iou_threshold`  | `float` | Intersection-over-union threshold for object separation. |

### Gradio Interface Components

| Component    | Description                              |
|--------------|------------------------------------------|
| Image Input  | To upload the image for detection.       |
| Sliders      | To adjust confidence and IoU thresholds. |
| Image Output | To display the detection results.        |