base_ocr.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. @cached_property
  75. def model(self) -> TextSystem:
  76. return OCR_MODEL.__getattribute__(self.lang)
  77. def pre_process(self, image):
  78. """
  79. 重写
  80. :param image:
  81. :return:
  82. """
  83. return image
  84. def after_process(self, result):
  85. """
  86. 重写
  87. :param result:
  88. :return:
  89. """
  90. return result
  91. @classmethod
  92. def crop(cls, image: np.array, roi: tuple) -> np.array:
  93. """
  94. 截取图片
  95. :param roi:
  96. :param image:
  97. :return:
  98. """
  99. x, y, w, h = roi
  100. return image[y:y + h, x:x + w]
  101. def ocr_item(self, image):
  102. """
  103. 这个函数区别于ocr_single_line,这个函数不对图片进行裁剪,是什么就是什么的
  104. :param image:
  105. :return:
  106. """
  107. # pre process
  108. start_time = time.time()
  109. image = self.pre_process(image)
  110. # ocr
  111. result, score = self.model.ocr_single_line(image)
  112. if score < self.score:
  113. result = ""
  114. # after proces
  115. result = self.after_process(result)
  116. # logger.info("ocr result score: %s%s" % (result,score))
  117. logger.attr(name='%s %ss' % (self.name, float2str(time.time() - start_time)),
  118. text=f'[{result}]')
  119. return result
  120. def ocr_single_line(self, image):
  121. """
  122. 只支持横方向的单行ocr,不支持竖方向的单行ocr
  123. 注意:这里使用了预处理和后处理
  124. :param image:
  125. :return:
  126. """
  127. # pre process
  128. start_time = time.time()
  129. image = self.crop(image, self.roi)
  130. image = self.pre_process(image)
  131. # ocr
  132. result, score = self.model.ocr_single_line(image)
  133. contains_digit = any(char.isdigit() for char in result)
  134. if score >= self.score:
  135. pass
  136. elif score >= self.min_score and contains_digit and self.mode in [OcrMode.DIGIT, OcrMode.DIGITCOUNTER,
  137. OcrMode.QUANTITY]:
  138. logger.warning(
  139. f'[{self.name}] Score {score:.2f} is low, but result "{result}" contains a digit. Accepting it.')
  140. print(f'能保留')
  141. else:
  142. result = ""
  143. # after proces
  144. result = self.after_process(result)
  145. # logger.info("ocr result score: %s" % score)
  146. logger.attr(name='%s %ss' % (self.name, float2str(time.time() - start_time)),
  147. text=f'[{result}]')
  148. return result
  149. def detect_and_ocr(self, image, logDisplay: bool = True) -> list[BoxedResult]:
  150. """
  151. 注意:这里使用了预处理和后处理
  152. :param image:
  153. :return:
  154. """
  155. # pre process
  156. start_time = time.time()
  157. image = self.crop(image, self.roi)
  158. image = self.pre_process(image)
  159. image = enlarge_canvas(image)
  160. # ocr
  161. boxed_results: list[BoxedResult] = self.model.detect_and_ocr(image)
  162. results = []
  163. # after proces
  164. for result in boxed_results:
  165. # logger.info("ocr result score: %s" % result.score)
  166. if result.score < self.score:
  167. continue
  168. result.ocr_text = self.after_process(result.ocr_text)
  169. results.append(result)
  170. if logDisplay:
  171. logger.attr(name='%s %ss' % (self.name, float2str(time.time() - start_time)),
  172. text=str([result.ocr_text for result in results]))
  173. return results
  174. def match(self, result: str, included: bool=False) -> bool:
  175. """
  176. 使用ocr获取结果后和keyword进行匹配
  177. :param result:
  178. :param included: ocr结果和keyword是否包含关系, 要么是包含关系,要么是相等关系
  179. :return:
  180. """
  181. if included:
  182. return self.keyword in result
  183. else:
  184. return self.keyword == result
  185. def filter(self, boxed_results: list[BoxedResult], keyword: str=None) -> list or None:
  186. """
  187. 使用ocr获取结果后和keyword进行匹配. 返回匹配的index list
  188. :param keyword: 如果不指定默认适用对象的keyword
  189. :param boxed_results:
  190. :return:
  191. """
  192. # 首先先将所有的ocr的str顺序拼接起来, 然后再进行匹配
  193. result = None
  194. strings = [boxed_result.ocr_text for boxed_result in boxed_results]
  195. concatenated_string = "".join(strings)
  196. if keyword is None:
  197. keyword = self.keyword
  198. if keyword in concatenated_string:
  199. result = [index for index, word in enumerate(strings) if keyword in word]
  200. else:
  201. result = None
  202. if result is not None:
  203. # logger.info("Filter result: %s" % result)
  204. return result
  205. # 如果适用顺序拼接还是没有匹配到,那可能是竖排的,使用单个字节的keyword进行匹配
  206. indices = []
  207. # 对于keyword中的每一个字符,都要在strings中进行匹配
  208. # 如果这个字符在strings中的某一个string中,那么就记录这个string的index
  209. max_index = len(strings) - 1
  210. for index, char in enumerate(keyword):
  211. for i, string in enumerate(strings):
  212. if char not in string:
  213. continue
  214. if i <= max_index:
  215. indices.append(i)
  216. break
  217. if indices:
  218. # 剔除掉重复的index
  219. indices = list(set(indices))
  220. return indices
  221. else:
  222. return None
  223. def detect_text(self, image) -> str:
  224. """
  225. 识别图片中的文字, 会按照顺序拼接起来
  226. :param image:
  227. :return:
  228. """
  229. # pre process
  230. start_time = time.time()
  231. image = self.crop(image, self.roi)
  232. image = self.pre_process(image)
  233. image = enlarge_canvas(image)
  234. # ocr
  235. boxed_results: list[BoxedResult] = self.model.detect_and_ocr(image)
  236. results = ''
  237. # after proces
  238. for result in boxed_results:
  239. # logger.info("ocr result score: %s" % result.score)
  240. if result.score < self.score:
  241. continue
  242. results += result.ocr_text
  243. # logger.info("ocr result score: %s" % score)
  244. logger.attr(name='%s %ss' % (self.name, float2str(time.time() - start_time)),
  245. text=f'[{results}]')
  246. return results
  247. # def test():
  248. # # strings = ["探", "索"]
  249. # # keyword = "探索"
  250. # # strings是截图ocr的结果,keyword是要匹配的关键字
  251. # strings = ['123', '456', '789', '101112', '131415', '456']
  252. # keyword = '123456'
  253. #
  254. # indices = []
  255. # # 对于keyword中的每一个字符,都要在strings中进行匹配
  256. # # 如果这个字符在strings中的某一个string中,那么就记录这个string的index
  257. # max_index = len(strings) - 1
  258. # for index, char in enumerate(keyword):
  259. # for i, string in enumerate(strings):
  260. # if char not in string:
  261. # continue
  262. # if i <= max_index:
  263. # indices.append(i)
  264. # break
  265. # if indices:
  266. # # 剔除掉重复的index
  267. # indices = list(set(indices))
  268. # return indices
  269. # else:
  270. # return None
  271. # print(test())