Abs6187 commited on
Commit
8e35641
·
verified ·
1 Parent(s): 73a7a8c

Update license_plate_ocr.py

Browse files
Files changed (1) hide show
  1. license_plate_ocr.py +71 -236
license_plate_ocr.py CHANGED
@@ -1,120 +1,76 @@
1
- import torch
2
- from transformers import TrOCRProcessor, VisionEncoderDecoderModel
3
- from PIL import Image, ImageEnhance, ImageFilter
4
  import cv2
5
  import numpy as np
 
6
  import re
7
- import easyocr
8
  import os
9
- from typing import List, Dict, Optional, Union
10
 
11
- class LicensePlateOCR:
12
  def __init__(self):
13
- self.trocr_processor = None
14
- self.trocr_model = None
15
- self.easyocr_reader = None
16
- self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
17
-
18
- def load_trocr_model(self):
19
- try:
20
- print("Loading TrOCR model...")
21
- self.trocr_processor = TrOCRProcessor.from_pretrained('microsoft/trocr-base-printed')
22
- self.trocr_model = VisionEncoderDecoderModel.from_pretrained('microsoft/trocr-base-printed')
23
- self.trocr_model.to(self.device)
24
- print(f"TrOCR model loaded on {self.device}")
25
- return True
26
- except Exception as e:
27
- print(f"Error loading TrOCR model: {e}")
28
- return False
29
 
30
- def load_easyocr_model(self):
31
  try:
32
- print("Loading EasyOCR model...")
33
- self.easyocr_reader = easyocr.Reader(['en'], gpu=torch.cuda.is_available())
34
- print("EasyOCR model loaded")
35
  return True
36
  except Exception as e:
37
- print(f"Error loading EasyOCR model: {e}")
38
  return False
39
 
40
- def preprocess_license_plate(self, image: Image.Image) -> List[Image.Image]:
41
- processed_images = []
42
-
43
  try:
44
- original = image.copy()
45
- processed_images.append(original)
46
-
47
- if image.mode != 'RGB':
48
- image = image.convert('RGB')
49
-
50
- enhancer = ImageEnhance.Contrast(image)
51
- high_contrast = enhancer.enhance(2.0)
52
- processed_images.append(high_contrast)
53
-
54
- enhancer = ImageEnhance.Sharpness(high_contrast)
55
- sharpened = enhancer.enhance(2.0)
56
- processed_images.append(sharpened)
57
 
58
- img_array = np.array(image)
59
- gray = cv2.cvtColor(img_array, cv2.COLOR_RGB2GRAY)
60
 
61
- clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8,8))
62
- clahe_img = clahe.apply(gray)
63
- clahe_pil = Image.fromarray(clahe_img).convert('RGB')
64
- processed_images.append(clahe_pil)
65
 
66
- _, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
67
- binary_pil = Image.fromarray(binary).convert('RGB')
68
- processed_images.append(binary_pil)
69
 
70
- denoised = cv2.bilateralFilter(gray, 9, 75, 75)
71
- denoised_pil = Image.fromarray(denoised).convert('RGB')
72
- processed_images.append(denoised_pil)
73
 
74
  except Exception as e:
75
- print(f"Error in preprocessing: {e}")
76
- processed_images = [image]
77
-
78
- return processed_images
79
 
80
- def extract_text_trocr(self, image: Image.Image) -> str:
81
- if self.trocr_processor is None or self.trocr_model is None:
82
- if not self.load_trocr_model():
83
- return ""
84
 
85
  try:
86
- pixel_values = self.trocr_processor(image, return_tensors="pt").pixel_values
87
- pixel_values = pixel_values.to(self.device)
88
-
89
- with torch.no_grad():
90
- generated_ids = self.trocr_model.generate(pixel_values, max_length=50)
91
 
92
- generated_text = self.trocr_processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
93
- return generated_text.strip()
94
 
95
- except Exception as e:
96
- print(f"Error in TrOCR extraction: {e}")
97
- return ""
98
-
99
- def extract_text_easyocr(self, image: Image.Image) -> str:
100
- if self.easyocr_reader is None:
101
- if not self.load_easyocr_model():
102
- return ""
103
-
104
- try:
105
- img_array = np.array(image)
106
- results = self.easyocr_reader.readtext(img_array, detail=0, paragraph=False)
107
 
108
  if results:
109
- text = ' '.join(results)
110
- return text.strip()
111
- return ""
112
-
 
 
 
 
113
  except Exception as e:
114
- print(f"Error in EasyOCR extraction: {e}")
115
- return ""
116
 
117
- def clean_license_plate_text(self, text: str) -> str:
118
  if not text:
119
  return ""
120
 
@@ -122,159 +78,38 @@ class LicensePlateOCR:
122
  text = re.sub(r'[^A-Z0-9\s-]', '', text)
123
  text = re.sub(r'\s+', ' ', text).strip()
124
 
125
- common_mistakes = {
126
- 'O': '0', 'I': '1', 'S': '5', 'B': '8',
127
- 'G': '6', 'Z': '2', 'T': '7'
128
- }
129
 
130
- for mistake, correction in common_mistakes.items():
131
- if len([c for c in text if c.isdigit()]) > len([c for c in text if c.isalpha()]):
132
- text = text.replace(mistake, correction)
133
 
134
  return text
135
-
136
- def validate_license_plate_format(self, text: str) -> bool:
137
- if not text or len(text) < 4:
138
- return False
139
-
140
- common_patterns = [
141
- r'^[A-Z]{2}\d{2}[A-Z]{2}\d{4}$', # XX00XX0000
142
- r'^[A-Z]{3}\d{4}$', # XXX0000
143
- r'^[A-Z]{2}\d{4}$', # XX0000
144
- r'^\d{3}[A-Z]{3}$', # 000XXX
145
- r'^[A-Z]\d{3}[A-Z]{3}$', # X000XXX
146
- r'^[A-Z]{2}\d{2}[A-Z]\d{3}$', # XX00X000
147
- ]
148
-
149
- text_clean = text.replace(' ', '').replace('-', '')
150
-
151
- for pattern in common_patterns:
152
- if re.match(pattern, text_clean):
153
- return True
154
-
155
- if 4 <= len(text_clean) <= 10:
156
- alpha_count = sum(c.isalpha() for c in text_clean)
157
- digit_count = sum(c.isdigit() for c in text_clean)
158
- if alpha_count > 0 and digit_count > 0:
159
- return True
160
-
161
- return False
162
-
163
- def extract_license_plate_text(self, image: Union[Image.Image, str, np.ndarray],
164
- use_preprocessing: bool = True) -> Dict[str, any]:
165
-
166
- try:
167
- if isinstance(image, str):
168
- if not os.path.exists(image):
169
- return {"error": f"Image file not found: {image}"}
170
- image = Image.open(image)
171
- elif isinstance(image, np.ndarray):
172
- image = Image.fromarray(image)
173
- elif not isinstance(image, Image.Image):
174
- return {"error": f"Unsupported image type: {type(image)}"}
175
-
176
- if image.size[0] == 0 or image.size[1] == 0:
177
- return {"error": "Image has zero dimensions"}
178
-
179
- results = {
180
- "original_image_size": image.size,
181
- "preprocessing_used": use_preprocessing,
182
- "extractions": [],
183
- "best_result": "",
184
- "confidence_score": 0.0,
185
- "is_valid_format": False
186
- }
187
-
188
- images_to_process = self.preprocess_license_plate(image) if use_preprocessing else [image]
189
-
190
- all_texts = []
191
-
192
- for i, processed_img in enumerate(images_to_process):
193
- try:
194
- trocr_text = self.extract_text_trocr(processed_img)
195
- easyocr_text = self.extract_text_easyocr(processed_img)
196
-
197
- trocr_clean = self.clean_license_plate_text(trocr_text)
198
- easyocr_clean = self.clean_license_plate_text(easyocr_text)
199
-
200
- extraction_result = {
201
- "preprocessing_step": i,
202
- "trocr_raw": trocr_text,
203
- "trocr_clean": trocr_clean,
204
- "easyocr_raw": easyocr_text,
205
- "easyocr_clean": easyocr_clean,
206
- "trocr_valid": self.validate_license_plate_format(trocr_clean),
207
- "easyocr_valid": self.validate_license_plate_format(easyocr_clean)
208
- }
209
-
210
- results["extractions"].append(extraction_result)
211
-
212
- if trocr_clean:
213
- all_texts.append((trocr_clean, extraction_result["trocr_valid"], "trocr"))
214
- if easyocr_clean:
215
- all_texts.append((easyocr_clean, extraction_result["easyocr_valid"], "easyocr"))
216
-
217
- except Exception as e:
218
- print(f"Error processing image variant {i}: {e}")
219
- continue
220
-
221
- if all_texts:
222
- valid_texts = [t for t in all_texts if t[1]]
223
- if valid_texts:
224
- best_text = max(valid_texts, key=lambda x: len(x[0]))
225
- results["best_result"] = best_text[0]
226
- results["confidence_score"] = 0.9
227
- results["is_valid_format"] = True
228
- results["best_method"] = best_text[2]
229
- else:
230
- longest_text = max(all_texts, key=lambda x: len(x[0]))
231
- results["best_result"] = longest_text[0]
232
- results["confidence_score"] = 0.6
233
- results["is_valid_format"] = False
234
- results["best_method"] = longest_text[2]
235
- else:
236
- results["error"] = "No text could be extracted from the image"
237
-
238
- return results
239
-
240
- except Exception as e:
241
- return {"error": f"Error in license plate extraction: {e}"}
242
 
243
- def extract_license_plate_text(image_path_or_pil: Union[str, Image.Image]) -> str:
244
- ocr = LicensePlateOCR()
245
- result = ocr.extract_license_plate_text(image_path_or_pil)
246
-
247
- if "error" in result:
248
- return f"Error: {result['error']}"
249
-
250
- return result.get("best_result", "No text found")
251
 
252
- def get_detailed_license_plate_analysis(image_path_or_pil: Union[str, Image.Image]) -> Dict:
253
- ocr = LicensePlateOCR()
254
- return ocr.extract_license_plate_text(image_path_or_pil)
 
 
 
 
 
 
 
 
 
 
 
 
 
255
 
256
  if __name__ == "__main__":
257
- ocr_engine = LicensePlateOCR()
258
-
259
- test_image_path = "license_plate_sample.jpg"
260
 
261
- if os.path.exists(test_image_path):
262
- print("Testing license plate OCR...")
263
-
264
- result = ocr_engine.extract_license_plate_text(test_image_path)
265
-
266
- print(f"Best Result: {result.get('best_result', 'No text found')}")
267
- print(f"Valid Format: {result.get('is_valid_format', False)}")
268
- print(f"Confidence: {result.get('confidence_score', 0):.2f}")
269
-
270
- print("\nDetailed Results:")
271
- for i, extraction in enumerate(result.get('extractions', [])):
272
- print(f" Step {i}:")
273
- print(f" TrOCR: {extraction['trocr_clean']} (Valid: {extraction['trocr_valid']})")
274
- print(f" EasyOCR: {extraction['easyocr_clean']} (Valid: {extraction['easyocr_valid']})")
275
  else:
276
- print(f"Test image {test_image_path} not found.")
277
- print("Usage example:")
278
- print(" from license_plate_ocr import extract_license_plate_text")
279
- print(" text = extract_license_plate_text('your_license_plate.jpg')")
280
- print(" print(text)")
 
1
+ import easyocr
 
 
2
  import cv2
3
  import numpy as np
4
+ from PIL import Image, ImageEnhance
5
  import re
 
6
  import os
 
7
 
8
+ class SimpleLicensePlateOCR:
9
  def __init__(self):
10
+ self.reader = None
11
+ self._initialize_reader()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
+ def _initialize_reader(self):
14
  try:
15
+ print("Initializing EasyOCR...")
16
+ self.reader = easyocr.Reader(['en'], gpu=False, verbose=False)
17
+ print("EasyOCR initialized successfully")
18
  return True
19
  except Exception as e:
20
+ print(f"Failed to initialize EasyOCR: {e}")
21
  return False
22
 
23
+ def preprocess_image(self, image):
 
 
24
  try:
25
+ if isinstance(image, Image.Image):
26
+ img = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
27
+ else:
28
+ img = image
 
 
 
 
 
 
 
 
 
29
 
30
+ gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
 
31
 
32
+ enhanced = cv2.equalizeHist(gray)
 
 
 
33
 
34
+ kernel = np.ones((1,1), np.uint8)
35
+ enhanced = cv2.morphologyEx(enhanced, cv2.MORPH_CLOSE, kernel)
36
+ enhanced = cv2.morphologyEx(enhanced, cv2.MORPH_OPEN, kernel)
37
 
38
+ return enhanced
 
 
39
 
40
  except Exception as e:
41
+ print(f"Preprocessing error: {e}")
42
+ return np.array(image) if isinstance(image, Image.Image) else image
 
 
43
 
44
+ def extract_text(self, image):
45
+ if self.reader is None:
46
+ return "OCR Reader not initialized"
 
47
 
48
  try:
49
+ processed_img = self.preprocess_image(image)
 
 
 
 
50
 
51
+ results = self.reader.readtext(processed_img, detail=False, paragraph=False)
 
52
 
53
+ if not results:
54
+ if isinstance(image, Image.Image):
55
+ img_array = np.array(image)
56
+ else:
57
+ img_array = image
58
+ results = self.reader.readtext(img_array, detail=False, paragraph=False)
 
 
 
 
 
 
59
 
60
  if results:
61
+ text = ' '.join(results).strip()
62
+ cleaned_text = self.clean_text(text)
63
+ print(f"OCR Results: Raw='{text}' Cleaned='{cleaned_text}'")
64
+ return cleaned_text if cleaned_text else text
65
+ else:
66
+ print("No text detected by OCR")
67
+ return "No text detected"
68
+
69
  except Exception as e:
70
+ print(f"OCR extraction error: {e}")
71
+ return f"OCR Error: {str(e)}"
72
 
73
+ def clean_text(self, text):
74
  if not text:
75
  return ""
76
 
 
78
  text = re.sub(r'[^A-Z0-9\s-]', '', text)
79
  text = re.sub(r'\s+', ' ', text).strip()
80
 
81
+ if len(text) < 3:
82
+ return text
 
 
83
 
84
+ text = text.replace(' ', '')
 
 
85
 
86
  return text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
 
88
+ ocr_instance = SimpleLicensePlateOCR()
 
 
 
 
 
 
 
89
 
90
+ def extract_license_plate_text(image):
91
+ try:
92
+ if isinstance(image, str):
93
+ if os.path.exists(image):
94
+ image = Image.open(image)
95
+ else:
96
+ return "Image file not found"
97
+
98
+ result = ocr_instance.extract_text(image)
99
+ print(f"Final OCR result: {result}")
100
+ return result
101
+
102
+ except Exception as e:
103
+ error_msg = f"Error in text extraction: {str(e)}"
104
+ print(error_msg)
105
+ return error_msg
106
 
107
  if __name__ == "__main__":
108
+ print("Testing License Plate OCR...")
 
 
109
 
110
+ test_image = "test_plate.jpg"
111
+ if os.path.exists(test_image):
112
+ result = extract_license_plate_text(test_image)
113
+ print(f"Test result: {result}")
 
 
 
 
 
 
 
 
 
 
114
  else:
115
+ print("No test image found. Place a license plate image as 'test_plate.jpg' to test.")