交互式目标检测: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:**使用默认阈值检测体育图像中的对象。
使用示例#
本节提供了用于创建 Gradio 界面以执行 Ultralytics YOLO26 目标检测的 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 关于动物行为监测的博客文章,其中展示了交互式可视化如何助力野生动物保护工作。