base_ocr.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # github https://github.com/runhey
  4. import time
  5. import cv2
  6. from ppocronnx.predict_system import BoxedResult
  7. from enum import Enum
  8. from module.base.decorator import cached_property
  9. from module.base.utils import area_pad, crop, float2str
  10. from module.ocr.ppocr import TextSystem
  11. from module.ocr.models import OCR_MODEL
  12. from module.exception import ScriptError
  13. from module.logger import logger
  14. class OcrMode(Enum):
  15. FULL = 1 # str: "Full"
  16. SINGLE = 2 # str: "Single"
  17. DIGIT = 3 # str: "Digit"
  18. DIGIT_COUNTER = 4 # str: "DigitCounter"
  19. DURATION = 5 # str: "Duration"
  20. class OcrMethod(Enum):
  21. DEFAULT = 1 # str: "Default"
  22. class BaseCor:
  23. lang: str = "cn"
  24. score: float = 0.6 # 阈值默认为0.5
  25. name: str = "ocr"
  26. mode: OcrMode = OcrMode.FULL
  27. method: OcrMethod = OcrMethod.DEFAULT # 占位符
  28. roi: list = [] # [x, y, width, height]
  29. area: list = [] # [x, y, width, height]
  30. keyword: str = "" # 默认为空
  31. def __init__(self,
  32. name: str,
  33. mode: str,
  34. method: str,
  35. roi: tuple,
  36. area: tuple,
  37. keyword: str) -> None:
  38. """
  39. :param name:
  40. :param mode:
  41. :param method:
  42. :param roi:
  43. :param area:
  44. :param keyword:
  45. """
  46. self.name = name
  47. if isinstance(mode, str):
  48. self.mode = OcrMode[mode.upper()]
  49. elif isinstance(mode, OcrMode):
  50. self.mode = mode
  51. if isinstance(method, str):
  52. self.method = OcrMethod[method.upper()]
  53. elif isinstance(method, OcrMethod):
  54. self.method = method
  55. self.roi: list = list(roi)
  56. self.area: list = list(area)
  57. self.keyword = keyword
  58. @cached_property
  59. def model(self) -> TextSystem:
  60. return OCR_MODEL.__getattribute__(self.lang)
  61. def pre_process(self, image):
  62. """
  63. 重写
  64. :param image:
  65. :return:
  66. """
  67. return image
  68. def after_process(self, result):
  69. """
  70. 重写
  71. :param result:
  72. :return:
  73. """
  74. return result
  75. def ocr_single_line(self, image):
  76. """
  77. 只支持横方向的单行ocr,不支持竖方向的单行ocr
  78. 注意:这里使用了预处理和后处理
  79. :param image:
  80. :return:
  81. """
  82. # pre process
  83. start_time = time.time()
  84. image = crop(image, self.roi, copy=False)
  85. image = self.pre_process(image)
  86. # ocr
  87. result, score = self.model.ocr_single_line(image)
  88. if score < self.score:
  89. result = ""
  90. # after proces
  91. result = self.after_process(result)
  92. logger.attr(name='%s %ss' % (self.name, float2str(time.time() - start_time)),
  93. text=str(result))
  94. return result
  95. def detect_and_ocr(self, image) -> list[BoxedResult]:
  96. """
  97. 注意:这里使用了预处理和后处理
  98. :param image:
  99. :return:
  100. """
  101. # pre process
  102. start_time = time.time()
  103. image = self.pre_process(image)
  104. # ocr
  105. boxed_results: list[BoxedResult] = self.model.detect_and_ocr(image)
  106. results = []
  107. # after proces
  108. for result in boxed_results:
  109. if result.score < self.score:
  110. continue
  111. boxed_results.ocr_text = self.after_process(boxed_results.ocr_text)
  112. results.append(result)
  113. logger.attr(name='%s %ss' % (self.name, float2str(time.time() - start_time)),
  114. text=str([result.ocr_text for result in results]))
  115. return results
  116. def match(self, result: str, included: bool=False) -> bool:
  117. """
  118. 使用ocr获取结果后和keyword进行匹配
  119. :param result:
  120. :param included: ocr结果和keyword是否包含关系, 要么是包含关系,要么是相等关系
  121. :return:
  122. """
  123. if included:
  124. return self.keyword in result
  125. else:
  126. return self.keyword == result
  127. def filter(self, boxed_results: list[BoxedResult], keyword: str=None) -> list:
  128. """
  129. 使用ocr获取结果后和keyword进行匹配. 返回匹配的index list
  130. :param keyword: 如果不指定默认适用对象的keyword
  131. :param boxed_results:
  132. :return:
  133. """
  134. # 首先先将所有的ocr的str顺序拼接起来, 然后再进行匹配
  135. result = None
  136. strings = [boxed_result.ocr_text for boxed_result in boxed_results]
  137. concatenated_string = "".join(strings)
  138. if keyword is None:
  139. keyword = self.keyword
  140. if keyword in concatenated_string:
  141. result = [index for index, _ in enumerate(strings)]
  142. else:
  143. result = None
  144. if result is not None:
  145. # logger.info("Filter result: %s" % result)
  146. return result
  147. # 如果适用顺序拼接还是没有匹配到,那可能是竖排的,使用单个字节的keyword进行匹配
  148. indices = []
  149. for index, char in enumerate(keyword):
  150. for i, string in enumerate(strings):
  151. if char not in string:
  152. break
  153. if i == len(strings) - 1:
  154. indices.append(index)
  155. if indices:
  156. return indices
  157. else:
  158. indices = None
  159. return indices