sub_ocr.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. prev = result
  95. result = int(result) if result else 0
  96. if str(result) != prev:
  97. logger.warning(f'OCR {self.name}: Result "{prev}" is revised to "{result}"')
  98. return result
  99. def ocr_digit(self, image) -> int:
  100. """
  101. 返回数字
  102. :param image:
  103. :return:
  104. """
  105. result = self.ocr_single(image)
  106. if result == "":
  107. return 0
  108. else:
  109. return int(result)
  110. class DigitCounter(Single):
  111. def after_process(self, result):
  112. result = super().after_process(result)
  113. replacements = {
  114. 'I': '1', 'D': '0', 'S': '5',
  115. 'B': '8', '?': '2', '?': '2',
  116. 'd': '6', 'o': '0', 'O': '0',
  117. '→': '1'
  118. }
  119. for old, new in replacements.items():
  120. result = result.replace(old, new)
  121. result = [char for char in result if char.isdigit() or char == '/']
  122. result = ''.join(result)
  123. return result
  124. @classmethod
  125. def ocr_str_digit_counter(cls, result: str) -> tuple[int, int, int]:
  126. result = re.search(r'(\d+)/(\d+)', result)
  127. if result:
  128. result = [int(s) for s in result.groups()]
  129. current, total = int(result[0]), int(result[1])
  130. # 不知道为什么加了这一句,妈的
  131. # current = min(current, total)
  132. if current > total:
  133. logger.warning(f'[{cls.name}]: Current {current} is greater than total {total}')
  134. return current, total - current, total
  135. else:
  136. logger.warning(f'Unexpected ocr result: {result}')
  137. return 0, 0, 0
  138. def ocr_digit_counter(self, image) -> tuple[int, int, int]:
  139. """
  140. 获取计数的结果
  141. :param image:
  142. :return: 例如 14/15,返回 (14, 1, 15) 。如果没有识别到,返回 (0, 0, 0)
  143. """
  144. result = self.ocr_single(image)
  145. if result == "":
  146. return 0, 0, 0
  147. return self.ocr_str_digit_counter(result)
  148. class Duration(Single):
  149. def after_process(self, result):
  150. result = result.replace('I', '1').replace('D', '0').replace('S', '5')
  151. result = result.replace('o', '0').replace('l', '1').replace('O', '0')
  152. result = result.replace('B', '8').replace(':', ':').replace(' ', '').replace('.', ':')
  153. result = super().after_process(result)
  154. return result
  155. @staticmethod
  156. def parse_time(string):
  157. """
  158. Args:
  159. string (str): `01:30:00`
  160. Returns:
  161. datetime.timedelta:
  162. """
  163. result = re.search(r'(\d{1,2}):?(\d{2}):?(\d{2})', string)
  164. if result:
  165. result = [int(s) for s in result.groups()]
  166. return timedelta(hours=result[0], minutes=result[1], seconds=result[2])
  167. else:
  168. logger.warning(f'Invalid duration: {string}')
  169. return timedelta(hours=0, minutes=0, seconds=0)
  170. def ocr_duration(self, image) -> timedelta:
  171. """
  172. :param image:
  173. :return:
  174. """
  175. result = self.ocr_single(image)
  176. if result == "":
  177. return timedelta(hours=0, minutes=0, seconds=0)
  178. return self.parse_time(result)
  179. class Quantity(BaseCor):
  180. """
  181. 专门用于识别超级多的数量,不支持多个区域的识别
  182. 可支持负数
  183. 比如:”6.33亿“ ”1.2万“ “53万/100” -> 530,000
  184. """
  185. def after_process(self, result):
  186. result = super().after_process(result)
  187. result = result.replace('I', '1').replace('D', '0').replace('S', '5')
  188. result = result.replace('B', '8').replace('?', '2').replace('?', '2').replace('d', '6')
  189. result = [char
  190. for char in result
  191. if char.isdigit() or char == '.' or char == '/' or char == '万' or char == '亿' or char == '千']
  192. result = ''.join(result)
  193. if '/' in result:
  194. result_split = result.split('/')
  195. result = result_split[0]
  196. result = cn2an.cn2an(result, 'smart')
  197. try:
  198. result = int(result)
  199. except ValueError:
  200. logger.warning(f'[{self.name}]: Invalid quantity: {result}')
  201. result = 0
  202. return result
  203. def ocr_quantity(self, image) -> int:
  204. """
  205. 返回数量
  206. :param image:
  207. :return:
  208. """
  209. boxed_results = self.detect_and_ocr(image)
  210. if not boxed_results:
  211. logger.warning(f'[{self.name}]: No text detected')
  212. return 0
  213. box = boxed_results[0].box
  214. 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]
  215. return boxed_results[0].ocr_text
  216. if __name__ == '__main__':
  217. import cv2
  218. image = cv2.imread(r'E:\Project\OnmyojiAutoScript-assets\jade.png')