Skip to content

Detect and Annotate

With Supervision, you can easily annotate predictions obtained from a variety of object detection and segmentation models. This document outlines how to run inference using the Ultralytics YOLOv8 model, load these predictions into Supervision, and annotate the image.

Run Inference

First, you'll need to obtain predictions from your object detection or segmentation model.

import cv2
from ultralytics import YOLO

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

Load Predictions into Supervision

Now that we have predictions from a model, we can load them into Supervision. We can do so using the sv.Detections.from_ultralytics method, which accepts model results from both detection and segmentation models.

import cv2
from ultralytics import YOLO
import supervision as sv

model = YOLO("yolov8n.pt")
image = cv2.imread("image.jpg")
results = model(image)[0]
detections = sv.Detections.from_ultralytics(results)

You can conveniently load predictions from other computer vision frameworks and libraries using:

Annotate Image

Finally, we can annotate the image with the predictions. Since we are working with an object detection model, we will use the sv.BoundingBoxAnnotator and sv.LabelAnnotator classes. If you are running the segmentation model sv.MaskAnnotator is a drop-in replacement for sv.BoundingBoxAnnotator that will allow you to draw masks instead of boxes.

import cv2
from ultralytics import YOLO
import supervision as sv

model = YOLO("yolov8n.pt")
image = cv2.imread("image.jpg")
results = model(image)[0]
detections = sv.Detections.from_ultralytics(results)

bounding_box_annotator = sv.BoundingBoxAnnotator()
label_annotator = sv.LabelAnnotator()

labels = [
    results.names[class_id]
    for class_id
    in detections.class_id
]

annotated_image = bounding_box_annotator.annotate(
    scene=image, detections=detections)
annotated_image = label_annotator.annotate(
    scene=annotated_image, detections=detections, labels=labels)

Predictions plotted on an image

Display Annotated Image

To display the annotated image in Jupyter Notebook or Google Colab, use the sv.plot_image function.

sv.plot_image(annotated_image)