首页
/ 使用Supervision实现目标检测与标注全流程指南

使用Supervision实现目标检测与标注全流程指南

2025-07-05 05:15:23作者:房伟宁

前言

在计算机视觉领域,目标检测和实例分割是两项基础且重要的任务。本文将详细介绍如何使用Supervision库完成从模型推理到结果标注的完整流程。Supervision作为一个强大的工具库,能够与多种主流计算机视觉框架无缝集成,大大简化了视觉任务的后处理工作。

环境准备

在开始之前,请确保已安装以下Python库:

  • OpenCV (cv2)
  • Supervision
  • 任一推理框架(Inference/Ultralytics/Transformers)

模型推理阶段

Supervision支持与多种流行框架的集成,下面分别介绍三种典型的使用场景:

1. 使用Inference框架

import cv2
from inference import get_model

model = get_model(model_id="yolov8n-640")
image = cv2.imread(<SOURCE_IMAGE_PATH>)
results = model.infer(image)[0]

2. 使用Ultralytics YOLO

import cv2
from ultralytics import YOLO

model = YOLO("yolov8n.pt")
image = cv2.imread(<SOURCE_IMAGE_PATH>)
results = model(image)[0]

3. 使用HuggingFace Transformers

import torch
from PIL import Image
from transformers import DetrImageProcessor, DetrForObjectDetection

processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50")
model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50")

image = Image.open(<SOURCE_IMAGE_PATH>)
inputs = processor(images=image, return_tensors="pt")

with torch.no_grad():
    outputs = model(**inputs)

width, height = image.size
target_size = torch.tensor([[height, width]])
results = processor.post_process_object_detection(
    outputs=outputs, target_sizes=target_size)[0]

结果转换与处理

获得模型输出后,需要将其转换为Supervision的标准格式:

Inference结果转换

import supervision as sv
detections = sv.Detections.from_inference(results)

Ultralytics结果转换

import supervision as sv
detections = sv.Detections.from_ultralytics(results)

Transformers结果转换

import supervision as sv
detections = sv.Detections.from_transformers(
    transformers_results=results,
    id2label=model.config.id2label)

Supervision还支持其他框架的结果转换,包括:

  • Deepsparse
  • Detectron2
  • MMDetection
  • Segment Anything Model
  • YOLO-NAS等

标注可视化

Supervision提供了多种标注工具,可根据需求选择:

1. 边界框标注

box_annotator = sv.BoxAnnotator()
annotated_image = box_annotator.annotate(
    scene=image, detections=detections)

2. 标签标注

label_annotator = sv.LabelAnnotator()
annotated_image = label_annotator.annotate(
    scene=annotated_image, detections=detections)

3. 自定义标签内容

labels = [
    f"{class_name} {confidence:.2f}"
    for class_name, confidence
    in zip(detections['class_name'], detections.confidence)
]
annotated_image = label_annotator.annotate(
    scene=annotated_image, detections=detections, labels=labels)

实例分割标注

对于分割任务,可以使用MaskAnnotator:

mask_annotator = sv.MaskAnnotator()
label_annotator = sv.LabelAnnotator(text_position=sv.Position.CENTER_OF_MASS)

annotated_image = mask_annotator.annotate(
    scene=image, detections=detections)
annotated_image = label_annotator.annotate(
    scene=annotated_image, detections=detections)

最佳实践建议

  1. 性能优化:对于实时应用,建议使用YOLO系列等轻量级模型
  2. 标注样式:可通过调整annotator参数自定义框线颜色、粗细、标签字体等
  3. 结果过滤:在转换detections时可设置confidence阈值过滤低质量检测
  4. 批量处理:Supervision支持对视频流或图像序列进行批处理

结语

通过Supervision库,我们能够以简洁的代码实现从模型推理到结果可视化的完整流程。无论是目标检测还是实例分割任务,Supervision都提供了丰富的工具集来满足不同场景的需求。希望本指南能帮助您快速上手并应用于实际项目中。