Views
No views yet
MVP model. Production upgrade: swap toppocrv5variant (same interface, better accuracy). Seeconfig.json→architecture_variantfor programmatic detection.
| Metric | Value |
|---|---|
| Architecture | SimpleCRNN (MVP) |
| Variant | crnn |
| ExactMatch | 97.6% |
| CharAccuracy | 98.2% |
| Parameters | 3,048,762 |
| Vocab size | 58 |
| Best epoch | 75 |
1from huggingface_hub import hf_hub_download
2
3model_path = hf_hub_download("chayuto/thai-id-ocr-crnn-english-reader", "model.pt")
4vocab_path = hf_hub_download("chayuto/thai-id-ocr-crnn-english-reader", "vocab.txt")
5config = hf_hub_download("chayuto/thai-id-ocr-crnn-english-reader", "config.json")Input: [B, 3, 48, 320] (RGB, normalized to [-1, 1])
→ CNN: 32→64→128→256 channels, BatchNorm+ReLU, MaxPool(2,2)×3
→ AdaptiveAvgPool2d((1, None)) → T=40 time steps
→ BiLSTM: hidden=256, layers=2, dropout=0.1
→ Linear(512 → 58)
→ CTC decode (blank=0, collapse repeats)
Output: Unicode stringtext_eng_zone (romanized Thai names).,-' and space (57 chars + CTC blank)1import cv2
2import numpy as np
3
4def preprocess(img_path, height=48, max_width=320):
5 img = cv2.imread(img_path)
6 h, w = img.shape[:2]
7 ratio = height / h
8 new_w = min(int(w * ratio), max_width)
9 img = cv2.resize(img, (new_w, height))
10 # Pad to max_width with white
11 if new_w < max_width:
12 pad = np.full((height, max_width - new_w, 3), 255, dtype=np.uint8)
13 img = np.concatenate([img, pad], axis=1)
14 # Normalize to [-1, 1]
15 img = img.astype(np.float32) / 255.0
16 img = (img - 0.5) / 0.5
17 return np.transpose(img, (2, 0, 1)) # CHW1def ctc_decode(indices, vocab_chars, blank_idx=0):
2 chars, prev = [], -1
3 for idx in indices:
4 if idx != blank_idx and idx != prev:
5 if 1 <= idx <= len(vocab_chars):
6 chars.append(vocab_chars[idx - 1])
7 prev = idx
8 return "".join(chars)1import torch
2import torch.nn as nn
3
4class SimpleCRNN(nn.Module):
5 def __init__(self, num_classes, img_h=48):
6 super().__init__()
7 self.cnn = nn.Sequential(
8 nn.Conv2d(3, 32, 3, padding=1), nn.BatchNorm2d(32), nn.ReLU(), nn.MaxPool2d(2, 2),
9 nn.Conv2d(32, 64, 3, padding=1), nn.BatchNorm2d(64), nn.ReLU(), nn.MaxPool2d(2, 2),
10 nn.Conv2d(64, 128, 3, padding=1), nn.BatchNorm2d(128), nn.ReLU(), nn.MaxPool2d(2, 2),
11 nn.Conv2d(128, 256, 3, padding=1), nn.BatchNorm2d(256), nn.ReLU(),
12 nn.AdaptiveAvgPool2d((1, None)),
13 )
14 self.rnn = nn.LSTM(256, 256, num_layers=2, bidirectional=True, batch_first=True, dropout=0.1)
15 self.fc = nn.Linear(512, num_classes)
16
17 def forward(self, x):
18 features = self.cnn(x).squeeze(2).permute(0, 2, 1)
19 rnn_out, _ = self.rnn(features)
20 return self.fc(rnn_out).permute(1, 0, 2) # (T, B, C) for CTC
21
22model = SimpleCRNN(num_classes=58)
23model.load_state_dict(torch.load(model_path, map_location="cpu"))
24model.eval()Camera Frame → YOLO26n Finder (5-class, single pass)
→ num_id_zone, num_dob_zone → Numeric Reader
→ text_eng_zone → English Reader
→ text_thai_zone → Thai Reader
→ Validator (Mod11 checksum, date logic)| File | Description |
|---|---|
model.pt | PyTorch state_dict (~12 MB) |
vocab.txt | Character vocabulary, one per line (<space> = space). CTC blank is implicit at index 0. |
config.json | Architecture params, training metadata, charset |