ocr.py 3.1 KB

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