-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplate_reader.py
More file actions
78 lines (66 loc) · 2.41 KB
/
plate_reader.py
File metadata and controls
78 lines (66 loc) · 2.41 KB
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
# plate_reader.py
from ultralytics import YOLO
from paddleocr import PaddleOCR
import cv2
import numpy as np
import os
class PlateReader:
def __init__(self, yolo_model_path: str, use_gpu: bool = False):
# Load YOLO detection model
self.det_model = YOLO(yolo_model_path)
print(f"[INFO] YOLO model loaded from {yolo_model_path}")
# Load PaddleOCR once
self.ocr = PaddleOCR(
use_angle_cls=False,
lang='en',
use_gpu=use_gpu,
show_log=False
)
print("[INFO] PaddleOCR initialized")
def read_plate(self, image_input):
"""
image_input: str (path) or numpy array (BGR)
Returns: list of dicts: [{text, ocr_conf, det_conf, bbox}]
"""
if isinstance(image_input, str):
img = cv2.imread(image_input)
if img is None:
raise ValueError(f"Cannot read image: {image_input}")
else:
img = image_input
H, W = img.shape[:2]
results = []
# 1) YOLO detect plates
detections = self.det_model.predict(source=img, conf=0.25, save=False, verbose=False)
for r in detections:
if r.boxes is None or len(r.boxes) == 0:
continue
for i, box in enumerate(r.boxes.xyxy):
x1, y1, x2, y2 = map(int, box.tolist())
det_conf = float(r.boxes.conf[i])
# Pad and crop
pad = 5
x1, y1 = max(0, x1-pad), max(0, y1-pad)
x2, y2 = min(W, x2+pad), min(H, y2+pad)
crop = img[y1:y2, x1:x2]
# OCR
ocr_result = self.ocr.ocr(crop, cls=False)
if not ocr_result or not ocr_result[0]:
results.append({
"text": "",
"ocr_conf": 0.0,
"det_conf": det_conf,
"bbox": [x1, y1, x2, y2]
})
continue
# take best line
best = max(ocr_result[0], key=lambda x: x[1][1])
text = best[1][0]
ocr_conf = float(best[1][1])
results.append({
"text": text,
"ocr_conf": ocr_conf,
"det_conf": det_conf,
"bbox": [x1, y1, x2, y2]
})
return results