How to filter YOLOv9 detections

Before you can train a computer vision model, you need labeled data on which to train your model. The more accurate the labels, or annotations, are, the higher the performance the model will achieve.

Overview

Before a model goes to production, you need to build logic around the model. This often involves filtering detections from the model. For instance, you may want to retrieve detections from a model that are above a specified confidence level, or detections in a particular region of an image or video.

Using supervision, you can filter detections from computer vision models in a few lines of code.

supervision is an open-source Python package with a range of utilities for working with computer vision models. For this tutorial, we will focus specifically on filtering detections with supervision, but you can also use supervision for model evaluation, visualizing detections, and more.

Below, we show how to filter

YOLOv9

detections with supervision. We will:

1. Install supervision
2. Load

YOLOv9

model detections
3. Show how to filter by confidence, area, and more

Without further ado, let's get started!

YOLOv9 and Image Annotation Resources

Explore these resources to enhance your understanding of YOLOv9 and image annotation techniques.

Install supervision

First, install the supervision pip package:

pip install supervision


Once you have installed supervision, you are ready to load your data and start writing logic to filter detections.

Load Data

supervision has a standard class called supervision.Detections() that provides a range of functions for working with bounding box and segmentation detections.

You can load predictions into a Detections() object in a single line of code using the supervision "from" loaders.

For

YOLOv9

predictions, we will use the

from_inference()

data loader:


import cv2
import inference
import supervision as sv

annotator = sv.BoxAnnotator()

def render(predictions, image):
    classes = {item["class_id"]: item["class"] for item in predictions["predictions"]}

    detections = sv.Detections.from_roboflow(predictions)

    print(predictions)

    image = annotator.annotate(
        scene=image, detections=detections, labels=[classes[i] for i in detections.class_id]
    )

    cv2.imshow("Prediction", image)
    cv2.waitKey(1)


inference.Stream(
    source="webcam",
    model="microsoft-coco/9",
    output_channel_order="BGR",
    use_main_thread=True,
    on_prediction=render,
    api_key="api_key"
)

Above, replace "microsoft-coco/9" with the model ID of a YOLOv9 model hosted on Roboflow.

To upload a model to Roboflow, first install the Roboflow Python package:

pip install roboflow

Then, create a new Python file and paste in the following code:


from roboflow import Roboflow

rf = Roboflow(api_key="API_KEY")
project = rf.workspace().project("PROJECT_ID")
project.version(DATASET_VERSION).deploy(model_type="yolov8", model_path=f"{HOME}/runs/detect/train/")

In the code above, add your API key and the path to the model weights you want to upload. Learn how to retrieve your API key. Your weights will be uploaded to Roboflow. Your model will shortly be accessible over an API, and available for use in Inference. To learn more about uploading model weights to Roboflow, check out our full guide to uploading weights to Roboflow.

With supervision, you can filter detections by:

  1. A specific class
  2. A set of classes
  3. Confidence
  4. Bounding box area
  5. Box dimensions
  6. A specific zone

You can combine these filters to build the logic you need.

For this guide, we will focus on filtering by classes and confidence. To learn about the other filters available in supervision, check out the Detections() quickstart guide.

Filter by Class

To filter

YOLOv9

detections by class, use the following code:


detections = detections[detections.class_id == 0]


Replace the number 0 with the ID of the class whose predictions you want to retriev.e

Filter by Set of Classes

To retrieve

YOLOv9

detections by class, use the following code:


selected_classes = [0, 2, 3]
detections = detections[np.isin(detections.class_id, selected_classes)]

Where selected_classes contains the list of class IDs you want to filter.

Filter by Confidence

To retrieve

YOLOv9

detections over a specified confidence level, use the following code:


detections = detections[detections.confidence > 0.5]

Next Steps

supervision provides an extensive range of functionalities for working with computer vision models. With supervision, you can:

1. Process and filter detections and segmentation masks from a range of popular models (YOLOv5, Ultralytics YOLOv8, MMDetection, and more).
2. Process and filter classifications
3. Compute confusion matrices
4. Plot bounding boxes and segmentation masks

And more! To learn about the full range of functionality in supervision, check out the supervision documentation.