Overview
Thedraw_boxes module provides utilities for visualizing object detection results by drawing bounding boxes on images with class labels and confidence scores.
Functions
draw_boxes()
Draw bounding boxes with class names and optional scores on an image.Parameters
An array of shape
(width, height, 3) with pixel values in the range [0, 1]. The image will be converted to PIL format internally.An array of shape
(num_boxes, 4) containing bounding box coordinates in the format (y_min, x_min, y_max, x_max).A list of integer indices into the
class_names array, indicating which class each box belongs to.A list of string class names (e.g.,
['person', 'car', 'dog']).An optional array of confidence scores for each box. When provided, scores are displayed alongside class names in the format “Class 0.95”.
Returns
A copy of the input image (as a numpy array) with bounding boxes, labels, and scores drawn on it.
Description
The function performs the following operations:- Converts the input image from normalized float values to PIL Image format
- Loads the FiraMono-Medium font for text rendering
- Generates distinct colors for each class using HSV color space
- Draws rectangles for each bounding box with the corresponding class color
- Adds text labels with class names and optional scores
- Returns the modified image as a numpy array
- Box thickness scales with image size (approximately 1/300 of combined width and height)
- Font size adapts to image height (3% of image height)
- Labels are positioned above boxes when space permits, otherwise below
- Each class receives a consistent, vibrant color
Example Usage
Example Without Scores
get_colors_for_classes()
Generate a list of random but consistent colors for object classes.Parameters
The number of distinct classes to generate colors for.
Returns
A list of RGB tuples
(R, G, B) with values in the range [0, 255]. The list contains num_classes colors.Description
This function generates visually distinct colors using HSV color space and caches them for consistency across multiple calls. Features:- Deterministic: Uses a fixed random seed (10101) for reproducible colors
- Cached: Returns previously generated colors if
num_classesmatches - Well-distributed: Generates colors evenly spaced in HSV hue space
- Shuffled: Colors are shuffled to decorrelate adjacent class indices
Example Usage
Notes
- The
draw_boxes()function requires the FiraMono-Medium.otf font file to be located atfont/FiraMono-Medium.otfrelative to the working directory - Input images should be normalized to [0, 1] range before passing to
draw_boxes() - Box coordinates follow the YOLO format:
(y_min, x_min, y_max, x_max) - The function prints each box’s label and coordinates to stdout for debugging

