交互式目标检测:Gradio 与 Ultralytics YOLO26 🚀#
交互式目标检测简介#
这个 Gradio 界面提供了一种简单且交互式的方式,使用 Ultralytics YOLO26 模型执行对象检测。用户可以上传图像并调整置信度阈值和交并比 (IoU) 阈值等参数,以获得实时的检测结果。
Watch: Deploy Ultralytics YOLO26 in a Browser with Gradio | Complete Tutorial | Vision AI 🌐
为什么要使用 Gradio 进行目标检测?#
- 用户友好界面: Gradio 为用户提供了一个直接的平台,无需任何编码即可上传图像并可视化检测结果。
- 实时调整: 可以动态调整置信度和 IoU 阈值等参数,从而实现即时反馈并优化检测结果。
- 广泛的易用性: 任何人都可以访问 Gradio Web 界面,使其成为演示、教育目的和快速实验的绝佳工具。
如何安装 Gradio#
pip install gradio如何使用该界面#
- 上传图像: 点击“上传图像”以选择用于目标检测的图像文件。
- 调整参数:
- 置信度阈值: 用于设置检测目标的最低置信度水平的滑块。
- IoU 阈值: 用于设置区分不同目标的 IoU 阈值的滑块。
- 查看结果: 将显示带有已检测目标及其标签的处理后的图像。
示例用例#
- 示例图像 1: 使用默认阈值进行公交车检测。
- 示例图像 2: 使用默认阈值在体育图像上进行检测。
使用示例#
本节提供了用于创建 Ultralytics YOLO26 目标检测 Gradio 界面的 Python 代码。通过加载相应的模型检查点,该示例可以适用于分类、实例分割、姿态估计和其他 YOLO 任务。
import gradio as gr
from PIL import 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 阈值。 |
| 图像输出 | 用于显示检测结果。 |
常见问题解答#
若要将 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()- 安装 Gradio: 使用命令
将 Gradio 用于 Ultralytics YOLO26 目标检测具有以下几个好处:
- 用户友好界面: Gradio 为用户提供了一个直观的界面,无需任何编程工作即可上传图像并可视化检测结果。
- 实时调整: 你可以动态调整检测参数(如置信度和 IoU 阈值)并立即看到效果。
- 易用性: Web 界面任何人都可以访问,使其在快速实验、教育目的和演示中非常有用。
有关更多详细信息,你可以阅读这篇关于放射学中 AI 的博客文章,其中展示了类似的可视化交互技术。
将 Ultralytics YOLO26 与 Gradio 结合使用的实际应用包括:
- 实时目标检测演示: 非常适合展示目标检测如何实时运行。
- 教育工具: 在学术环境中用于教授目标检测和计算机视觉概念非常有用。
- 原型开发: 高效地快速开发和测试原型目标检测应用程序。
- 社区与协作: 方便与社区分享模型以获取反馈和进行协作。
有关类似用例的示例,请查看关于动物行为监测的 Ultralytics 博客,该博客展示了交互式可视化如何增强野生动物保护工作。