Metadata-Version: 2.4
Name: onnxtr
Version: 0.10.0
Summary: Onnx Text Recognition (OnnxTR): docTR Onnx-Wrapper for high-performance OCR on documents.
Author-email: Felix Dittrich <felixdittrich92@gmail.com>
Maintainer: Felix Dittrich
License-Expression: Apache-2.0
Project-URL: repository, https://github.com/felixdittrich92/OnnxTR
Project-URL: tracker, https://github.com/felixdittrich92/OnnxTR/issues
Project-URL: changelog, https://github.com/felixdittrich92/OnnxTR/releases
Keywords: OCR,deep learning,computer vision,onnx,text detection,text recognition,docTR,document analysis,document processing,document AI
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Education
Classifier: Intended Audience :: Science/Research
Classifier: Natural Language :: English
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Python: <4,>=3.11.0
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy<3.0.0,>=1.16.0
Requires-Dist: scipy<2.0.0,>=1.4.0
Requires-Dist: pypdfium2<6.0.0,>=4.11.0
Requires-Dist: pyclipper<2.0.0,>=1.2.0
Requires-Dist: rapidfuzz<4.0.0,>=3.0.0
Requires-Dist: langdetect<2.0.0,>=1.0.9
Requires-Dist: huggingface-hub<3.0.0,>=1.0.0
Requires-Dist: Pillow>=9.2.0
Requires-Dist: defusedxml>=0.7.0
Requires-Dist: anyascii>=0.3.2
Requires-Dist: tqdm>=4.30.0
Provides-Extra: cpu
Requires-Dist: onnxruntime>=1.18.0; extra == "cpu"
Requires-Dist: opencv-python<6.0.0,>=4.5.0; extra == "cpu"
Provides-Extra: gpu
Requires-Dist: onnxruntime-gpu>=1.18.0; extra == "gpu"
Requires-Dist: opencv-python<6.0.0,>=4.5.0; extra == "gpu"
Provides-Extra: openvino
Requires-Dist: onnxruntime-openvino>=1.18.0; extra == "openvino"
Requires-Dist: opencv-python<6.0.0,>=4.5.0; extra == "openvino"
Provides-Extra: cpu-headless
Requires-Dist: onnxruntime>=1.18.0; extra == "cpu-headless"
Requires-Dist: opencv-python-headless<6.0.0,>=4.5.0; extra == "cpu-headless"
Provides-Extra: gpu-headless
Requires-Dist: onnxruntime-gpu>=1.18.0; extra == "gpu-headless"
Requires-Dist: opencv-python-headless<6.0.0,>=4.5.0; extra == "gpu-headless"
Provides-Extra: openvino-headless
Requires-Dist: onnxruntime-openvino>=1.18.0; extra == "openvino-headless"
Requires-Dist: opencv-python-headless<6.0.0,>=4.5.0; extra == "openvino-headless"
Provides-Extra: html
Requires-Dist: weasyprint>=55.0; extra == "html"
Provides-Extra: viz
Requires-Dist: matplotlib>=3.1.0; extra == "viz"
Requires-Dist: mplcursors>=0.3; extra == "viz"
Provides-Extra: testing
Requires-Dist: pytest>=5.3.2; extra == "testing"
Requires-Dist: coverage[toml]>=4.5.4; extra == "testing"
Requires-Dist: requests>=2.20.0; extra == "testing"
Requires-Dist: pytest-memray>=1.7.0; extra == "testing"
Requires-Dist: psutil>=7.0.0; extra == "testing"
Requires-Dist: onnx>=1.18.0; extra == "testing"
Requires-Dist: pandas>=2.0.0; extra == "testing"
Provides-Extra: quality
Requires-Dist: ruff>=0.1.5; extra == "quality"
Requires-Dist: mypy>=0.812; extra == "quality"
Requires-Dist: pre-commit>=2.17.0; extra == "quality"
Provides-Extra: dev
Requires-Dist: onnxruntime>=1.18.0; extra == "dev"
Requires-Dist: opencv-python<6.0.0,>=4.5.0; extra == "dev"
Requires-Dist: weasyprint>=55.0; extra == "dev"
Requires-Dist: matplotlib>=3.1.0; extra == "dev"
Requires-Dist: mplcursors>=0.3; extra == "dev"
Requires-Dist: pytest>=5.3.2; extra == "dev"
Requires-Dist: coverage[toml]>=4.5.4; extra == "dev"
Requires-Dist: requests>=2.20.0; extra == "dev"
Requires-Dist: pytest-memray>=1.7.0; extra == "dev"
Requires-Dist: psutil>=7.0.0; extra == "dev"
Requires-Dist: onnx>=1.18.0; extra == "dev"
Requires-Dist: pandas>=2.0.0; extra == "dev"
Requires-Dist: ruff>=0.1.5; extra == "dev"
Requires-Dist: mypy>=0.812; extra == "dev"
Requires-Dist: pre-commit>=2.17.0; extra == "dev"
Dynamic: license-file

<p align="center">
  <img src="https://github.com/felixdittrich92/OnnxTR/raw/main/docs/images/logo.jpg" width="40%">
</p>

[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](LICENSE)
![Build Status](https://github.com/felixdittrich92/onnxtr/workflows/builds/badge.svg)
[![codecov](https://codecov.io/gh/felixdittrich92/OnnxTR/graph/badge.svg?token=WVFRCQBOLI)](https://codecov.io/gh/felixdittrich92/OnnxTR)
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/4fff4d764bb14fb8b4f4afeb9587231b)](https://app.codacy.com/gh/felixdittrich92/OnnxTR/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)
[![CodeFactor](https://www.codefactor.io/repository/github/felixdittrich92/onnxtr/badge)](https://www.codefactor.io/repository/github/felixdittrich92/onnxtr)
[![Socket Badge](https://badge.socket.dev/pypi/package/onnxtr/0.10.0?artifact_id=tar-gz)](https://badge.socket.dev/pypi/package/onnxtr/0.10.0?artifact_id=tar-gz)
[![Pypi](https://img.shields.io/badge/pypi-v0.10.0-blue.svg)](https://pypi.org/project/OnnxTR/)
[![Docker Images](https://img.shields.io/badge/Docker-4287f5?style=flat&logo=docker&logoColor=white)](https://github.com/felixdittrich92/OnnxTR/pkgs/container/onnxtr)
[![Hugging Face Spaces](https://img.shields.io/badge/%F0%9F%A4%97%20Hugging%20Face-Spaces-blue)](https://huggingface.co/spaces/Felix92/OnnxTR-OCR)
![PyPI - Downloads](https://img.shields.io/pypi/dm/onnxtr)

> :warning: Please note that this is a wrapper around the [doctr](https://github.com/mindee/doctr) library to provide a Onnx pipeline for docTR. For feature requests, which are not directly related to the Onnx pipeline, please refer to the base project.

**Optical Character Recognition made seamless & accessible to anyone, powered by Onnx**

What you can expect from this repository:

- efficient ways to parse textual information (localize and identify each word) from your documents
- a Onnx pipeline for docTR, a wrapper around the [doctr](https://github.com/mindee/doctr) library - no PyTorch or TensorFlow dependencies
- more lightweight package with faster inference latency and less required resources
- 8-Bit quantized models for faster inference on CPU

![OCR_example](https://github.com/felixdittrich92/OnnxTR/raw/main/docs/images/ocr.png)

## Installation

### Prerequisites

Python 3.11 (or higher) and [pip](https://pip.pypa.io/en/stable/) are required to install OnnxTR.

### Latest release

You can then install the latest release of the package using [pypi](https://pypi.org/project/OnnxTR/) as follows:

**NOTE:**

Currently supported execution providers by default are: CPU, CUDA (NVIDIA GPU), OpenVINO (Intel CPU | GPU), CoreML (Apple Silicon).

For GPU support please take a look at: [ONNX Runtime](https://onnxruntime.ai/getting-started).

- **Prerequisites:** CUDA & cuDNN needs to be installed before [Version table](https://onnxruntime.ai/docs/execution-providers/CUDA-ExecutionProvider.html).

```shell
# standard cpu support
pip install "onnxtr[cpu]"
pip install "onnxtr[cpu-headless]"  # same as cpu but with opencv-headless
# with gpu support
pip install "onnxtr[gpu]"
pip install "onnxtr[gpu-headless]"  # same as gpu but with opencv-headless
# OpenVINO cpu | gpu support for Intel CPUs | GPUs
pip install "onnxtr[openvino]"
pip install "onnxtr[openvino-headless]"  # same as openvino but with opencv-headless
# with HTML support
pip install "onnxtr[html]"
# with support for visualization
pip install "onnxtr[viz]"
# with support for all dependencies
pip install "onnxtr[html, gpu, viz]"
```

**Recommendation:**

If you have:

- a NVIDIA GPU, use one of the `gpu` variants
- an Intel CPU or GPU, use one of the `openvino` variants
- an Apple Silicon Mac, use one of the `cpu` variants (CoreML is auto-detected)
- otherwise, use one of the `cpu` variants

**OpenVINO:**

By default OnnxTR running with the OpenVINO execution provider backend uses the `CPU` device with `FP32` precision, to change the device or for further configuaration please refer to the [ONNX Runtime OpenVINO documentation](https://onnxruntime.ai/docs/execution-providers/OpenVINO-ExecutionProvider.html#summary-of-options).

### Reading files

Documents can be interpreted from PDF / Images / Webpages / Multiple page images using the following code snippet:

```python
from onnxtr.io import DocumentFile

# PDF
pdf_doc = DocumentFile.from_pdf("path/to/your/doc.pdf")
# Image
single_img_doc = DocumentFile.from_images("path/to/your/img.jpg")
# Webpage (requires `weasyprint` to be installed)
webpage_doc = DocumentFile.from_url("https://www.yoursite.com")
# Multiple page images
multi_img_doc = DocumentFile.from_images(["path/to/page1.jpg", "path/to/page2.jpg"])
# Every PDF and image of a folder
folder_doc = DocumentFile.from_files("path/to/your/folder")
```

Long documents and large image collections can be opened lazily: the pages are only decoded when they are processed,
chunk by chunk (`page_chunk_size` pages at a time), so that the memory footprint does not grow with the number of pages.

```python
from onnxtr.io import DocumentFile
from onnxtr.models import ocr_predictor

model = ocr_predictor(page_chunk_size=16)
doc = DocumentFile.from_pdf("path/to/your/long_doc.pdf", lazy=True)
# Predict on the whole document
result = model(doc)
# Or get the pages one by one, as soon as they are processed
for page in model.stream(doc, on_error="skip"):  # skip the pages that cannot be read
    print(page.page_idx, page.render())
```

### Putting it together

Let's use the default `ocr_predictor` model for an example:

```python
from onnxtr.io import DocumentFile
from onnxtr.models import ocr_predictor, EngineConfig

model = ocr_predictor(
    det_arch="fast_base",  # detection architecture
    reco_arch="vitstr_base",  # recognition architecture
    det_bs=2,  # detection batch size
    reco_bs=512,  # recognition batch size
    # Document related parameters
    assume_straight_pages=True,  # set to `False` if the pages are not straight (rotation, perspective, etc.) (default: True)
    straighten_pages=False,  # set to `True` if the pages should be straightened before final processing (default: False)
    export_as_straight_boxes=False,  # set to `True` if the boxes should be exported as if the pages were straight (default: False)
    # Recognition related parameters
    confidence_aggregation="mean",  # set to "mean", "min", "max", "median", "geometric_mean", "harmonic_mean" or a Python callable taking the 1D array of probabilities and returning a float (default: None) - it aggregates the character probabilities of the recognition model into the word confidence
    # Preprocessing related parameters
    preserve_aspect_ratio=True,  # set to `False` if the aspect ratio should not be preserved (default: True)
    symmetric_pad=True,  # set to `False` to disable symmetric padding (default: True)
    # Additional parameters - meta information
    detect_orientation=False,  # set to `True` if the orientation of the pages should be detected (default: False)
    detect_language=False,  # set to `True` if the language of the pages should be detected (default: False)
    # Layout & table analysis
    detect_layout=False,  # set to `True` to attach the detected layout regions to each page (default: False)
    layout_arch="lw_detr_s",  # layout architecture to use (default: lw_detr_s)
    ignore_regions=None,  # optional list of layout classes to mask out before detection, e.g. ["Picture"] (default: None)
    detect_tables=False,  # set to `True` to recognize the structure of detected tables (default: False)
    detect_formulas=False,  # set to `True` to read the detected formulas as LaTeX (default: False)
    # Orientation specific parameters in combination with `assume_straight_pages=False` and/or `straighten_pages=True`
    disable_crop_orientation=False,  # set to `True` if the crop orientation classification should be disabled (default: False)
    disable_page_orientation=False,  # set to `True` if the general page orientation classification should be disabled (default: False)
    # DocumentBuilder specific parameters
    resolve_lines=True,  # whether words should be automatically grouped into lines (default: True)
    resolve_blocks=False,  # whether lines should be automatically grouped into blocks (default: False)
    paragraph_break=0.035,  # relative length of the minimum space separating paragraphs (default: 0.035)
    keep_reading_order=False,  # whether the elements should be re-ordered by reading order (default: False)
    preserve_original_coords=False,  # with `straighten_pages=True`, map the boxes back onto the original page (default: False)
    # Long documents
    page_chunk_size=32,  # maximum number of pages of a lazy page source / iterator processed at once (default: 32)
    # OnnxTR specific parameters
    # NOTE: 8-Bit quantized models are not available for FAST, MASTER, VIPTR, LW-DETR, TableCenterNet and FormulaNet models
    # (the full precision model is loaded instead) and can in general lead to poorer accuracy
    load_in_8_bit=False,  # set to `True` to load 8-bit quantized models instead of the full precision onces (default: False)
    # Advanced engine configuration options
    det_engine_cfg=EngineConfig(),  # detection model engine configuration (default: internal predefined configuration)
    reco_engine_cfg=EngineConfig(),  # recognition model engine configuration (default: internal predefined configuration)
    clf_engine_cfg=EngineConfig(),  # classification (orientation) model engine configuration (default: internal predefined configuration)
    layout_engine_cfg=EngineConfig(),  # layout model engine configuration (default: internal predefined configuration)
    table_engine_cfg=EngineConfig(),  # table structure model engine configuration (default: internal predefined configuration)
    formula_engine_cfg=EngineConfig(),  # formula recognition model engine configuration (default: internal predefined configuration)
)
# PDF
doc = DocumentFile.from_pdf("path/to/your/doc.pdf")
# Analyze
result = model(doc)
# Display the result (requires matplotlib & mplcursors to be installed)
result.show()
```

![Visualization sample](https://github.com/felixdittrich92/OnnxTR/raw/main/docs/images/doctr_example_script.gif)

Or even rebuild the original document from its predictions:

```python
import matplotlib.pyplot as plt

synthetic_pages = result.synthesize()
plt.imshow(synthetic_pages[0])
plt.axis("off")
plt.show()
```

![Synthesis sample](https://github.com/felixdittrich92/OnnxTR/raw/main/docs/images/synthesized_sample.png)

The `ocr_predictor` returns a `Document` object with a nested structure (with `Page`, `Block`, `Line`, `Word`, `Artefact`).
To get a better understanding of the document model, check out [documentation](https://mindee.github.io/doctr/modules/io.html#document-structure):

You can also export them as a nested dict, more appropriate for JSON format / render it or export as XML (hocr format):

```python
json_output = result.export()  # nested dict
text_output = result.render()  # human-readable text
xml_output = result.export_as_xml()  # hocr format
for output in xml_output:
    xml_bytes_string = output[0]
    xml_element = output[1]
```

<details>
  <summary>Advanced engine configuration options</summary>

You can also define advanced engine configurations for the models / predictors:

```python
from onnxruntime import SessionOptions

from onnxtr.models import ocr_predictor, EngineConfig

general_options = (
    SessionOptions()
)  # For configuartion options see: https://onnxruntime.ai/docs/api/python/api_summary.html#sessionoptions
general_options.enable_cpu_mem_arena = False

# NOTE: The following would force to run only on the GPU if no GPU is available it will raise an error
# List of strings e.g. ["CUDAExecutionProvider", "CPUExecutionProvider"] or a list of tuples with the provider and its options e.g.
# [("CUDAExecutionProvider", {"device_id": 0}), ("CPUExecutionProvider", {"arena_extend_strategy": "kSameAsRequested"})]
providers = [
    ("CUDAExecutionProvider", {"device_id": 0, "cudnn_conv_algo_search": "DEFAULT"})
]  # For available providers see: https://onnxruntime.ai/docs/execution-providers/

engine_config = EngineConfig(session_options=general_options, providers=providers)
# We use the default predictor with the custom engine configuration
# NOTE: You can define differnt engine configurations for detection, recognition and classification depending on your needs
predictor = ocr_predictor(det_engine_cfg=engine_config, reco_engine_cfg=engine_config, clf_engine_cfg=engine_config)
```

You can also dynamically configure whether the memory arena should shrink:

```python
from random import random
from onnxruntime import RunOptions, SessionOptions

from onnxtr.models import ocr_predictor, EngineConfig


def arena_shrinkage_handler(run_options: RunOptions) -> RunOptions:
    """
    Shrink the memory arena on 10% of inference runs.
    """
    if random() < 0.1:
        run_options.add_run_config_entry("memory.enable_memory_arena_shrinkage", "cpu:0")
    return run_options


engine_config = EngineConfig(run_options_provider=arena_shrinkage_handler)
engine_config.session_options.enable_mem_pattern = False

predictor = ocr_predictor(det_engine_cfg=engine_config, reco_engine_cfg=engine_config, clf_engine_cfg=engine_config)
```

</details>

### Layout analysis & table structure recognition

Layout regions and table structure can be attached to every page. Table recognition builds on the
layout model, so enabling `detect_tables` enables layout detection as well.

```python
from onnxtr.io import DocumentFile
from onnxtr.models import ocr_predictor

doc = DocumentFile.from_pdf("path/to/your/doc.pdf")

model = ocr_predictor(detect_layout=True, detect_tables=True)
result = model(doc)

page = result.pages[0]

# Layout regions (DocLayNet classes: Text, Title, Table, Picture, Page-header, ...)
for region in page.layout:
    print(region.type, region.confidence, region.geometry)

# Tables - words falling inside a table are regrouped here and removed from `page.blocks`
for table in page.tables:
    print(f"{table.num_rows}x{table.num_cols}")
    for cell in table.cells:
        print(cell.row_start, cell.col_start, cell.value)
```

`ignore_regions` masks the given layout classes out of the page before detection and recognition
run, which is useful to skip figures or stamps:

```python
model = ocr_predictor(detect_layout=True, ignore_regions=["Picture", "Formula"])
```

Two layout architectures are available: `lw_detr_s` (default) and the more accurate `lw_detr_m`
(`ocr_predictor(detect_layout=True, layout_arch="lw_detr_m")`).

### Formula recognition

With `detect_formulas=True`, the formula regions found by the layout model are masked out of the text detection,
cropped and read as LaTeX by a formula recognition model (FormulaNet). They are exported as display math
(e.g. `$$...$$` in Markdown).

```python
from onnxtr.io import DocumentFile
from onnxtr.models import formula_predictor, ocr_predictor

model = ocr_predictor(detect_formulas=True)
result = model(DocumentFile.from_pdf("path/to/your/doc.pdf"))
for formula in result.pages[0].formulas:
    print(formula.value, formula.confidence, formula.geometry)
print(result.export_as_markdown())

# Or read formula images directly
predictor = formula_predictor("formulanet")
latex, confidence = predictor(DocumentFile.from_images("path/to/formula.png"))[0]
```

### Exporting the results

Beyond `export()` and `render()`, a document can be exported to several document formats:

```python
result.export_as("markdown")  # or "md"
result.export_as("asciidoc")  # or "adoc"
result.export_as("html")
result.export_as("xml")  # hOCR, one (bytes, ElementTree) tuple per page
result.export_as("text")  # or "txt"
result.export_as("json")  # or "dict"

# Reading order can be resolved on export
result.export_as("markdown", direction="auto")  # auto | ltr | rtl | ttb-rtl | ttb-ltr
```

## Command line interface

OnnxTR ships an `onnxtr-cli` command for running OCR without writing any Python:

```shell
# Basic usage - the export format is inferred from the output extension
onnxtr-cli --input_path path/to/your/doc.pdf --output results.json

# Pick architectures, export as markdown
onnxtr-cli --input_path page.png --output results.md \
    --det_arch fast_base --reco_arch parseq

# Layout regions and table structure, as hOCR (one file per page)
onnxtr-cli --input_path doc.pdf --output results.xml \
    --detect_layout --detect_tables

# Rotated documents, mapping the boxes back onto the original page
onnxtr-cli --input_path scan.jpg --output results.json \
    --no-assume_straight_pages --straighten_pages --preserve_original_coords

# Run on GPU with the 8-bit quantized models
onnxtr-cli --input_path doc.pdf --output results.txt --device cuda --load_in_8_bit

# Several inputs (images, PDFs and URLs) concatenated into a single document
onnxtr-cli --input_path a.png b.pdf https://example.com --output results.html

# Formulas read as LaTeX (display math in the Markdown export)
onnxtr-cli --input_path paper.pdf --output results.md --detect_formulas

# A whole folder of PDFs and images, processed chunk by chunk, skipping the files that cannot be read
onnxtr-cli --input_path path/to/folder --output results.json --page_chunk_size 16 --on_error skip
```

`--device` accepts `auto` (the default, picking CUDA when available), `cpu`, `cuda`, or an
explicit onnxruntime execution provider name. Run `onnxtr-cli --help` for the full option list,
which mirrors the `ocr_predictor` arguments.

## Loading custom exported models

You can also load docTR custom exported models:
For exporting please take a look at the [doctr documentation](https://mindee.github.io/doctr/using_doctr/using_model_export.html#export-to-onnx).

```python
from onnxtr.models import ocr_predictor, linknet_resnet18, parseq

reco_model = parseq("path_to_custom_model.onnx", vocab="ABC")
det_model = linknet_resnet18("path_to_custom_model.onnx")
model = ocr_predictor(det_arch=det_model, reco_arch=reco_model)
```

## Loading models from HuggingFace Hub

You can also load models from the HuggingFace Hub:

```python
from onnxtr.io import DocumentFile
from onnxtr.models import ocr_predictor, from_hub

img = DocumentFile.from_images(["<image_path>"])
# Load your model from the hub
model = from_hub("onnxtr/my-model")

# Pass it to the predictor
# If your model is a recognition model:
predictor = ocr_predictor(det_arch="db_mobilenet_v3_large", reco_arch=model)

# If your model is a detection model:
predictor = ocr_predictor(det_arch=model, reco_arch="crnn_mobilenet_v3_small")

# Get your predictions
res = predictor(img)
```

HF Hub search: [here](https://huggingface.co/models?search=onnxtr).

Collection: [here](https://huggingface.co/collections/Felix92/onnxtr-66bf213a9f88f7346c90e842)

Or push your own models to the hub:

```python
from onnxtr.models import parseq, push_to_hf_hub, login_to_hub
from onnxtr.utils.vocabs import VOCABS

# Login to the hub
login_to_hub()

# Recogniton model
model = parseq("~/onnxtr-parseq-multilingual-v1.onnx", vocab=VOCABS["multilingual"])
push_to_hf_hub(
    model,
    model_name="onnxtr-parseq-multilingual-v1",
    task="recognition",  # The task for which the model is intended [detection, recognition, classification]
    arch="parseq",  # The name of the model architecture
    override=False,  # Set to `True` if you want to override an existing model / repository
)

# Detection model
model = linknet_resnet18("~/onnxtr-linknet-resnet18.onnx")
push_to_hf_hub(model, model_name="onnxtr-linknet-resnet18", task="detection", arch="linknet_resnet18", override=True)
```

## Models architectures

Credits where it's due: this repository provides ONNX models for the following architectures, converted from the docTR models:

### Text Detection

- DBNet: [Real-time Scene Text Detection with Differentiable Binarization](https://arxiv.org/pdf/1911.08947.pdf).
- LinkNet: [LinkNet: Exploiting Encoder Representations for Efficient Semantic Segmentation](https://arxiv.org/pdf/1707.03718.pdf)
- FAST: [FAST: Faster Arbitrarily-Shaped Text Detector with Minimalist Kernel Representation](https://arxiv.org/pdf/2111.02394.pdf)

### Text Recognition

- CRNN: [An End-to-End Trainable Neural Network for Image-based Sequence Recognition and Its Application to Scene Text Recognition](https://arxiv.org/pdf/1507.05717.pdf).
- SAR: [Show, Attend and Read:A Simple and Strong Baseline for Irregular Text Recognition](https://arxiv.org/pdf/1811.00751.pdf).
- MASTER: [MASTER: Multi-Aspect Non-local Network for Scene Text Recognition](https://arxiv.org/pdf/1910.02562.pdf).
- ViTSTR: [Vision Transformer for Fast and Efficient Scene Text Recognition](https://arxiv.org/pdf/2105.08582.pdf).
- PARSeq: [Scene Text Recognition with Permuted Autoregressive Sequence Models](https://arxiv.org/pdf/2207.06966).
- VIPTR: [A Vision Permutable Extractor for Fast and Efficient Scene Text Recognition](https://arxiv.org/abs/2401.10110).

### Layout Detection

- LW-DETR: [LW-DETR: A Transformer Replacement to YOLO for Real-Time Detection](https://arxiv.org/pdf/2406.03459v1).

### Table Structure Recognition

- TableCenterNet: [TableCenterNet: A one-stage network for table structure recognition](https://arxiv.org/abs/2504.17522).

### Formula Recognition

- FormulaNet: [PP-FormulaNet: Bridging Accuracy and Efficiency in Advanced Formula Recognition](https://arxiv.org/abs/2503.18382).

```python
predictor = ocr_predictor()
predictor.list_archs()
{
    "detection_archs": [
        "db_resnet34",
        "db_resnet50",
        "db_mobilenet_v3_large",
        "linknet_resnet18",
        "linknet_resnet34",
        "linknet_resnet50",
        "fast_tiny",  # No 8-bit support
        "fast_small",  # No 8-bit support
        "fast_base",  # No 8-bit support
    ],
    "recognition_archs": [
        "crnn_vgg16_bn",
        "crnn_mobilenet_v3_small",
        "crnn_mobilenet_v3_large",
        "sar_resnet31",
        "master",  # No 8-bit support
        "vitstr_small",
        "vitstr_base",
        "parseq",
        "viptr_tiny",  # No 8-bit support
    ],
    "layout_archs": [
        "lw_detr_s",  # No 8-bit support
        "lw_detr_m",  # No 8-bit support
    ],
    "table_structure_archs": [
        "tablecenternet",  # No 8-bit support
    ],
    "formula_archs": [
        "formulanet",  # No 8-bit support
    ],
}
```

### Documentation

This repository is in sync with the [doctr](https://github.com/mindee/doctr) library, which provides a high-level API to perform OCR on documents.
This repository stays up-to-date with the latest features and improvements from the base project.
So we can refer to the [doctr documentation](https://mindee.github.io/doctr/) for more detailed information.

NOTE:

- `pretrained` is the default in OnnxTR, and not available as a parameter.
- docTR specific environment variables (e.g.: DOCTR_CACHE_DIR -> ONNXTR_CACHE_DIR) needs to be replaced with `ONNXTR_` prefix.

### Benchmarks

The CPU benchmarks were measured on an `i7-14700K Intel CPU` and the GPU benchmarks on an `RTX 4080 Nvidia GPU`.

Benchmarking performed on the FUNSD dataset and CORD dataset: mean time of the end-to-end OCR (text detection & recognition) of a page, on the original images, after a warm-up and with the default settings of each library.

docTR / OnnxTR models used for the benchmarks are `fast_base` (full precision) | `db_resnet50` (8-bit variant) for detection and `crnn_vgg16_bn` for recognition. The float16 models of OnnxTR are converted with `scripts/convert_to_float16.py`.

The smallest combination in OnnxTR of `db_mobilenet_v3_large` and `crnn_mobilenet_v3_small` takes as comparison `~0.15s / Page` on the FUNSD dataset and `~0.12s / Page` on the CORD dataset in **full precision** on CPU.

- CPU benchmarks:

|Library                                       |FUNSD (199 pages)              |CORD  (900 pages)              |
|----------------------------------------------|-------------------------------|-------------------------------|
|docTR (CPU) - v1.1.0                          |~1.01s / Page                  |~0.45s / Page                  |
|**OnnxTR (CPU)** - v0.10.0                    |~0.60s / Page                  |~0.27s / Page                  |
|**OnnxTR (CPU) 8-bit** - v0.10.0              |**~0.28s / Page**              |**~0.15s / Page**              |
|**OnnxTR (CPU-OpenVINO)** - v0.10.0           |~0.68s / Page                  |~0.30s / Page                  |
|EasyOCR (CPU) - v1.7.2                        |~1.83s / Page                  |~1.80s / Page                  |
|PyTesseract (CPU) - v0.3.13 (Tesseract 5.5.0) |~0.38s / Page                  |~0.44s / Page                  |
|Surya (CPU) - v0.17.1                         |~27.00s / Page                 |~12.93s / Page                 |
|PaddleOCR (CPU) - no cls - v3.7.0             |~10.53s / Page                 |~8.39s / Page                  |

- GPU benchmarks:

|Library                            |FUNSD (199 pages)              |CORD  (900 pages)              |
|-----------------------------------|-------------------------------|-------------------------------|
|docTR (GPU) - v1.1.0               |~0.068s / Page                 |~0.030s / Page                 |
|docTR (GPU) float16 - v1.1.0       |**~0.057s / Page**             |**~0.027s / Page**             |
|**OnnxTR (GPU)** - v0.10.0         |**~0.057s / Page**             |~0.039s / Page                 |
|**OnnxTR (GPU) float16** - v0.10.0 |~0.059s / Page                 |~0.048s / Page                 |
|EasyOCR (GPU) - v1.7.2             |~0.34s / Page                  |~0.14s / Page                  |
|Surya (GPU) - v0.17.1              |~1.23s / Page                  |~0.53s / Page                  |
|PaddleOCR (GPU) - no cls - v3.7.0  |~0.15s / Page                  |~0.090s / Page                 |

OnnxTR runs with onnxruntime(-gpu) 1.23.2 and onnxruntime-openvino 1.24.1, docTR and EasyOCR with PyTorch 2.15, PaddleOCR with PaddlePaddle 3.3.1 and its default text detection / recognition models (`PP-OCRv6_medium_det` + `PP-OCRv6_medium_rec`). Surya 0.17.1 is the last release running the text recognition with PyTorch (newer releases rely on a vLLM / llama.cpp inference server).

## Citation

If you wish to cite please refer to the base project citation, feel free to use this [BibTeX](http://www.bibtex.org/) reference:

```bibtex
@misc{doctr2021,
    title={docTR: Document Text Recognition},
    author={Mindee},
    year={2021},
    publisher = {GitHub},
    howpublished = {\url{https://github.com/mindee/doctr}}
}
```

```bibtex
@misc{onnxtr2024,
    title={OnnxTR: Optical Character Recognition made seamless & accessible to anyone, powered by Onnx},
    author={Felix Dittrich},
    year={2024},
    publisher = {GitHub},
    howpublished = {\url{https://github.com/felixdittrich92/OnnxTR}}
}
```

## License

Distributed under the Apache 2.0 License. See [`LICENSE`](https://github.com/felixdittrich92/OnnxTR?tab=Apache-2.0-1-ov-file#readme) for more information.
