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3 |
+
license: mit
|
| 4 |
+
language:
|
| 5 |
+
- th
|
| 6 |
+
- en
|
| 7 |
+
tags:
|
| 8 |
+
- ocr
|
| 9 |
+
- text-recognition
|
| 10 |
+
- thai-id-card
|
| 11 |
+
- crnn
|
| 12 |
+
- ctc
|
| 13 |
+
- on-device
|
| 14 |
+
- mobile
|
| 15 |
+
- numeric-ocr
|
| 16 |
+
- citizen-id
|
| 17 |
+
pipeline_tag: image-to-text
|
| 18 |
+
---
|
| 19 |
+
|
| 20 |
+
# Thai ID Nano OCR β Numeric OCR Reader (SimpleCRNN (MVP))
|
| 21 |
+
|
| 22 |
+
> **MVP model.** Production upgrade: swap to `ppocrv5` variant (same interface,
|
| 23 |
+
> better accuracy). See `config.json` β `architecture_variant` for programmatic detection.
|
| 24 |
+
|
| 25 |
+
CTC-based text recognition model for Thai National ID card **numeric** fields,
|
| 26 |
+
designed for on-device inference at 30fps on mobile.
|
| 27 |
+
|
| 28 |
+
| Metric | Value |
|
| 29 |
+
|--------|-------|
|
| 30 |
+
| Architecture | SimpleCRNN (MVP) |
|
| 31 |
+
| Variant | `crnn` |
|
| 32 |
+
| ExactMatch | 98.6% |
|
| 33 |
+
| CharAccuracy | 99.4% |
|
| 34 |
+
| Parameters | 3,026,703 |
|
| 35 |
+
| Vocab size | 15 |
|
| 36 |
+
| Best epoch | 10 |
|
| 37 |
+
|
| 38 |
+
## Quick Start
|
| 39 |
+
|
| 40 |
+
```python
|
| 41 |
+
from huggingface_hub import hf_hub_download
|
| 42 |
+
|
| 43 |
+
model_path = hf_hub_download("chayuto/thai-id-ocr-crnn-numeric-reader", "model.pt")
|
| 44 |
+
vocab_path = hf_hub_download("chayuto/thai-id-ocr-crnn-numeric-reader", "vocab.txt")
|
| 45 |
+
config = hf_hub_download("chayuto/thai-id-ocr-crnn-numeric-reader", "config.json")
|
| 46 |
+
```
|
| 47 |
+
|
| 48 |
+
## Architecture
|
| 49 |
+
|
| 50 |
+
**SimpleCRNN** β CNN (4-layer) + BiLSTM (2-layer) + CTC decoder.
|
| 51 |
+
|
| 52 |
+
```
|
| 53 |
+
Input: [B, 3, 48, 320] (RGB, normalized to [-1, 1])
|
| 54 |
+
β CNN: 32β64β128β256 channels, BatchNorm+ReLU, MaxPool(2,2)Γ3
|
| 55 |
+
β AdaptiveAvgPool2d((1, None)) β T=40 time steps
|
| 56 |
+
β BiLSTM: hidden=256, layers=2, dropout=0.1
|
| 57 |
+
β Linear(512 β 15)
|
| 58 |
+
β CTC decode (blank=0, collapse repeats)
|
| 59 |
+
Output: Unicode string
|
| 60 |
+
```
|
| 61 |
+
|
| 62 |
+
## Field Details
|
| 63 |
+
|
| 64 |
+
- **Zones:** `num_id_zone` (13-digit CID), `num_dob_zone` (DD/MM/YYYY)
|
| 65 |
+
- **Charset:** `0123456789/- .` (14 chars + CTC blank)
|
| 66 |
+
- **Post-validation:** CID Modulo 11 checksum on digit 13
|
| 67 |
+
|
| 68 |
+
## Input Preprocessing
|
| 69 |
+
|
| 70 |
+
```python
|
| 71 |
+
import cv2
|
| 72 |
+
import numpy as np
|
| 73 |
+
|
| 74 |
+
def preprocess(img_path, height=48, max_width=320):
|
| 75 |
+
img = cv2.imread(img_path)
|
| 76 |
+
h, w = img.shape[:2]
|
| 77 |
+
ratio = height / h
|
| 78 |
+
new_w = min(int(w * ratio), max_width)
|
| 79 |
+
img = cv2.resize(img, (new_w, height))
|
| 80 |
+
# Pad to max_width with white
|
| 81 |
+
if new_w < max_width:
|
| 82 |
+
pad = np.full((height, max_width - new_w, 3), 255, dtype=np.uint8)
|
| 83 |
+
img = np.concatenate([img, pad], axis=1)
|
| 84 |
+
# Normalize to [-1, 1]
|
| 85 |
+
img = img.astype(np.float32) / 255.0
|
| 86 |
+
img = (img - 0.5) / 0.5
|
| 87 |
+
return np.transpose(img, (2, 0, 1)) # CHW
|
| 88 |
+
```
|
| 89 |
+
|
| 90 |
+
## CTC Decoding
|
| 91 |
+
|
| 92 |
+
```python
|
| 93 |
+
def ctc_decode(indices, vocab_chars, blank_idx=0):
|
| 94 |
+
chars, prev = [], -1
|
| 95 |
+
for idx in indices:
|
| 96 |
+
if idx != blank_idx and idx != prev:
|
| 97 |
+
if 1 <= idx <= len(vocab_chars):
|
| 98 |
+
chars.append(vocab_chars[idx - 1])
|
| 99 |
+
prev = idx
|
| 100 |
+
return "".join(chars)
|
| 101 |
+
```
|
| 102 |
+
|
| 103 |
+
## Loading the Model
|
| 104 |
+
|
| 105 |
+
```python
|
| 106 |
+
import torch
|
| 107 |
+
import torch.nn as nn
|
| 108 |
+
|
| 109 |
+
class SimpleCRNN(nn.Module):
|
| 110 |
+
def __init__(self, num_classes, img_h=48):
|
| 111 |
+
super().__init__()
|
| 112 |
+
self.cnn = nn.Sequential(
|
| 113 |
+
nn.Conv2d(3, 32, 3, padding=1), nn.BatchNorm2d(32), nn.ReLU(), nn.MaxPool2d(2, 2),
|
| 114 |
+
nn.Conv2d(32, 64, 3, padding=1), nn.BatchNorm2d(64), nn.ReLU(), nn.MaxPool2d(2, 2),
|
| 115 |
+
nn.Conv2d(64, 128, 3, padding=1), nn.BatchNorm2d(128), nn.ReLU(), nn.MaxPool2d(2, 2),
|
| 116 |
+
nn.Conv2d(128, 256, 3, padding=1), nn.BatchNorm2d(256), nn.ReLU(),
|
| 117 |
+
nn.AdaptiveAvgPool2d((1, None)),
|
| 118 |
+
)
|
| 119 |
+
self.rnn = nn.LSTM(256, 256, num_layers=2, bidirectional=True, batch_first=True, dropout=0.1)
|
| 120 |
+
self.fc = nn.Linear(512, num_classes)
|
| 121 |
+
|
| 122 |
+
def forward(self, x):
|
| 123 |
+
features = self.cnn(x).squeeze(2).permute(0, 2, 1)
|
| 124 |
+
rnn_out, _ = self.rnn(features)
|
| 125 |
+
return self.fc(rnn_out).permute(1, 0, 2) # (T, B, C) for CTC
|
| 126 |
+
|
| 127 |
+
model = SimpleCRNN(num_classes=15)
|
| 128 |
+
model.load_state_dict(torch.load(model_path, map_location="cpu"))
|
| 129 |
+
model.eval()
|
| 130 |
+
```
|
| 131 |
+
|
| 132 |
+
## Pipeline Context
|
| 133 |
+
|
| 134 |
+
This model is one of 3 Reader experts in the **Thai ID Nano OCR** pipeline:
|
| 135 |
+
|
| 136 |
+
```
|
| 137 |
+
Camera Frame β YOLO26n Finder (5-class, single pass)
|
| 138 |
+
β num_id_zone, num_dob_zone β Numeric Reader
|
| 139 |
+
β text_eng_zone β English Reader
|
| 140 |
+
β text_thai_zone β Thai Reader
|
| 141 |
+
β Validator (Mod11 checksum, date logic)
|
| 142 |
+
```
|
| 143 |
+
|
| 144 |
+
Total pipeline: <15 MB, 30fps on mobile.
|
| 145 |
+
|
| 146 |
+
## Files
|
| 147 |
+
|
| 148 |
+
| File | Description |
|
| 149 |
+
|------|-------------|
|
| 150 |
+
| `model.pt` | PyTorch `state_dict` (~12 MB) |
|
| 151 |
+
| `vocab.txt` | Character vocabulary, one per line (`<space>` = space). CTC blank is implicit at index 0. |
|
| 152 |
+
| `config.json` | Architecture params, training metadata, charset |
|
| 153 |
+
|
| 154 |
+
## License
|
| 155 |
+
|
| 156 |
+
MIT
|