# classification

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

## Classes

### ClassificationParser

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.

#### Methods

##### init

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

Initializes the parser node.

Parameters

 * `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.
 * `is_softmax` (`bool`): If False, the scores are converted to probabilities using softmax function.

##### build

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

Configures the parser.

Parameters

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

Returns

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

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

Parameters

 * `scores` (`np.ndarray`): Model score tensor.
 * `is_softmax` (`bool`): Whether scores already contain probabilities. If false, apply softmax.

Returns

 * `np.ndarray`: One-dimensional array with one score per class.

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

Parameters

 * `output` (`dai.NNData`): Neural network output carrying tensors and source timestamps, sequence number, and optional image
   transformation.
 * `scores` (`np.ndarray`): Confidence scores corresponding to the computed payload.

##### extract

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

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

 * `np.ndarray`: Flattened dequantized class scores. Their count must match configured classes when a nonzero class count is set.

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.

##### setClasses

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

Sets the class names for the classification model.

Parameters

 * `classes` (`list[str]`): List of class names to be used for linking with their respective scores.

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

##### setSoftmax

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

Sets the softmax flag for the classification model.

Parameters

 * `is_softmax` (`bool`): If False, the parser will convert the scores to probabilities using softmax function.

#### Attributes

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