# classification

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

## Classes

| Name | Description |
| --- | --- |
| [`ClassificationParser`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/classification.md#classification-Classes-ClassificationParser) | Postprocessing logic for Classification model. |

### class `ClassificationParser`

```python
class ClassificationParser(BaseParser)
```

Postprocessing logic for Classification model.

Output messages:

Type : dai.beta.Classifications

Description: An object with attributes `classes` and `scores`. `classes` is a list of classes, sorted in descending order of
scores. `scores` is a list of corresponding scores.

Attributes

| Name | Description |
| --- | --- |
| `classes` | List of class names to be used for linking with their respective scores. Expected to be in the same order as Neural Network's output. If not provided, the message will only return sorted scores. |
| `is_softmax` | If False, the scores are converted to probabilities using softmax function. |
| `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/classification.md#classification-Classes-ClassificationParser-__init__) | Initializes the parser node. |
| [`build`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/classification.md#classification-Classes-ClassificationParser-build) | Configures the parser. |
| [`compute`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/classification.md#classification-Classes-ClassificationParser-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/classification.md#classification-Classes-ClassificationParser-emit) | Create a `dai.beta.Classifications` message and send it on `out`. |
| [`extract`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/classification.md#classification-Classes-ClassificationParser-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/classification.md#classification-Classes-ClassificationParser-run) | Read queued network outputs, parse them, and emit results while running. |
| [`setClasses`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/classification.md#classification-Classes-ClassificationParser-setClasses) | Sets the class names for the classification model. |
| [`setOutputLayerName`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/classification.md#classification-Classes-ClassificationParser-setOutputLayerName) | Sets the name of the output layer. |
| [`setSoftmax`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/classification.md#classification-Classes-ClassificationParser-setSoftmax) | Sets the softmax flag for the classification model. |

#### method `__init__`

```python
def __init__(
    output_layer_name: str = '',
    classes: list[str] = None,
    is_softmax: bool = True,
)
```

Initializes the parser node.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `output_layer_name` | `str` | Name of the output layer relevant to the parser. | `''` |
| `classes` | `list[str]` | List of class names to be used for linking with their respective scores. Expected to be in the same order as Neural Network's output. If not provided, the message will only return sorted scores. | `None` |
| `is_softmax` | `bool` | If False, the scores are converted to probabilities using softmax function. | `True` |

#### method `build`

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

Configures the parser.

Args

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

Returns

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

#### static method `compute`

```python
def compute(scores: np.ndarray, *, is_softmax: bool = True) -> np.ndarray
```

Compute parser results from extracted tensors without sending messages.

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

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `scores` | `np.ndarray` | Model score tensor. | required |
| `is_softmax` | `bool` | Whether scores already contain probabilities. If false, apply softmax. | `True` |

Returns

| Type | Description |
| --- | --- |
| `np.ndarray` | One-dimensional array with one score per class. |

#### method `emit`

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

Create a `dai.beta.Classifications` 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 |
| `scores` | `np.ndarray` | Confidence scores corresponding to the computed payload. | required |

#### method `extract`

```python
def extract(output: dai.NNData) -> np.ndarray
```

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 |
| --- | --- |
| `np.ndarray` | Flattened dequantized class scores. Their count must match configured classes when a nonzero class count is set. |

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 `setClasses`

```python
def setClasses(classes: list[str])
```

Sets the class names for the classification model.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `classes` | `list[str]` | List of class names to be used for linking with their respective scores. | 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 |

#### method `setSoftmax`

```python
def setSoftmax(is_softmax: bool)
```

Sets the softmax flag for the classification model.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `is_softmax` | `bool` | If False, the parser will convert the scores to probabilities using softmax function. | required |
