Note
本文档由 WeHub 基于上游 README 翻译整理,属于社区翻译,非官方中文文档。
English · 原始项目 · 上游 README
原作者、版权与许可证归属以原始项目及本仓库 LICENSE 文件为准。
我们是你在计算机视觉(computer vision)领域的必备工具包。 从数据加载到实时区域计数,我们提供各类基础模块,让你可以专注于围绕模型构建应用。🤝
在 Python>=3.10 环境中,使用 pip 安装 supervision 包。
pip install supervision
如需了解 conda、mamba 及从源码安装的更多内容,请参阅我们的指南.
Supervision 在设计上是模型无关(model agnostic)的。你可以直接接入任意分类、检测或分割模型。为方便使用,我们为 Ultralytics、Transformers、MMDetection、Inference 等最流行的库创建了连接器。其他集成方式(如 rfdetr)已能直接返回 sv.Detections。
使用 pip install pillow rfdetr 安装本示例所需的可选依赖。
import supervision as sv
from PIL import Image
from rfdetr import RFDETRSmall
image = Image.open("path/to/image.jpg")
model = RFDETRSmall()
detections = model.predict(image, threshold=0.5)
len(detections)
# 5
inference
使用 Inference 运行需要 Roboflow API KEY.
import supervision as sv
from PIL import Image
from inference import get_model
image = Image.open("path/to/image.jpg")
model = get_model(model_id="rfdetr-small", api_key="ROBOFLOW_API_KEY")
result = model.infer(image)[0]
detections = sv.Detections.from_inference(result)
len(detections)
# 5
Supervision 提供大量高度可自定义的标注器,,可让你为具体场景组合出完美的可视化效果。
import cv2
import supervision as sv
image = cv2.imread("path/to/image.jpg")
# Assuming detections are obtained from a model
detections = sv.Detections(...)
box_annotator = sv.BoxAnnotator()
annotated_frame = box_annotator.annotate(scene=image.copy(), detections=detections)
https://github.com/roboflow/supervision/assets/26109316/691e219c-0565-4403-9218-ab5644f39bce
Supervision 提供一组工具函数,支持以支持的格式之一加载、拆分、合并和保存数据集。
import supervision as sv
from roboflow import Roboflow
project = Roboflow().workspace("WORKSPACE_ID").project("PROJECT_ID")
dataset = project.version("PROJECT_VERSION").download("coco")
ds = sv.DetectionDataset.from_coco(
images_directory_path=f"{dataset.location}/train",
annotations_path=f"{dataset.location}/train/_annotations.coco.json",
)
path, image, annotation = ds[0]
# loads image on demand
for path, image, annotation in ds:
# loads image on demand
pass
load
dataset = sv.DetectionDataset.from_yolo(
images_directory_path=...,
annotations_directory_path=...,
data_yaml_path=...,
)
dataset = sv.DetectionDataset.from_pascal_voc(
images_directory_path=...,
annotations_directory_path=...,
)
dataset = sv.DetectionDataset.from_coco(
images_directory_path=...,
annotations_path=...,
)
split
train_dataset, test_dataset = dataset.split(split_ratio=0.7)
test_dataset, valid_dataset = test_dataset.split(split_ratio=0.5)
len(train_dataset), len(test_dataset), len(valid_dataset)
# (700, 150, 150)
merge
ds_1 = sv.DetectionDataset(...)
len(ds_1)
# 100
ds_1.classes
# ['dog', 'person']
ds_2 = sv.DetectionDataset(...)
len(ds_2)
# 200
ds_2.classes
# ['cat']
ds_merged = sv.DetectionDataset.merge([ds_1, ds_2])
len(ds_merged)
# 300
ds_merged.classes
# ['cat', 'dog', 'person']
save
dataset.as_yolo(
images_directory_path=...,
annotations_directory_path=...,
data_yaml_path=...,
)
dataset.as_pascal_voc(
images_directory_path=...,
annotations_directory_path=...,
)
dataset.as_coco(
images_directory_path=...,
annotations_path=...,
)
convert
sv.DetectionDataset.from_yolo(
images_directory_path=...,
annotations_directory_path=...,
data_yaml_path=...,
).as_pascal_voc(
images_directory_path=...,
annotations_directory_path=...,
)
想了解如何使用 Supervision?可浏览我们的操作指南,、端到端示例、速查表, 和 cookbook!
Dwell Time Analysis with Computer Vision | Real-Time Stream Processing
你是否使用 supervision 构建了很酷的项目?告诉我们!
https://github.com/roboflow/supervision/assets/26109316/c9436828-9fbf-4c25-ae8c-60e9c81b3900
https://github.com/roboflow/supervision/assets/26109316/3ac6982f-4943-4108-9b7f-51787ef1a69f
访问我们的文档 页面,了解 supervision 如何帮助你更快、更可靠地构建计算机视觉应用。
我们欢迎你的反馈!请查阅我们的贡献指南开始参与。感谢 🙏 所有贡献者!