交互式物体检测:Gradio &Ultralytics YOLOv8 🚀
交互式物体检测简介
该 Gradio 界面提供了一种简便的交互式方法,可使用模型进行对象检测。 Ultralytics YOLOv8模型进行物体检测。用户可以上传图像,调整置信度阈值和交集-重合(IoU)阈值等参数,以获得实时检测结果。
为什么使用 Gradio 进行物体检测?
- 用户界面友好:Gradio 为用户提供了一个简单明了的平台,无需任何编码要求即可上传图像并可视化检测结果。
- 实时调整:可对置信度和 IoU 阈值等参数进行实时调整,从而获得即时反馈并优化检测结果。
- 广泛的可访问性:任何人都可以访问 Gradio 网页界面,使其成为演示、教育和快速实验的绝佳工具。
如何安装 Gradio
如何使用界面
- 上传图像:点击 "上传图像",选择图像文件进行对象检测。
- 调整参数:
- 置信度阈值:滑块,用于设置检测物体的最低置信度。
- IoU 门限:滑块用于设置用于区分不同物体的 IoU 门限。
- 查看结果:将显示经过处理的图像和检测到的物体及其标签。
使用实例
- 示例图像 1:使用默认阈值进行总线检测。
- 示例图像 2:使用默认阈值对运动图像进行检测。
使用示例
本节提供Python 代码,用于创建 Gradio 与Ultralytics YOLOv8 模型的接口。支持分类任务、检测任务、分割任务和关键点任务。
import gradio as gr
import PIL.Image as Image
from ultralytics import ASSETS, YOLO
model = YOLO("yolov8n.pt")
def predict_image(img, conf_threshold, iou_threshold):
"""Predicts and plots labeled objects in an image using YOLOv8 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 YOLOv8n 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 临界值。 |
图像输出 | 显示检测结果。 |