Skip to main content

Khmer OCR (CRNN + CTC) with bundled weights and old Khmer inscription fonts

Project description

khmer-ocr-ocm

Khmer OCR built on a CRNN + CTC pipeline, trained primarily on modern Khmer text and fine-tuned on datasets that include old Khmer inscription samples. The package ships with:

  • the fine-tuned CRNN weights (model.pth, ~36 MB)
  • the matching character vocabulary (vocab.json)
  • a set of Khmer fonts — including the Angkor family of old Khmer inscription fonts — used for synthetic dataset generation

Install

pip install khmer-ocr-ocm

The wheel includes the bundled model, so the first prediction works offline without any additional download.

Quick start

from khmer_ocr_ocm import KhmerOCR

ocr = KhmerOCR()                       # auto-picks CUDA if available
text = ocr.predict("line.png")         # OCR one cropped line image
print(text)

Batch a folder of line images to a CSV:

ocr.predict_folder("crops/", out_csv="preds.csv")

Command-line interface:

khmer-ocr-ocm predict line.png
khmer-ocr-ocm predict-folder crops/ --out-csv preds.csv

Old Khmer inscription

The bundled Angkor fonts (ANG-TRAN, Angkor0..Angkor5, PANGKOR) are accessible programmatically:

from khmer_ocr_ocm import get_inscription_font_paths
print(get_inscription_font_paths())

The companion Colab notebook (notebooks/test_pypi_model.ipynb) includes a 5-sample test set that renders inscription-style lines with these fonts and runs them through the model.

Generate synthetic training data

from khmer_ocr_ocm.dataset import generate_dataset

generate_dataset(
    corpus=["ព្រះរាជាណាចក្រកម្ពុជា", "សួស្ដី"],
    out_dir="data/synth",
    use_inscription_fonts=True,        # render with Angkor fonts
)

Continue fine-tuning

from khmer_ocr_ocm.finetune import finetune

finetune(
    train_folder="data/train",
    val_folder="data/val",
    labels_file="labels.txt",
    out_model="crnn_khmer_v22.pth",
    num_epochs=10,
    batch_size=32,
    lr=3e-5,
)

Model architecture

  • CNN: 7 conv blocks, BatchNorm, ReLU, spatially-asymmetric MaxPool for width-preserving downsampling
  • BiLSTM: 2 layers, hidden = 256, bidirectional
  • FC + CTC: 984 classes (983 Khmer/ASCII chars + blank at index 0)
  • Input: 1×48×512 grayscale, normalized to [-1, 1]

Files bundled

src/khmer_ocr_ocm/assets/
├── model.pth                 # V21 fine-tuned CRNN (~36 MB)
├── vocab.json                # char2idx / idx2char
└── fonts/
    ├── inscription/          # Angkor* old Khmer fonts
    └── khmer/                # modern Khmer fonts for synthetic data

License

Code: MIT. Fonts retain their original licenses — see LICENSE.

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

khmer_ocr_ocm-0.1.0.tar.gz (35.1 MB view details)

Uploaded Source

Built Distribution

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

khmer_ocr_ocm-0.1.0-py3-none-any.whl (35.1 MB view details)

Uploaded Python 3

File details

Details for the file khmer_ocr_ocm-0.1.0.tar.gz.

File metadata

  • Download URL: khmer_ocr_ocm-0.1.0.tar.gz
  • Upload date:
  • Size: 35.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.2

File hashes

Hashes for khmer_ocr_ocm-0.1.0.tar.gz
Algorithm Hash digest
SHA256 13a566ecc7cf024ede45964337820eb9a39545242d80c4eaded4eec25a14843e
MD5 3db0275106cf9db8c8e71acb5a6e9490
BLAKE2b-256 7ec8142525fe4e204a8144fe44d8e73c386c65fc83187def097051565c1ad062

See more details on using hashes here.

File details

Details for the file khmer_ocr_ocm-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: khmer_ocr_ocm-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 35.1 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.2

File hashes

Hashes for khmer_ocr_ocm-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 89cfedfe07dc937fde881878bc7ab9261695392139f8e187885ba091761bbe05
MD5 86b1e7666c6c154d92e0aaef5b8247e4
BLAKE2b-256 609782058b6ce634ef6d0c166f696ed4ba7ec91d6c4d77aa268887d171177401

See more details on using hashes here.

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