|
| 1 | +""" |
| 2 | +ObjectTracker module using DepthAI's built-in ObjectTracker node. |
| 3 | +
|
| 4 | +Configures the ObjectTracker node within a DepthAI pipeline and parses |
| 5 | +tracklet output into TrackedObject data classes. |
| 6 | +
|
| 7 | +The ObjectTracker node is part of the on-device pipeline: |
| 8 | + SpatialDetectionNetwork.out ──► ObjectTracker ──► XLinkOut("tracklets") |
| 9 | +
|
| 10 | +This module provides: |
| 11 | +- configure_tracker_node(): sets up the node in a shared pipeline |
| 12 | +- parse_tracklets(): converts raw DepthAI tracklets into TrackedObject list |
| 13 | +
|
| 14 | +Reference: https://docs.luxonis.com/software/depthai/depthai-components/nodes/objecttracker/ |
| 15 | +""" |
| 16 | + |
| 17 | +from typing import List |
| 18 | + |
| 19 | +import depthai as dai |
| 20 | + |
| 21 | +from .tracked_object import TrackedObject, TrackingStatus |
| 22 | + |
| 23 | + |
| 24 | +# Map DepthAI tracklet status to our TrackingStatus enum |
| 25 | +_STATUS_MAP = { |
| 26 | + dai.Tracklet.TrackingStatus.NEW: TrackingStatus.NEW, |
| 27 | + dai.Tracklet.TrackingStatus.TRACKED: TrackingStatus.TRACKED, |
| 28 | + dai.Tracklet.TrackingStatus.LOST: TrackingStatus.LOST, |
| 29 | + dai.Tracklet.TrackingStatus.REMOVED: TrackingStatus.LOST, |
| 30 | +} |
| 31 | + |
| 32 | +# Available tracker algorithms |
| 33 | +TRACKER_TYPES = { |
| 34 | + "ZERO_TERM_COLOR_HISTOGRAM": dai.TrackerType.ZERO_TERM_COLOR_HISTOGRAM, |
| 35 | + "ZERO_TERM_IMAGELESS": dai.TrackerType.ZERO_TERM_IMAGELESS, |
| 36 | + "SHORT_TERM_IMAGELESS": dai.TrackerType.SHORT_TERM_IMAGELESS, |
| 37 | + "SHORT_TERM_KCF": dai.TrackerType.SHORT_TERM_KCF, |
| 38 | +} |
| 39 | + |
| 40 | + |
| 41 | +def configure_tracker_node( |
| 42 | + pipeline: dai.Pipeline, |
| 43 | + spatial_detection_network: dai.node.SpatialDetectionNetwork, |
| 44 | + tracker_type: str = "SHORT_TERM_IMAGELESS", |
| 45 | + labels_to_track: List[int] = None, |
| 46 | +) -> dai.node.ObjectTracker: |
| 47 | + """ |
| 48 | + Create and configure an ObjectTracker node in the DepthAI pipeline. |
| 49 | +
|
| 50 | + This wires the tracker to the SpatialDetectionNetwork outputs. |
| 51 | + Teammates provide the pipeline and spatial_detection_network node; |
| 52 | + this function adds the tracker on top. |
| 53 | +
|
| 54 | + Args: |
| 55 | + pipeline: The shared DepthAI pipeline (created by teammates). |
| 56 | + spatial_detection_network: The detection network node whose |
| 57 | + outputs we consume. |
| 58 | + tracker_type: Algorithm name. One of: |
| 59 | + ZERO_TERM_COLOR_HISTOGRAM, ZERO_TERM_IMAGELESS, |
| 60 | + SHORT_TERM_IMAGELESS, SHORT_TERM_KCF. |
| 61 | + labels_to_track: List of class label indices to track. |
| 62 | + If None, tracks all detected labels. |
| 63 | +
|
| 64 | + Returns: |
| 65 | + The configured ObjectTracker node (already linked to inputs |
| 66 | + and to an XLinkOut named "tracklets"). |
| 67 | + """ |
| 68 | + if tracker_type not in TRACKER_TYPES: |
| 69 | + raise ValueError( |
| 70 | + f"Unknown tracker_type '{tracker_type}'. " |
| 71 | + f"Options: {list(TRACKER_TYPES.keys())}" |
| 72 | + ) |
| 73 | + |
| 74 | + # --- create tracker node --- |
| 75 | + tracker = pipeline.create(dai.node.ObjectTracker) |
| 76 | + tracker.setTrackerType(TRACKER_TYPES[tracker_type]) |
| 77 | + tracker.setTrackerIdAssignmentPolicy( |
| 78 | + dai.TrackerIdAssignmentPolicy.UNIQUE_ID, |
| 79 | + ) |
| 80 | + |
| 81 | + if labels_to_track is not None: |
| 82 | + tracker.setDetectionLabelsToTrack(labels_to_track) |
| 83 | + |
| 84 | + # --- link detection network outputs into tracker inputs --- |
| 85 | + # passthrough frame (RGB preview used for detection) |
| 86 | + spatial_detection_network.passthrough.link(tracker.inputTrackerFrame) |
| 87 | + # detection frame (same frame, used for re-identification) |
| 88 | + spatial_detection_network.passthrough.link(tracker.inputDetectionFrame) |
| 89 | + # detection results (bounding boxes + spatial coords) |
| 90 | + spatial_detection_network.out.link(tracker.inputDetections) |
| 91 | + |
| 92 | + # --- create XLinkOut so host can read tracklets --- |
| 93 | + tracker_out = pipeline.create(dai.node.XLinkOut) |
| 94 | + tracker_out.setStreamName("tracklets") |
| 95 | + tracker.out.link(tracker_out.input) |
| 96 | + |
| 97 | + return tracker |
| 98 | + |
| 99 | + |
| 100 | +def parse_tracklets( |
| 101 | + tracklets_data: dai.Tracklets, |
| 102 | + label_map: List[str], |
| 103 | + frame_width: int, |
| 104 | + frame_height: int, |
| 105 | +) -> List[TrackedObject]: |
| 106 | + """ |
| 107 | + Convert raw DepthAI Tracklets output into a list of TrackedObject. |
| 108 | +
|
| 109 | + Called each frame after reading from the device output queue. |
| 110 | +
|
| 111 | + Args: |
| 112 | + tracklets_data: Raw tracklets from device.getOutputQueue("tracklets").get() |
| 113 | + label_map: Ordered list of class names matching model label indices |
| 114 | + (e.g. ["person", "car", "landing_pad"]). |
| 115 | + frame_width: Original frame width in pixels (for denormalizing bbox). |
| 116 | + frame_height: Original frame height in pixels. |
| 117 | +
|
| 118 | + Returns: |
| 119 | + List of TrackedObject with persistent IDs, status, and smoothed |
| 120 | + spatial coordinates. |
| 121 | + """ |
| 122 | + tracked_objects: List[TrackedObject] = [] |
| 123 | + |
| 124 | + for tracklet in tracklets_data.tracklets: |
| 125 | + # --- status --- |
| 126 | + status = _STATUS_MAP.get(tracklet.status, TrackingStatus.LOST) |
| 127 | + |
| 128 | + # skip objects that have been fully removed |
| 129 | + if tracklet.status == dai.Tracklet.TrackingStatus.REMOVED: |
| 130 | + continue |
| 131 | + |
| 132 | + # --- label --- |
| 133 | + label_index = tracklet.label |
| 134 | + label = ( |
| 135 | + label_map[label_index] |
| 136 | + if label_index < len(label_map) |
| 137 | + else str(label_index) |
| 138 | + ) |
| 139 | + |
| 140 | + # --- confidence --- |
| 141 | + confidence = tracklet.srcImgDetection.confidence |
| 142 | + |
| 143 | + # --- smoothed spatial coordinates (meters) --- |
| 144 | + spatial = tracklet.spatialCoordinates |
| 145 | + x = spatial.x / 1000.0 # mm -> m |
| 146 | + y = spatial.y / 1000.0 |
| 147 | + z = spatial.z / 1000.0 |
| 148 | + |
| 149 | + # --- bounding box (denormalize from 0-1 to pixels) --- |
| 150 | + roi = tracklet.roi.denormalize(frame_width, frame_height) |
| 151 | + bbox_x = int(roi.topLeft().x) |
| 152 | + bbox_y = int(roi.topLeft().y) |
| 153 | + bbox_width = int(roi.bottomRight().x - roi.topLeft().x) |
| 154 | + bbox_height = int(roi.bottomRight().y - roi.topLeft().y) |
| 155 | + |
| 156 | + tracked_objects.append( |
| 157 | + TrackedObject( |
| 158 | + object_id=tracklet.id, |
| 159 | + status=status, |
| 160 | + label=label, |
| 161 | + confidence=confidence, |
| 162 | + x=x, |
| 163 | + y=y, |
| 164 | + z=z, |
| 165 | + bbox_x=bbox_x, |
| 166 | + bbox_y=bbox_y, |
| 167 | + bbox_width=bbox_width, |
| 168 | + bbox_height=bbox_height, |
| 169 | + ) |
| 170 | + ) |
| 171 | + |
| 172 | + return tracked_objects |
0 commit comments