ocr.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. from dataclasses import dataclass
  2. from typing import Any
  3. from core.line_parser import LineParser
  4. from core.parser import *
  5. from core.direction import *
  6. import numpy as np
  7. from paddleocr import PaddleOCR
  8. # <- 传入pic pic_type
  9. # 1. 旋转pic (to 正向)
  10. # 2. 重写识别pic (get res)
  11. # 3. 行处理res (get res)
  12. # 4. 对res字段逻辑识别 (get dict)
  13. # -> dict
  14. # 身份证OCR
  15. @dataclass
  16. class CetOcr:
  17. ocr: PaddleOCR
  18. # 角度探测器
  19. angle_detector: AngleDetector
  20. # 检测
  21. # <- 传入pic pic_type
  22. # -> dict
  23. def predict(self, image: np.ndarray) -> ():
  24. # 旋转后img angle result(生ocr)
  25. image, angle, result, image_type = self._pre_process(image)
  26. print(f'---------- detect angle: {angle} 角度 --------')
  27. return self._post_process(result, angle, image_type)
  28. # 预处理(旋转图片)
  29. # <- img(cv2) img_type
  30. # -> 正向的img(旋转后) 源img角度 result(ocr生)
  31. def _pre_process(self, image) -> (np.ndarray, int, Any):
  32. # pic角度 result(ocr生)
  33. angle, result, image_type = self.angle_detector.detect_angle(image)
  34. if angle == 1:
  35. image = cv2.rotate(image, cv2.ROTATE_90_COUNTERCLOCKWISE)
  36. print(angle) # 逆时针
  37. if angle == 2:
  38. image = cv2.rotate(image, cv2.ROTATE_180)
  39. if angle == 3:
  40. image = cv2.rotate(image, cv2.ROTATE_90_CLOCKWISE)
  41. return image, angle, result, image_type
  42. # 获取模型检测结果
  43. def _ocr(self, image):
  44. result = self.ocr.ocr(image, cls=True)
  45. print("------------------")
  46. print(result)
  47. if not result:
  48. raise Exception('无法识别')
  49. confs = [line[1][1] for line in result]
  50. # 将检测到的文字放到一个列表中
  51. txts = [line[1][0] for line in result]
  52. # print("......................................")
  53. # print(txts)
  54. # print("......................................")
  55. return txts, confs, result
  56. # <- result(正向img_生ocr) angle img_type
  57. # == 对 正向img_res 进行[行处理]
  58. # -> 最后要返回的结果 dict
  59. def _post_process(self, result, angle: int, image_type):
  60. filters = [lambda x: x.is_slope, lambda x: x.txt.replace(' ', '').encode('utf-8').isalpha()]
  61. line_parser = LineParser(result, filters)
  62. line_result = line_parser.parse()
  63. print('-------------')
  64. print(line_result)
  65. print('-------------')
  66. conf = line_parser.confidence
  67. if int(image_type) == 0:
  68. parser = CETParser(line_result)
  69. elif int(image_type) == 1:
  70. parser = TEMParser(line_result)
  71. else:
  72. raise Exception('无法识别')
  73. # 字段逻辑处理后对res(dict)
  74. ocr_res = parser.parse()
  75. res = {
  76. "confidence": conf,
  77. "card_type": str(image_type),
  78. "orientation": angle, # 原angle是逆时针,转成顺时针
  79. **ocr_res
  80. }
  81. print(res)
  82. return res
  83. # def _get_type(self, image) -> int: