# gather_data

Python API: `depthai_nodes.node.gather_data`

## Classes

| Name | Description |
| --- | --- |
| [`GatherData`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/gather_data.md#gather_data-Classes-GatherData) | Threaded host node that groups (“gathers”) multiple data messages around a single reference message, matched by timestamp. |
| [`HasDetections`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/gather_data.md#gather_data-Classes-HasDetections) | Protocol for references exposing a list of detections. |

### class `GatherData`

```python
class GatherData(dai.node.ThreadedHostNode, Generic)
```

Threaded host node that groups (“gathers”) multiple data messages around a single reference message, matched by timestamp.

The node receives two input streams:

 * reference_input: reference messages (e.g., detections) that define a grouping key (timestamp) and determine how many data items
   should be gathered for that reference.
 * data_input: messages to be collected for the nearest reference timestamp within a tolerance derived from the camera FPS.

For each reference timestamp, the node waits until the number of gathered data messages equals `wait_count_fn(reference)`. Once
ready, it emits a `depthai_nodes.GatheredData` message containing the reference message and the gathered items.

The default `wait_count_fn` uses `len(reference.detections)`, which works out-of-the-box for messages that expose a `detections`
attribute (e.g. `dai.ImgDetections`).

By default, `preserveOrder` holds later groups behind earlier references and `maxPendingFrames` limits pending references and
unmatched data messages to 32. Limits and the optional `timeout` discard whole incomplete groups. These options require references
to arrive in source timestamp order. Pass `preserveOrder=False` and `maxPendingFrames=None` for ready-first, unbounded behavior.

> **Note**
> * Timestamp matching uses `Buffer.getTimestamp().total_seconds()` and a tolerance of `1 / (camera_fps * FPS_TOLERANCE_DIVISOR)`.
 * If `wait_count_fn(reference) == 0`, an empty group is ready immediately, but ordered output waits for earlier references.
 * The node periodically polls inputs using `tryGet()` at a rate derived from `camera_fps` and `INPUT_CHECKS_PER_FPS`.

Inputs:

 * `_data_input : dai.Node.Input`: Stream of data messages to be gathered (type `TGathered`).
 * `_reference_input : dai.Node.Input`: Stream of reference messages used for grouping and deciding how many items to gather (type
   `TReference`).

Outputs:

 * `out : dai.Node.Output`: Emits `depthai_nodes.GatheredData` objects with: `reference_data` (the matched reference) and `items`
   (list of data).

Attributes

| Name | Type | Description |
| --- | --- | --- |
| `FPS_TOLERANCE_DIVISOR` | `float` | Divides the per-frame time interval to compute timestamp matching tolerance. Higher values make matching stricter. |
| `INPUT_CHECKS_PER_FPS` | `int` | Number of polling iterations per frame interval. Effective loop sleep is `1 / (INPUT_CHECKS_PER_FPS * camera_fps)`. |

Properties

| Name | Type | Description |
| --- | --- | --- |
| [`out`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/gather_data.md#gather_data-Classes-GatherData-out) | `dai.Node.Output` | Return the gathered output stream. |

Methods

| Name | Description |
| --- | --- |
| [`__init__`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/gather_data.md#gather_data-Classes-GatherData-__init__) | Initializes the GatherData node. |
| [`build`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/gather_data.md#gather_data-Classes-GatherData-build) | Connect the data and reference streams used for gathering. |
| [`run`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/gather_data.md#gather_data-Classes-GatherData-run) | Poll both inputs, match messages by timestamp, and emit ready groups. |
| [`setCameraFps`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/gather_data.md#gather_data-Classes-GatherData-setCameraFps) | Set the camera frame rate used for timestamp matching. |
| [`setWaitCountFn`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/gather_data.md#gather_data-Classes-GatherData-setWaitCountFn) | Set the expected gathered-item count for each reference. |

#### property `out`

```python
out: dai.Node.Output
```

Return the gathered output stream.

#### method `__init__`

```python
def __init__()
```

Initializes the GatherData node.

#### method `build`

```python
def build(
    cameraFps: int,
    inputData: dai.Node.Output,
    inputReference: dai.Node.Output,
    waitCountFn: Callable[[TReference], int] | None = None,
    *,
    timeout: timedelta | None = None,
    maxPendingFrames: int | None = 32,
    preserveOrder: bool = True,
) -> GatherData[TReference, TGathered]
```

Connect the data and reference streams used for gathering.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `cameraFps` | `int` | Camera frame rate used to derive timestamp matching tolerance and polling interval. | required |
| `inputData` | `dai.Node.Output` | Upstream output producing the data messages to gather. | required |
| `inputReference` | `dai.Node.Output` | Upstream output producing the reference messages. | required |
| `waitCountFn` | `Callable[[TReference], int] | None` | Optional callback returning the number of data messages expected for a given reference. If omitted, defaults to len(reference.detections). | `None` |
| `timeout` | `timedelta | None` | Positive wait from reference arrival before dropping an incomplete group, including when inputs are idle. None disables expiry. | `None` |
| `maxPendingFrames` | `int | None` | Positive limit on pending references and unmatched data messages. Oldest incomplete references and orphan messages are dropped when their respective limit is exceeded. Defaults to 32. None disables the limits. | `32` |
| `preserveOrder` | `bool` | Emit groups in source timestamp order, holding later groups until earlier references complete or are dropped. References must arrive in order. Complete groups are emitted even after timeout. Defaults to True. False emits groups as soon as they are ready. | `True` |

Returns

| Type | Description |
| --- | --- |
| `GatherData[TReference, TGathered]` | The configured node instance. |

Raises

| Type | Description |
| --- | --- |
| `ValueError` | If FPS, timeout or the pending-frame limit is not positive. |

#### method `run`

```python
def run()
```

Poll both inputs, match messages by timestamp, and emit ready groups.

#### method `setCameraFps`

```python
def setCameraFps(fps: int)
```

Set the camera frame rate used for timestamp matching.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `fps` | `int` | Positive camera frame rate used for matching tolerance and polling. | required |

Raises

| Type | Description |
| --- | --- |
| `ValueError` | If the frame rate is not positive. |

#### method `setWaitCountFn`

```python
def setWaitCountFn(fn: Callable[[TReference], int])
```

Set the expected gathered-item count for each reference.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `fn` | `Callable[[TReference], int]` | Callback accepting the reference message and returning the number of items to wait for. A count of zero makes an empty group ready. | required |

### class `HasDetections`

```python
class HasDetections(Protocol)
```

Protocol for references exposing a list of detections.

Properties

| Name | Type | Description |
| --- | --- | --- |
| [`detections`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/gather_data.md#gather_data-Classes-HasDetections-detections) | `list` | Return the detections used to derive the default wait count. |

#### property `detections`

```python
detections: list
```

Return the detections used to derive the default wait count.

## Module Attributes

| Name | Description |
| --- | --- |
| [`TGathered`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/gather_data.md#gather_data-Module-Attributes-TGathered) |  |
| [`TReference`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/gather_data.md#gather_data-Module-Attributes-TReference) |  |

### type variable `TGathered`

```python
TGathered = TypeVar('TGathered', bound=dai.Buffer)
```

### type variable `TReference`

```python
TReference = TypeVar('TReference', bound=dai.Buffer)
```
