# segmentation

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

## Classes

### SegmentationParser

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

 * `ValueError`: If the number of output layers is not 1.
 * `ValueError`: If the number of dimensions of the output tensor is not 3.

#### Methods

##### init

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

Initializes the parser node.

Parameters

 * `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.
 * `background_class` (`bool`): Whether class index 0 should be treated as background.

##### build

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

Configures the parser.

Parameters

 * `head_config` (`dict[str, Any]`): The head configuration for the parser.

Returns

 * `SegmentationParser`: The parser object with the head configuration set.

##### 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); see that helper for tensor layout and validation details.

Parameters

 * `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.
 * `classes_in_one_layer` (`bool`): Whether a single channel already encodes class IDs rather than foreground scores.
 * `background_class` (`bool`): Replace winning class 0 with 255 for multi-class score tensors.

Returns

 * `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.

##### 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.

Parameters

 * `output` (`dai.NNData`): Neural network output carrying tensors and source timestamps, sequence number, and optional image
   transformation.
 * `class_map` (`np.ndarray`): HW class-index mask with 255 reserved for background.

##### extract

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

Select and dequantize the model tensors needed for parsing.

Parameters

 * `output` (`dai.NNData`): Neural network output carrying tensors and source timestamps, sequence number, and optional image
   transformation.

Returns

 * Dequantized segmentation tensor, with the first batch item selected if the tensor is 4D.

Raises

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

##### run

```python
def run(self):
```

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.

##### setBackgroundClass

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

Sets whether class index 0 should be treated as background.

Parameters

 * `background_class` (`bool`): Whether class index 0 is background.

##### setClassesInOneLayer

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

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

Parameters

 * `classes_in_one_layer` (`bool`): Whether all classes are in one layer.

##### setOutputLayerName

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

Sets the name of the output layer.

Parameters

 * `output_layer_name` (`str`): The name of the output layer.

#### Attributes

##### 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.
