Overview
TheDetections
class encapsulates a wide array of detection data. The class can represent bounding box, segmentation, and classification results.
Arguments
Type of detection (
DetectionType.BOUNDING_BOX
, DetectionType.SEGMENTATION
, or DetectionType.CLASSIFICATION
).Coordinates of bounding boxes
[x1, y1, x2, y2]
for each detection, where (x1, y1) refers to the top left coordinate and (x2, y2) refers to the bottom right coordinate.Class IDs for each detection, indexing into
classes
.Array of class names, indexed by
class_ids
.Additional custom data related to detections (Default: empty dictionary).
Segmentation masks for each detection, required for segmentation types.
Confidence scores for each detection.
Examples
Bounding Box Detections
Classification Detections
Segmentation Detections
We will support Segmentation detections shortly.Methods
Detections.split()
Splits the
Detections
object into a list of Detections
objects, each containing a single detection.Returns:List['Detections']
: A list of Detections objects, each containing a single detection.
Detections.from_classification(assigned_classes)
Creates a
Detections
instance from a list of class namesParameters:assigned_classes
(List[str]
): The class name(s) that have been assigned to the classification detection.all_classes
(Optional[List[str]]
): List of strings representing all possible class names that can be assigned in the classification detection.
Detections
: A classification Detections object.
Detections.from_yolov5(yolov5_results)
Creates a
Detections
instance from a YOLOv5 inference result.Parameters:yolov5_results
(yolov5.models.common.Detections
): The output Detections instance from YOLOv5.
Detections
: A bounding box Detections object.
Detections.from_ultralytics(ultralytics_results)
Creates a
Detections
instance from an Ultralytics YOLOv8 inference result.Parameters:ultralytics_results
(ultralytics.yolo.engine.results.Results
): The output Results instance from YOLOv8.
Detections
: A bounding box or segmentation Detections object.
Detections.empty()
Creates an empty
Detections
object.Returns:Detections
: An empty bounding box Detections object.
Detections.to_supervision()
Converts the
Detections
instance to a supervision.Detections
instance.Returns:sv.Detections
: A supervision Detections instance.
Properties
confidence_scores
Returns the list of confidence scores associated with the detections.
area
Returns a NumPy array containing the area of each detection.Specifically, if segmentation masks are available, a NumPy array containing the area of each mask is returned. Otherwise, a NumPy array containing the area of each bounding box is returned.
box_area
Returns a NumPy array containing the area of each bounding box.
class_names
Return the list of class names associated with the detections.
Example
Creating a Detection from YOLOv5
Converting to Supervision
Detections
class, including creation, manipulation, and conversion of detection data.