How to blur SAM model predictions

Learn how to blur model predictions.

Overview

There are many scenarios in which you may want to blur detections from an object detection model. For example, consider a scenario where you are counting the number of cars going through a drive-through. With computer vision, you can both count the number of cars while blurring any faces that are present.

In this guide, we are going to walk you through how to blur predictions from a

Segment Anything Model (SAM)

model in a few lines of code using the open source supervision Python package.

We will:

1. Install supervision
2. Load data
3. Plot and blur predictions with a supervision BlurAnnotator

Without further ado, let's get started!

SAM and Image Annotation Resources

Explore these resources to enhance your understanding of SAM 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

First, we are going to load our dataset into a supervision.DetectionDataset() object. This object will contain information about all the images in a dataset. You can load datasets from many different model types, from YOLO to MMDetection. For this guide, we will use the

Segment Anything Model (SAM)

data loader.

You can load data using the following code:


import supervision as sv

detections = sv.Detections.from_sam(...)

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

Plot Detections with BlurAnnotator

Supervision has a range of annotators that let you visualize detections from a computer vision model. For this guide, we are going to use the BlurAnnotator, which allows you to plot bounding boxes and blur their contents.

You can use the following code to plot predictions and blur their contents:


blur_annotator = sv.BlurAnnotator()

annotated_frame = blur_annotator.annotate(
	scene=image.copy(),
	detections=detections
)

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

You can try the blur annotator on example images in the interactive playground below. Upload an image that contains any common object in the Microsoft COCO class list (i.e. vehicles, cats, dogs, cell phones), then run the workflow to see the objects blurred.

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.