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