Add unified CRNN whole-plate reader (segmentation-free) + update card

#4
by DibaAi - opened
Files changed (6) hide show
  1. README.md +134 -0
  2. ocr_cnn.labels.json +30 -0
  3. ocr_cnn.onnx +3 -0
  4. ocr_crnn.labels.json +1 -0
  5. ocr_crnn.onnx +3 -0
  6. plate_yolo.onnx +3 -0
README.md ADDED
@@ -0,0 +1,134 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 ALPR Models — Iranian License Plates (ONNX)
20
+
21
+ A complete, portable model bundle for reading **Iranian vehicle license plates**,
22
+ powering the **Platrix** real‑time ALPR engine. Every model ships as a portable
23
+ **ONNX** graph, so it runs anywhere [ONNX Runtime](https://onnxruntime.ai/) runs —
24
+ no TensorFlow or PyTorch required at inference time.
25
+
26
+ The recommended pipeline is two models: **detect the plate** (YOLO) → **read the
27
+ whole plate** (CRNN).
28
+
29
+ - **Author:** [Dibachain](https://huggingface.co/Dibachain)
30
+
31
+ > **Project & source code:** **https://github.com/AliAkrami1375/Platrix**
32
+
33
+ ---
34
+
35
+ ## Files
36
+
37
+ | File | Description |
38
+ |------|-------------|
39
+ | `plate_yolo.onnx` | **Plate detector** — a YOLOv8 model that localizes the plate in a full frame (single "plate" class). |
40
+ | `ocr_crnn.onnx` | **Whole‑plate reader** *(recommended)* — a CRNN+CTC model that reads the entire plate in one pass (no character segmentation). Input `1×1×32×128` grayscale; output `(T, 33)` logits, CTC‑decoded. Trained on realistic full‑plate images with the official plate font + augmentation. |
41
+ | `ocr_crnn.labels.json` | Class list for the CRNN (CTC blank is the last index). |
42
+ | `ocr_cnn.onnx` | **Per‑character classifier** *(alternative)* — a small CNN that classifies a single segmented glyph (`1×1×32×32` → 28 classes). Trained on real plate character crops. |
43
+ | `ocr_cnn.labels.json` | Class list for the per‑character CNN. |
44
+
45
+ ---
46
+
47
+ ## How the pipeline works
48
+
49
+ 1. **Detect** the plate in the frame with `plate_yolo.onnx`.
50
+ 2. **Read** the whole plate crop with `ocr_crnn.onnx` (CRNN + CTC) — no character
51
+ segmentation, so there are no split/merge errors. Output is decoded with a
52
+ greedy CTC pass into the plate string.
53
+
54
+ The CRNN is trained on realistic full‑plate images rendered with the **official
55
+ Iranian plate font and layout**, plus perspective / rotation / motion‑blur /
56
+ illumination augmentation, so it transfers to real plate crops.
57
+
58
+ ---
59
+
60
+ ## Usage — whole‑plate reader (recommended)
61
+
62
+ ```python
63
+ import json
64
+ import numpy as np
65
+ import cv2
66
+ import onnxruntime as ort
67
+ from huggingface_hub import hf_hub_download
68
+
69
+ onnx = hf_hub_download("Dibachain/ocr-persian", "ocr_crnn.onnx")
70
+ labels = json.load(open(hf_hub_download("Dibachain/ocr-persian", "ocr_crnn.labels.json"), encoding="utf-8"))
71
+ blank = len(labels) # CTC blank is the last index
72
+
73
+ sess = ort.InferenceSession(onnx, providers=["CPUExecutionProvider"])
74
+ name = sess.get_inputs()[0].name
75
+
76
+ def read_plate(plate_bgr):
77
+ gray = cv2.cvtColor(plate_bgr, cv2.COLOR_BGR2GRAY)
78
+ img = cv2.resize(gray, (128, 32)).astype("float32") / 255.0
79
+ logits = sess.run(None, {name: img.reshape(1, 1, 32, 128)})[0][0] # (T, C)
80
+ idx = logits.argmax(1)
81
+ out, prev = [], -1
82
+ for i in idx:
83
+ if i != blank and i != prev and i < len(labels):
84
+ out.append(labels[int(i)])
85
+ prev = int(i)
86
+ return "".join(out) # e.g. "81و63813"
87
+ ```
88
+
89
+ ### With Platrix (end‑to‑end)
90
+
91
+ ```bash
92
+ git clone https://github.com/AliAkrami1375/Platrix.git
93
+ cd Platrix && pip install -r requirements.txt
94
+ huggingface-cli download Dibachain/ocr-persian \
95
+ ocr_crnn.onnx ocr_crnn.labels.json plate_yolo.onnx --local-dir models/
96
+ platrix serve # auto-uses YOLO + CRNN, dashboard on http://localhost:8080
97
+ ```
98
+
99
+ ---
100
+
101
+ ## Labels
102
+
103
+ - **CRNN** (`ocr_crnn.labels.json`): the digits `0–9` and Persian plate letters
104
+ (`ا ب پ ت ث ج ح د ز ژ س ص ط ع ق ل م ن و ه ی`). The CTC **blank** is the extra,
105
+ last index (`len(labels)`).
106
+ - **Per‑character CNN** (`ocr_cnn.labels.json`): 28 classes, one neuron per glyph.
107
+
108
+ ---
109
+
110
+ ## Model architectures
111
+
112
+ - **CRNN reader:** a 5‑block CNN → 2‑layer bidirectional LSTM → linear, trained
113
+ with CTC loss. Input `1×1×32×128`. ~96% exact‑plate accuracy on held‑out
114
+ synthetic plates and strong transfer to real photos.
115
+ - **Per‑character CNN:** `Conv → Conv → Pool → Conv → Pool → FC → FC`, trained on
116
+ real plate character crops (~98% validation accuracy).
117
+
118
+ ---
119
+
120
+ ## Intended use & limitations
121
+
122
+ - **Intended for** parking, access control and traffic‑analytics pipelines that
123
+ first detect and segment plates, then classify each character with this model.
124
+ - **Not** an end‑to‑end plate reader on its own — it classifies **single,
125
+ pre‑segmented characters**. Overall accuracy on a full plate depends on the
126
+ quality of the upstream detection and segmentation.
127
+ - Real‑world plates vary in font, angle, lighting and wear; for best results,
128
+ pair with a strong plate detector and clean segmentation.
129
+
130
+ ---
131
+
132
+ ## License
133
+
134
+ 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
ocr_crnn.labels.json ADDED
@@ -0,0 +1 @@
 
 
1
+ ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "ا", "ب", "ت", "ث", "ج", "ح", "د", "ز", "س", "ش", "ص", "ط", "ع", "ق", "ل", "م", "ن", "ه", "و", "پ", "ژ", "ی"]
ocr_crnn.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9ff09e9d14585c4227647344b52eb4cc2d3292e5b93ee1c5f68d5ece59943a18
3
+ size 10452525
plate_yolo.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a54e475c402e6036bb5c70f1a6ff75179e76098a5c8039bb5d148c0b6421f5c6
3
+ size 12608775