base_ocr.py 7.7 KB

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