Update to real-plate-trained model (Iranis, 28 classes, ~98% val)
#2
by DibaAi - opened
- README.md +128 -0
- ocr_cnn.labels.json +30 -0
- ocr_cnn.onnx +3 -0
README.md
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
library_name: onnx
|
| 4 |
+
pipeline_tag: image-classification
|
| 5 |
+
tags:
|
| 6 |
+
- ocr
|
| 7 |
+
- persian
|
| 8 |
+
- farsi
|
| 9 |
+
- alpr
|
| 10 |
+
- anpr
|
| 11 |
+
- license-plate
|
| 12 |
+
- iran
|
| 13 |
+
- onnx
|
| 14 |
+
- computer-vision
|
| 15 |
+
language:
|
| 16 |
+
- fa
|
| 17 |
+
---
|
| 18 |
+
|
| 19 |
+
# Persian OCR — Character Recognition (ONNX)
|
| 20 |
+
|
| 21 |
+
A compact convolutional neural network that recognizes **Persian license‑plate
|
| 22 |
+
characters** — the digits `0–9` and the Persian letters used on Iranian vehicle
|
| 23 |
+
plates — trained on **real‑world plate character crops**. It is the OCR stage of
|
| 24 |
+
the **Platrix** real‑time ALPR engine and ships as a portable **ONNX** graph, so
|
| 25 |
+
it runs anywhere [ONNX Runtime](https://onnxruntime.ai/) runs — no TensorFlow or
|
| 26 |
+
PyTorch required at inference time.
|
| 27 |
+
|
| 28 |
+
- **Task:** single‑character image classification (28 classes)
|
| 29 |
+
- **Input:** `1 × 1 × 32 × 32` grayscale tensor, values in `[0, 1]` (white glyph on black)
|
| 30 |
+
- **Output:** `1 × 44` logits → `argmax` → character via the bundled label map
|
| 31 |
+
- **Format:** ONNX (opset 13), ~2.2 MB
|
| 32 |
+
- **Author:** [Dibachain](https://huggingface.co/Dibachain)
|
| 33 |
+
|
| 34 |
+
> **Project & source code:** **https://github.com/AliAkrami1375/Platrix**
|
| 35 |
+
|
| 36 |
+
---
|
| 37 |
+
|
| 38 |
+
## Files
|
| 39 |
+
|
| 40 |
+
| File | Description |
|
| 41 |
+
|------|-------------|
|
| 42 |
+
| `ocr_cnn.onnx` | The inference graph (opset 13) |
|
| 43 |
+
| `ocr_cnn.labels.json` | Ordered list mapping each output neuron to its character |
|
| 44 |
+
|
| 45 |
+
---
|
| 46 |
+
|
| 47 |
+
## How the pipeline works
|
| 48 |
+
|
| 49 |
+
Platrix reads a plate in three stages; this model is stage 3:
|
| 50 |
+
|
| 51 |
+
1. **Detect** the plate region in the frame.
|
| 52 |
+
2. **Segment** the plate into individual character images (normalized to a
|
| 53 |
+
white‑on‑black `32 × 32` glyph).
|
| 54 |
+
3. **Recognize** each glyph with this model and assemble the plate string.
|
| 55 |
+
|
| 56 |
+
The training images are preprocessed **identically** to the segmenter's output
|
| 57 |
+
(Otsu binarization, tight crop, square‑pad, resize) so the model sees the same
|
| 58 |
+
glyph framing in training and in production. Training also applies random affine
|
| 59 |
+
augmentation (scale / shift / rotation) for robustness to how characters are
|
| 60 |
+
framed.
|
| 61 |
+
|
| 62 |
+
---
|
| 63 |
+
|
| 64 |
+
## Usage
|
| 65 |
+
|
| 66 |
+
```python
|
| 67 |
+
import json
|
| 68 |
+
import numpy as np
|
| 69 |
+
import onnxruntime as ort
|
| 70 |
+
from huggingface_hub import hf_hub_download
|
| 71 |
+
|
| 72 |
+
onnx_path = hf_hub_download("Dibachain/ocr-persian", "ocr_cnn.onnx")
|
| 73 |
+
labels_path = hf_hub_download("Dibachain/ocr-persian", "ocr_cnn.labels.json")
|
| 74 |
+
|
| 75 |
+
labels = json.load(open(labels_path, encoding="utf-8"))
|
| 76 |
+
session = ort.InferenceSession(onnx_path, providers=["CPUExecutionProvider"])
|
| 77 |
+
input_name = session.get_inputs()[0].name
|
| 78 |
+
|
| 79 |
+
def classify(glyph_32x32_uint8):
|
| 80 |
+
"""glyph: a 32x32 grayscale image, white character on black background."""
|
| 81 |
+
x = (glyph_32x32_uint8.astype("float32") / 255.0).reshape(1, 1, 32, 32)
|
| 82 |
+
logits = session.run(None, {input_name: x})[0]
|
| 83 |
+
return labels[int(np.argmax(logits))]
|
| 84 |
+
```
|
| 85 |
+
|
| 86 |
+
### With Platrix (end‑to‑end plate reading)
|
| 87 |
+
|
| 88 |
+
```bash
|
| 89 |
+
git clone https://github.com/AliAkrami1375/Platrix.git
|
| 90 |
+
cd Platrix
|
| 91 |
+
pip install -r requirements.txt
|
| 92 |
+
# place ocr_cnn.onnx + ocr_cnn.labels.json in ./models/
|
| 93 |
+
PLATRIX_OCR=onnx platrix serve # dashboard on http://localhost:8080
|
| 94 |
+
```
|
| 95 |
+
|
| 96 |
+
---
|
| 97 |
+
|
| 98 |
+
## Labels
|
| 99 |
+
|
| 100 |
+
28 classes: the digits `0–9` and the Persian letters that appear on Iranian
|
| 101 |
+
plates (`ا ب پ ت ج د س ص ط ع ق ل م ن و ه ی` and special markers). Each neuron
|
| 102 |
+
maps to exactly one plate character; the exact order is in `ocr_cnn.labels.json`.
|
| 103 |
+
|
| 104 |
+
---
|
| 105 |
+
|
| 106 |
+
## Model architecture
|
| 107 |
+
|
| 108 |
+
A small CNN: `Conv(32) → Conv(32) → MaxPool → Conv(64) → MaxPool → FC(128) → FC(28)`
|
| 109 |
+
with dropout, trained with Adam and cross‑entropy on real plate character crops
|
| 110 |
+
with affine augmentation (~98% validation accuracy).
|
| 111 |
+
|
| 112 |
+
---
|
| 113 |
+
|
| 114 |
+
## Intended use & limitations
|
| 115 |
+
|
| 116 |
+
- **Intended for** parking, access control and traffic‑analytics pipelines that
|
| 117 |
+
first detect and segment plates, then classify each character with this model.
|
| 118 |
+
- **Not** an end‑to‑end plate reader on its own — it classifies **single,
|
| 119 |
+
pre‑segmented characters**. Overall accuracy on a full plate depends on the
|
| 120 |
+
quality of the upstream detection and segmentation.
|
| 121 |
+
- Real‑world plates vary in font, angle, lighting and wear; for best results,
|
| 122 |
+
pair with a strong plate detector and clean segmentation.
|
| 123 |
+
|
| 124 |
+
---
|
| 125 |
+
|
| 126 |
+
## License
|
| 127 |
+
|
| 128 |
+
Released under the **MIT License**. © Dibachain.
|
ocr_cnn.labels.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[
|
| 2 |
+
"0",
|
| 3 |
+
"1",
|
| 4 |
+
"2",
|
| 5 |
+
"3",
|
| 6 |
+
"4",
|
| 7 |
+
"5",
|
| 8 |
+
"6",
|
| 9 |
+
"7",
|
| 10 |
+
"8",
|
| 11 |
+
"9",
|
| 12 |
+
"ا",
|
| 13 |
+
"ب",
|
| 14 |
+
"ت",
|
| 15 |
+
"ج",
|
| 16 |
+
"د",
|
| 17 |
+
"س",
|
| 18 |
+
"ص",
|
| 19 |
+
"ط",
|
| 20 |
+
"ع",
|
| 21 |
+
"ق",
|
| 22 |
+
"ل",
|
| 23 |
+
"م",
|
| 24 |
+
"ن",
|
| 25 |
+
"ه",
|
| 26 |
+
"و",
|
| 27 |
+
"پ",
|
| 28 |
+
"ژ",
|
| 29 |
+
"ی"
|
| 30 |
+
]
|
ocr_cnn.onnx
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:7d573c51cc855a8e080f1f88597477f4fb5a2b9cafa1bb125bd6038e441f5bca
|
| 3 |
+
size 2226402
|