How to Use SAHI

How to use SAHI with Azure Analyze Image

Slicing Aided Hyper Inference (SAHI) is a technique to improve small object detection performance with computer vision models. SAHI cuts an image into smaller images then runs inference on each smaller image. Predictions are then aggregated back together.

In this guide, we are going to walk through how to use SAHI with

Azure Analyze Image

to improve your ability to detect small objects with a vision model.

We will:

1. Install supervision
2. Load a model
3. Run inference using the sv.InferenceSlicer object

Without further ado, let's get started!

Step #1: 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.

Step #2: Load Model

First, we are going to load a model for use in running inference. For this guide, we will use a

Azure Analyze Image

model. We will then define a function that will run inference on an image and load the data into an sv.Detections object.

Let's load our model then define a function that, given an image, will run inference:

import requests
import supervision as sv

image = open(input, "rb").read()

endpoint = ""
subscription_key = ""

headers = {
    "Content-Type": "application/octet-stream",
    "Ocp-Apim-Subscription-Key": subscription_key

response =,

detections = sv.Detections.from_azure_analyze_image(response)

Replace the ... with the response object from your model.

Step #3: Run Inference with sv.InferenceSlicer

The sv.InferenceSlicer object takes a callback function that returns an sv.Detections object. The slicer divides a provided image into smaller parts, runs inference on each, then combines the results into a single sv.Detections object. We can process the Detections object using supervision to accomplish tasks like plotting bounding boxes and filtering predictions.

slicer = sv.InferenceSlicer(callback=callback)
detections = slicer(image=image)

prediction_num = len(sliced_detections.xyxy)

box_annotator = sv.BoxAnnotator()

annotated_frame = box_annotator.annotate(

sv.plot_image(image=annotated_frame, size=(16, 16))

The above code uses SAHI to process an image then plots the results from inference on an image. This image is then displayed.

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.

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