Add Persian OCR model (ONNX) + labels + model card

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