# SegmentationParser

SegmentationParser node takes the raw output of a segmentation NN and applies per-pixel argmax to convert the raw output tensor
into a single-channel segmentation mask, which is then output as a
[SegmentationMask](https://docs.luxonis.com/software-v3/depthai/depthai-components/messages/segmentation_mask.md) message. Some
segmentation NNs output the segmentation mask directly as a single-channel output, in which case the SegmentationParser node can
be used to simply convert the raw output tensor into a SegmentationMask message without applying argmax.

## How to place it

#### Python

```python
pipeline = dai.Pipeline()
segmentation = pipeline.create(dai.node.SegmentationParser)
```

#### C++

```cpp
dai::Pipeline pipeline;
auto segmentation = pipeline.create<dai::node::SegmentationParser>();
```

## Inputs and Outputs

## Usage

#### Python

```python
model = dai.NNModelDescription("luxonis/deeplab-v3-plus:512x512")
pipeline = dai.Pipeline()
camera = pipeline.create(dai.node.Camera).build()
nn = pipeline.create(dai.node.NeuralNetwork).build(camera, model)
parser = pipeline.create(dai.node.SegmentationParser).build(nn.out, model )
```

#### C++

```cpp
dai::NNModelDescription model("luxonis/deeplab-v3-plus:512x512");
dai::Pipeline pipeline;
auto camera = pipeline.create<dai::node::Camera>()->build();
auto nn = pipeline.create<dai::node::NeuralNetwork>()->build(camera, model);
auto parser = pipeline.create<dai::node::SegmentationParser>()->build(nn->out, model);
```

## Examples of functionality

 * [Semantic segmentation
   (Python)](https://github.com/luxonis/depthai-core/blob/main/examples/python/Segmentation/semantic_segmentation.py) - Run a
   DeepLabv3+ segmentation model and display the segmentation masks on a video stream.
 * [Segmentation examples (C++)](https://github.com/luxonis/depthai-core/tree/main/examples/cpp/Segmentation) - Browse the C++
   segmentation examples in `depthai-core`.

## Reference

### dai::node::SegmentationParser

Kind: class

SegmentationParser node. Parses raw segmentation output from segmentation neural networks into a dai::SegmentationMask datatype.
The parser supports two output model types:

Single-channel output where the model argmaxes the class probabilities internally and outputs a single channel mask with class
indices.; Multi-channel output where each channel corresponds to the probability map for a specific class. The parser will perform
argmax across channels to generate the final mask. The parser can be configured to treat the first class (index 0) as the
background class, which will be ignored in the final segmentation mask. warning: Only OAK4 supports running SegmentationParser on
device. On other platforms, the node will automatically switch to host execution.

#### std::variant< NNModelDescription , NNArchive , std::string > Model

Kind: typedef

#### std::shared_ptr< SegmentationParserConfig > initialConfig

Kind: variable

Initial config to use when parsing segmentation masks.

#### Input input

Kind: variable

Input NN results with segmentation data to parser

#### Input inputConfig

Kind: variable

Input SegmentationParserConfig message with ability to modify parameters in runtime.

#### Output out

Kind: variable

Outputs segmentation mask

#### ~SegmentationParser()

Kind: function

#### SegmentationParser()

Kind: function

#### SegmentationParser(std::unique_ptr< Properties > props)

Kind: function

#### std::shared_ptr< SegmentationParser > build(Node::Output & nnInput, const Model & model)

Kind: function

Build SegmentationParser node.

parameters: nnInput: Output to link; model: Neural network model

#### std::shared_ptr< SegmentationParser > build(Node::Output & nnInput, const dai::nn_archive::v1::Head & head)

Kind: function

Build SegmentationParser node with the specific head from NNArchive . Useful when model outputs multiple segmentation heads.

parameters: nnInput: Output to link; head: Specific head from NNArchive to use for this parser

#### void setNNArchive(const NNArchive & nnArchive)

Kind: function

Set NNArchive for this Node .

parameters: nnArchive: NNArchive to set

#### void setNNArchiveHead(const dai::nn_archive::v1::Head & head)

Kind: function

Set NNArchive head for this Node .

parameters: head: NNArchive head to set

#### void setLabels(const std::vector< std::string > & labels)

Kind: function

Sets the class labels associated with the segmentation mask. The label at index $i$ in the labels vector corresponds to the value
$i$ in the segmentation mask data array. parameters: labels: Vector of class labels

#### std::vector< std::string > getLabels()

Kind: function

Returns the class labels associated with the segmentation mask.

#### void setBackgroundClass(bool backgroundClass)

Kind: function

Sets whether the first class (index 0) is considered the background class. If true, the pixels classified as index 0 will be
treated as background. parameters: backgroundClass: Boolean indicating if the first class is the background class note: Only
applicable if the number of classes is greater than 1 and the output classes are not in a single layer (eg. classesInOneLayer =
false).

#### bool getBackgroundClass()

Kind: function

Gets whether the first class (index 0) is considered the background class.

#### void setRunOnHost(bool runOnHost)

Kind: function

Specify whether to run on host or device By default, the node will run on device.

#### bool runOnHost()

Kind: function

Check if the node is set to run on host

#### void run()

Kind: function

#### void buildInternal()

Kind: function

Function called from within the create function to build the node. This function is useful for initialization, setting up inputs
and outputs = stuff that cannot be perform in the constuctor.

#### DeviceNodeCRTP()

Kind: function

#### DeviceNodeCRTP(const std::shared_ptr< Device > & device)

Kind: function

#### DeviceNodeCRTP(std::unique_ptr< Properties > props)

Kind: function

#### DeviceNodeCRTP(std::unique_ptr< Properties > props, bool confMode)

Kind: function

#### DeviceNodeCRTP(const std::shared_ptr< Device > & device, std::unique_ptr< Properties > props, bool confMode)

Kind: function

### Need assistance?

Head over to [Discussion Forum](https://discuss.luxonis.com/) for technical support or any other questions you might have.
