跳至内容

Interactive Object Detection: Gradio & Ultralytics YOLO11 🚀

交互式物体检测简介

This Gradio interface provides an easy and interactive way to perform object detection using the Ultralytics YOLO11 model. Users can upload images and adjust parameters like confidence threshold and intersection-over-union (IoU) threshold to get real-time detection results.



观看: Gradio Integration with Ultralytics YOLO11

为什么使用 Gradio 进行物体检测?

  • 用户界面友好:Gradio 为用户提供了一个简单明了的平台,无需任何编码要求即可上传图像并可视化检测结果。
  • 实时调整:可对置信度和 IoU 阈值等参数进行实时调整,从而获得即时反馈并优化检测结果。
  • 广泛的可访问性:任何人都可以访问 Gradio 网页界面,使其成为演示、教育和快速实验的绝佳工具。

Gradio 示例截图

如何安装 Gradio

pip install gradio

如何使用界面

  1. 上传图像:点击 "上传图像",选择图像文件进行对象检测。
  2. 调整参数:
    • 置信度阈值:滑块,用于设置检测物体的最低置信度。
    • IoU 门限:滑块用于设置用于区分不同物体的 IoU 门限。
  3. 查看结果:将显示经过处理的图像和检测到的物体及其标签。

使用实例

  • 示例图像 1:使用默认阈值进行总线检测。
  • 示例图像 2:使用默认阈值对运动图像进行检测。

使用示例

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

import gradio as gr
import PIL.Image as Image

from ultralytics import ASSETS, YOLO

model = YOLO("yolo11n.pt")


def predict_image(img, conf_threshold, iou_threshold):
    """Predicts objects in an image using a YOLO11 model with adjustable confidence and IOU thresholds."""
    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 YOLO11n 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()

参数说明

参数名称类型说明
imgImage进行对象检测的图像。
conf_thresholdfloat检测物体的可信度阈值。
iou_thresholdfloat用于物体分离的 "相交-重合阈值"。

Gradio 界面组件

组件说明
图像输入上传图像进行检测。
滑块调整置信度和 IoU 临界值。
图像输出显示检测结果。

常见问题

How do I use Gradio with Ultralytics YOLO11 for object detection?

To use Gradio with Ultralytics YOLO11 for object detection, you can follow these steps:

  1. 安装 Gradio: 使用命令 pip install gradio.
  2. 创建界面:编写Python 脚本初始化 Gradio 界面。详情可参考文档中提供的代码示例。
  3. 上传和调整:上传图像并在 Gradio 界面上调整置信度和 IoU 阈值,以获得实时对象检测结果。

下面是一个最基本的代码片段,供参考:

import gradio as gr

from ultralytics import YOLO

model = YOLO("yolo11n.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,
    )
    return results[0].plot() if results else None


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 YOLO11",
    description="Upload images for YOLO11 object detection.",
)
iface.launch()

What are the benefits of using Gradio for Ultralytics YOLO11 object detection?

Using Gradio for Ultralytics YOLO11 object detection offers several benefits:

  • 用户友好界面:Gradio 为用户提供了直观的界面,用户无需编写任何代码即可上传图像并查看检测结果。
  • 实时调整:您可以动态调整检测参数,如置信度和 IoU 阈值,并立即看到效果。
  • 可访问性任何人都可以访问网络界面,因此对于快速实验、教育目的和演示非常有用。

更多详情,请阅读本篇博文

Can I use Gradio and Ultralytics YOLO11 together for educational purposes?

Yes, Gradio and Ultralytics YOLO11 can be utilized together for educational purposes effectively. Gradio's intuitive web interface makes it easy for students and educators to interact with state-of-the-art deep learning models like Ultralytics YOLO11 without needing advanced programming skills. This setup is ideal for demonstrating key concepts in object detection and computer vision, as Gradio provides immediate visual feedback which helps in understanding the impact of different parameters on the detection performance.

How do I adjust the confidence and IoU thresholds in the Gradio interface for YOLO11?

In the Gradio interface for YOLO11, you can adjust the confidence and IoU thresholds using the sliders provided. These thresholds help control the prediction accuracy and object separation:

  • 置信度阈值:确定检测物体的最低置信度。滑动可提高或降低所需的置信度。
  • IoU 阈值:设置用于区分重叠对象的交叉-重合阈值。调整此值可细化对象分离。

有关这些参数的更多信息,请访问参数解释部分

What are some practical applications of using Ultralytics YOLO11 with Gradio?

Practical applications of combining Ultralytics YOLO11 with Gradio include:

  • 实时物体检测演示:是实时展示物体检测工作原理的理想之选。
  • 教育工具:适用于教学环境,教授物体检测和计算机视觉概念。
  • 原型开发:可快速高效地开发和测试物体检测应用原型。
  • 社区与合作:方便与社区共享模型,以获得反馈和开展合作。

有关类似用例,请访问Ultralytics 博客

Providing this information within the documentation will help in enhancing the usability and accessibility of Ultralytics YOLO11, making it more approachable for users at all levels of expertise.

📅 Created 8 months ago ✏️ Updated 20 days ago

评论