Add YOLO plate detector + end-to-end model card

#3
by DibaAi - opened
Files changed (4) hide show
  1. README.md +129 -0
  2. ocr_cnn.labels.json +30 -0
  3. ocr_cnn.onnx +3 -0
  4. plate_yolo.onnx +3 -0
README.md ADDED
@@ -0,0 +1,129 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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` | Character-recognition CNN (opset 13) |
43
+ | `ocr_cnn.labels.json` | Ordered list mapping each output neuron to its character |
44
+ | `plate_yolo.onnx` | **Plate detector** — a YOLOv8n model that localizes plates in a full frame (single "plate" class). Pair it with the OCR for end-to-end reading. |
45
+
46
+ ---
47
+
48
+ ## How the pipeline works
49
+
50
+ Platrix reads a plate in three stages; this model is stage 3:
51
+
52
+ 1. **Detect** the plate region in the frame.
53
+ 2. **Segment** the plate into individual character images (normalized to a
54
+ white‑on‑black `32 × 32` glyph).
55
+ 3. **Recognize** each glyph with this model and assemble the plate string.
56
+
57
+ The training images are preprocessed **identically** to the segmenter's output
58
+ (Otsu binarization, tight crop, square‑pad, resize) so the model sees the same
59
+ glyph framing in training and in production. Training also applies random affine
60
+ augmentation (scale / shift / rotation) for robustness to how characters are
61
+ framed.
62
+
63
+ ---
64
+
65
+ ## Usage
66
+
67
+ ```python
68
+ import json
69
+ import numpy as np
70
+ import onnxruntime as ort
71
+ from huggingface_hub import hf_hub_download
72
+
73
+ onnx_path = hf_hub_download("Dibachain/ocr-persian", "ocr_cnn.onnx")
74
+ labels_path = hf_hub_download("Dibachain/ocr-persian", "ocr_cnn.labels.json")
75
+
76
+ labels = json.load(open(labels_path, encoding="utf-8"))
77
+ session = ort.InferenceSession(onnx_path, providers=["CPUExecutionProvider"])
78
+ input_name = session.get_inputs()[0].name
79
+
80
+ def classify(glyph_32x32_uint8):
81
+ """glyph: a 32x32 grayscale image, white character on black background."""
82
+ x = (glyph_32x32_uint8.astype("float32") / 255.0).reshape(1, 1, 32, 32)
83
+ logits = session.run(None, {input_name: x})[0]
84
+ return labels[int(np.argmax(logits))]
85
+ ```
86
+
87
+ ### With Platrix (end‑to‑end plate reading)
88
+
89
+ ```bash
90
+ git clone https://github.com/AliAkrami1375/Platrix.git
91
+ cd Platrix
92
+ pip install -r requirements.txt
93
+ # place ocr_cnn.onnx + ocr_cnn.labels.json in ./models/
94
+ PLATRIX_OCR=onnx platrix serve # dashboard on http://localhost:8080
95
+ ```
96
+
97
+ ---
98
+
99
+ ## Labels
100
+
101
+ 28 classes: the digits `0–9` and the Persian letters that appear on Iranian
102
+ plates (`ا ب پ ت ج د س ص ط ع ق ل م ن و ه ی` and special markers). Each neuron
103
+ maps to exactly one plate character; the exact order is in `ocr_cnn.labels.json`.
104
+
105
+ ---
106
+
107
+ ## Model architecture
108
+
109
+ A small CNN: `Conv(32) → Conv(32) → MaxPool → Conv(64) → MaxPool → FC(128) → FC(28)`
110
+ with dropout, trained with Adam and cross‑entropy on real plate character crops
111
+ with affine augmentation (~98% validation accuracy).
112
+
113
+ ---
114
+
115
+ ## Intended use & limitations
116
+
117
+ - **Intended for** parking, access control and traffic‑analytics pipelines that
118
+ first detect and segment plates, then classify each character with this model.
119
+ - **Not** an end‑to‑end plate reader on its own — it classifies **single,
120
+ pre‑segmented characters**. Overall accuracy on a full plate depends on the
121
+ quality of the upstream detection and segmentation.
122
+ - Real‑world plates vary in font, angle, lighting and wear; for best results,
123
+ pair with a strong plate detector and clean segmentation.
124
+
125
+ ---
126
+
127
+ ## License
128
+
129
+ 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
plate_yolo.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a54e475c402e6036bb5c70f1a6ff75179e76098a5c8039bb5d148c0b6421f5c6
3
+ size 12608775