ocr.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. from dataclasses import dataclass
  2. from core.line_parser import LineParser
  3. from core.parser import *
  4. from core.direction import *
  5. import numpy as np
  6. from paddleocr import PaddleOCR
  7. @dataclass
  8. class IdCardOcr:
  9. ocr: PaddleOCR
  10. angle_detector: AngleDetector
  11. def predict(self, image: np.ndarray, image_type: str = '0'):
  12. # image, angle = self._pre_process(image)
  13. txts, confs, result = self._ocr(image)
  14. angle = self.angle_detector.detect_angle(image, result)
  15. line_parser = LineParser(result)
  16. line_result = line_parser.parse()
  17. conf = line_parser.confidence
  18. if int(image_type) == 0:
  19. parser = FrontParser(line_result)
  20. elif int(image_type) == 1:
  21. parser = BackParser(line_result)
  22. else:
  23. raise Exception('无法识别')
  24. return self._post_process(conf, angle, parser, image_type)
  25. def _pre_process(self, image) -> (np.ndarray, int):
  26. angle = detect_angle(image)
  27. print(angle) # 逆时针
  28. if angle == 180:
  29. image = cv2.rotate(image, cv2.ROTATE_180)
  30. if angle == 90:
  31. image = cv2.rotate(image, cv2.ROTATE_90_CLOCKWISE)
  32. if angle == 270:
  33. image = cv2.rotate(image, cv2.ROTATE_90_COUNTERCLOCKWISE)
  34. return image, angle
  35. def _ocr(self, image):
  36. # 获取模型检测结果
  37. result = self.ocr.ocr(image, cls=True)
  38. print("------------------")
  39. print(result)
  40. if not result:
  41. raise Exception('无法识别')
  42. confs = [line[1][1] for line in result]
  43. # 将检测到的文字放到一个列表中
  44. txts = [line[1][0] for line in result]
  45. # print("......................................")
  46. # print(txts)
  47. # print("......................................")
  48. return txts, confs, result
  49. def _post_process(self, conf, angle: int, parser: Parser, image_type: str):
  50. ocr_res = parser.parse()
  51. res = {
  52. "confidence": conf,
  53. "card_type": image_type,
  54. "orientation": angle // 90, # 原angle是逆时针,转成顺时针
  55. **ocr_res
  56. }
  57. print(res)
  58. return res