|
| 1 | +import time |
| 2 | +from queue import PriorityQueue |
| 3 | +from typing import ( |
| 4 | + Callable, |
| 5 | + Dict, |
| 6 | + Generic, |
| 7 | + List, |
| 8 | + Optional, |
| 9 | + Protocol, |
| 10 | + TypeVar, |
| 11 | + runtime_checkable, |
| 12 | +) |
| 13 | + |
| 14 | +import depthai as dai |
| 15 | + |
| 16 | +from depthai_nodes import GatheredData |
| 17 | + |
| 18 | + |
| 19 | +@runtime_checkable |
| 20 | +class HasDetections(Protocol): |
| 21 | + @property |
| 22 | + def detections(self) -> List: |
| 23 | + ... |
| 24 | + |
| 25 | + |
| 26 | +TReference = TypeVar("TReference", bound=dai.Buffer) |
| 27 | +TGathered = TypeVar("TGathered", bound=dai.Buffer) |
| 28 | + |
| 29 | + |
| 30 | +class GatherData(dai.node.ThreadedHostNode, Generic[TReference, TGathered]): |
| 31 | + FPS_TOLERANCE_DIVISOR = 2.0 |
| 32 | + INPUT_CHECKS_PER_FPS = 100 |
| 33 | + """A class for gathering data. Gathers n messages based on reference_data. To |
| 34 | + determine n, wait_count_fn function is used. The default wait_count_fn function is |
| 35 | + waiting for len(TReference.detection). This means the node works out-of-the-box with |
| 36 | + dai.ImgDetections and ImgDetectionsExtended. |
| 37 | +
|
| 38 | + Attributes |
| 39 | + ---------- |
| 40 | + FPS_TOLERANCE_DIVISOR: float |
| 41 | + Divisor for the FPS tolerance. |
| 42 | + INPUT_CHECKS_PER_FPS: int |
| 43 | + Number of input checks per FPS. |
| 44 | + input_data: dai.Node.Input |
| 45 | + Input to be gathered. |
| 46 | + input_reference: dai.Node.Input |
| 47 | + Input to determine how many gathered items to wait for. |
| 48 | + output: dai.Node.Output |
| 49 | + Output for gathered data. |
| 50 | + """ |
| 51 | + |
| 52 | + def __init__(self) -> None: |
| 53 | + """Initializes the GatherData node.""" |
| 54 | + self._camera_fps: Optional[int] = None |
| 55 | + self._unmatched_data: List[TGathered] = [] |
| 56 | + self._data_by_reference_ts: Dict[float, List[TGathered]] = {} |
| 57 | + self._reference_data: Dict[float, TReference] = {} |
| 58 | + self._ready_timestamps = PriorityQueue() |
| 59 | + self._wait_count_fn = self._default_wait_count_fn |
| 60 | + |
| 61 | + self.input_data = self.createInput() |
| 62 | + self.input_reference = self.createInput() |
| 63 | + self.out = self.createOutput() |
| 64 | + |
| 65 | + @staticmethod |
| 66 | + def _default_wait_count_fn(reference: TReference) -> int: |
| 67 | + assert isinstance(reference, HasDetections) |
| 68 | + return len(reference.detections) |
| 69 | + |
| 70 | + def build( |
| 71 | + self, |
| 72 | + camera_fps: int, |
| 73 | + wait_count_fn: Optional[Callable[[TReference], int]] = None, |
| 74 | + ) -> "GatherData[TReference, TGathered]": |
| 75 | + """Builds and configures the GatherData node with the specified parameters. |
| 76 | +
|
| 77 | + Parameters |
| 78 | + ---------- |
| 79 | + camera_fps : int |
| 80 | + The frames per second (FPS) setting for the camera. This affects the rate |
| 81 | + at which data is gathered. |
| 82 | +
|
| 83 | + wait_count_fn : Optional[Callable[[TReference], int]], default=None |
| 84 | + A function that takes a reference and returns the number of frames to wait |
| 85 | + before gathering data. This allows customizing the waiting behavior based on the reference data. |
| 86 | + If None, the default wait count function will be used. The default function matches based on length of TReference.detections array. |
| 87 | +
|
| 88 | + Returns |
| 89 | + ------- |
| 90 | + GatherData[TReference, TGathered] |
| 91 | + The configured GatherData node instance. |
| 92 | +
|
| 93 | + Examples |
| 94 | + -------- |
| 95 | + >>> gather_node = GatherData() |
| 96 | + >>> # Build with default wait count function |
| 97 | + >>> gather_node.build(camera_fps=30) |
| 98 | + >>> |
| 99 | + >>> # Build with custom wait count function that always waits for 2 messages |
| 100 | + >>> def custom_wait(ref): |
| 101 | + >>> return 2 |
| 102 | + >>> gather_node.build(camera_fps=60, wait_count_fn=custom_wait) |
| 103 | + """ |
| 104 | + self.set_camera_fps(camera_fps) |
| 105 | + if wait_count_fn is None: |
| 106 | + wait_count_fn = self._default_wait_count_fn |
| 107 | + self.set_wait_count_fn(wait_count_fn) |
| 108 | + return self |
| 109 | + |
| 110 | + def set_camera_fps(self, fps: int) -> None: |
| 111 | + if fps <= 0: |
| 112 | + raise ValueError(f"Camera FPS must be positive, got {fps}") |
| 113 | + self._camera_fps = fps |
| 114 | + |
| 115 | + def run(self) -> None: |
| 116 | + if not self._camera_fps: |
| 117 | + raise ValueError("Camera FPS not set. Call build() before run().") |
| 118 | + |
| 119 | + while self.isRunning(): |
| 120 | + try: |
| 121 | + input_data: TGathered = self.input_data.tryGet() |
| 122 | + input_reference: TReference = self.input_reference.tryGet() |
| 123 | + except dai.MessageQueue.QueueException: |
| 124 | + break |
| 125 | + if input_data: |
| 126 | + self._add_data(input_data) |
| 127 | + self._send_ready_data() |
| 128 | + if input_reference: |
| 129 | + self._add_reference(input_reference) |
| 130 | + self._send_ready_data() |
| 131 | + |
| 132 | + time.sleep(1 / self.INPUT_CHECKS_PER_FPS / self._camera_fps) |
| 133 | + |
| 134 | + def _send_ready_data(self) -> None: |
| 135 | + ready_data = self._pop_ready_data() |
| 136 | + if ready_data: |
| 137 | + self._clear_old_data(ready_data) |
| 138 | + self.out.send(ready_data) |
| 139 | + |
| 140 | + def _add_data(self, data: TGathered) -> None: |
| 141 | + data_ts = self._get_total_seconds_ts(data) |
| 142 | + best_matching_reference_ts = self._get_matching_reference_ts(data_ts) |
| 143 | + |
| 144 | + if best_matching_reference_ts is not None: |
| 145 | + self._add_data_by_reference_ts(data, best_matching_reference_ts) |
| 146 | + self._update_ready_timestamps(best_matching_reference_ts) |
| 147 | + else: |
| 148 | + self._unmatched_data.append(data) |
| 149 | + |
| 150 | + def _get_matching_reference_ts(self, data_ts: float) -> Optional[float]: |
| 151 | + for reference_ts in self._reference_data.keys(): |
| 152 | + if self._timestamps_in_tolerance(reference_ts, data_ts): |
| 153 | + return reference_ts |
| 154 | + return None |
| 155 | + |
| 156 | + def _add_reference( |
| 157 | + self, |
| 158 | + reference: TReference, |
| 159 | + ) -> None: |
| 160 | + reference_ts = self._get_total_seconds_ts(reference) |
| 161 | + self._reference_data[reference_ts] = reference |
| 162 | + self._try_match_data(reference_ts) |
| 163 | + self._update_ready_timestamps(reference_ts) |
| 164 | + |
| 165 | + def _try_match_data(self, reference_ts: float) -> None: |
| 166 | + matched_data: List[TGathered] = [] |
| 167 | + for data in self._unmatched_data: |
| 168 | + data_ts = self._get_total_seconds_ts(data) |
| 169 | + if self._timestamps_in_tolerance(reference_ts, data_ts): |
| 170 | + self._add_data_by_reference_ts(data, reference_ts) |
| 171 | + matched_data.append(data) |
| 172 | + |
| 173 | + for matched in matched_data: |
| 174 | + self._unmatched_data.remove(matched) |
| 175 | + |
| 176 | + def _timestamps_in_tolerance(self, timestamp1: float, timestamp2: float) -> bool: |
| 177 | + difference = abs(timestamp1 - timestamp2) |
| 178 | + return difference < (1 / self._camera_fps / self.FPS_TOLERANCE_DIVISOR) |
| 179 | + |
| 180 | + def _add_data_by_reference_ts(self, data: TGathered, reference_ts: float) -> None: |
| 181 | + if reference_ts in self._data_by_reference_ts: |
| 182 | + self._data_by_reference_ts[reference_ts].append(data) |
| 183 | + else: |
| 184 | + self._data_by_reference_ts[reference_ts] = [data] |
| 185 | + |
| 186 | + def _update_ready_timestamps(self, timestamp: float) -> None: |
| 187 | + if not self._timestamp_ready(timestamp): |
| 188 | + return |
| 189 | + |
| 190 | + self._ready_timestamps.put(timestamp) |
| 191 | + |
| 192 | + def _timestamp_ready(self, timestamp: float) -> bool: |
| 193 | + reference = self._reference_data.get(timestamp) |
| 194 | + if not reference: |
| 195 | + return False |
| 196 | + |
| 197 | + wait_for_count = self._get_wait_count(reference) |
| 198 | + if wait_for_count == 0: |
| 199 | + return True |
| 200 | + |
| 201 | + recognitions = self._data_by_reference_ts.get(timestamp) |
| 202 | + if not recognitions: |
| 203 | + return False |
| 204 | + |
| 205 | + return wait_for_count == len(recognitions) |
| 206 | + |
| 207 | + def _get_wait_count(self, reference: TReference) -> int: |
| 208 | + return self._wait_count_fn(reference) |
| 209 | + |
| 210 | + def _pop_ready_data(self) -> Optional[GatheredData]: |
| 211 | + if self._ready_timestamps.empty(): |
| 212 | + return None |
| 213 | + |
| 214 | + timestamp = self._ready_timestamps.get() |
| 215 | + return GatheredData( |
| 216 | + reference_data=self._reference_data.pop(timestamp), |
| 217 | + gathered=self._data_by_reference_ts.pop(timestamp, None) or [], |
| 218 | + ) |
| 219 | + |
| 220 | + def _clear_old_data(self, ready_data: GatheredData) -> None: |
| 221 | + current_timestamp = self._get_total_seconds_ts(ready_data) |
| 222 | + self._clear_unmatched_data(current_timestamp) |
| 223 | + self._clear_old_references(current_timestamp) |
| 224 | + |
| 225 | + def _clear_unmatched_data(self, current_timestamp: float) -> None: |
| 226 | + unmatched_data_to_remove = [] |
| 227 | + for unmatched_data in self._unmatched_data: |
| 228 | + if self._get_total_seconds_ts(unmatched_data) < current_timestamp: |
| 229 | + unmatched_data_to_remove.append(unmatched_data) |
| 230 | + |
| 231 | + for unmatched_data in unmatched_data_to_remove: |
| 232 | + self._unmatched_data.remove(unmatched_data) |
| 233 | + |
| 234 | + def _get_total_seconds_ts(self, buffer_like: dai.Buffer) -> float: |
| 235 | + return buffer_like.getTimestamp().total_seconds() |
| 236 | + |
| 237 | + def _clear_old_references(self, current_timestamp: float) -> None: |
| 238 | + reference_keys_to_pop = [] |
| 239 | + for reference_ts in self._reference_data.keys(): |
| 240 | + if reference_ts < current_timestamp: |
| 241 | + reference_keys_to_pop.append(reference_ts) |
| 242 | + |
| 243 | + for reference_ts in reference_keys_to_pop: |
| 244 | + self._reference_data.pop(reference_ts) |
| 245 | + self._data_by_reference_ts.pop(reference_ts, None) |
| 246 | + |
| 247 | + def set_wait_count_fn(self, fn: Callable[[TReference], int]) -> None: |
| 248 | + self._wait_count_fn = fn |
0 commit comments