# model

Python API: `luxonis_train.config.predefined_models.ocr_recognition.v1.model`

The OCR text recognition model.

## Classes

### OCRRecognitionModel

Text recognition on a cropped image, after [PPOCRv4](https://github.com/PaddlePaddle/PaddleOCR).

The dataset needs the text of each image in the `text` metadata field:

```python
for path, label in zip(image_paths, labels):
    if label:
        yield {
            "file": path,
            "annotation": {"metadata": {"text": label}},
        }
```

Set `alphabet` to the characters to recognize, either as a list or as one of the named alphabets, and `max_text_len` to the
longest string to expect.

 * `Throughput:`: Frames per second at 48x320. * `light`: 50 on RVC2, 300 on RVC4

> **Example**
> The `model` section of a config:

```yaml
model:
  predefined_model:
    name: OCRRecognitionModel
    params:
      variant: light
```

 * `Components:`: * Nodes:
   [PPLCNetV3](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/backbones/pplcnet_v3/pplcnet_v3.md)
   ->
   [SVTRNeck](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/necks/svtr_neck/svtr_neck.md)
   ->
   [OCRCTCHead](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/heads/ocr_ctc_head.md)
    * Losses:
      [CTCLoss](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/attached_modules/losses/ctc_loss.md)
    * Metrics:
      [OCRAccuracy](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/attached_modules/metrics/ocr_accuracy.md)
    * Visualizers:
      [OCRVisualizer](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/attached_modules/visualizers/ocr_visualizer.md)
    * Main metric:
      [OCRAccuracy](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/attached_modules/metrics/ocr_accuracy.md)
    * Variants: `light`

#### Methods

##### init

```python
def __init__(alphabet: list[str] | AlphabetName = 'english', max_text_len: int = 40, ignore_unknown: bool = True, **kwargs):
```

Initialize the model with its default components.

> **Example**
> ```pycon
>>> model = OCRRecognitionModel(
...     alphabet=["a", "b"], max_text_len=8
... )
>>> backbone, _, head = model.nodes
>>> backbone.params
{'max_text_len': 8}
>>> head.params
{'alphabet': ['a', 'b'], 'ignore_unknown': True}
```

Parameters

 * `alphabet` (`list[str] | AlphabetName`): The characters that the head predicts. A name selects a predefined alphabet and logs an info message: * `"english"`: `a` to `z` and `A` to `Z`;
    * `"english_lowercase"`: `a` to `z`;
    * `"numeric"`: `0` to `9`;
    * `"alphanumeric"`: `"english"` and `"numeric"`;
    * `"alphanumeric_lowercase"`: `"english_lowercase"` and `"numeric"`;
    * `"punctuation"`: the space and the ASCII punctuation characters;
    * `"ascii"`: the printable ASCII characters, codes `32` to `126`. The model passes a list on without a change. It does not read the value when `head_params` holds `alphabet`.
 * `max_text_len` (`int`): The number of sequence steps of the backbone output, and so the longest text the model can predict.
 * `ignore_unknown` (`bool`): Whether the head drops a label character that is not in the alphabet. With `False`, the head maps it to an extra `"<UNK>"` class.
 * `**kwargs`: Keyword arguments for [SimplePredefinedModel.init](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/config/predefined_models/base_predefined_model.md).

Raises

 * `ValueError`: When `alphabet` is a string that names no predefined alphabet, and `head_params` holds no `alphabet`.

##### get_variants

```python
def get_variants() -> tuple[str, dict[str, Params]]:
```

Get the default variant name and the available variants.

The model has one variant, `light`. It sets `backbone_variant` to `"rec-light"`, the recognition variant of [PPLCNetV3](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/backbones/pplcnet_v3/pplcnet_v3.md). The variant sets no `weights`, so no node loads a checkpoint.

> **Example**
> ```pycon
>>> OCRRecognitionModel.get_variants()
('light', {'light': {'backbone_variant': 'rec-light'}})
```

Returns

 * `tuple[str, dict[str, Params]]`: `"light"` and the single variant with its constructor arguments.

## Attributes

### AlphabetName
