交互式目标检测:Gradio 与 Ultralytics YOLO26 🚀
交互式目标检测简介
This Gradio interface provides an easy and interactive way to perform object detection using the Ultralytics YOLO26 model. Users can upload images and adjust parameters like confidence threshold and intersection-over-union (IoU) threshold to get real-time detection results.
Watch: Gradio Integration with Ultralytics YOLO26
为什么要使用 Gradio 进行目标检测?
- 用户友好的界面: Gradio 提供了一个直观的平台,用户无需任何编程即可上传图像并可视化检测结果。
- 实时调整: 可以动态调整置信度和 IoU 阈值等参数,从而实现对检测结果的即时反馈和优化。
- 广泛的可访问性: 任何人都可以访问 Gradio Web 界面,使其成为演示、教学和快速实验的绝佳工具。
如何安装 Gradio
pip install gradio如何使用该界面
- 上传图像: 点击“上传图像”以选择用于目标检测的图像文件。
- 调整参数:
- 置信度阈值: 通过滑块设置检测对象的最低置信度水平。
- IoU 阈值: 通过滑块设置用于区分不同对象的 IoU 阈值。
- 查看结果: 将显示带有检测到对象及其标签的处理后图像。
示例用例
- 示例图像 1: 使用默认阈值进行公交车检测。
- 示例图像 2: 使用默认阈值对体育图像进行检测。
使用示例
本节提供了使用 Ultralytics YOLO26 模型创建 Gradio 界面的 Python 代码。该代码支持分类任务、检测任务、分割任务和关键点任务。
import gradio as gr
import PIL.Image as Image
from ultralytics import ASSETS, YOLO
model = YOLO("yolo26n.pt")
def predict_image(img, conf_threshold, iou_threshold):
"""Predicts objects in an image using a YOLO26 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 YOLO26n 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 | 用于对象分离的交并比阈值。 |
Gradio 界面组件
| 组件 | 描述 |
|---|---|
| 图像输入 | 用于上传检测图像。 |
| 滑块 | 用于调整置信度和 IoU 阈值。 |
| 图像输出 | 用于显示检测结果。 |
常见问题 (FAQ)
如何将 Gradio 与 Ultralytics YOLO26 结合用于目标检测?
要将 Gradio 与 Ultralytics YOLO26 结合用于目标检测,你可以按照以下步骤操作:
- 安装 Gradio: 使用命令
pip install gradio。 - Create Interface: Write a Python script to initialize the Gradio interface. You can refer to the provided code example in the documentation for details.
- 上传并调整: 上传你的图像,并在 Gradio 界面上调整置信度和 IoU 阈值,以获得实时目标检测结果。
这是一个供参考的最小代码片段:
import gradio as gr
from ultralytics import YOLO
model = YOLO("yolo26n.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 YOLO26",
description="Upload images for YOLO26 object detection.",
)
iface.launch()使用 Gradio 进行 Ultralytics YOLO26 目标检测有什么好处?
将 Gradio 用于 Ultralytics YOLO26 目标检测具有以下优点:
- 用户友好的界面: Gradio 提供了一个直观的界面,用户无需任何编程工作即可上传图像并可视化检测结果。
- 实时调整: 你可以动态调整置信度和 IoU 阈值等检测参数,并立即查看效果。
- 可访问性: 该 Web 界面任何人都可以访问,非常适合快速实验、教学和演示。
有关更多详细信息,你可以阅读这篇 关于放射学 AI 的博客文章,其中展示了类似的交互式可视化技术。
我可以将 Gradio 和 Ultralytics YOLO26 一起用于教学目的吗?
是的,Gradio 和 Ultralytics YOLO26 可以有效地结合用于教学目的。Gradio 直观的 Web 界面使学生和教育工作者能够轻松地与 Ultralytics YOLO26 等最先进的 深度学习 模型进行交互,而无需高级编程技能。这种设置非常适合演示目标检测和 计算机视觉 中的关键概念,因为 Gradio 提供了即时的视觉反馈,有助于理解不同参数对检测性能的影响。
如何在 Gradio 界面中调整 YOLO26 的置信度和 IoU 阈值?
在 YOLO26 的 Gradio 界面中,你可以使用提供的滑块来调整置信度和 IoU 阈值。这些阈值有助于控制预测 准确度 和对象分离:
- 置信度阈值: 确定检测对象的最低置信度水平。滑动以增加或减少所需的置信度。
- IoU 阈值: 设置用于区分重叠对象的交并比阈值。调整此值以细化对象分离效果。
有关这些参数的更多信息,请访问 参数说明部分。
使用 Ultralytics YOLO26 结合 Gradio 有哪些实际应用?
将 Ultralytics YOLO26 与 Gradio 结合的实际应用包括:
- 实时目标检测演示: 非常适合展示目标检测如何实时工作。
- 教育工具: 在学术环境中用于教授目标检测和计算机视觉概念非常有用。
- 原型开发: 可高效地快速开发和测试原型目标检测应用程序。
- 社区与协作: 方便与社区分享模型以获得反馈和协作。
有关类似用例的示例,请查看 关于动物行为监测的 Ultralytics 博客,该博客展示了交互式可视化如何增强野生动物保护工作。