欢迎光临
我们一直在努力

YOLO-World 零样本目标检测实战:文本类别提示与视频筛选

YOLO-World 零样本目标检测实战:文本类别提示与视频筛选


这篇教程根据我复现 YOLO-World 零样本目标检测流程时整理,重点演示如何设置文本类别提示、跑通图片推理、再把同一套模型迁移到视频帧上做目标筛选。

YOLO-World 的特点是不用重新训练就能通过文本类别完成目标检测。本文适合想快速验证开放词汇检测效果、并把它接到图片或视频流里的同学。

本文会重点跑通以下流程:

  • 安装 YOLO-World 依赖
  • 准备图片和视频示例
  • 设置文本类别提示并执行检测
  • 通过 NMS 和阈值过滤结果
  • 把同一套检测逻辑应用到视频帧并导出结果

如果你正在系统学习目标检测、实例分割、OCR、多目标跟踪或视觉大模型,建议收藏本文;配套 notebook、示例图片和运行环境说明后续会继续整理。如果环境配置卡住,可以在评论区说明具体报错。

📚 文章目录

  • YOLO-World 零样本目标检测实战:文本类别提示与视频筛选
    • ⚙️ 环境准备
    • 🖼️ 准备示例资源
    • 🔍 加载 YOLO-World 模型
    • 🏷️ 设置文本类别
    • 📷 图片目标检测
    • 🎞️ 视频检测准备
    • 📏 视频过滤与导出
    • 📌 小结
    • 📚 同系列教程汇总

⚙️ 环境准备

先检查运行环境并安装依赖。建议优先使用带 NVIDIA GPU 的环境,避免推理和训练阶段显存不足。

!nvidiasmi

import os
HOME = os.getcwd()
print(HOME)

!pip install q inferencegpu[yoloworld]==0.9.13
!pip install q supervision==0.19.0rc3

!pip install q supervision==0.19.0rc3

import cv2
import supervision as sv

from tqdm import tqdm
from inference.models import YOLOWorld

🖼️ 准备示例资源

这一段会准备图片和视频示例。实际复现时,可以直接换成你自己从数据集后台下载的资源。

# 请从数据集后台下载示例图片和视频,并放到当前目录。
# 文件名保持为 dog.jpeg 和 yellow-filling.mp4。

SOURCE_IMAGE_PATH = f"{HOME}/dog.jpeg"
SOURCE_VIDEO_PATH = f"{HOME}/yellow-filling.mp4"

🔍 加载 YOLO-World 模型

先把零样本模型跑通,确认文本类别提示和推理接口都正常。

model = YOLOWorld(model_id="yolo_world/l")

🏷️ 设置文本类别

YOLO-World 通过文本类别列表来定义要检测的目标。

classes = ["person", "backpack", "dog", "eye", "nose", "ear", "tongue"]
model.set_classes(classes)

📷 图片目标检测

先在静态图片上验证类别提示和置信度过滤。

image = cv2.imread(SOURCE_IMAGE_PATH)
results = model.infer(image)
detections = sv.Detections.from_inference(results)

BOUNDING_BOX_ANNOTATOR = sv.BoundingBoxAnnotator(thickness=2)
LABEL_ANNOTATOR = sv.LabelAnnotator(text_thickness=2, text_scale=1, text_color=sv.Color.BLACK)

annotated_image = image.copy()
annotated_image = BOUNDING_BOX_ANNOTATOR.annotate(annotated_image, detections)
annotated_image = LABEL_ANNOTATOR.annotate(annotated_image, detections)
sv.plot_image(annotated_image, (10, 10))

在这里插入图片描述

image = cv2.imread(SOURCE_IMAGE_PATH)
results = model.infer(image, confidence=0.003)
detections = sv.Detections.from_inference(results)

labels = [
f"{classes[class_id]} {confidence:0.3f}"
for class_id, confidence
in zip(detections.class_id, detections.confidence)
]

annotated_image = image.copy()
annotated_image = BOUNDING_BOX_ANNOTATOR.annotate(annotated_image, detections)
annotated_image = LABEL_ANNOTATOR.annotate(annotated_image, detections, labels=labels)
sv.plot_image(annotated_image, (10, 10))

在这里插入图片描述

image = cv2.imread(SOURCE_IMAGE_PATH)
results = model.infer(image, confidence=0.003)
detections = sv.Detections.from_inference(results).with_nms(threshold=0.1)

labels = [
f"{classes[class_id]} {confidence:0.3f}"
for class_id, confidence
in zip(detections.class_id, detections.confidence)
]

annotated_image = image.copy()
annotated_image = BOUNDING_BOX_ANNOTATOR.annotate(annotated_image, detections)
annotated_image = LABEL_ANNOTATOR.annotate(annotated_image, detections, labels=labels)
sv.plot_image(annotated_image, (10, 10))

在这里插入图片描述

🎞️ 视频检测准备

把同一套模型接到视频帧生成器上,观察连续帧中的检测效果。

generator = sv.get_video_frames_generator(SOURCE_VIDEO_PATH)
frame = next(generator)

sv.plot_image(frame, (10, 10))

在这里插入图片描述

classes = ["yellow filling"]
model.set_classes(classes)

results = model.infer(frame, confidence=0.002)
detections = sv.Detections.from_inference(results).with_nms(threshold=0.1)

annotated_image = frame.copy()
annotated_image = BOUNDING_BOX_ANNOTATOR.annotate(annotated_image, detections)
annotated_image = LABEL_ANNOTATOR.annotate(annotated_image, detections)
sv.plot_image(annotated_image, (10, 10))

在这里插入图片描述

📏 视频过滤与导出

最后对小目标或低置信度结果做过滤,再把标注后的视频写出。

video_info = sv.VideoInfo.from_video_path(SOURCE_VIDEO_PATH)
video_info

width, height = video_info.resolution_wh
frame_area = width * height
frame_area

results = model.infer(frame, confidence=0.002)
detections = sv.Detections.from_inference(results).with_nms(threshold=0.1)
detections.area

(detections.area / frame_area) < 0.10

detections = detections[(detections.area / frame_area) < 0.10]

annotated_image = frame.copy()
annotated_image = BOUNDING_BOX_ANNOTATOR.annotate(annotated_image, detections)
annotated_image = LABEL_ANNOTATOR.annotate(annotated_image, detections)
sv.plot_image(annotated_image, (10, 10))

在这里插入图片描述

TARGET_VIDEO_PATH = f"{HOME}/yellow-filling-output.mp4"

frame_generator = sv.get_video_frames_generator(SOURCE_VIDEO_PATH)
video_info = sv.VideoInfo.from_video_path(SOURCE_VIDEO_PATH)

width, height = video_info.resolution_wh
frame_area = width * height
frame_area

with sv.VideoSink(target_path=TARGET_VIDEO_PATH, video_info=video_info) as sink:
for frame in tqdm(frame_generator, total=video_info.total_frames):
results = model.infer(frame, confidence=0.002)
detections = sv.Detections.from_inference(results).with_nms(threshold=0.1)
detections = detections[(detections.area / frame_area) < 0.10]

annotated_frame = frame.copy()
annotated_frame = BOUNDING_BOX_ANNOTATOR.annotate(annotated_frame, detections)
annotated_frame = LABEL_ANNOTATOR.annotate(annotated_frame, detections)
sink.write_frame(annotated_frame)


📌 小结

YOLO-World 的复现重点不在训练,而在文本类别、置信度阈值和视频过滤策略。实际项目里,先用几张示例图验证类别提示,再接入视频流会更稳。

这一类 notebook 建议按“先环境、再数据、再单样例、最后批量推理”的顺序复现。遇到报错时,优先检查 GPU、依赖版本、数据集目录和模型权重路径。

后续我会继续按源项目顺序整理同系列中的目标检测、实例分割、OCR、多目标跟踪和视觉大模型教程。

📚 同系列教程汇总

  • Google Gemini 3.5 Flash 零样本目标检测教程:从提示词到可视化结果

  • GLM-OCR 文档识别实战教程:从验证码、公式到车牌 OCR

  • RF-DETR + ByteTrack 多目标跟踪实战教程:从命令行到 Python 视频轨迹可视化

  • SAM 3 图像分割实战教程:文本、框和点提示的多种分割方式

  • YOLO-World 零样本目标检测实战:文本类别提示与视频筛选-本文

赞(0)
未经允许不得转载:171主机测评 » YOLO-World 零样本目标检测实战:文本类别提示与视频筛选
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址