Image-to-Text
PyTorch
Safetensors
Khmer
ocr
khmer
handwriting-recognition
text-detection
crnn
ctc

Khmer Handwritten OCR: text-line detection + recognition

Two small models that read Khmer text (handwritten, printed, or scene text) and return Unicode Khmer text:

  • a text recognizer (CRNN + CTC, 9.5M parameters) that reads one cropped line or word (read);
  • a Khmer text-line detector (ResNet-18 segmentation network, 11.6M parameters) that finds every text line on a page or photo (read_page).

Both read on their own, without a language model (an optional character language model is included). The recognizer runs on CPU in about 0.15 s per line.

Built for a Year 4 thesis in Data Science and Engineering, Royal University of Phnom Penh (Oun MengHeang).

Results

All results without a language model (greedy decoding). Character error rate (CER) and word error rate (WER), lower is better. Words are segmented with khmercut.

Text lines (read)

Test set CER WER Lines fully correct
SoyVitou handwritten (211 Khmer samples) 11.6% 38.6% 36.0%
CADT handwritten (444 samples, unseen pages) 3.9% 16.3% 57.4%
WildKhmerST scene text (1,499 lines) 9.5% 24.9% 59.5%
SoyVitou printed (1,000 samples) 0.9% 3.7% 90.6%

For comparison, Tesseract (khm, single-line mode) on the same images: 61.4% / 88.0% / 49.2% / 46.6% CER.

Full pages (read_page: line detection + recognition)

Test images Lines found (IoU β‰₯ 0.5) Error With docTR FAST detector instead
Handwritten pages (40 pages, 461 lines; real handwritten test crops laid out on blank pages) 97.6% 6.0% page CER 93.3% found, 17.3% page CER
Street photos (WildKhmerST test photos, 1,504 Khmer lines) 69.3% 36.2% line CER 52.2% found, 51.1% line CER
Document photos (KHOB benchmark level 2, 607 annotated lines) 69.2% 23.9% line CER 78.1% found, 25.2% line CER
Scanned Khmer documents (KHOB benchmark level 1, 53 pages) 67.9% 11.6% page CER 75.0% found, 10.5% page CER

Page CER compares all annotated text with all recognized text, each joined in reading order (handwritten pages: spaces ignored). Line CER: each annotated line against the text of the detected line matched to it (empty if missed). On the handwritten pages the recognizer reaches 5.4% CER when given the exact line boxes, so line detection now adds less than one point. "Lines found" counts a line as missed when it is split or merged differently from the annotation; the photo test sets only annotate Khmer text.

Versions

Version Change Handwritten CER (SoyVitou / CADT, no LM)
1 Pre-training on printed text, fine-tuning on all real data 13.4% / 4.7%
2 + handwriting augmentation (elastic distortion, ink blots) 12.2% / 3.9%
3 (current) + first trained on 60k synthetic handwriting-style lines, then on real data; new Khmer line detector 11.6% / 3.9%

Mixing the synthetic lines into the real data in one run gave no gain (12.2% β†’ 11.8% SoyVitou, 3.9% β†’ 4.1% CADT); training on them first and finishing on real data did. Older versions are in this repository's commit history.

Usage

# pip install torch torchvision numpy pillow safetensors opencv-python-headless huggingface_hub
from huggingface_hub import snapshot_download
import sys
folder = snapshot_download("mengheang/khmer-handwritten-ocr")
sys.path.insert(0, folder)
from khmer_ocr import KhmerOCR

ocr = KhmerOCR(folder)
print(ocr.read("line.png"))                 # one text line or word, the model on its own
print(ocr.read("line.png", use_lm=True))    # + character language model (beam search)

# Full page or photo: our line detector + recognition, in reading order
for line in ocr.read_page("page.jpg"):
    print(line["box"], line["text"])

# Optional: docTR's FAST word detector instead (pip install python-doctr)
ocr.read_page("page.jpg", photo=True)

read expects an image of one text line or word. read_page finds the text lines itself.

Models

Recognizer

Part Details
Input Grayscale, height 64 px, width scaled with aspect ratio (max 1,800 px)
CNN ResNet-style, 4 stages, reduces width by 4 (one output frame per 4 pixels)
Sequence model 2-layer bidirectional LSTM, hidden size 256
Output 241 symbols (Khmer block, Khmer symbols, digits, ASCII) + CTC blank
Decoding Greedy (default), or CTC prefix beam search (beam 16) + character 6-gram language model

Line detector

Part Details
Input RGB page, long side resized to at most 1,536 px
Network ResNet-18 encoder (ImageNet initialisation) + feature pyramid, output at 1/2 resolution
Target The middle half (vertically) of every text line, so neighbouring lines stay apart even when tall vowels and subscripts touch
Post-processing Threshold 0.6 β†’ connected components β†’ rotated rectangle β†’ grown back to the line height (0.75 Γ— 2) β†’ line box

Training

Recognizer:

  1. Pre-training (3 epochs) on 386k synthetic and printed images: KimkosalYon 200k synthetic lines, emhengly 136k printed words, SoyVitou 62k printed.
  2. Synthetic handwriting (6 epochs) on 60k generated lines: 17 Khmer fonts (handwriting-style Fasthand and Freehand 60% of the time), half real text and half random valid Khmer syllables, with heavy distortion (slant, wavy baseline, elastic warping, stroke width, pen pressure, paper texture, ruled lines, blur, JPEG).
  3. Fine-tuning (20 epochs) on real data only: SoyVitou handwritten (2,894 Khmer samples) and CADT handwritten (3,152), each repeated 4x, plus WildKhmerST scene text (26,348 lines) and SoyVitou printed.

Line detector: 15 epochs on generated pages (training-split crops of real handwriting, synthetic handwriting and printed lines, with random layouts, ruled paper, perspective, shadows and camera noise), then 8 epochs with 35% real WildKhmerST training photos. Selected on handwritten validation pages and validation photos.

AdamW, one-cycle learning rate, mixed precision. One RTX 5070 Ti laptop GPU.

Training data and credits

Dataset License
SoyVitou Khmer handwritten 4.2k MIT
LaySopanha Khmer Handwritten Dataset (CADT capstone: Ly Leab, Lay Sopanha, Narith Sopheakleap, Hinge Sothida, Sok Sothika) CC BY 4.0
WildKhmerST CC BY-NC 4.0
KimkosalYon Khmer OCR 200k MIT
SoyVitou 62k printed MIT
emhengly Khmer text recognition Apache 2.0
Khmer fonts from Google Fonts (synthetic handwriting) SIL Open Font License

Because WildKhmerST is non-commercial, this model is released under CC BY-NC 4.0 (non-commercial use only).

Limitations

  • Trained on a few thousand handwritten samples from a limited number of writers; unusual handwriting styles will have more errors.
  • No public dataset has full Khmer handwritten pages, so the handwritten page test uses real handwritten lines laid out on blank pages; real notebook photos are harder.
  • Only roughly horizontal text is supported. On clean scanned documents the line detector is slightly behind docTR (photo=True).
  • Common errors: similar-looking consonants (រ/αž“, ម/αž‡) and small vowel signs.
  • Test splits are our own (SoyVitou Khmer-only test samples; CADT split by page), so numbers are not directly comparable to other papers.
Downloads last month
120
Safetensors
Model size
9.46M params
Tensor type
F32
Β·
Inference Providers NEW
This model isn't deployed by any Inference Provider. πŸ™‹ Ask for provider support

Datasets used to train mengheang/khmer-handwritten-ocr