You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.6 KiB
Python

2 years ago
"""
模块作者
代码编写焦雅雯
代码优化/整合/打包王昱博
模块用途
使用OCR库进行车牌号识别和初步分类
"""
import cv2
import easyocr
import hyperlpr3 as lpr3
class OCR:
@staticmethod
def SwapChars(text: str) -> str:
text = text.replace('I', '1')
text = text.replace('O', '0')
return text
@classmethod
2 years ago
def RecognizeLicensePlate(cls, image: cv2.Mat, lpr_text: str) -> tuple:
2 years ago
reader = easyocr.Reader(['ch_sim', 'en'], model_storage_directory='./easyocr_model')
result = reader.readtext(image)
license_plate = ""
for res in result:
license_plate += res[-2] # 如果车牌号码是两行的,按行识别出来再拼接起来
2 years ago
license_plate = cls.SwapChars(license_plate)
2 years ago
if lpr_text is not None:
license_plate = lpr_text
2 years ago
if '\u8b66' in license_plate:
2 years ago
car_type = '警用车辆'
2 years ago
elif '\u573a\u5185' in license_plate:
2 years ago
car_type = '场内车辆'
2 years ago
elif '\u6302' in license_plate:
2 years ago
car_type = '挂车/半挂车'
elif len(license_plate) > 7:
car_type = '新能源车辆'
2 years ago
else:
2 years ago
car_type = '小型轿车'
2 years ago
return license_plate, car_type
@classmethod
def RecognizeLicensePlate2(cls, image: cv2.Mat):
reco = lpr3.LicensePlateCatcher()
results = reco(image)
for code, conf, _type, box in results:
x0, y0, x1, y1 = box
cut_image = image[y0:y1, x0:x1]
return code, conf, cut_image
2 years ago
return None, None, None