Skip to main content

CLI for preparing Handwritten Text Recognition (HTR) datasets for PyLaia traininig.

Project description

HTR CLI

PyPI version CI License: MIT

CLI for preparing HTR datasets

This CLI tool offers a quick way to (optionally) pull PAGE-XML data from Transkribus[^1] and to prepare perfectly valid dataset/ directory ready for feeding PyLaia.

If you find any value in this project please leave a star and consider to offer me a coffee (Paypal or Github sponsor).

Installation

The package is published on PyPi as htr-cli and can be installed with pip / pipx:

pip install htr-cli
# or, recommended:
pipx install htr-cli

With uv:

uv tool install htr-cli

Verify the install:

htr-cli --help

For devs:

git clone https://github.com/ndrscalia/htr-cli.git
cd htr-cli
python -m venv .venv
source .venv/bin/activate
pip install -e ".[dev]" # or `uv sync --all-extras` with no venv activation

Run the test suite:

# with pip:
pytest
ruff check .

# with uv (no venv activation)
uv run pytest
uv run ruff check

Usage

The CLI exposes the following sub-commands (in pipeline order):

init
scaffold
data-extraction
split-dataset
process-images
process-images-tfe

init

Interactive setup. If used, asks for Transkribus email and password and sores them in your OS keyring under the service name htr-cli.

scaffold

Creates the directory layout the rest of the pipeline expects:

.
└── root/
    ├── dataset/
       ├── images/
          ├── train
          ├── val
          └── test
       └── pyalaia's stuff and more
    └── data/
        ├── images
        └── xml_texts

pull-transkribus

Downloads GT pages from Transkribus (default). For every collection, walks documents and pages, filtering by --page-status, and writes each page's image do data/images/ and its PAGE-XML counterpart to data/xml_tests/. Naming pattern: {collection}_{docId}_{pageId}_{imageId}_{filename}.{jpg,xml}. This subcommand relies on transkribus-client, which in turn relies on legacy API that might be discontinued soon.

data-extraction

Parses every XML found in data/xml_texts/ and emits the intermediate dataset files:

  • polygons_coordinates.json: per-line polygon coordinates for later cropping.
  • lines.csv: one row per line: page, region id, region order, reading order, line id, raw text, "unclear" flag.
  • dataset/syms.txt: PyLaia's CTC symbol vocabulary.
  • dataset/{tokens,lexicon_characters,dictionary}.txt: different files for KenLM model creation and further post-processing (coming soon).

Optional positional argument filters by region type and --unclear-name sets the custom-tag name that skips the line. E.g.:

htr-cli data-extraction paragraph --unclear-name "unclear"

NBSP chars are normalize to plain space.

split-dataset

Reads lines.csv and partitions by page (not line) using a deterministic random_state. The following options are available:

  • --val-size: validation set size (default to 10%);
  • --test-size: test set size (default to 0%);
  • --omit-unclear / -u: omit lines where an "unclear" tag appears.

If you only want validation set, skip passing --test-size.

This subcommand write the following files:

  • dataset/{train,val,test}_ids.txt
  • dataset/{train,val,test}.txt
  • dataset/{train,val,test}_text.txt
  • dataset/corpus_characters.txt (corpus for char KenLM training)
  • missing_reading_order.csv (lines dropped if no reading order available)

process-images

The pure-Python preprocessing pipeline. For each entry in polygons_coordinates.json: loads the source image, masks out everything outside the polygon, crops to the polygon's bounding box, converts to grayscale, then runs the selected pipeline:

  • --full-pipeline (default): contrast stretch - modified Sauvola - deslope - deslant (TFE port) - moment-normalize.
  • --light-pipeline: deslope - deslant (vendored DeslantImg) - modified Sauvola.

Each stage is individually toggleable (--no-contrast-stretch, --no-enhance-sauvola, etc.). Final image is resized to norm_height (positional arg, default 64 px) preserving aspect ratio, padded 10 px left/right with white, and written to dataset/images/{train,val,test}/ based on which split the line belongs to.

At the beginning of every run, dataset/images_processing_ckpt.txt is written and allows to interrupt and resume processing. If you want to abort processing the images and start from scratch, you have to delete that file.

process-images-tfe

Same as process-image, but uses TextFeatExtractor C++ library and the default parameters are based on Transkribus' params:

tfe = TextFeatExtractor(
    stretch=True,
    enh=True,
    enh_win=30,
    enh_prm=0.1,
    enh3_prm0=0,
    enh3_prm2=0,
    deslope=True,
    deslant=True,
    normxheight=0,
    normheight=64,
    momentnorm=True,
    fcontour_dilate=0,
    padding=10,
    maxwidth=6000,
  )

This subcommand requires textfeat + pagexml Python bindings, which are not on PyPi and will only run on Linux (see the Dockerfile.tfe section below).

Use this if you want the C++ behavior, and process-images when you want a pure-Python install that works on macOS or outside Docker in general.

Differences in performances have not been compared extensively with my simpler implementation, but the two commands take more or less the same time to run and both mess up some images.

To get further help:

htr-cli --help
htr-cli COMMAND --help

Dockerfile.tfe

This is needed to run htr-cli image-proessing-tfe if you are not on Linux.

Build the docker image (one-time, from the repo root):

docker build -t textfeatextractor .

To run the docker image that allows you to use TextFeatExtractor, mount the entire project directory into the container so the htr-cli package, data/, and dataset/ are all visible inside:

docker run -it -v "$(pwd)":/workspace -w /workspace textfeatextractor bash
  • -v "$(pwd)":/workspace bind-mounts the repo root at /workspace. Use an absolute path (e.g. /Users/andreascalia/code/my_project) instead of $(pwd) if you're invoking the command from outside the project.
  • -w /workspace sets the working directory so relative paths like data/, dataset/, and polygons_coordinates.json resolve the way the CLI expects.

The Dockerfile only installs the C++ deps (pagexml, textfeat); install the CLI as explained at the beginning of these docs.

Future Updates

  • Further post-processing options to get better CER and WER.
  • Standard configs file to easier PyLaia's use.
  • ALTO XML format support.
  • Kraken support.

[^1]: This feature relies on legacy API. It might not work anymore in the near future.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

htr_cli-0.1.1.tar.gz (113.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

htr_cli-0.1.1-py3-none-any.whl (24.4 kB view details)

Uploaded Python 3

File details

Details for the file htr_cli-0.1.1.tar.gz.

File metadata

  • Download URL: htr_cli-0.1.1.tar.gz
  • Upload date:
  • Size: 113.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for htr_cli-0.1.1.tar.gz
Algorithm Hash digest
SHA256 634e20bd63227b941c4db00cae479a041a2d098aff524662d94cd2771657f09b
MD5 cf1af4afeabbf3ca5997321d7c323be2
BLAKE2b-256 909d3834565738be26b1edd5ca28beac814e057815c2d04f6d3cbf346400396b

See more details on using hashes here.

Provenance

The following attestation bundles were made for htr_cli-0.1.1.tar.gz:

Publisher: publish.yml on ndrscalia/htr-cli

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file htr_cli-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: htr_cli-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 24.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for htr_cli-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e62e35061a5e91fb521248a700bcb5215644936e190f30ec18f0616ec6274316
MD5 247106e45f931e266db1bb4af96db450
BLAKE2b-256 f484ca3fb29e05ead978a9302066ce22e58b2e297009778bfe5855873a4923d7

See more details on using hashes here.

Provenance

The following attestation bundles were made for htr_cli-0.1.1-py3-none-any.whl:

Publisher: publish.yml on ndrscalia/htr-cli

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page