base_ocr.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 module.ocr.ppocr import TextSystem
  12. from module.ocr.models import 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) -> TextSystem:
  80. return OCR_MODEL.__getattribute__(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) -> list or None:
  190. """
  191. 使用ocr获取结果后和keyword进行匹配. 返回匹配的index list
  192. :param keyword: 如果不指定默认适用对象的keyword
  193. :param boxed_results:
  194. :return:
  195. """
  196. # 首先先将所有的ocr的str顺序拼接起来, 然后再进行匹配
  197. result = None
  198. strings = [boxed_result.ocr_text for boxed_result in boxed_results]
  199. concatenated_string = "".join(strings)
  200. if keyword is None:
  201. keyword = self.keyword
  202. if keyword in concatenated_string:
  203. result = [index for index, word in enumerate(strings) if keyword in word]
  204. else:
  205. result = None
  206. if result is not None:
  207. # logger.info("Filter result: %s" % result)
  208. return result
  209. # 如果适用顺序拼接还是没有匹配到,那可能是竖排的,使用单个字节的keyword进行匹配
  210. indices = []
  211. # 对于keyword中的每一个字符,都要在strings中进行匹配
  212. # 如果这个字符在strings中的某一个string中,那么就记录这个string的index
  213. max_index = len(strings) - 1
  214. for index, char in enumerate(keyword):
  215. for i, string in enumerate(strings):
  216. if char not in string:
  217. continue
  218. if i <= max_index:
  219. indices.append(i)
  220. break
  221. if indices:
  222. # 剔除掉重复的index
  223. indices = list(set(indices))
  224. return indices
  225. else:
  226. return None
  227. def detect_text(self, image) -> str:
  228. """
  229. 识别图片中的文字, 会按照顺序拼接起来
  230. :param image:
  231. :return:
  232. """
  233. # pre process
  234. start_time = time.time()
  235. image = self.crop(image, self.roi)
  236. image = self.pre_process(image)
  237. image = enlarge_canvas(image)
  238. # ocr
  239. boxed_results: list[BoxedResult] = self.model.detect_and_ocr(image)
  240. results = ''
  241. # after proces
  242. for result in boxed_results:
  243. # logger.info("ocr result score: %s" % result.score)
  244. if result.score < self.score:
  245. continue
  246. results += result.ocr_text
  247. # logger.info("ocr result score: %s" % score)
  248. logger.attr(name='%s %ss' % (self.name, float2str(time.time() - start_time)),
  249. text=f'[{results}]')
  250. return results
  251. # def test():
  252. # # strings = ["探", "索"]
  253. # # keyword = "探索"
  254. # # strings是截图ocr的结果,keyword是要匹配的关键字
  255. # strings = ['123', '456', '789', '101112', '131415', '456']
  256. # keyword = '123456'
  257. #
  258. # indices = []
  259. # # 对于keyword中的每一个字符,都要在strings中进行匹配
  260. # # 如果这个字符在strings中的某一个string中,那么就记录这个string的index
  261. # max_index = len(strings) - 1
  262. # for index, char in enumerate(keyword):
  263. # for i, string in enumerate(strings):
  264. # if char not in string:
  265. # continue
  266. # if i <= max_index:
  267. # indices.append(i)
  268. # break
  269. # if indices:
  270. # # 剔除掉重复的index
  271. # indices = list(set(indices))
  272. # return indices
  273. # else:
  274. # return None
  275. # print(test())