Once you know what objects are in an image, you can count them, either in aggregate or by class. This is useful for a range of use cases. For example, you can check if all five of a list of classes are present (which you could use in a quality assurance checklist), count the number of scratches in a product, and more.
In this guide, we are going to walk through how to count objects in
detections.
We will:
1. Install supervision
2. Run inference with a model
3. Count objects with Python
Without further ado, let's get started!
First, install the supervision pip package:
Once you have installed supervision, you are ready to load your data and start writing logic to filter detections.
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
data loader.
You can load data using the following code:
Replace the ... with the response object from your model.
To count all of the objects that match a single class, we can use the following code:
Replace "class name" with the name of the class with which you are working.
To count all of the objects that match one of multiple classes, we can use the following code:
Replace the selected_classes list with the IDs of the classes with which you are working.
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.