base_ocr.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. import numpy as np
  7. from ppocronnx.predict_system import BoxedResult
  8. from enum import Enum
  9. from module.base.decorator import cached_property
  10. from module.base.utils import area_pad, crop, float2str
  11. from typing import Any
  12. from module.ocr.models import get_ocr_model
  13. from module.exception import ScriptError
  14. from module.logger import logger
  15. def enlarge_canvas(image):
  16. """
  17. copy from https://github.com/LmeSzinc/StarRailCopilot
  18. Enlarge image into a square fill with black background. In the structure of PaddleOCR,
  19. image with w:h=1:1 is the best while 3:1 rectangles takes three times as long.
  20. Also enlarge into the integer multiple of 32 cause PaddleOCR will downscale images to 1/32.
  21. """
  22. height, width = image.shape[:2]
  23. length = int(max(width, height) // 32 * 32 + 32)
  24. border = (0, length - height, 0, length - width)
  25. if sum(border) > 0:
  26. image = cv2.copyMakeBorder(image, *border, borderType=cv2.BORDER_CONSTANT, value=(0, 0, 0))
  27. return image
  28. class OcrMode(Enum):
  29. FULL = 1 # str: "Full"
  30. SINGLE = 2 # str: "Single"
  31. DIGIT = 3 # str: "Digit"
  32. DIGITCOUNTER = 4 # str: "DigitCounter"
  33. DURATION = 5 # str: "Duration"
  34. QUANTITY = 6 # str: "Quantity"
  35. class OcrMethod(Enum):
  36. DEFAULT = 1 # str: "Default"
  37. class BaseCor:
  38. lang: str = "ch"
  39. score: float = 0.6 # 阈值默认为0.5
  40. min_score: float = 0.3 # 宽松阈值,用于挽救数字等结果
  41. name: str = "ocr"
  42. mode: OcrMode = OcrMode.FULL
  43. method: OcrMethod = OcrMethod.DEFAULT # 占位符
  44. roi: list = [] # [x, y, width, height]
  45. area: list = [] # [x, y, width, height]
  46. keyword: str = "" # 默认为空
  47. def __init__(self,
  48. name: str,
  49. mode: str,
  50. method: str,
  51. roi: tuple,
  52. area: tuple,
  53. keyword: str) -> None:
  54. """
  55. :param name:
  56. :param mode:
  57. :param method:
  58. :param roi:
  59. :param area:
  60. :param keyword:
  61. """
  62. self.name = name.upper()
  63. if isinstance(mode, str):
  64. self.mode = OcrMode[mode.upper()]
  65. elif isinstance(mode, OcrMode):
  66. self.mode = mode
  67. if isinstance(method, str):
  68. self.method = OcrMethod[method.upper()]
  69. elif isinstance(method, OcrMethod):
  70. self.method = method
  71. self.roi: list = list(roi)
  72. self.area: list = list(area)
  73. self.keyword = keyword
  74. def __str__(self):
  75. return f"{self.name}"
  76. def __repr__(self):
  77. return f"{self.name}"
  78. @cached_property
  79. def model(self) -> Any:
  80. return get_ocr_model(self.lang)
  81. def pre_process(self, image):
  82. """
  83. 重写
  84. :param image:
  85. :return:
  86. """
  87. return image
  88. def after_process(self, result):
  89. """
  90. 重写
  91. :param result:
  92. :return:
  93. """
  94. return result
  95. @classmethod
  96. def crop(cls, image: np.array, roi: tuple) -> np.array:
  97. """
  98. 截取图片
  99. :param roi:
  100. :param image:
  101. :return:
  102. """
  103. x, y, w, h = roi
  104. return image[y:y + h, x:x + w]
  105. def ocr_item(self, image):
  106. """
  107. 这个函数区别于ocr_single_line,这个函数不对图片进行裁剪,是什么就是什么的
  108. :param image:
  109. :return:
  110. """
  111. # pre process
  112. start_time = time.time()
  113. image = self.pre_process(image)
  114. # ocr
  115. result, score = self.model.ocr_single_line(image)
  116. if score < self.score:
  117. result = ""
  118. # after proces
  119. result = self.after_process(result)
  120. # logger.info("ocr result score: %s%s" % (result,score))
  121. logger.attr(name='%s %ss' % (self.name, float2str(time.time() - start_time)),
  122. text=f'[{result}]')
  123. return result
  124. def ocr_single_line(self, image):
  125. """
  126. 只支持横方向的单行ocr,不支持竖方向的单行ocr
  127. 注意:这里使用了预处理和后处理
  128. :param image:
  129. :return:
  130. """
  131. # pre process
  132. start_time = time.time()
  133. image = self.crop(image, self.roi)
  134. image = self.pre_process(image)
  135. # ocr
  136. result, score = self.model.ocr_single_line(image)
  137. contains_digit = any(char.isdigit() for char in result)
  138. if score >= self.score:
  139. pass
  140. elif score >= self.min_score and contains_digit and self.mode in [OcrMode.DIGIT, OcrMode.DIGITCOUNTER,
  141. OcrMode.QUANTITY]:
  142. logger.warning(
  143. f'[{self.name}] Score {score:.2f} is low, but result "{result}" contains a digit. Accepting it.')
  144. print(f'能保留')
  145. else:
  146. result = ""
  147. # after proces
  148. result = self.after_process(result)
  149. # logger.info("ocr result score: %s" % score)
  150. logger.attr(name='%s %ss' % (self.name, float2str(time.time() - start_time)),
  151. text=f'[{result}]')
  152. return result
  153. def detect_and_ocr(self, image, logDisplay: bool = True) -> list[BoxedResult]:
  154. """
  155. 注意:这里使用了预处理和后处理
  156. :param image:
  157. :return:
  158. """
  159. # pre process
  160. start_time = time.time()
  161. image = self.crop(image, self.roi)
  162. image = self.pre_process(image)
  163. image = enlarge_canvas(image)
  164. # ocr
  165. boxed_results: list[BoxedResult] = self.model.detect_and_ocr(image)
  166. results = []
  167. # after proces
  168. for result in boxed_results:
  169. # logger.info("ocr result score: %s" % result.score)
  170. if result.score < self.score:
  171. continue
  172. result.ocr_text = self.after_process(result.ocr_text)
  173. results.append(result)
  174. if logDisplay:
  175. logger.attr(name='%s %ss' % (self.name, float2str(time.time() - start_time)),
  176. text=str([result.ocr_text for result in results]))
  177. return results
  178. def match(self, result: str, included: bool=False) -> bool:
  179. """
  180. 使用ocr获取结果后和keyword进行匹配
  181. :param result:
  182. :param included: ocr结果和keyword是否包含关系, 要么是包含关系,要么是相等关系
  183. :return:
  184. """
  185. if included:
  186. return self.keyword in result
  187. else:
  188. return self.keyword == result
  189. def filter(self, boxed_results: list[BoxedResult], keyword: str=None, exact: bool=False) -> list or None:
  190. """
  191. 使用ocr获取结果后和keyword进行匹配. 返回匹配的index list
  192. :param keyword: 如果不指定默认适用对象的keyword
  193. :param boxed_results:
  194. :param exact: 是否只匹配完整的单个 OCR 文本
  195. :return:
  196. """
  197. # 首先先将所有的ocr的str顺序拼接起来, 然后再进行匹配
  198. result = None
  199. strings = [boxed_result.ocr_text for boxed_result in boxed_results]
  200. concatenated_string = "".join(strings)
  201. if keyword is None:
  202. keyword = self.keyword
  203. if exact:
  204. result = [index for index, word in enumerate(strings) if word == keyword]
  205. return result or None
  206. elif keyword in concatenated_string:
  207. result = [index for index, word in enumerate(strings) if keyword in word]
  208. else:
  209. result = None
  210. if result is not None:
  211. # logger.info("Filter result: %s" % result)
  212. return result
  213. # 如果适用顺序拼接还是没有匹配到,那可能是竖排的,使用单个字节的keyword进行匹配
  214. indices = []
  215. # 对于keyword中的每一个字符,都要在strings中进行匹配
  216. # 如果这个字符在strings中的某一个string中,那么就记录这个string的index
  217. max_index = len(strings) - 1
  218. for index, char in enumerate(keyword):
  219. for i, string in enumerate(strings):
  220. if char not in string:
  221. continue
  222. if i <= max_index:
  223. indices.append(i)
  224. break
  225. if indices:
  226. # 剔除掉重复的index
  227. indices = list(set(indices))
  228. return indices
  229. else:
  230. return None
  231. def detect_text(self, image) -> str:
  232. """
  233. 识别图片中的文字, 会按照顺序拼接起来
  234. :param image:
  235. :return:
  236. """
  237. # pre process
  238. start_time = time.time()
  239. image = self.crop(image, self.roi)
  240. image = self.pre_process(image)
  241. image = enlarge_canvas(image)
  242. # ocr
  243. boxed_results: list[BoxedResult] = self.model.detect_and_ocr(image)
  244. results = ''
  245. # after proces
  246. for result in boxed_results:
  247. # logger.info("ocr result score: %s" % result.score)
  248. if result.score < self.score:
  249. continue
  250. results += result.ocr_text
  251. # logger.info("ocr result score: %s" % score)
  252. logger.attr(name='%s %ss' % (self.name, float2str(time.time() - start_time)),
  253. text=f'[{results}]')
  254. return results
  255. # def test():
  256. # # strings = ["探", "索"]
  257. # # keyword = "探索"
  258. # # strings是截图ocr的结果,keyword是要匹配的关键字
  259. # strings = ['123', '456', '789', '101112', '131415', '456']
  260. # keyword = '123456'
  261. #
  262. # indices = []
  263. # # 对于keyword中的每一个字符,都要在strings中进行匹配
  264. # # 如果这个字符在strings中的某一个string中,那么就记录这个string的index
  265. # max_index = len(strings) - 1
  266. # for index, char in enumerate(keyword):
  267. # for i, string in enumerate(strings):
  268. # if char not in string:
  269. # continue
  270. # if i <= max_index:
  271. # indices.append(i)
  272. # break
  273. # if indices:
  274. # # 剔除掉重复的index
  275. # indices = list(set(indices))
  276. # return indices
  277. # else:
  278. # return None
  279. # print(test())