from dataclasses import dataclass from core.line_parser import LineParser from core.parser import * from core.direction import * import numpy as np from paddleocr import PaddleOCR @dataclass class IdCardOcr: ocr: PaddleOCR angle_detector: AngleDetector def predict(self, image: np.ndarray, image_type: str = '0'): # image, angle = self._pre_process(image) txts, confs, result = self._ocr(image) angle = self.angle_detector.detect_angle(image, result) line_parser = LineParser(result) line_result = line_parser.parse() conf = line_parser.confidence if int(image_type) == 0: parser = FrontParser(line_result) elif int(image_type) == 1: parser = BackParser(line_result) else: raise Exception('无法识别') return self._post_process(conf, angle, parser, image_type) def _pre_process(self, image) -> (np.ndarray, int): angle = detect_angle(image) print(angle) # 逆时针 if angle == 180: image = cv2.rotate(image, cv2.ROTATE_180) if angle == 90: image = cv2.rotate(image, cv2.ROTATE_90_CLOCKWISE) if angle == 270: image = cv2.rotate(image, cv2.ROTATE_90_COUNTERCLOCKWISE) return image, angle def _ocr(self, image): # 获取模型检测结果 result = self.ocr.ocr(image, cls=True) print("------------------") print(result) if not result: raise Exception('无法识别') confs = [line[1][1] for line in result] # 将检测到的文字放到一个列表中 txts = [line[1][0] for line in result] # print("......................................") # print(txts) # print("......................................") return txts, confs, result def _post_process(self, conf, angle: int, parser: Parser, image_type: str): ocr_res = parser.parse() res = { "confidence": conf, "card_type": image_type, "orientation": angle // 90, # 原angle是逆时针,转成顺时针 **ocr_res } print(res) return res