# segmentation

Python API: `depthai_nodes.node.parsers.segmentation`

## Classes

| Name | Description |
| --- | --- |
| [`SegmentationParser`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/segmentation.md#segmentation-Classes-SegmentationParser) | Parser class for parsing the output of the segmentation models. |

### class `SegmentationParser`

```python
class SegmentationParser(BaseParser)
```

Parser class for parsing the output of the segmentation models.

> **Note**
> Emits `dai.SegmentationMask` messages. dai.SegmentationMask containing the segmentation mask. Every pixel belongs to exactly one class. Unassigned pixels are represented with "255" and class pixels with non-negative integers.

Raises

| Type | Description |
| --- | --- |
| `ValueError` | If the number of output layers is not 1. |
| `ValueError` | If the number of dimensions of the output tensor is not 3. |

Attributes

| Name | Description |
| --- | --- |
| `background_class` |  |
| `class_names` |  |
| `classes_in_one_layer` | Whether all classes are in one layer in the multi-class segmentation model. Default is False. If True, the parser will use np.max instead of np.argmax to get the class map. |
| `n_classes` |  |
| `output_layer_name` | Name of the output layer relevant to the parser. |

Methods

| Name | Description |
| --- | --- |
| [`__init__`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/segmentation.md#segmentation-Classes-SegmentationParser-__init__) | Initializes the parser node. |
| [`build`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/segmentation.md#segmentation-Classes-SegmentationParser-build) | Configures the parser. |
| [`compute`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/segmentation.md#segmentation-Classes-SegmentationParser-compute) | Compute parser results from extracted tensors without sending messages. |
| [`emit`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/segmentation.md#segmentation-Classes-SegmentationParser-emit) | Create a `dai.SegmentationMask` message and send it on `out`. |
| [`extract`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/segmentation.md#segmentation-Classes-SegmentationParser-extract) | Select and dequantize the model tensors needed for parsing. |
| [`run`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/segmentation.md#segmentation-Classes-SegmentationParser-run) | Read queued network outputs, parse them, and emit results while running. |
| [`setBackgroundClass`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/segmentation.md#segmentation-Classes-SegmentationParser-setBackgroundClass) | Sets whether class index 0 should be treated as background. |
| [`setClassesInOneLayer`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/segmentation.md#segmentation-Classes-SegmentationParser-setClassesInOneLayer) | Sets the flag indicating whether all classes are in one layer. |
| [`setOutputLayerName`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/segmentation.md#segmentation-Classes-SegmentationParser-setOutputLayerName) | Sets the name of the output layer. |

#### method `__init__`

```python
def __init__(
    output_layer_name: str = '',
    classes_in_one_layer: bool = False,
    background_class: bool = False,
)
```

Initializes the parser node.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `output_layer_name` | `str` | Name of the output layer relevant to the parser. | `''` |
| `classes_in_one_layer` | `bool` | Whether all classes are in one layer in the multi- class segmentation model. Default is False. If True, the parser will use np.max instead of np.argmax to get the class map. | `False` |
| `background_class` | `bool` | Whether class index 0 should be treated as background. | `False` |

#### method `build`

```python
def build(head_config: dict[str, Any]) -> SegmentationParser
```

Configures the parser.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `head_config` | `dict[str, Any]` | The head configuration for the parser. | required |

Returns

| Type | Description |
| --- | --- |
| `SegmentationParser` | The parser object with the head configuration set. |

#### static method `compute`

```python
def compute(
    segmentation_mask: np.ndarray,
    *,
    classes_in_one_layer: bool = False,
    background_class: bool = False,
) -> np.ndarray
```

Compute parser results from extracted tensors without sending messages.

> **Note**
> Uses [depthai_nodes.node.parsers.utils.segmentation.compute_segmentation_class_map](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/utils/segmentation.md#segmentation-Functions-compute_segmentation_class_map); see that helper for tensor layout and validation details.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `segmentation_mask` | `np.ndarray` | CHW or HWC tensor, optionally batched. A 4D tensor uses its first batch item; the smallest axis is treated as the class axis. | required |
| `classes_in_one_layer` | `bool` | Whether a single channel already encodes class IDs rather than foreground scores. | `False` |
| `background_class` | `bool` | Replace winning class 0 with 255 for multi-class score tensors. | `False` |

Returns

| Type | Description |
| --- | --- |
| `np.ndarray` | An HW uint8 label map. Unassigned pixels are 255. A single foreground-score channel is compared against zero and emits class 0 for positive pixels. |

#### method `emit`

```python
def emit(output: dai.NNData, class_map: np.ndarray)
```

Create a `dai.SegmentationMask` message and send it on `out`.

Copies source timestamps and sequence number, and carries the source image transformation when present.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `output` | `dai.NNData` | Neural network output carrying tensors and source timestamps, sequence number, and optional image transformation. | required |
| `class_map` | `np.ndarray` | HW class-index mask with 255 reserved for background. | required |

#### method `extract`

```python
def extract(output: dai.NNData)
```

Select and dequantize the model tensors needed for parsing.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `output` | `dai.NNData` | Neural network output carrying tensors and source timestamps, sequence number, and optional image transformation. | required |

Returns

| Type | Description |
| --- | --- |
|  | Dequantized segmentation tensor, with the first batch item selected if the tensor is 4D. |

Raises

| Type | Description |
| --- | --- |
| `ValueError` | If no output name is configured and the message does not contain exactly one layer, or configured class requirements are not met. |

#### method `run`

```python
def run()
```

Read queued network outputs, parse them, and emit results while running.

The pipeline invokes this processing loop. It exits when the input queue closes or the node stops.

#### method `setBackgroundClass`

```python
def setBackgroundClass(background_class: bool)
```

Sets whether class index 0 should be treated as background.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `background_class` | `bool` | Whether class index 0 is background. | required |

#### method `setClassesInOneLayer`

```python
def setClassesInOneLayer(classes_in_one_layer: bool)
```

Sets the flag indicating whether all classes are in one layer.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `classes_in_one_layer` | `bool` | Whether all classes are in one layer. | required |

#### method `setOutputLayerName`

```python
def setOutputLayerName(output_layer_name: str)
```

Sets the name of the output layer.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `output_layer_name` | `str` | The name of the output layer. | required |
