ocr.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. # 检测
  12. def predict(self, image: np.ndarray, image_type: str = '0'):
  13. # image, angle = self._pre_process(image)
  14. # 识别出的 => 字段、置信度、(字段,置信度)
  15. txts, confs, result = self._ocr(image)
  16. # 角度
  17. angle = self.angle_detector.detect_angle(image, result)
  18. return self._post_process(result, angle, image_type)
  19. def _pre_process(self, image) -> (np.ndarray, int):
  20. angle = detect_angle(image)
  21. print(angle) # 逆时针
  22. if angle == 180:
  23. image = cv2.rotate(image, cv2.ROTATE_180)
  24. if angle == 90:
  25. image = cv2.rotate(image, cv2.ROTATE_90_CLOCKWISE)
  26. if angle == 270:
  27. image = cv2.rotate(image, cv2.ROTATE_90_COUNTERCLOCKWISE)
  28. return image, angle
  29. def _ocr(self, image):
  30. # 获取模型检测结果
  31. result = self.ocr.ocr(image, cls=True)
  32. print("------------------")
  33. print(result)
  34. if not result:
  35. raise Exception('无法识别')
  36. confs = [line[1][1] for line in result]
  37. # 将检测到的文字放到一个列表中
  38. txts = [line[1][0] for line in result]
  39. # print("......................................")
  40. # print(txts)
  41. # print("......................................")
  42. return txts, confs, result
  43. def _post_process(self, result, angle: int, image_type: str):
  44. line_parser = LineParser(result)
  45. line_result = line_parser.parse()
  46. conf = line_parser.confidence
  47. if int(image_type) == 0:
  48. parser = FrontParser(line_result)
  49. elif int(image_type) == 1:
  50. parser = BackParser(line_result)
  51. else:
  52. raise Exception('无法识别')
  53. ocr_res = parser.parse()
  54. res = {
  55. "confidence": conf,
  56. "card_type": image_type,
  57. "orientation": angle // 90, # 原angle是逆时针,转成顺时针
  58. **ocr_res
  59. }
  60. print(res)
  61. return res