base_ocr.py 9.2 KB

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