sub_ocr.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. import cn2an
  7. from datetime import timedelta
  8. from module.ocr.ppocr import TextSystem
  9. from module.exception import ScriptError
  10. from module.base.utils import area_pad, crop, float2str
  11. from module.ocr.base_ocr import BaseCor, OcrMode, OcrMethod
  12. from module.ocr.utils import merge_area
  13. from module.logger import logger
  14. class Full(BaseCor):
  15. """
  16. 这个类适用于大ROI范围的文本识别。可以支持多条文本识别, 默认不支持竖方向的文本识别
  17. """
  18. def after_process(self, result):
  19. return result
  20. def ocr_full(self, image, keyword: str=None, exact: bool=False) -> tuple:
  21. """
  22. 检测整个图片的文本,并对结果进行过滤。返回的是匹配到的keyword的左边。如果没有匹配到返回(0, 0, 0, 0)
  23. :param image:
  24. :param keyword:
  25. :param exact: 是否只匹配完整的单个 OCR 文本
  26. :return:
  27. """
  28. if keyword is None:
  29. keyword = self.keyword
  30. boxed_results = self.detect_and_ocr(image)
  31. if not boxed_results:
  32. return 0, 0, 0, 0
  33. index_list = self.filter(boxed_results, keyword, exact=exact)
  34. logger.info(f"OCR [{self.name}] detected in {index_list}")
  35. # 如果一个都没有匹配到
  36. if not index_list:
  37. return 0, 0, 0, 0
  38. # 如果匹配到了多个,则合并所有的坐标,返回合并后的坐标
  39. if len(index_list) > 1:
  40. area_list = [(
  41. boxed_results[index].box[0, 0], # x
  42. boxed_results[index].box[0, 1], # y
  43. boxed_results[index].box[1, 0] - boxed_results[index].box[0, 0], # width
  44. boxed_results[index].box[2, 1] - boxed_results[index].box[0, 1], # height
  45. ) for index in index_list]
  46. area = merge_area(area_list)
  47. self.area = area[0]+self.roi[0], area[1]+self.roi[1], area[2], area[3]
  48. else:
  49. box = boxed_results[index_list[0]].box
  50. 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]
  51. logger.info(f"OCR [{self.name}] detected in {self.area}")
  52. return self.area
  53. class Single(BaseCor):
  54. """
  55. 这个类使用于单行文本识别(所识别的ROI不会动)
  56. """
  57. def after_process(self, result):
  58. return result
  59. def ocr_single(self, image) -> str:
  60. """
  61. 检测某个固定位置的roi的文本。可以是横方向也可以是竖方向
  62. :param image:
  63. :return: 返回到识别的文字, 如果没有返回空字符串
  64. """
  65. if self.roi:
  66. result = self.ocr_single_line(image)
  67. if result != "":
  68. return result
  69. # 如果没有识别到,这个时候考虑到可能是竖方向的文本, 使用detect_and_ocr来进行识别
  70. logger.info(f"[{self.name}] Try to detect vertically")
  71. result = self.detect_and_ocr(image)
  72. if not result:
  73. logger.info(f"[{self.name}]: No text detected in ROI")
  74. return ""
  75. if result[0].ocr_text != "" and result[0].score > self.score:
  76. return result[0].ocr_text
  77. # 如果还是没有识别到。那可能就是真的没有识别到了
  78. return ""
  79. else:
  80. raise ScriptError("Roi is empty")
  81. class Digit(Single):
  82. def after_process(self, result):
  83. result = super().after_process(result)
  84. replacements = {
  85. 'I': '1', 'D': '0', 'S': '5',
  86. 'B': '8', '?': '2', '?': '2',
  87. 'd': '6', 'o': '0', 'O': '0',
  88. '→': '1'
  89. }
  90. for old, new in replacements.items():
  91. result = result.replace(old, new)
  92. result = [char for char in result if char.isdigit()]
  93. result = ''.join(result)
  94. # 如果结果全部是 '0' 且长度 >= 2,视为 OCR 错误(例如 900 → 00),强制设为 "1"
  95. if result and all(ch == '0' for ch in result) and len(result) >= 2:
  96. logger.warning(f'OCR [{self.name} {result}] all-zero with length>=2, force set to "1"')
  97. result = '1'
  98. prev = result
  99. result = int(result) if result else 0
  100. if str(result) != prev:
  101. logger.warning(f'OCR {self.name}: Result "{prev}" is revised to "{result}"')
  102. return result
  103. def ocr_digit(self, image) -> int:
  104. """
  105. 返回数字
  106. :param image:
  107. :return:
  108. """
  109. result = self.ocr_single(image)
  110. if result == "":
  111. return 0
  112. else:
  113. return int(result)
  114. class DigitCounter(Single):
  115. def after_process(self, result):
  116. result = super().after_process(result)
  117. replacements = {
  118. 'I': '1', 'D': '0', 'S': '5',
  119. 'B': '8', '?': '2', '?': '2',
  120. 'd': '6', 'o': '0', 'O': '0',
  121. '→': '1'
  122. }
  123. for old, new in replacements.items():
  124. result = result.replace(old, new)
  125. result = [char for char in result if char.isdigit() or char == '/']
  126. result = ''.join(result)
  127. return result
  128. @classmethod
  129. def ocr_str_digit_counter(cls, result: str) -> tuple[int, int, int]:
  130. result = re.search(r'(\d+)/(\d+)', result)
  131. if result:
  132. result = [int(s) for s in result.groups()]
  133. current, total = int(result[0]), int(result[1])
  134. # 不知道为什么加了这一句,妈的
  135. # current = min(current, total)
  136. if current > total:
  137. logger.warning(f'[{cls.name}]: Current {current} is greater than total {total}')
  138. return current, total - current, total
  139. else:
  140. logger.warning(f'Unexpected ocr result: {result}')
  141. return 0, 0, 0
  142. def ocr_digit_counter(self, image) -> tuple[int, int, int]:
  143. """
  144. 获取计数的结果
  145. :param image:
  146. :return: 例如 14/15,返回 (14, 1, 15) 。如果没有识别到,返回 (0, 0, 0)
  147. """
  148. result = self.ocr_single(image)
  149. if result == "":
  150. return 0, 0, 0
  151. return self.ocr_str_digit_counter(result)
  152. class Duration(Single):
  153. def after_process(self, result):
  154. result = result.replace('I', '1').replace('D', '0').replace('S', '5')
  155. result = result.replace('o', '0').replace('l', '1').replace('O', '0')
  156. result = result.replace('B', '8').replace(':', ':').replace(' ', '').replace('.', ':')
  157. result = super().after_process(result)
  158. return result
  159. @staticmethod
  160. def parse_time(string):
  161. """
  162. Args:
  163. string (str): `01:30:00`
  164. Returns:
  165. datetime.timedelta:
  166. """
  167. result = re.search(r'(\d{1,2}):?(\d{2}):?(\d{2})', string)
  168. if result:
  169. result = [int(s) for s in result.groups()]
  170. return timedelta(hours=result[0], minutes=result[1], seconds=result[2])
  171. else:
  172. logger.warning(f'Invalid duration: {string}')
  173. return timedelta(hours=0, minutes=0, seconds=0)
  174. def ocr_duration(self, image) -> timedelta:
  175. """
  176. :param image:
  177. :return:
  178. """
  179. result = self.ocr_single(image)
  180. if result == "":
  181. return timedelta(hours=0, minutes=0, seconds=0)
  182. return self.parse_time(result)
  183. class Quantity(BaseCor):
  184. """
  185. 专门用于识别超级多的数量,不支持多个区域的识别
  186. 可支持负数
  187. 比如:”6.33亿“ ”1.2万“ “53万/100” -> 530,000
  188. """
  189. def after_process(self, result):
  190. result = super().after_process(result)
  191. result = result.replace('I', '1').replace('D', '0').replace('S', '5')
  192. result = result.replace('B', '8').replace('?', '2').replace('?', '2').replace('d', '6')
  193. result = [char
  194. for char in result
  195. if char.isdigit() or char == '.' or char == '/' or char == '万' or char == '亿' or char == '千']
  196. result = ''.join(result)
  197. if '/' in result:
  198. result_split = result.split('/')
  199. result = result_split[0]
  200. result = cn2an.cn2an(result, 'smart')
  201. try:
  202. result = int(result)
  203. except ValueError:
  204. logger.warning(f'[{self.name}]: Invalid quantity: {result}')
  205. result = 0
  206. return result
  207. def ocr_quantity(self, image) -> int:
  208. """
  209. 返回数量
  210. :param image:
  211. :return:
  212. """
  213. boxed_results = self.detect_and_ocr(image)
  214. if not boxed_results:
  215. logger.warning(f'[{self.name}]: No text detected')
  216. return 0
  217. box = boxed_results[0].box
  218. 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]
  219. return boxed_results[0].ocr_text
  220. if __name__ == '__main__':
  221. import cv2
  222. image = cv2.imread(r'E:\Project\OnmyojiAutoScript-assets\jade.png')