Overview
TheBoundingBoxDrawer class implements the BoundingBoxDrawerInterface to visualize object detection bounding boxes with class labels on images. It uses Ultralytics’ Annotator utility for consistent styling.
Class Definition
BoundingBoxDrawer
Draws bounding boxes with class labels using the Ultralytics annotation framework.Attributes
Line thickness for bounding box borders. Default is
2 pixels.Methods
draw
Draws bounding boxes with class labels on an image.Parameters
Input image in BGR format (OpenCV format). The image will be annotated with bounding boxes and labels.
Array of bounding boxes in
xyxy format, where each box is [x1, y1, x2, y2]:x1, y1: Top-left corner coordinatesx2, y2: Bottom-right corner coordinates
(N, 4) where N is the number of boxes.Dictionary mapping class IDs to class names. For example:Used to display readable labels on bounding boxes.
List of class IDs corresponding to each bounding box. Used to retrieve class names from
trash_classes and determine box colors.Returns
The annotated image with:
- Bounding boxes drawn with class-specific colors
- Class labels displayed on each box
- Line thickness of 2 pixels
Annotator Usage
Internally, the drawer uses Ultralytics’Annotator class:
colors() function from Ultralytics generates consistent, visually distinct colors for each class ID.
Usage Example
Customization
To change the line thickness:Interface
BoundingBoxDrawerInterface
Abstract base class defining the contract for bounding box drawing implementations.Integration with Detection Pipeline
Typically used in the main drawing pipeline after mask visualization:Drawing class orchestrates:
- Mask drawing (semi-transparent overlays)
- Bounding box drawing (labeled boxes)
- Track drawing (motion trails)