# model

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

The OCR text recognition model.

## Classes

| Name | Description |
| --- | --- |
| [`OCRRecognitionModel`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/config/predefined_models/ocr_recognition/v1/model.md#model-Classes-OCRRecognitionModel) | Text recognition on a cropped image, after [PPOCRv4](https://github.com/PaddlePaddle/PaddleOCR). |

### class `OCRRecognitionModel`

```python
class OCRRecognitionModel(SimplePredefinedModel)
```

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#pplcnet_v3-Classes-PPLCNetV3)
   ->
   [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#svtr_neck-Classes-SVTRNeck)
   ->
   [OCRCTCHead](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/heads/ocr_ctc_head.md#ocr_ctc_head-Classes-OCRCTCHead)
    * 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#ctc_loss-Classes-CTCLoss)
    * 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#ocr_accuracy-Classes-OCRAccuracy)
    * 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#ocr_visualizer-Classes-OCRVisualizer)
    * 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#ocr_accuracy-Classes-OCRAccuracy)
    * Variants: `light`

Methods

| Name | Description |
| --- | --- |
| [`__init__`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/config/predefined_models/ocr_recognition/v1/model.md#model-Classes-OCRRecognitionModel-__init__) | Initialize the model with its default components. |
| [`get_variants`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/config/predefined_models/ocr_recognition/v1/model.md#model-Classes-OCRRecognitionModel-get_variants) | Get the default variant name and the available variants. |

#### method `__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}
```

Args

 * `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#base_predefined_model-Classes-SimplePredefinedModel-__init__).

Raises

| Type | Description |
| --- | --- |
| `ValueError` | When `alphabet` is a string that names no predefined alphabet, and `head_params` holds no `alphabet`. |

#### static method `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#pplcnet_v3-Classes-PPLCNetV3). The variant sets no `weights`, so no node loads a checkpoint.

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

Returns

| Type | Description |
| --- | --- |
| `tuple[str, dict[str, Params]]` | `"light"` and the single variant with its constructor arguments. |

## Module Attributes

| Name | Type | Description |
| --- | --- | --- |
| [`AlphabetName`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/config/predefined_models/ocr_recognition/v1/model.md#model-Module-Attributes-AlphabetName) | `TypeAlias` |  |

### type alias `AlphabetName`

```python
AlphabetName: TypeAlias = Literal[
    'english',
    'english_lowercase',
    'numeric',
    'alphanumeric',
    'alphanumeric_lowercase',
    'punctuation',
    'ascii',
]
```
