# calamari-ocr/calamari architecture

> Architecture diagram of the GitHub repository calamari-ocr/calamari, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-16). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/calamari-ocr/calamari
- Repository: https://github.com/calamari-ocr/calamari

## Overview

Calamari is a modular Python OCR engine. Its principal workflow takes image-and-text data (or prediction images), prepares samples, runs a configurable recognition model, decodes its CTC output, and returns text predictions. Separate capabilities include training and validation, dataset format handling, evaluation, and ensemble voting. The graph emphasizes these domain stages and the model’s configurable layer/graph internals. Some connections are documented only by the README or are unverified because their implementation was not sampled; those are marked dashed.

Main entry points: [`calamari_ocr/ocr/dataset/data.py`](https://github.com/calamari-ocr/calamari/blob/master/calamari_ocr/ocr/dataset/data.py) (Sample pipeline), [`calamari_ocr/ocr/training/params.py`](https://github.com/calamari-ocr/calamari/blob/master/calamari_ocr/ocr/training/params.py) (Training configuration), [`calamari_ocr/ocr/dataset/pipeline.py`](https://github.com/calamari-ocr/calamari/blob/master/calamari_ocr/ocr/dataset/pipeline.py) (Data pipeline), [`calamari_ocr/ocr/model/ctcdecoder/ctc_decoder.py`](https://github.com/calamari-ocr/calamari/blob/master/calamari_ocr/ocr/model/ctcdecoder/ctc_decoder.py) (CTC decoder)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_data["Data and preparation"]
  node_data_setup["Sample pipeline<br/>[data.py]"]
  node_generator["Sample generator<br/>[base.py]"]
  node_image_prep["Image preparation"]
  node_text_prep["Text processing"]
  node_augmentation["Augmentation<br/>[data_augmenter.py]"]
  node_pipeline["Data pipeline<br/>[pipeline.py]"]
end

subgraph group_recognition["Recognition"]
  node_model["Recognition model<br/>[model.py]"]
  node_graph["Model graph<br/>[graph.py]"]
  node_model_layers["Configurable layers"]
  node_codec["Character codec<br/>[codec.py]"]
  node_decoder["CTC decoder<br/>[ctc_decoder.py]"]
  node_beam_search["Word beam search<br/>[WordBeamSearch.py]"]
  node_predictor["Prediction API<br/>[predictor.py]"]
  node_prediction_result["Prediction result<br/>[params.py]"]
end

subgraph group_operations["Training and evaluation"]
  node_trainer["Trainer<br/>[trainer.py]"]
  node_train_params["Training configuration<br/>[params.py]"]
  node_eval["Evaluator<br/>[evaluator.py]"]
  node_saved_model["Saved models<br/>[saved_model.py]"]
end

subgraph group_ensemble["Ensembling"]
  node_voter["Ensemble voting"]
end

subgraph group_formats["Dataset formats"]
  node_file_reader["File reader<br/>[file.py]"]
  node_pagexml_reader["PAGE reader<br/>[reader.py]"]
  node_abbyy_reader["ABBYY reader<br/>[reader.py]"]
  node_hdf5_reader["HDF5 reader<br/>[reader.py]"]
end

node_user(("OCR user"))

node_user -.->|"requests OCR"| node_predictor
node_user -.->|"trains models"| node_trainer
node_data_setup -->|"configures"| node_file_reader
node_data_setup -->|"uses"| node_pipeline
node_data_setup -->|"configures"| node_image_prep
node_data_setup -->|"configures"| node_text_prep
node_data_setup -->|"configures"| node_augmentation
node_file_reader -->|"extends"| node_generator
node_pagexml_reader -->|"extends"| node_generator
node_pipeline -->|"creates"| node_generator
node_pipeline -.->|"supplies samples"| node_model
node_train_params -->|"configures"| node_model
node_train_params -->|"selects data pipeline"| node_train_params
node_model -->|"builds"| node_graph
node_graph -->|"uses"| node_model_layers
node_predictor -.->|"runs inference"| node_model
node_predictor -.->|"decodes output"| node_decoder
node_decoder -->|"dispatches"| node_beam_search
node_decoder -.->|"produces"| node_prediction_result
node_trainer -.->|"consumes data"| node_pipeline
node_trainer -.->|"uses configuration"| node_train_params
node_eval -->|"loads samples"| node_data_setup
node_eval -->|"uses"| node_text_prep
node_voter -->|"uses decoding"| node_decoder
node_saved_model -->|"loads training settings"| node_train_params
node_data_setup -->|"supports format"| node_pagexml_reader
node_data_setup -->|"supports format"| node_abbyy_reader
node_data_setup -->|"supports format"| node_hdf5_reader

click node_data_setup "https://github.com/calamari-ocr/calamari/blob/master/calamari_ocr/ocr/dataset/data.py"
click node_file_reader "https://github.com/calamari-ocr/calamari/blob/master/calamari_ocr/ocr/dataset/datareader/file.py"
click node_pagexml_reader "https://github.com/calamari-ocr/calamari/blob/master/calamari_ocr/ocr/dataset/datareader/pagexml/reader.py"
click node_abbyy_reader "https://github.com/calamari-ocr/calamari/blob/master/calamari_ocr/ocr/dataset/datareader/abbyy/reader.py"
click node_hdf5_reader "https://github.com/calamari-ocr/calamari/blob/master/calamari_ocr/ocr/dataset/datareader/hdf5/reader.py"
click node_generator "https://github.com/calamari-ocr/calamari/blob/master/calamari_ocr/ocr/dataset/datareader/base.py"
click node_image_prep "https://github.com/calamari-ocr/calamari/blob/master/calamari_ocr/ocr/dataset/imageprocessors/default_image_processors.py"
click node_text_prep "https://github.com/calamari-ocr/calamari/blob/master/calamari_ocr/ocr/dataset/textprocessors/default_text_processor.py"
click node_augmentation "https://github.com/calamari-ocr/calamari/blob/master/calamari_ocr/ocr/augmentation/data_augmenter.py"
click node_pipeline "https://github.com/calamari-ocr/calamari/blob/master/calamari_ocr/ocr/dataset/pipeline.py"
click node_model "https://github.com/calamari-ocr/calamari/blob/master/calamari_ocr/ocr/model/model.py"
click node_graph "https://github.com/calamari-ocr/calamari/blob/master/calamari_ocr/ocr/model/graph.py"
click node_model_layers "https://github.com/calamari-ocr/calamari/tree/master/calamari_ocr/ocr/model/layers"
click node_codec "https://github.com/calamari-ocr/calamari/blob/master/calamari_ocr/ocr/dataset/codec.py"
click node_decoder "https://github.com/calamari-ocr/calamari/blob/master/calamari_ocr/ocr/model/ctcdecoder/ctc_decoder.py"
click node_beam_search "https://github.com/calamari-ocr/calamari/blob/master/calamari_ocr/thirdparty/ctcwordbeamsearch/WordBeamSearch.py"
click node_predictor "https://github.com/calamari-ocr/calamari/blob/master/calamari_ocr/ocr/predict/predictor.py"
click node_prediction_result "https://github.com/calamari-ocr/calamari/blob/master/calamari_ocr/ocr/predict/params.py"
click node_trainer "https://github.com/calamari-ocr/calamari/blob/master/calamari_ocr/ocr/training/trainer.py"
click node_train_params "https://github.com/calamari-ocr/calamari/blob/master/calamari_ocr/ocr/training/params.py"
click node_eval "https://github.com/calamari-ocr/calamari/blob/master/calamari_ocr/ocr/evaluator.py"
click node_voter "https://github.com/calamari-ocr/calamari/blob/master/calamari_ocr/ocr/voting/confidence_voter.py"
click node_saved_model "https://github.com/calamari-ocr/calamari/blob/master/calamari_ocr/ocr/savedmodel/saved_model.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_data_setup,node_generator,node_image_prep,node_text_prep,node_augmentation,node_pipeline,node_user toneBlue
class node_model,node_graph,node_model_layers,node_codec,node_decoder,node_beam_search,node_predictor,node_prediction_result toneAmber
class node_trainer,node_train_params,node_eval,node_saved_model toneMint
class node_voter toneRose
class node_file_reader,node_pagexml_reader,node_abbyy_reader,node_hdf5_reader toneIndigo
```

## Components

### Data and preparation

- [Sample pipeline](https://github.com/calamari-ocr/calamari/blob/master/calamari_ocr/ocr/dataset/data.py): `calamari_ocr/ocr/dataset/data.py`
- [Sample generator](https://github.com/calamari-ocr/calamari/blob/master/calamari_ocr/ocr/dataset/datareader/base.py): `calamari_ocr/ocr/dataset/datareader/base.py`
- [Image preparation](https://github.com/calamari-ocr/calamari/blob/master/calamari_ocr/ocr/dataset/imageprocessors/default_image_processors.py): `calamari_ocr/ocr/dataset/imageprocessors/default_image_processors.py`
- [Text processing](https://github.com/calamari-ocr/calamari/blob/master/calamari_ocr/ocr/dataset/textprocessors/default_text_processor.py): `calamari_ocr/ocr/dataset/textprocessors/default_text_processor.py`
- [Augmentation](https://github.com/calamari-ocr/calamari/blob/master/calamari_ocr/ocr/augmentation/data_augmenter.py): `calamari_ocr/ocr/augmentation/data_augmenter.py`
- [Data pipeline](https://github.com/calamari-ocr/calamari/blob/master/calamari_ocr/ocr/dataset/pipeline.py): `calamari_ocr/ocr/dataset/pipeline.py`

### Recognition

- [Recognition model](https://github.com/calamari-ocr/calamari/blob/master/calamari_ocr/ocr/model/model.py): `calamari_ocr/ocr/model/model.py`
- [Model graph](https://github.com/calamari-ocr/calamari/blob/master/calamari_ocr/ocr/model/graph.py): `calamari_ocr/ocr/model/graph.py`
- [Configurable layers](https://github.com/calamari-ocr/calamari/tree/master/calamari_ocr/ocr/model/layers): `calamari_ocr/ocr/model/layers`
- [Character codec](https://github.com/calamari-ocr/calamari/blob/master/calamari_ocr/ocr/dataset/codec.py): `calamari_ocr/ocr/dataset/codec.py`
- [CTC decoder](https://github.com/calamari-ocr/calamari/blob/master/calamari_ocr/ocr/model/ctcdecoder/ctc_decoder.py): `calamari_ocr/ocr/model/ctcdecoder/ctc_decoder.py`
- [Word beam search](https://github.com/calamari-ocr/calamari/blob/master/calamari_ocr/thirdparty/ctcwordbeamsearch/WordBeamSearch.py): `calamari_ocr/thirdparty/ctcwordbeamsearch/WordBeamSearch.py`
- [Prediction API](https://github.com/calamari-ocr/calamari/blob/master/calamari_ocr/ocr/predict/predictor.py): `calamari_ocr/ocr/predict/predictor.py`
- [Prediction result](https://github.com/calamari-ocr/calamari/blob/master/calamari_ocr/ocr/predict/params.py): `calamari_ocr/ocr/predict/params.py`

### Training and evaluation

- [Trainer](https://github.com/calamari-ocr/calamari/blob/master/calamari_ocr/ocr/training/trainer.py): `calamari_ocr/ocr/training/trainer.py`
- [Training configuration](https://github.com/calamari-ocr/calamari/blob/master/calamari_ocr/ocr/training/params.py): `calamari_ocr/ocr/training/params.py`
- [Evaluator](https://github.com/calamari-ocr/calamari/blob/master/calamari_ocr/ocr/evaluator.py): `calamari_ocr/ocr/evaluator.py`
- [Saved models](https://github.com/calamari-ocr/calamari/blob/master/calamari_ocr/ocr/savedmodel/saved_model.py): `calamari_ocr/ocr/savedmodel/saved_model.py`

### Ensembling

- [Ensemble voting](https://github.com/calamari-ocr/calamari/blob/master/calamari_ocr/ocr/voting/confidence_voter.py): `calamari_ocr/ocr/voting/confidence_voter.py`

### Dataset formats

- [File reader](https://github.com/calamari-ocr/calamari/blob/master/calamari_ocr/ocr/dataset/datareader/file.py): `calamari_ocr/ocr/dataset/datareader/file.py`
- [PAGE reader](https://github.com/calamari-ocr/calamari/blob/master/calamari_ocr/ocr/dataset/datareader/pagexml/reader.py): `calamari_ocr/ocr/dataset/datareader/pagexml/reader.py`
- [ABBYY reader](https://github.com/calamari-ocr/calamari/blob/master/calamari_ocr/ocr/dataset/datareader/abbyy/reader.py): `calamari_ocr/ocr/dataset/datareader/abbyy/reader.py`
- [HDF5 reader](https://github.com/calamari-ocr/calamari/blob/master/calamari_ocr/ocr/dataset/datareader/hdf5/reader.py): `calamari_ocr/ocr/dataset/datareader/hdf5/reader.py`

### Other components

- **OCR user**

## Connections

- OCR user → Prediction API: requests OCR
- OCR user → Trainer: trains models
- Sample pipeline → File reader: configures (evidence: `calamari_ocr/ocr/dataset/data.py`)
- Sample pipeline → Data pipeline: uses (evidence: `calamari_ocr/ocr/dataset/data.py`)
- Sample pipeline → Image preparation: configures (evidence: `calamari_ocr/ocr/dataset/data.py`)
- Sample pipeline → Text processing: configures (evidence: `calamari_ocr/ocr/dataset/data.py`)
- Sample pipeline → Augmentation: configures (evidence: `calamari_ocr/ocr/dataset/data.py`)
- File reader → Sample generator: extends (evidence: `calamari_ocr/ocr/dataset/datareader/file.py`)
- PAGE reader → Sample generator: extends (evidence: `calamari_ocr/ocr/dataset/datareader/pagexml/reader.py`)
- Data pipeline → Sample generator: creates (evidence: `calamari_ocr/ocr/dataset/pipeline.py`)
- Data pipeline → Recognition model: supplies samples
- Training configuration → Recognition model: configures (evidence: `calamari_ocr/ocr/training/params.py`)
- Training configuration → Training configuration: selects data pipeline (evidence: `calamari_ocr/ocr/training/params.py`)
- Recognition model → Model graph: builds (evidence: `calamari_ocr/ocr/model/params.py`)
- Model graph → Configurable layers: uses (evidence: `calamari_ocr/ocr/model/graph.py`)
- Prediction API → Recognition model: runs inference
- Prediction API → CTC decoder: decodes output
- CTC decoder → Word beam search: dispatches (evidence: `calamari_ocr/ocr/model/ctcdecoder/ctcwordbeamsearchdecoder.py`)
- CTC decoder → Prediction result: produces (evidence: `calamari_ocr/ocr/model/ctcdecoder/ctc_decoder.py`)
- Trainer → Data pipeline: consumes data
- Trainer → Training configuration: uses configuration
- Evaluator → Sample pipeline: loads samples (evidence: `calamari_ocr/ocr/evaluator.py`)
- Evaluator → Text processing: uses (evidence: `calamari_ocr/ocr/evaluator.py`)
- Ensemble voting → CTC decoder: uses decoding (evidence: `calamari_ocr/ocr/voting/confidence_voter.py`)
- Saved models → Training configuration: loads training settings (evidence: `calamari_ocr/ocr/savedmodel/saved_model.py`)
- Sample pipeline → PAGE reader: supports format (evidence: `calamari_ocr/ocr/dataset/params.py`)
- Sample pipeline → ABBYY reader: supports format (evidence: `calamari_ocr/ocr/dataset/params.py`)
- Sample pipeline → HDF5 reader: supports format (evidence: `calamari_ocr/ocr/dataset/params.py`)
