交互式目标检测:Gradio & Ultralytics YOLO11 🚀
交互式对象检测简介
此 Gradio 界面提供了一种使用 Ultralytics YOLO11 模型执行目标检测的简单交互方式。用户可以上传图像并调整置信度阈值和 intersection-over-union (IoU) 阈值等参数,以获得实时检测结果。
观看: Gradio 与 Ultralytics YOLO11 的集成
为什么要将 Gradio 用于目标检测?
- 用户友好的界面: Gradio 提供了一个简单的平台,用户无需任何编码即可上传图像并可视化检测结果。
- 实时调整: 可以在运行时调整置信度和 IoU 阈值等参数,从而可以立即获得反馈并优化检测结果。
- 广泛的易用性: 任何人都可以访问 Gradio Web 界面,使其成为演示、教育目的和快速实验的绝佳工具。
如何安装 Gradio
pip install gradio
如何使用界面
- 上传图像: 点击“上传图像”以选择用于对象检测的图像文件。
- 调整参数:
- 置信度阈值: 用于设置检测对象的最小置信度级别的滑块。
- IoU 阈值: 用于设置 IoU 阈值的滑块,以区分不同的对象。
- 查看结果: 将显示带有检测到的对象及其标签的已处理图像。
用例示例
- 示例图像 1: 使用默认阈值检测公交车。
- 示例图像 2: 使用默认阈值检测运动图像。
使用示例
本节提供用于创建带有 Ultralytics YOLO11 模型的 Gradio 界面的 python 代码。该代码支持分类任务、检测任务、分割任务和关键点任务。
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()
参数说明
参数名称 | 类型 | 描述 |
---|---|---|
img |
Image |
将要执行对象检测的图像。 |
conf_threshold |
float |
用于检测物体的置信度阈值。 |
iou_threshold |
float |
用于对象分离的 Intersection-over-union 阈值。 |
Gradio 界面组件
组件 | 描述 |
---|---|
图像输入 | 要上传图像以进行检测。 |
滑块 | 要调整置信度和 IoU 阈值。 |
图像输出 | 显示检测结果。 |
常见问题
如何将 Gradio 与 Ultralytics YOLO11 结合使用以进行目标检测?
要将 Gradio 与 Ultralytics YOLO11 结合使用以进行对象检测,您可以按照以下步骤操作:
- 安装 Gradio: 使用以下命令
pip install gradio
. - 创建界面: 编写 python 脚本以初始化 Gradio 界面。您可以参考文档中提供的代码示例了解详细信息。
- 上传和调整: 上传您的图像,并在 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()
使用 Gradio 进行 Ultralytics YOLO11 对象检测有哪些好处?
使用 Gradio 进行 Ultralytics YOLO11 目标检测有以下几个优点:
- 用户友好的界面: Gradio 提供了一个直观的界面,用户无需任何编码工作即可上传图像并可视化检测结果。
- 实时调整: 您可以动态调整检测参数(如置信度和 IoU 阈值),并立即看到效果。
- 可访问性: 任何人都可访问 Web 界面,使其可用于快速实验、教育目的和演示。
有关更多详细信息,您可以阅读这篇关于放射学中的 AI 的博客文章,其中展示了类似的交互式可视化技术。
我可以将Gradio和Ultralytics YOLO11一起用于教育目的吗?
是的,Gradio 和 Ultralytics YOLO11 可以有效地结合用于教育目的。Gradio 直观的 Web 界面使学生和教育工作者可以轻松地与 Ultralytics YOLO11 等最先进的 深度学习 模型进行交互,而无需高级编程技能。此设置非常适合演示对象检测和 计算机视觉 中的关键概念,因为 Gradio 提供了即时的视觉反馈,有助于理解不同参数对检测性能的影响。
如何在 YOLO11 的 Gradio 界面中调整置信度和 IoU 阈值?
在 YOLO11 的 Gradio 界面中,您可以使用提供的滑块调整置信度和 IoU 阈值。这些阈值有助于控制预测精度和对象分离:
- 置信度阈值: 确定检测对象的最小置信度级别。 滑动以增加或减少所需的置信度。
- IoU 阈值: 设置 intersection-over-union 阈值以区分重叠对象。调整此值以优化对象分离。
有关这些参数的更多信息,请访问参数解释部分。
将 Ultralytics YOLO11 与 Gradio 结合使用有哪些实际应用?
将 Ultralytics YOLO11 与 Gradio 结合的实际应用包括:
- 实时目标检测演示: 非常适合展示目标检测如何在实时环境中工作。
- 教育工具: 在学术环境中用于教授目标检测和计算机视觉概念非常有用。
- 原型开发: 能够快速开发和测试原型对象检测应用程序。
- 社区与协作 (Community and Collaborations): 方便与社区共享模型,以获得反馈和协作。
如需类似用例的示例,请查看 Ultralytics 关于动物行为监测的博客,其中展示了交互式可视化如何加强野生动物保护工作。