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
...

# Install package (choose cpu or gpu)
uv pip install git+https://github.com/Kansallisarkisto-AI/nafrec.git[cpu]

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
...

# Install package (choose cpu or gpu)
pip install git+https://github.com/Kansallisarkisto-AI/nafrec.git[cpu]

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 model detects 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 median 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
    • Length of generated text
    • Predicted script type of the row ("latin" or "cyrillic")
    • Script type prediction confidence value

Metadata

Release files for nafrec 0.3.0

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.3.0
File Size Uploaded Provenance
nafrec-0.3.0.tar.gz 73.9 kB With provenance Details

Built distribution (wheel)

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

Total release size: 136.9 kB

Release files / nafrec-0.3.0.tar.gz

Download URL nafrec-0.3.0.tar.gz
Size 73.9 kB
Tags Source
SHA-256 checksum
How to use checksums
160f638e074cd71abc7608c2a16b958c5fb8dff73c7281def0cbac12aec3c2f8
BLAKE2b-256 checksum
How to use checksums
18279c16871b9e02b4e900cff15b50ccab69d98cb94e93d53a4510ff9a2a2e69
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.4 {"installer":{"name":"uv","version":"0.13.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","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 Oct 21, 2026.

Transparency log

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

Download URL nafrec-0.3.0-py3-none-any.whl
Size 63.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
30a34ecb1d7c6b7f9a40f6414d392db7d180b9f4bd9bb8dc71e46dc094be323e
BLAKE2b-256 checksum
How to use checksums
c281eb080737d4a5bade45d5869afc058e378bb71f2756b205ccdaa75536c436
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.4 {"installer":{"name":"uv","version":"0.13.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","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 Oct 21, 2026.

Transparency log

Security for nafrec 0.3.0

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

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for nafrec-0.3.0.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 Oct 21, 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.

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 release files Full provenance

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