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