File size: 8,457 Bytes
fb7f78e
af30bc6
fb7f78e
 
af30bc6
 
fb7f78e
af30bc6
603954b
 
a0ba8c9
 
603954b
fb7f78e
603954b
 
a0ba8c9
 
603954b
 
 
 
 
 
 
a0ba8c9
 
603954b
 
fb7f78e
7d1df89
 
 
 
 
af30bc6
fb7f78e
 
af30bc6
fb7f78e
 
 
 
af30bc6
fb7f78e
 
af30bc6
 
 
 
 
 
fb7f78e
 
af30bc6
fb7f78e
603954b
af30bc6
603954b
 
7d1df89
 
 
 
 
 
 
 
 
af30bc6
603954b
 
 
 
 
fb7f78e
af30bc6
 
603954b
af30bc6
603954b
 
 
 
 
 
 
 
af30bc6
603954b
af30bc6
603954b
af30bc6
603954b
af30bc6
603954b
 
 
fb7f78e
af30bc6
 
603954b
 
af30bc6
603954b
 
af30bc6
603954b
 
 
 
 
 
 
af30bc6
7d1df89
603954b
af30bc6
7d1df89
af30bc6
603954b
af30bc6
603954b
af30bc6
603954b
 
 
 
fb7f78e
af30bc6
fb7f78e
af30bc6
 
 
fb7f78e
af30bc6
fb7f78e
 
084c5ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
af30bc6
 
fb7f78e
 
084c5ef
 
 
 
fb7f78e
af30bc6
fb7f78e
 
af30bc6
fb7f78e
 
 
af30bc6
 
 
 
fb7f78e
 
af30bc6
 
 
fb7f78e
 
 
 
af30bc6
 
 
 
 
fb7f78e
 
af30bc6
fb7f78e
af30bc6
 
 
 
 
fb7f78e
af30bc6
fb7f78e
 
 
 
 
af30bc6
fb7f78e
 
 
af30bc6
 
 
 
 
fb7f78e
 
af30bc6
fb7f78e
 
 
 
 
 
 
 
af30bc6
fb7f78e
 
af30bc6
fb7f78e
af30bc6
 
fb7f78e
 
 
 
af30bc6
 
 
 
 
 
7d1df89
af30bc6
 
fb7f78e
 
af30bc6
fb7f78e
 
 
af30bc6
 
 
 
 
fb7f78e
 
af30bc6
 
 
 
fb7f78e
 
 
 
 
 
 
af30bc6
 
 
fb7f78e
af30bc6
fb7f78e
 
 
af30bc6
 
fb7f78e
af30bc6
fb7f78e
af30bc6
 
 
fb7f78e
af30bc6
fb7f78e
a0ba8c9
 
 
70ebdb9
 
fb7f78e
 
af30bc6
 
 
 
 
 
 
 
fb7f78e
7d1df89
 
 
 
 
 
 
 
 
fb7f78e
 
 
af30bc6
 
 
 
 
 
 
 
fb7f78e
 
 
 
 
084c5ef
 
fb7f78e
 
 
 
 
 
084c5ef
fb7f78e
 
 
 
 
 
084c5ef
 
 
 
 
fb7f78e
084c5ef
 
fb7f78e
084c5ef
 
af30bc6
084c5ef
af30bc6
084c5ef
 
af30bc6
084c5ef
 
 
 
 
 
 
 
fb7f78e
70ebdb9
 
af30bc6
 
 
 
 
 
084c5ef
af30bc6
1
2
3
4
5
6
7
8
9
10
11
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
import re

import cv2
import numpy as np
import torch

from PIL import Image

try:
    from paddleocr import PaddleOCR
except Exception as e:
    print(f"❌ PaddleOCR Import Error: {e}")
    PaddleOCR = None

try:
    from ultralytics import YOLO
except Exception as e:
    print(f"❌ YOLO Import Error: {e}")
    YOLO = None

try:
    from transformers import (
        AutoImageProcessor,
        AutoModelForImageClassification
    )
except Exception as e:
    print(f"❌ Transformers Import Error: {e}")
    AutoImageProcessor = None
    AutoModelForImageClassification = None

# ================= DEVICE CONFIG ================= #

DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'
print(f"🖥️  Using device: {DEVICE.upper()}")

# ================= STATES ================= #

STATE_CODES = {

    "TN": "Tamil Nadu",
    "KA": "Karnataka",
    "KL": "Kerala",
    "AP": "Andhra Pradesh",
    "TS": "Telangana",
    "MH": "Maharashtra",
    "DL": "Delhi",
    "GJ": "Gujarat",
    "RJ": "Rajasthan",
    "UP": "Uttar Pradesh",
    "WB": "West Bengal",
    "HR": "Haryana",
    "PB": "Punjab"
}

# ================= YOLO ================= #

yolo_model = None

if YOLO is not None:
    try:
        yolo_model = YOLO("license-plate-finetune-v1s.pt")
        
        # Move model to device and optimize
        yolo_model.to(DEVICE)
        yolo_model.overrides['conf'] = 0.5
        yolo_model.overrides['iou'] = 0.45
        yolo_model.overrides['max_det'] = 100
        
        print(f"✅ YOLO Loaded on {DEVICE.upper()}")

    except Exception as e:
        print("YOLO Error:", e)
        yolo_model = None
else:
    print("⚠️  YOLO/ultralytics not installed")

# ================= OCR ================= #

ocr = None

if PaddleOCR is not None:
    try:

        ocr = PaddleOCR(
            use_angle_cls=True,
            lang="en",
            show_log=False
        )

        print("✅ OCR Loaded")

    except Exception as e:

        print("OCR Error:", e)

        ocr = None
else:
    print("⚠️  PaddleOCR not installed")

# ================= VEHICLE MODEL ================= #

processor = None
vehicle_model = None

if AutoImageProcessor is not None and AutoModelForImageClassification is not None:
    try:

        processor = AutoImageProcessor.from_pretrained(
            "dima806/vehicle_10_types_image_detection"
        )

        vehicle_model = AutoModelForImageClassification.from_pretrained(
            "dima806/vehicle_10_types_image_detection"
        )

        vehicle_model.to(DEVICE)
        vehicle_model.eval()

        print(f"✅ Vehicle Model Loaded on {DEVICE.upper()}")

    except Exception as e:

        print("Vehicle Model Error:", e)

        processor = None
        vehicle_model = None
else:
    print("⚠️  Transformers not installed")

# ================= REGEX ================= #

plate_regex = re.compile(
    r"[A-Z]{2}[0-9]{1,2}[A-Z]{1,3}[0-9]{3,4}"
)

# ================= PREPROCESS ================= #

def preprocess_plate(crop):
    """Lightweight preprocessing - faster than full CLAHE"""
    try:
        # Skip heavy CLAHE, use simple resize + adaptive threshold
        gray = cv2.cvtColor(crop, cv2.COLOR_RGB2GRAY)
        resized = cv2.resize(gray, (320, 96))
        
        # Use adaptive thresholding instead of CLAHE (faster)
        enhanced = cv2.adaptiveThreshold(
            resized, 255, 
            cv2.ADAPTIVE_THRESH_GAUSSIAN_C, 
            cv2.THRESH_BINARY, 11, 2
        )
        
        # Light bilateral filter only
        filtered = cv2.bilateralFilter(enhanced, 5, 30, 30)
        
        return cv2.cvtColor(filtered, cv2.COLOR_GRAY2BGR)
    except Exception as e:
        print(f"Preprocess error: {e}")
        return crop

# ================= AUGMENT ================= #

def build_crops(crop):
    """Return only best crop variant instead of 3"""
    # Only return the original crop - no multiple variants
    # This reduces OCR calls from 3x to 1x
    return [crop]

# ================= OCR ================= #

def run_ocr(image):

    if ocr is None:
        return []

    return ocr.ocr(
        image,
        cls=True
    )

def parse_ocr(ocr_out):

    texts = []
    confs = []

    if not ocr_out:
        return texts, confs

    items = (
        ocr_out[0]
        if isinstance(ocr_out[0], list)
        else ocr_out
    )

    for item in items:

        try:

            txt, conf = item[1]

            texts.append(txt)

            confs.append(float(conf))

        except:
            continue

    return texts, confs

# ================= CLEAN ================= #

def clean_text(text):

    return re.sub(
        r"[^A-Z0-9]",
        "",
        text.upper()
    )

def fix_common(text):

    return (
        text.replace("O", "0")
        .replace("I", "1")
        .replace("B", "8")
        .replace("Z", "2")
        .replace("S", "5")
    )

# ================= VEHICLE CLASSIFY ================= #

def classify_vehicle(image_np):

    try:

        if processor is None:
            return "unknown", 0.0

        image_pil = Image.fromarray(image_np)

        inputs = processor(
            images=image_pil,
            return_tensors="pt"
        )

        inputs = {
            k: v.to(DEVICE)
            for k, v in inputs.items()
        }

        with torch.no_grad():

            outputs = vehicle_model(**inputs)

        logits = outputs.logits

        probs = torch.nn.functional.softmax(
            logits,
            dim=-1
        )

        pred = probs.argmax(-1).item()

        confidence = float(
            probs.max().item()
        )

        label = vehicle_model.config.id2label[pred]

        return label, confidence

    except Exception as e:

        print("Classification Error:", e)

        return "unknown", 0.0

# ================= STATE ================= #

def extract_state(plate):

    if len(plate) < 2:
        return "UNKNOWN"

    code = plate[:2]

    return code if code in STATE_CODES else "UNKNOWN"

# ================= DETECT ================= #

def detect_plate(image):

    if isinstance(image, Image.Image):
        image = np.array(image.convert("RGB"))

    # ENABLE vehicle classification
    vehicle_type, vehicle_conf = classify_vehicle(image)

    if yolo_model is None:

        return (
            "",
            "UNKNOWN",
            vehicle_type,
            vehicle_conf,
            False
        )

    # YOLO detection with optimizations for speed
    results = yolo_model(
        image,
        conf=0.5,           # Confidence threshold
        iou=0.45,           # IoU threshold  
        imgsz=384,          # Image size
        verbose=False,      # No logging
        device=0 if DEVICE == 'cuda' else 'cpu'  # Use GPU if available
    )
    boxes = results[0].boxes

    if boxes is None or len(boxes) == 0:

        return (
            "",
            "UNKNOWN",
            vehicle_type,
            vehicle_conf,
            False
        )

    h, w = image.shape[:2]
    xyxy = boxes.xyxy.cpu().numpy()
    confs = boxes.conf.cpu().numpy()

    best_plate = ""
    best_confidence = 0.0

    for i, (x1, y1, x2, y2) in enumerate(xyxy):

        if confs[i] < 0.5:
            continue

        pad = int(0.12 * max(x2 - x1, y2 - y1))
        l = max(int(x1 - pad), 0)
        t = max(int(y1 - pad), 0)
        r = min(int(x2 + pad), w - 1)
        b = min(int(y2 + pad), h - 1)

        crop = image[t:b, l:r]
        
        # Only ONE preprocessing + OCR per detection (no variants)
        pre = preprocess_plate(crop)
        ocr_out = run_ocr(pre)
        texts, confs_ocr = parse_ocr(ocr_out)

        # Find best text in this detection
        for txt, cf in zip(texts, confs_ocr):

            if cf < 0.3:
                continue

            norm = fix_common(clean_text(txt))

            if len(norm) < 4:
                continue

            # Check if matches Indian plate regex
            match = plate_regex.search(norm)
            if match:
                plate = match.group(0)
                # Early exit on first good match
                if cf > best_confidence:
                    best_plate = plate
                    best_confidence = cf

    plate = best_plate.upper() if best_plate else ""
    state = extract_state(plate) if plate else "UNKNOWN"

    return (
        plate,
        state,
        vehicle_type,
        vehicle_conf,
        len(plate) > 0
    )