Model card: new intro, scores and quick start; add base_model and datasets metadata; library_name onnx
Browse files
README.md
CHANGED
|
@@ -18,18 +18,79 @@ tags:
|
|
| 18 |
- text-detection
|
| 19 |
- text-recognition
|
| 20 |
- onnx
|
|
|
|
| 21 |
- crnn
|
| 22 |
- yolo11
|
| 23 |
- dbnet
|
| 24 |
- ctc
|
| 25 |
-
library_name:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
---
|
| 27 |
|
| 28 |
-
# ThaOCR
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
## Models
|
| 35 |
|
|
@@ -184,12 +245,3 @@ The repo mixes licences, so the Hub tag is `license: other`. Per stage:
|
|
| 184 |
| Table structure | `table/*.onnx` | **AGPL-3.0** — fine-tuned from Ultralytics YOLO11 weights; training data incl. TabRecSet (CC BY / BY-SA 4.0, attribution required) |
|
| 185 |
|
| 186 |
Training data licences are listed in the Datasets table above. FUNSD and KhmerEduDoc were used for evaluation only (non-commercial); no model weights were trained on them.
|
| 187 |
-
|
| 188 |
-
## Use
|
| 189 |
-
```python
|
| 190 |
-
import onnxruntime as ort, json
|
| 191 |
-
sess = ort.InferenceSession("rec/crnn_m_v2_stamp2_int8.onnx")
|
| 192 |
-
vocab = json.load(open("rec/crnn_m_v2_stamp2.vocab.json"))["itos"]
|
| 193 |
-
# grey line crop, height 48, shape [1,1,48,W] float32 in 0..1 -> greedy CTC over the output, blank id 0
|
| 194 |
-
```
|
| 195 |
-
Pre/post-processing (letterbox + NMS, DB unclip, width bucketing + CTC) is small numpy/OpenCV code in the ThaOCR backend (`backend/app/services/stages/`).
|
|
|
|
| 18 |
- text-detection
|
| 19 |
- text-recognition
|
| 20 |
- onnx
|
| 21 |
+
- onnxruntime
|
| 22 |
- crnn
|
| 23 |
- yolo11
|
| 24 |
- dbnet
|
| 25 |
- ctc
|
| 26 |
+
library_name: onnx
|
| 27 |
+
base_model:
|
| 28 |
+
- Ultralytics/YOLO11
|
| 29 |
+
- PaddlePaddle/PP-OCRv5_mobile_det
|
| 30 |
+
base_model_relation: finetune
|
| 31 |
+
datasets:
|
| 32 |
+
- Thareah/khmer-handwriting
|
| 33 |
+
- SoyVitou/khmer-handwritten-dataset-4.2k
|
| 34 |
+
- bsmock/pubtables-1m
|
| 35 |
+
- jbarrow/CommonForms
|
| 36 |
---
|
| 37 |
|
| 38 |
+
# ThaOCR — Khmer + English document OCR for CPU, browser and phone (ONNX)
|
| 39 |
|
| 40 |
+
**A complete document OCR pipeline for Khmer (ខ្មែរ) and English in about 41 MB.** It finds the regions of a page, detects every text line, reads it, and works out fonts, paragraphs and reading order. Every model is plain ONNX (opset 17) and runs with `onnxruntime` on CPU, in the browser (ONNX Runtime Web) or on a phone (ORT Mobile). No GPU needed.
|
| 41 |
+
|
| 42 |
+
- **Reads** printed Khmer and English, handwriting, and text under official stamps
|
| 43 |
+
- **Understands the page**: 22 layout classes (tables, stamps, signatures, handwriting, form fields…), paragraphs, lists, reading order, font family and size; table rows and columns in preview
|
| 44 |
+
- **Small**: about 41 MB for the five default models, about 26 MB with the int8 recogniser
|
| 45 |
+
- **Documented**: every version, its training data and its scores are listed below
|
| 46 |
+
|
| 47 |
+
## How good is it
|
| 48 |
+
|
| 49 |
+
Scores of the default models on their validation sets. Most of these sets are synthetic; the rows on real data say so.
|
| 50 |
+
|
| 51 |
+
| stage | score |
|
| 52 |
+
|---|---|
|
| 53 |
+
| Recognition, printed text | CER 0.22 % (synthetic Khmer print) · 0.29 % (real English lines) |
|
| 54 |
+
| Recognition, real documents | CER 12.7 % (held-out set of real documents) |
|
| 55 |
+
| Recognition, real handwriting | CER 9.8 % on [SoyVitou/khmer-handwritten-dataset-4.2k](https://huggingface.co/datasets/SoyVitou/khmer-handwritten-dataset-4.2k) · 21.3 % on [Thareah/khmer-handwriting](https://huggingface.co/datasets/Thareah/khmer-handwriting) |
|
| 56 |
+
| Layout | mAP50 0.974 · mAP50-95 0.850 (22 classes) |
|
| 57 |
+
| Text-line detection | F1 0.894 synthetic · 0.880 DocLayNet · 0.697 real Khmer PDFs |
|
| 58 |
+
| Font | family 92.0 % · weight 98.0 % · slant 99.9 % |
|
| 59 |
+
| Structure | role accuracy 0.988 · reading order Kendall tau 0.943 |
|
| 60 |
+
|
| 61 |
+
Clean printed text is the strong case. Real handwriting and noisy scans are harder and still improving; the full numbers are in [How the models were trained](#how-the-models-were-trained).
|
| 62 |
+
|
| 63 |
+
## Quick start
|
| 64 |
+
|
| 65 |
+
Download the whole set, so the relative paths in `models.json` resolve:
|
| 66 |
+
|
| 67 |
+
```bash
|
| 68 |
+
pip install -U huggingface_hub onnxruntime numpy pillow
|
| 69 |
+
hf download Thareah/ThaOCR-models --local-dir ThaOCR
|
| 70 |
+
```
|
| 71 |
+
|
| 72 |
+
Read one cropped text line with the recogniser:
|
| 73 |
+
|
| 74 |
+
```python
|
| 75 |
+
import json
|
| 76 |
+
import numpy as np
|
| 77 |
+
import onnxruntime as ort
|
| 78 |
+
from PIL import Image
|
| 79 |
+
|
| 80 |
+
sess = ort.InferenceSession("ThaOCR/rec/crnn_m_v2_stamp2_int8.onnx")
|
| 81 |
+
vocab = json.load(open("ThaOCR/rec/crnn_m_v2_stamp2.vocab.json", encoding="utf-8"))["itos"]
|
| 82 |
+
|
| 83 |
+
img = Image.open("line.png").convert("L") # one cropped text line, grey
|
| 84 |
+
img = img.resize((max(48, round(img.width * 48 / img.height)), 48))
|
| 85 |
+
x = np.asarray(img, dtype=np.float32)[None, None] / 255.0 # [1, 1, 48, W], values 0..1
|
| 86 |
+
|
| 87 |
+
logits = sess.run(None, {sess.get_inputs()[0].name: x})[0] # [time, batch, 195]
|
| 88 |
+
ids = logits.argmax(-1).reshape(-1)
|
| 89 |
+
keep = (ids != 0) & (ids != np.r_[-1, ids[:-1]]) # greedy CTC: drop blanks (id 0) and repeats
|
| 90 |
+
print("".join(vocab[i] for i in ids[keep]))
|
| 91 |
+
```
|
| 92 |
+
|
| 93 |
+
A full page goes through layout, text-line detection, recognition, font and structure in that order. `models.json` is the manifest the ThaOCR backend reads: `file`/`vocab` are relative to the root of this repo, `defaults` names the model used for each stage. Pre/post-processing (letterbox + NMS, DB unclip, width bucketing + CTC) is small numpy/OpenCV code in the ThaOCR backend (`backend/app/services/stages/`).
|
| 94 |
|
| 95 |
## Models
|
| 96 |
|
|
|
|
| 245 |
| Table structure | `table/*.onnx` | **AGPL-3.0** — fine-tuned from Ultralytics YOLO11 weights; training data incl. TabRecSet (CC BY / BY-SA 4.0, attribution required) |
|
| 246 |
|
| 247 |
Training data licences are listed in the Datasets table above. FUNSD and KhmerEduDoc were used for evaluation only (non-commercial); no model weights were trained on them.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|