Skip to main content

nafrec

An HTR and OCR pipeline for historical text recognition, which classifies text lines into cyrillic or latin based on their script types and then passes them to a proper (TrOCR or PP-OCRv6) text recognition model.

ArchXAI project logo

Installation

This project uses pyproject.toml for dependency management. You can install the required packages using one of the following methods:

uv works for all supported platforms including ROCm.

# Install uv if you haven't already
curl -LsSf https://astral.sh/uv/install.sh | sh

# Create a virtual environment and activate it
uv venv --python 3.12
source .venv/bin/activate  # On Windows: .venv\Scripts\activate

# Install correct version of PyTorch for cpu, AMD ROCm or CUDA 13.x
uv pip install torch torchvision --index-url https://download.pytorch.org/whl/cu132

# Install package (choose cpu or gpu)
uv pip install nafrec[gpu]

Using pip with venv

pip will not work with the ROCm extra.

# Create a virtual environment and activate it
# Use Python 3.12
python -m venv .venv
source .venv/bin/activate  # On Windows: .venv\Scripts\activate

# Install correct version of PyTorch for cpu, AMD ROCm or CUDA 13.x
uv pip install torch torchvision --index-url https://download.pytorch.org/whl/cu132

# Install package (choose cpu or gpu)
pip install nafrec[gpu]

Models

The following pre-trained models can be downloaded from Hugging Face:

After downloading, update the model paths in your command line arguments or configuration.

Pipeline Overview

The pipeline processes images through the following steps:

  1. Input: Historical document image
  2. Detection: RF-DETR or Paddle models detect text regions and text lines
  3. Cropping: Text lines are cropped from the original image based on detected coordinates
  4. Text type classification: Classification model labels every text line as "cyrillic" or "latin" based on the predicted script type
  5. Recognition: Based on the classification results, text line images ase passed to the proper TrOCR or PP-OCRv6 model which recognizes text from each cropped line
  6. Output: ALTO XML and/or PAGE XML file containing region coordinates, text line coordinates, and recognized text

Usage

Run the pipeline using the command nafrec:

nafrec \
    --detection_model_path /path/to/rfdetr/model.pth \
    --script_classification_model_path /path/to/script/classification/model.pt \
    --recognition_model_path /path/to/trocr/model/folder/ \
    --cyrillic_recognition_model_path /path/to/cyrillic/trocr/model/folder/ \
    --processor_path /path/to/trocr/processor/folder/ \
    --cyrillic_processor_path /path/to/cyrillic/trocr/processor/folder/ \
    --input_folder /path/to/images/

Most models including the cyrillic model are optional; many different combinations are supported.

Notes

Nvidia backends ("gpu" and "gpu-optimum") seem stable, but expect small differences if TensorRT is enabled. TensorRT will need to write engine caches into the working directory (in the future we might make this configurable). This backend is currently configured for CUDA 13.x.

CPU backends ("cpu" and "cpu-openvino") seem stable overall.

The AMD GPU execution providers for onnxruntime (ROCm and MIGraphX) are quite unstable. The ROCm provider seems to work on MI250X (only the first call fails, works after retrying), but only with completely static shapes i.e. padding batch size to be static and using the same minimum and maximum width. The MIGraphX provider runs on MI250X but gives incorrect results. MI300+ series and consumer AMD GPUs are untested. Paddle detection and layout models are currently untested on AMD GPUs.

This package is designed to rely on Hugging Face transformers, ONNX Runtime and Roboflow rfdetr libraries only, Paddlepaddle is not a dependency and all pre/postprocessing is handled by this library or our fork of ppocrv6_onnx. For this reason, the results with Paddle models can differ slightly from Paddlepaddle results.

Our fine-tuned PP-OCRv6 medium models use a raised image height of 96 px compared to the default of 48 px, so they are only guaranteed to work with this package. Most other packages have hardcoded width or height limits that are unsuitable.

Choosing package extras

Extra Description
cpu Basic CPU backend
cpu-openvino CPU backend with Intel OpenVINO acceleration for ONNX models
gpu Basic Nvidia GPU backend, also installs correct TensorRT for CUDA 13.x
gpu-optimum Same as basic Nvidia GPU backend with optimum-onnx acceleration for TrOCR models (xadupre/transformers5 feature branch ahead of release version)
gpu-amd-rocm Older AMD GPU backend compatible with MI250X (ROCm 7.0.2)
gpu-amd-migraphx Newer AMD GPU backend, possibly compatible with MI300 series (ROCm 7.2.4)

Arguments

See ARGS.md

Choosing models

  • Latin only, or cyrillic only: configure just one recognition model. Script classification is disabled and all lines go to that model.
  • Latin and cyrillic: configure both recognition models (and their processors) plus --script_classification_model_path. Each line is classified as latin or cyrillic and sent to the matching model.
  • PP-OCRv6 for latin: add --use_ppocr --ppocr_model_path ... instead of --recognition_model_path / --processor_path. The cyrillic model, if used, stays TrOCR.
  • Detection: the default is RF-DETR. Use --detector paddle with --paddle_det_model_path (and optionally --paddle_layout_model_path) for the Paddle backend.

nafrec checks these combinations at startup and reports missing options before processing begins.

Example

nafrec \
    --detection_model_path /path/to/rfdetr/model.pth \
    --script_classification_model_path /path/to/script/classification/model.pt \
    --recognition_model_path /path/to/trocr/model/folder/ \
    --cyrillic_recognition_model_path /path/to/cyrillic/trocr/model/folder/ \
    --processor_path /path/to/trocr/processor/folder/ \
    --cyrillic_processor_path /path/to/cyrillic/trocr/processor/folder/ \
    --input_folder /path/to/images/ \
    --confidence_threshold 0.2 \
    --page_xml 1 \
    --xml_folder /output/path

More examples

Latin-script lines recognized with PP-OCRv6, Paddle detection with a layout model, on two GPUs with fp16 and TensorRT:

nafrec \
    --input_folder /path/to/images/ \
    --detector paddle \
    --paddle_det_model_path /path/to/det.onnx \
    --paddle_layout_model_path /path/to/layout.onnx \
    --paddle_layout_ignore_classes image chart seal \
    --use_ppocr \
    --ppocr_model_path /path/to/rec.onnx \
    --gpu_ids 0 1 \
    --fp16_det --fp16_rec \
    --enable_tensorrt

RF-DETR with tiled detection on CPU only:

nafrec \
    --input_folder /path/to/images/ \
    --detection_model_path /path/to/rfdetr/model.pth \
    --recognition_model_path /path/to/trocr/model/folder/ \
    --processor_path /path/to/trocr/processor/folder/ \
    --device cpu \
    --cpu_processes 4 \
    --tile_size 768

Python API

nafrec can also be used as a library, for example to keep the models loaded and process images on request, or to supply your own text lines. See USAGE.md.

Output

The pipeline generates XML files (ALTO or PAGE format) containing:

  • Detected text region coordinates
  • Text line coordinates within each region
  • Recognized text for each line
  • Model metadata and confidence scores

By default a .json file is also written for each image (disable with --output_json 0). It contains for each image

  • Image name
  • Image height
  • Image width
  • Page level mean of recognition line confidence values
  • Page level 25. percentile of recognition line confidence values
  • Page level 75. percentile of recognition line confidence values
  • Number of lines where recognition model generated over 100 characters
  • Predicted (majority) language of the recognized text
  • Text region detection confidences
  • Text region polygons
  • Text region names
  • For each text region, text line level information containing
    • Recognized text content
    • Text line polygon coordinates
    • Text line polygon detection confidence value
    • Text line recognition confidence value
    • Predicted script type of the row ("latin" or "cyrillic")
    • Script type prediction confidence value

Metadata

Release files for nafrec 0.4.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for nafrec 0.4.1
File Size Uploaded Provenance Actions
nafrec-0.4.1.tar.gz 100.3 kB With provenance Details for nafrec-0.4.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for nafrec 0.4.1
File Interpreter ABI Platform Actions
nafrec-0.4.1-py3-none-any.whl Python 3 none any Details for nafrec-0.4.1-py3-none-any.whl

Total release size: 166.5 kB

Release files / nafrec-0.4.1.tar.gz

Download URL nafrec-0.4.1.tar.gz
Size 100.3 kB
Tags Source
SHA-256 checksum
How to use checksums
255413287fc2a50c69629d189cad98a6a8c97d148895b5c6eada4f0ad1b06a1c
BLAKE2b-256 checksum
How to use checksums
0cf8a0fa952ce9b0050207ae257196d1522337c47de49d6eb5c4239fc69b784c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.9 {"installer":{"name":"uv","version":"0.13.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 18, 2026.

Transparency log

Release files / nafrec-0.4.1-py3-none-any.whl

Download URL nafrec-0.4.1-py3-none-any.whl
Size 66.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
71417433c2b090c6176b98c4b4985c5fd301748c09735873fa585778b9c146f5
BLAKE2b-256 checksum
How to use checksums
3889ac05c4b479b84c9a12a15ff12d779bfc085db732aeba6080d734480ae2a2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.9 {"installer":{"name":"uv","version":"0.13.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 18, 2026.

Transparency log

Security for nafrec 0.4.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for nafrec-0.4.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 18, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page