sub_ocr.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # github https://github.com/runhey
  4. import cv2
  5. import re
  6. from datetime import timedelta
  7. from module.ocr.ppocr import TextSystem
  8. from module.exception import ScriptError
  9. from module.base.utils import area_pad, crop, float2str
  10. from module.ocr.base_ocr import BaseCor, OcrMode, OcrMethod
  11. from module.ocr.utils import merge_area
  12. from module.logger import logger
  13. class Full(BaseCor):
  14. """
  15. 这个类适用于大ROI范围的文本识别。可以支持多条文本识别, 默认不支持竖方向的文本识别
  16. """
  17. def after_process(self, result):
  18. return result
  19. def ocr_full(self, image, keyword: str=None) -> tuple:
  20. """
  21. 检测整个图片的文本,并对结果进行过滤。返回的是匹配到的keyword的左边。如果没有匹配到返回(0, 0, 0, 0)
  22. :param image:
  23. :param keyword:
  24. :return:
  25. """
  26. if keyword is None:
  27. keyword = self.keyword
  28. boxed_results = self.detect_and_ocr(image)
  29. if not boxed_results:
  30. return 0, 0, 0, 0
  31. index_list = self.filter(boxed_results, keyword)
  32. logger.info(f"OCR [{self.name}] detected in {index_list}")
  33. # 如果一个都没有匹配到
  34. if not index_list:
  35. return 0, 0, 0, 0
  36. # 如果匹配到了多个,则合并所有的坐标,返回合并后的坐标
  37. if len(index_list) > 1:
  38. area_list = [(
  39. boxed_results[index].box[0, 0], # x
  40. boxed_results[index].box[0, 1], # y
  41. boxed_results[index].box[1, 0] - boxed_results[index].box[0, 0], # width
  42. boxed_results[index].box[2, 1] - boxed_results[index].box[0, 1], # height
  43. ) for index in index_list]
  44. area = merge_area(area_list)
  45. self.area = area[0]+self.roi[0], area[1]+self.roi[1], area[2], area[3]
  46. else:
  47. box = boxed_results[index_list[0]].box
  48. self.area = box[0, 0]+self.roi[0], box[0, 1]+self.roi[1], box[1, 0] - box[0, 0], box[2, 1] - box[0, 1]
  49. logger.info(f"OCR [{self.name}] detected in {self.area}")
  50. return self.area
  51. class Single(BaseCor):
  52. """
  53. 这个类使用于单行文本识别(所识别的ROI不会动)
  54. """
  55. def after_process(self, result):
  56. return result
  57. def ocr_single(self, image) -> str:
  58. """
  59. 检测某个固定位置的roi的文本。可以是横方向也可以是竖方向
  60. :param image:
  61. :return: 返回到识别的文字, 如果没有返回空字符串
  62. """
  63. if self.roi:
  64. result = self.ocr_single_line(image)
  65. if result != "":
  66. return result
  67. # 如果没有识别到,这个时候考虑到可能是竖方向的文本, 使用detect_and_ocr来进行识别
  68. logger.info(f"[{self.name}] Try to detect vertically")
  69. result = self.detect_and_ocr(image)
  70. if not result:
  71. logger.info(f"[{self.name}]: No text detected in ROI")
  72. return ""
  73. if result[0].ocr_text != "" and result[0].score > self.score:
  74. return result[0].ocr_text
  75. # 如果还是没有识别到。那可能就是真的没有识别到了
  76. return ""
  77. else:
  78. raise ScriptError("Roi is empty")
  79. class Digit(Single):
  80. def after_process(self, result):
  81. result = super().after_process(result)
  82. result = result.replace('I', '1').replace('D', '0').replace('S', '5')
  83. result = result.replace('B', '8').replace('?', '2').replace('?', '2')
  84. result = [char for char in result if char.isdigit()]
  85. result = ''.join(result)
  86. prev = result
  87. result = int(result) if result else 0
  88. if str(result) != prev:
  89. logger.warning(f'OCR {self.name}: Result "{prev}" is revised to "{result}"')
  90. return result
  91. def ocr_digit(self, image) -> int:
  92. """
  93. 返回数字
  94. :param image:
  95. :return:
  96. """
  97. result = self.ocr_single(image)
  98. if result == "":
  99. return 0
  100. else:
  101. return int(result)
  102. class DigitCounter(Single):
  103. def after_process(self, result):
  104. result = super().after_process(result)
  105. result = result.replace('I', '1').replace('D', '0').replace('S', '5')
  106. result = result.replace('B', '8')
  107. return result
  108. @classmethod
  109. def ocr_str_digit_counter(cls, result: str) -> tuple[int, int, int]:
  110. result = re.search(r'(\d+)/(\d+)', result)
  111. if result:
  112. result = [int(s) for s in result.groups()]
  113. current, total = int(result[0]), int(result[1])
  114. # 不知道为什么加了这一句,妈的
  115. # current = min(current, total)
  116. if current > total:
  117. logger.warning(f'[{cls.name}]: Current {current} is greater than total {total}')
  118. return current, total - current, total
  119. else:
  120. logger.warning(f'Unexpected ocr result: {result}')
  121. return 0, 0, 0
  122. def ocr_digit_counter(self, image) -> tuple[int, int, int]:
  123. """
  124. 获取计数的结果
  125. :param image:
  126. :return: 例如 14/15,返回 (14, 1, 15) 。如果没有识别到,返回 (0, 0, 0)
  127. """
  128. result = self.ocr_single(image)
  129. if result == "":
  130. return 0, 0, 0
  131. return self.ocr_str_digit_counter(result)
  132. class Duration(Single):
  133. def after_process(self, result):
  134. result = super().after_process(result)
  135. result = result.replace('I', '1').replace('D', '0').replace('S', '5')
  136. result = result.replace('B', '8').replace(':', ':')
  137. return result
  138. @staticmethod
  139. def parse_time(string):
  140. """
  141. Args:
  142. string (str): `01:30:00`
  143. Returns:
  144. datetime.timedelta:
  145. """
  146. result = re.search(r'(\d{1,2}):?(\d{2}):?(\d{2})', string)
  147. if result:
  148. result = [int(s) for s in result.groups()]
  149. return timedelta(hours=result[0], minutes=result[1], seconds=result[2])
  150. else:
  151. logger.warning(f'Invalid duration: {string}')
  152. return timedelta(hours=0, minutes=0, seconds=0)
  153. def ocr_duration(self, image) -> timedelta:
  154. """
  155. :param image:
  156. :return:
  157. """
  158. result = self.ocr_single(image)
  159. if result == "":
  160. return timedelta(hours=0, minutes=0, seconds=0)
  161. return self.parse_time(result)
  162. if __name__ == '__main__':
  163. import cv2
  164. image = cv2.imread(r'E:\Project\OnmyojiAutoScript-assets\jade.png')