Supervision: Computer Vision Toolkit by Roboflow
In computer vision projects, after a model detects objects, there is still much to do: drawing bounding boxes, filtering detections, counting objects, and tracking movement. All of this requires substantial and repetitive code if written from scratch.
Supervision by Roboflow is a Python toolkit that simplifies all post-detection tasks. With a consistent API, Supervision works with various detection models like YOLO, Detectron2, and SAM, allowing you to focus on application logic rather than boilerplate code.What Is Supervision?
Supervision is an open-source Python library by Roboflow that simplifies computer vision workflows. Its key features include:
- Detections Object: Universal representation for detection results from various models
- Annotators: Various visual annotator types (bounding box, mask, label, trace, halo)
- Filtering: Filter detections by confidence, class, zone, and other criteria
- Polygon Zones: Define polygon zones for counting and area analysis
- Line Counters: Count objects crossing virtual lines
- Object Tracking: ByteTrack integration for cross-frame object tracking
- Video Processing: Utilities for frame-by-frame video processing
- FPS Monitor: Real-time performance monitoring
Installation
Basic Installation
pip install supervision
Installation with Model Dependencies
# With YOLO (Ultralytics)
pip install supervision ultralytics
With Detectron2
pip install supervision detectron2
With SAM (Segment Anything)
pip install supervision segment-anything
Verify Installation
import supervision as sv
print(f"Supervision version: {sv.version}")
Detections Object
sv.Detections is the core object in Supervision that universally represents detection results.
Creating Detections from Various Models
import supervision as sv
import numpy as np
From Ultralytics YOLO
from ultralytics import YOLO
model = YOLO("yolov8n.pt")
results = model("image.jpg")
detections = sv.Detections.fromultralytics(results[0])
print(f"Number of detections: {len(detections)}")
print(f"Bounding boxes: {detections.xyxy}")
print(f"Confidence: {detections.confidence}")
print(f"Class IDs: {detections.classid}")
From Detectron2
from detectron2.engine import DefaultPredictor
from detectron2.config import getcfg
cfg = getcfg()
cfg.mergefromfile("config.yaml")
predictor = DefaultPredictor(cfg)
outputs = predictor(image)
detections = sv.Detections.fromdetectron2(outputs)
From SAM (Segment Anything)
from segmentanything import sammodelregistry, SamAutomaticMaskGenerator
sam = sammodelregistryvith.pth"">"vith"
maskgenerator = SamAutomaticMaskGenerator(sam)
masks = maskgenerator.generate(image)
detections = sv.Detections.fromsam(masks)
Creating Detections Manually
# Create detections manually
detections = sv.Detections(
xyxy=np.array([
[100, 200, 300, 400],
[150, 250, 350, 450]
]),
confidence=np.array([0.95, 0.87]),
classid=np.array([0, 1])
)
Annotators: Detection Visualization
Supervision provides various annotators for visualizing detection results.
Bounding Box Annotator
import supervision as sv
import cv2
image = cv2.imread("image.jpg")
Bounding box annotator
bboxannotator = sv.BoxAnnotator(
thickness=2,
color=sv.ColorPalette.fromhex(["#FF0000", "#00FF00", "#0000FF"])
)
annotatedimage = bboxannotator.annotate(
scene=image.copy(),
detections=detections
)
cv2.imwrite("annotatedbbox.jpg", annotatedimage)