Metadata-Version: 2.4
Name: khmer-ocr-ocm
Version: 0.1.0
Summary: Khmer OCR (CRNN + CTC) with bundled weights and old Khmer inscription fonts
Author-email: Chem Vatho <chemvatho@gmail.com>
License: MIT
Project-URL: Homepage, https://github.com/chemvatho/khmer-ocr-ocm
Project-URL: Issues, https://github.com/chemvatho/khmer-ocr-ocm/issues
Keywords: khmer,ocr,crnn,ctc,pytorch,inscription,angkor
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Scientific/Engineering :: Image Recognition
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: torch>=2.0
Requires-Dist: numpy>=1.21
Requires-Dist: Pillow>=9.0
Provides-Extra: dev
Requires-Dist: pytest>=7; extra == "dev"
Requires-Dist: build>=1; extra == "dev"
Requires-Dist: twine>=4; extra == "dev"
Dynamic: license-file

# 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

```bash
pip install khmer-ocr-ocm
```

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

## Quick start

```python
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:

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

Command-line interface:

```bash
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:

```python
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

```python
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

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