123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- from dataclasses import dataclass
- import cv2
- 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):
- img_type = int(image_type)
- image, angle, result = self._rotate_img(image, img_type)
- print(f'---------- detect angle: {angle} 图片角度 ----------')
- if angle != 0:
- _, _, result = self._ocr(image)
- return self._post_process(result, angle, image_type)
-
- def _rotate_img(self, image, image_type) -> (np.ndarray, int):
- angle, result = self.angle_detector.detect_angle(image, image_type)
- if angle == 1:
- image = cv2.rotate(image, cv2.ROTATE_90_COUNTERCLOCKWISE)
- if angle == 2:
- image = cv2.rotate(image, cv2.ROTATE_180)
- if angle == 3:
- image = cv2.rotate(image, cv2.ROTATE_90_CLOCKWISE)
-
- return image, angle, result
- 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, result, angle: int, image_type: str):
-
- line_parser = LineParser(result)
- line_result = line_parser.parse()
- conf = line_parser.confidence
-
-
- if int(image_type) == 0:
- pass
- parser = PeopleRegBookParser(line_result)
- elif int(image_type) == 1:
- pass
- parser = FrontRegBookParser(line_result)
- else:
- raise Exception('未传入 image_type')
-
- ocr_res = parser.parse()
- res = {
- "confidence": conf,
- "img_type": str(image_type),
- "orientation": angle,
- **ocr_res
- }
- return res
|