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

发布时间:2026/7/15 0:32:28
YOLO-World 零样本目标检测实战:文本类别提示与视频筛选 YOLO-World 零样本目标检测实战文本类别提示与视频筛选这篇教程根据我复现 YOLO-World 零样本目标检测流程时整理重点演示如何设置文本类别提示、跑通图片推理、再把同一套模型迁移到视频帧上做目标筛选。YOLO-World 的特点是不用重新训练就能通过文本类别完成目标检测。本文适合想快速验证开放词汇检测效果、并把它接到图片或视频流里的同学。本文会重点跑通以下流程安装 YOLO-World 依赖准备图片和视频示例设置文本类别提示并执行检测通过 NMS 和阈值过滤结果把同一套检测逻辑应用到视频帧并导出结果如果你正在系统学习目标检测、实例分割、OCR、多目标跟踪或视觉大模型建议收藏本文配套 notebook、示例图片和运行环境说明后续会继续整理。如果环境配置卡住可以在评论区说明具体报错。 文章目录YOLO-World 零样本目标检测实战文本类别提示与视频筛选⚙️ 环境准备️ 准备示例资源 加载 YOLO-World 模型️ 设置文本类别 图片目标检测️ 视频检测准备 视频过滤与导出 小结 同系列教程汇总⚙️ 环境准备先检查运行环境并安装依赖。建议优先使用带 NVIDIA GPU 的环境避免推理和训练阶段显存不足。!nvidia-smiimportos HOMEos.getcwd()print(HOME)!pip install-q inference-gpu[yolo-world]0.9.13!pip install-q supervision0.19.0rc3!pip install-q supervision0.19.0rc3importcv2importsupervisionassvfromtqdmimporttqdmfrominference.modelsimportYOLOWorld️ 准备示例资源这一段会准备图片和视频示例。实际复现时可以直接换成你自己从数据集后台下载的资源。# 请从数据集后台下载示例图片和视频并放到当前目录。# 文件名保持为 dog.jpeg 和 yellow-filling.mp4。SOURCE_IMAGE_PATHf{HOME}/dog.jpegSOURCE_VIDEO_PATHf{HOME}/yellow-filling.mp4 加载 YOLO-World 模型先把零样本模型跑通确认文本类别提示和推理接口都正常。modelYOLOWorld(model_idyolo_world/l)️ 设置文本类别YOLO-World 通过文本类别列表来定义要检测的目标。classes[person,backpack,dog,eye,nose,ear,tongue]model.set_classes(classes) 图片目标检测先在静态图片上验证类别提示和置信度过滤。imagecv2.imread(SOURCE_IMAGE_PATH)resultsmodel.infer(image)detectionssv.Detections.from_inference(results)BOUNDING_BOX_ANNOTATORsv.BoundingBoxAnnotator(thickness2)LABEL_ANNOTATORsv.LabelAnnotator(text_thickness2,text_scale1,text_colorsv.Color.BLACK)annotated_imageimage.copy()annotated_imageBOUNDING_BOX_ANNOTATOR.annotate(annotated_image,detections)annotated_imageLABEL_ANNOTATOR.annotate(annotated_image,detections)sv.plot_image(annotated_image,(10,10))imagecv2.imread(SOURCE_IMAGE_PATH)resultsmodel.infer(image,confidence0.003)detectionssv.Detections.from_inference(results)labels[f{classes[class_id]}{confidence:0.3f}forclass_id,confidenceinzip(detections.class_id,detections.confidence)]annotated_imageimage.copy()annotated_imageBOUNDING_BOX_ANNOTATOR.annotate(annotated_image,detections)annotated_imageLABEL_ANNOTATOR.annotate(annotated_image,detections,labelslabels)sv.plot_image(annotated_image,(10,10))imagecv2.imread(SOURCE_IMAGE_PATH)resultsmodel.infer(image,confidence0.003)detectionssv.Detections.from_inference(results).with_nms(threshold0.1)labels[f{classes[class_id]}{confidence:0.3f}forclass_id,confidenceinzip(detections.class_id,detections.confidence)]annotated_imageimage.copy()annotated_imageBOUNDING_BOX_ANNOTATOR.annotate(annotated_image,detections)annotated_imageLABEL_ANNOTATOR.annotate(annotated_image,detections,labelslabels)sv.plot_image(annotated_image,(10,10))️ 视频检测准备把同一套模型接到视频帧生成器上观察连续帧中的检测效果。generatorsv.get_video_frames_generator(SOURCE_VIDEO_PATH)framenext(generator)sv.plot_image(frame,(10,10))classes[yellow filling]model.set_classes(classes)resultsmodel.infer(frame,confidence0.002)detectionssv.Detections.from_inference(results).with_nms(threshold0.1)annotated_imageframe.copy()annotated_imageBOUNDING_BOX_ANNOTATOR.annotate(annotated_image,detections)annotated_imageLABEL_ANNOTATOR.annotate(annotated_image,detections)sv.plot_image(annotated_image,(10,10)) 视频过滤与导出最后对小目标或低置信度结果做过滤再把标注后的视频写出。video_infosv.VideoInfo.from_video_path(SOURCE_VIDEO_PATH)video_infowidth,heightvideo_info.resolution_wh frame_areawidth*height frame_arearesultsmodel.infer(frame,confidence0.002)detectionssv.Detections.from_inference(results).with_nms(threshold0.1)detections.area(detections.area/frame_area)0.10detectionsdetections[(detections.area/frame_area)0.10]annotated_imageframe.copy()annotated_imageBOUNDING_BOX_ANNOTATOR.annotate(annotated_image,detections)annotated_imageLABEL_ANNOTATOR.annotate(annotated_image,detections)sv.plot_image(annotated_image,(10,10))TARGET_VIDEO_PATHf{HOME}/yellow-filling-output.mp4frame_generatorsv.get_video_frames_generator(SOURCE_VIDEO_PATH)video_infosv.VideoInfo.from_video_path(SOURCE_VIDEO_PATH)width,heightvideo_info.resolution_wh frame_areawidth*height frame_areawithsv.VideoSink(target_pathTARGET_VIDEO_PATH,video_infovideo_info)assink:forframeintqdm(frame_generator,totalvideo_info.total_frames):resultsmodel.infer(frame,confidence0.002)detectionssv.Detections.from_inference(results).with_nms(threshold0.1)detectionsdetections[(detections.area/frame_area)0.10]annotated_frameframe.copy()annotated_frameBOUNDING_BOX_ANNOTATOR.annotate(annotated_frame,detections)annotated_frameLABEL_ANNOTATOR.annotate(annotated_frame,detections)sink.write_frame(annotated_frame) 小结YOLO-World 的复现重点不在训练而在文本类别、置信度阈值和视频过滤策略。实际项目里先用几张示例图验证类别提示再接入视频流会更稳。这一类 notebook 建议按“先环境、再数据、再单样例、最后批量推理”的顺序复现。遇到报错时优先检查 GPU、依赖版本、数据集目录和模型权重路径。后续我会继续按源项目顺序整理同系列中的目标检测、实例分割、OCR、多目标跟踪和视觉大模型教程。 同系列教程汇总Google Gemini 3.5 Flash 零样本目标检测教程从提示词到可视化结果GLM-OCR 文档识别实战教程从验证码、公式到车牌 OCRRF-DETR ByteTrack 多目标跟踪实战教程从命令行到 Python 视频轨迹可视化SAM 3 图像分割实战教程文本、框和点提示的多种分割方式YOLO-World 零样本目标检测实战文本类别提示与视频筛选-本文