Skip to main content

OCR Foundry

Synthetic OCR training data generator with legacy Khmer/English behavior and opt-in multilingual whole-line rendering. Use ocr-foundry; existing khocr-gen commands run the same CLI. The v0.2 distribution ocr-foundry installs both executables. Python imports remain khocr_gen, and Rust and KHOCR_GEN_* environment contracts are unchanged.

See branding and compatibility for migration details. Repository links to https://github.com/0x1d1e/ocr-foundry are valid now.

Installation

pip install ocr-foundry

pip install ocr-foundry is the v0.2 distribution. It provides both the ocr-foundry and khocr-gen commands.

The PyPI project khocr-gen is deprecated. Release 0.2.0 contains no code and depends on this package. khocr-gen==0.1.13 is the last standalone release. If khocr-gen==0.1.13 is already installed, upgrade or remove it before installing ocr-foundry, because both distributions would otherwise provide the khocr-gen command.

The rust extra is not installable from PyPI because khocr-gen-core is not published. Native use is a source checkout with uv sync --extra dev --extra rust.

Requirements

  • Python >= 3.12
  • Rust toolchain (cargo/rustc, edition 2021) - only needed to build the optional acceleration extension

Quick Start

1. Prepare fonts

fonts/
├── khmer/     <- .ttf / .otf Khmer fonts
└── english/   <- .ttf / .otf English fonts

Fonts placed directly in fonts/ (not in a subdirectory) are added to both pools as fallbacks.

2. Prepare corpus

A plain UTF-8 text file, one string per line:

សួស្តី
Hello World
ស្វាគមន៍ Welcome

3. Generate a dataset

ocr-foundry generate --corpus corpus/corpus.txt --fonts fonts --output data --copies 3 --storage lmdb

4. View generated images

# Summary of an LMDB database
ocr-foundry view --lmdb data/train/lmdb

# Extract images
ocr-foundry view --lmdb data/train/lmdb --output-dir extracted

# Show labels only
ocr-foundry view --lmdb data/train/lmdb --labels-only

Multilingual mode and evaluation

--multilingual requires Pillow RAQM and renders horizontal logical Unicode lines using one covering font by default, with opt-in cluster-aware font fallback (--cluster-font-fallback) segmenting grapheme clusters, preserving combining marks and Khmer coeng sequences, and aligning multi-run segments to a shared baseline. Labels retain normalized logical text without visual reordering. Arbitrary recursive font pools, explicit NFC/no-normalization policies, and auto/ltr/rtl direction are supported. JSONL manifests ingest rich metadata, emit provenance.jsonl sidecars, and record font hashes in metadata.jsonl.

The source recipe configs/multilingual-realistic.yml includes clean samples, disables rare oversampling and foreground crops, and enables a measured line-ink height guard, with opt-in binary glyph-mask guards (--min-glyph-height-for-augmentation) and script/font intensity scaling. Page layout composition (ocr-foundry layout) adds opt-in whitespace word annotations, native RAQM vertical writing (words and vertical modes requiring shared libraqm with linked FreeType), and paginated table hierarchy. Rendering checks are not a universal language-quality claim; see the capability matrix and page layout contract.

ocr-foundry evaluate --reference reference.jsonl --predictions predictions.jsonl --normalization NFC

The offline evaluator reports corpus CER, exact-line accuracy and reference-language groups from ID-keyed JSONL. It downloads no model. See the schema and frozen real-document protocol and provenance/migration roadmap.

Configuration

All generation parameters can be specified via CLI flags, a YAML config file, or both (CLI overrides YAML). See CONFIG.md for details. Example: generate.yml

Config loading order (lowest to highest priority):

  1. argparse built-in defaults
  2. YAML config file values
  3. Explicit CLI flags

CLI Reference

See CLI_REFERENCE.md for complete command documentation.

Documentation

Acknowledgments

OCR Foundry builds on these open-source libraries:

  • khmernormalizer (MIT) - Khmer text normalization
  • OpenCV (Apache 2.0) - Image processing and augmentation
  • Pillow (HPND) - Font rendering and image creation
  • Albumentations (MIT) - Augmentation primitives
  • NumPy (BSD-3-Clause) - Numerical operations
  • LMDB (OpenLDAP 2.8) - Embedded database for training pipelines

License

MIT

Metadata

Release files for ocr-foundry 0.2.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 ocr-foundry 0.2.1
File Size Uploaded Provenance Actions
ocr_foundry-0.2.1.tar.gz 495.4 kB Without provenance Details for ocr_foundry-0.2.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for ocr-foundry 0.2.1
File Interpreter ABI Platform Actions
ocr_foundry-0.2.1-py3-none-any.whl Python 3 none any Details for ocr_foundry-0.2.1-py3-none-any.whl

Total release size: 678.8 kB

Release files / ocr_foundry-0.2.1.tar.gz

Download URL ocr_foundry-0.2.1.tar.gz
Size 495.4 kB
Tags Source
SHA-256 checksum
How to use checksums
5ab7396d26ba46378d5fcb18f9fdefce1ff9bf52ffff5cb4f0d5d17a40b9bef8
BLAKE2b-256 checksum
How to use checksums
f871cce0b5b2704fac256ac15acb9add511fa773dd6c7592d699f78ee4c401df
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
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}

Release files / ocr_foundry-0.2.1-py3-none-any.whl

Download URL ocr_foundry-0.2.1-py3-none-any.whl
Size 183.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
50fb7aef36bc3133f38c90eab1f7d14a110929fe9a7d8ff106602633fb8f123c
BLAKE2b-256 checksum
How to use checksums
2071a2d55f4c25dd5632fad09462d177c61d6c51a10757f5fd0ff709e8e85a75
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
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}

Security for ocr-foundry 0.2.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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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