Overview
TheTrackDrawer class implements the TrackDrawerInterface to visualize object tracking by drawing motion trails on images. It maintains a history of object centroids and renders connected lines to show movement paths.
Class Definition
TrackDrawer
Draws tracking lines for moving objects by maintaining and visualizing centroid history.Attributes
Dictionary storing the tracking history for each tracked object. Keys are track IDs (integers), and values are lists of centroid coordinates (tuples of floats).Structure:Each track maintains up to 50 historical points.
Line thickness for drawing track trails. Default is
2 pixels.Methods
draw
Draws tracking lines on an image based on object movement history.Parameters
Input image in BGR format (OpenCV format). The image will be annotated with tracking lines.
List of tracking IDs for detected objects. Each ID uniquely identifies an object across frames.
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. Each box corresponds to a track ID in tracks_ids.Returns
The annotated image with:
- Colored tracking lines connecting historical centroids
- Lines drawn with 2px thickness
- Colors determined by track ID for consistent visualization
Centroid Calculation
For each bounding box, the centroid (center point) is calculated as:(x1, y1) is the top-left corner and (x2, y2) is the bottom-right corner of the bounding box.
History Management
The tracker maintains a sliding window of the last 50 centroid positions:- Memory efficiency by limiting history size
- Smooth visualization of recent movement
- Automatic cleanup of old tracking data
Track Line Visualization
Lines are drawn connecting consecutive centroid positions:colors() function.
Usage Example
Customization
To change the line thickness:Interface
TrackDrawerInterface
Abstract base class defining the contract for track drawing implementations.Integration with Detection Pipeline
Typically used in the main drawing pipeline after mask and bounding box visualization:Drawing class orchestrates:
- Mask drawing (semi-transparent overlays)
- Bounding box drawing (labeled boxes)
- Track drawing (motion trails) - Final step
Tracking Behavior
- New objects: When a new track ID appears, a new entry is created in
track_history - Existing objects: Centroids are appended to the existing history
- Lost objects: If an object is not detected in subsequent frames, its history persists but stops growing
- Reappearing objects: If a track ID reappears, it continues from its existing history
Performance Considerations
- The 50-point limit keeps memory usage constant regardless of video length
- Line drawing is optimized by OpenCV’s native
cv2.line()function - History is stored per track ID using
defaultdictfor automatic initialization