""" 模块作者: 代码编写:焦雅雯 代码优化/整合/打包:王昱博 模块用途: 使用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 def RecognizeLicensePlate(cls, image: cv2.Mat) -> tuple: 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] # 如果车牌号码是两行的,按行识别出来再拼接起来 if '\u8b66' in license_plate: car_type = 'police' elif '\u573a\u5185' in license_plate: car_type = 'internal' elif '\u6302' in license_plate: car_type = 'bigCar' else: car_type = 'smallCar' 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 return None, None, image