base_ocr.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. from ppocronnx.predict_system import BoxedResult
  7. from enum import Enum
  8. from module.base.decorator import cached_property
  9. from module.base.utils import area_pad, crop, float2str
  10. from module.ocr.ppocr import TextSystem
  11. from module.ocr.models import OCR_MODEL
  12. from module.exception import ScriptError
  13. from module.logger import logger
  14. class OcrMode(Enum):
  15. FULL = 1 # str: "Full"
  16. SINGLE_LINE = 2 # str: "SingleLine"
  17. DIGIT = 3 # str: "Digit"
  18. DIGIT_COUNTER = 4 # str: "DigitCounter"
  19. DURATION = 5 # str: "Duration"
  20. class OcrMethod(Enum):
  21. DEFAULT = 1 # str: "Default"
  22. class BaseCor:
  23. lang: str = "cn"
  24. score: float = 0.5 # 阈值默认为0.5
  25. name: str = "ocr"
  26. mode: OcrMode = OcrMode.FULL
  27. method: OcrMethod = OcrMethod.DEFAULT # 占位符
  28. roi: list = [] # [x, y, width, height]
  29. area: list = [] # [x, y, width, height]
  30. keyword: str = "" # 默认为空
  31. @cached_property
  32. def model(self) -> TextSystem:
  33. return OCR_MODEL.__getattribute__(self.lang)
  34. def pre_process(self, image):
  35. """
  36. 重写
  37. :param image:
  38. :return:
  39. """
  40. return image
  41. def after_process(self, result):
  42. """
  43. 重写
  44. :param result:
  45. :return:
  46. """
  47. return result
  48. def ocr_single_line(self, image):
  49. """
  50. 只支持横方向的单行ocr,不支持竖方向的单行ocr
  51. 注意:这里使用了预处理和后处理
  52. :param image:
  53. :return:
  54. """
  55. # pre process
  56. start_time = time.time()
  57. image = crop(image, self.roi, copy=False)
  58. image = self.pre_process(image)
  59. # ocr
  60. result, _ = self.model.ocr_single_line(image)
  61. # after proces
  62. result = self.after_process(result)
  63. logger.attr(name='%s %ss' % (self.name, float2str(time.time() - start_time)),
  64. text=str(result))
  65. return result
  66. def detect_and_ocr(self, image) -> list[BoxedResult]:
  67. """
  68. 注意:这里使用了预处理和后处理
  69. :param image:
  70. :return:
  71. """
  72. # pre process
  73. start_time = time.time()
  74. image = self.pre_process(image)
  75. # ocr
  76. results: list[BoxedResult] = self.model.detect_and_ocr(image)
  77. # after proces
  78. for result in results:
  79. result.ocr_text = self.after_process(result.ocr_text)
  80. logger.attr(name='%s %ss' % (self.name, float2str(time.time() - start_time)),
  81. text=str([result.ocr_text for result in results]))
  82. return results
  83. def match(self, result: str, included: bool=False) -> bool:
  84. """
  85. 使用ocr获取结果后和keyword进行匹配
  86. :param result:
  87. :param included: ocr结果和keyword是否包含关系, 要么是包含关系,要么是相等关系
  88. :return:
  89. """
  90. if included:
  91. return self.keyword in result
  92. else:
  93. return self.keyword == result