# frame_cropper

Python API: `depthai_nodes.node.frame_cropper`

## Classes

| Name | Description |
| --- | --- |
| [`FrameCropper`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/frame_cropper.md#frame_cropper-Classes-FrameCropper) | A host node that crops detection regions from frames and outputs one cropped `dai.ImgFrame` per region. |

### class `FrameCropper`

```python
class FrameCropper(BaseThreadedHostNode)
```

A host node that crops detection regions from frames and outputs one cropped `dai.ImgFrame` per region.

`FrameCropper` is a convenience wrapper around an internal `dai.node.ImageManip` configured for cropping + resizing. It supports
two input modes:

 * fromImgDetections: Provide `dai.ImgDetections` and the node will generate `dai.ImageManipConfig` messages for each detection
   via a `dai.node.Script` node. Each config is paired with the corresponding input frame, producing one cropped output frame per
   detection.
 * fromManipConfigs: Provide an upstream stream of cropping configs packed in `dai.MessageGroup` messages. An on-device
   `dai.node.Script` node pairs each config with the current frame and forwards them to the internal `dai.node.ImageManip`.

Configuration is provided via `fromImgDetections` or `fromManipConfigs`. The pipeline nodes are constructed only once `build` is
called.

> **Note**
> * Exactly one configuration path must be selected: only one of `fromImgDetections` and `fromManipConfigs` can be used.
 * Output frames are always resized to `outputSize` using the provided `resizeMode` (default: `CENTER_CROP`).
 * In `fromImgDetections` mode, a `dai.node.Script` node drives the cropping by emitting one `dai.ImageManipConfig` per detection.
 * In `fromManipConfigs` mode, the `inputManipConfigs` stream must output `dai.MessageGroup` messages where each value is a
   `dai.ImageManipConfig`. Key naming is arbitrary.

Use `fromImgDetections(padding=0.0)` to set optional padding around each detection region. `build(outputSize, resizeMode)` sets
the crop size and resize mode.

Outputs:

 * `out : dai.Node.Output`: Stream of cropped `dai.ImgFrame` messages. One output frame is produced per crop configuration (per
   detection in `fromImgDetections` mode; per config in the received `MessageGroup` in `fromManipConfigs` mode).

See also:

 * `dai.node.ImageManip`: Node used to perform cropping and resizing.
 * `dai.ImageManipConfig`: Cropping configuration messages forwarded to ImageManip.
 * `dai.ImgDetections`: Detection message type used in `fromImgDetections` mode.
 * `dai.MessageGroup`: Message type expected by `fromManipConfigs`.

Attributes

| Name | Description |
| --- | --- |
| `IMG_DETECTIONS_SCRIPT_CONTENT` |  |
| `MANIP_CONFIGS_SCRIPT_CONTENT` |  |
| `SYNCED_MANIP_CONFIGS_SCRIPT_CONTENT` |  |

Properties

| Name | Description |
| --- | --- |
| [`out`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/frame_cropper.md#frame_cropper-Classes-FrameCropper-out) | Return the cropped frame output stream. |

Methods

| Name | Description |
| --- | --- |
| [`__init__`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/frame_cropper.md#frame_cropper-Classes-FrameCropper-__init__) | Initialize logging and the platform-specific output image format. |
| [`build`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/frame_cropper.md#frame_cropper-Classes-FrameCropper-build) | Connect image input and construct the configured crop pipeline. |
| [`fromImgDetections`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/frame_cropper.md#frame_cropper-Classes-FrameCropper-fromImgDetections) | Select detection-driven cropping before calling `build()`. |
| [`fromManipConfigs`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/frame_cropper.md#frame_cropper-Classes-FrameCropper-fromManipConfigs) | Select cropping from groups of ImageManip configuration messages. |
| [`run`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/frame_cropper.md#frame_cropper-Classes-FrameCropper-run) | No-op because cropping is driven entirely by on-device Script nodes. |

#### property `out`

Return the cropped frame output stream.

#### method `__init__`

```python
def __init__()
```

Initialize logging and the platform-specific output image format.

Raises

| Type | Description |
| --- | --- |
| `ValueError` | If the pipeline device platform has no configured image-frame format. |

#### method `build`

```python
def build(inputImage: dai.Node.Output) -> FrameCropper
```

Connect image input and construct the configured crop pipeline.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `inputImage` | `dai.Node.Output` | Image stream to crop. Call `fromImgDetections()` or `fromManipConfigs()` first. | required |

Returns

| Type | Description |
| --- | --- |
| `FrameCropper` | This node, with cropped frames available on `out`. |

Raises

| Type | Description |
| --- | --- |
| `RuntimeError` | If no crop configuration mode has been selected. |

#### method `fromImgDetections`

```python
def fromImgDetections(
    inputImgDetections: dai.Node.Output,
    outputSize: tuple[int, int],
    resizeMode: dai.ImageManipConfig.ResizeMode = dai.ImageManipConfig.ResizeMode.CENTER_CROP,
    padding: float = 0.0,
    syncThreshold: timedelta = timedelta(milliseconds=10),
) -> FrameCropper
```

Select detection-driven cropping before calling `build()`.

> **Note**
> Produces one crop per detection. The image stream is connected later by `build()`.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `inputImgDetections` | `dai.Node.Output` | Output stream of `dai.ImgDetections` to synchronize with image frames. | required |
| `outputSize` | `tuple[int, int]` | Crop output size as `(width, height)` pixels. | required |
| `resizeMode` | `dai.ImageManipConfig.ResizeMode` | ImageManip resize policy applied to each crop. | `dai.ImageManipConfig.ResizeMode.CENTER_CROP` |
| `padding` | `float` | Normalized padding added to each side of the detection region. | `0.0` |
| `syncThreshold` | `timedelta` | Maximum timestamp difference used to synchronize detections and frames. | `timedelta(milliseconds=10)` |

Returns

| Type | Description |
| --- | --- |
| `FrameCropper` | This node for fluent configuration. |

Raises

| Type | Description |
| --- | --- |
| `RuntimeError` | If either crop configuration mode was already selected. |

#### method `fromManipConfigs`

```python
def fromManipConfigs(
    inputManipConfigs: dai.Node.Output,
    maxOutputFrameSize: int,
    waitForConfig: bool,
    syncThreshold: timedelta | None = None,
) -> FrameCropper
```

Select cropping from groups of ImageManip configuration messages.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `inputManipConfigs` | `dai.Node.Output` | Stream of `dai.MessageGroup` objects whose values are `dai.ImageManipConfig` messages. Group keys are arbitrary. | required |
| `maxOutputFrameSize` | `int` | Maximum output image buffer size in bytes. | required |
| `waitForConfig` | `bool` | If true, synchronize each frame with a configuration group. Otherwise, reuse the latest group for subsequent frames. | required |
| `syncThreshold` | `timedelta | None` | Optional timestamp tolerance. May only be set when `waitForConfig` is true. | `None` |

Returns

| Type | Description |
| --- | --- |
| `FrameCropper` | This node for fluent configuration. |

Raises

| Type | Description |
| --- | --- |
| `RuntimeError` | If a configuration mode was already selected, or a sync threshold is supplied without waiting for configuration. |

#### method `run`

```python
def run()
```

No-op because cropping is driven entirely by on-device Script nodes.
