ocr.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # github https://github.com/runhey
  4. import numpy as np
  5. import cv2
  6. from module.ocr.base_ocr import BaseCor, OcrMode, OcrMethod
  7. from module.ocr.sub_ocr import Full, Single, Digit, DigitCounter, Duration, Quantity
  8. from module.logger import logger
  9. class RuleOcr(Digit, DigitCounter, Duration, Single, Full, Quantity):
  10. def __init__(self, *args, **kwargs):
  11. super().__init__(*args, **kwargs)
  12. def after_process(self, result):
  13. match self.mode:
  14. case OcrMode.FULL: return Full.after_process(self, result)
  15. case OcrMode.SINGLE: return Single.after_process(self, result)
  16. case OcrMode.DIGIT: return Digit.after_process(self, result)
  17. case OcrMode.DIGITCOUNTER: return DigitCounter.after_process(self, result)
  18. case OcrMode.DURATION: return Duration.after_process(self, result)
  19. case OcrMode.QUANTITY: return Quantity.after_process(self, result)
  20. case _: return result
  21. def ocr(self, image, keyword=None):
  22. match self.mode:
  23. case OcrMode.FULL: return Full.ocr_full(self, image, keyword)
  24. case OcrMode.SINGLE: return Single.ocr_single(self, image)
  25. case OcrMode.DIGIT: return Digit.ocr_digit(self, image)
  26. case OcrMode.DIGITCOUNTER: return DigitCounter.ocr_digit_counter(self, image)
  27. case OcrMode.DURATION: return Duration.ocr_duration(self, image)
  28. case OcrMode.QUANTITY: return Quantity.ocr_quantity(self, image)
  29. case _: return None
  30. def coord(self) -> tuple:
  31. """
  32. 获取一个区域,随机返回一个坐标
  33. :return:
  34. """
  35. area = None
  36. if self.mode == OcrMode.FULL:
  37. area = self.area
  38. else:
  39. area = self.roi
  40. x, y, w, h = self.area
  41. x = np.random.randint(x, x + w)
  42. y = np.random.randint(y, y + h)
  43. return x, y
  44. if __name__ == "__main__":
  45. O_MALL_RESOURCE_1 = RuleOcr(roi=(144, 7, 100, 43), area=(144, 7, 100, 43), mode="Quantity", method="Default",
  46. keyword="", name="mall_resource_1")
  47. O_MALL_RESOURCE_2 = RuleOcr(roi=(326, 8, 124, 39), area=(326, 8, 124, 39), mode="Quantity", method="Default",
  48. keyword="", name="mall_resource_2")
  49. O_MALL_RESOURCE_3 = RuleOcr(roi=(533, 9, 107, 38), area=(533, 9, 107, 38), mode="Quantity", method="Default",
  50. keyword="", name="mall_resource_3")
  51. O_MALL_RESOURCE_4 = RuleOcr(roi=(739, 8, 100, 39), area=(739, 8, 100, 39), mode="Quantity", method="Default",
  52. keyword="", name="mall_resource_4")
  53. O_MALL_RESOURCE_5 = RuleOcr(roi=(935, 11, 100, 37), area=(935, 11, 100, 37), mode="Quantity", method="Default",
  54. keyword="", name="mall_resource_5")
  55. O_MALL_RESOURCE_6 = RuleOcr(roi=(1129, 6, 100, 41), area=(1129, 6, 100, 41), mode="Quantity", method="Default",
  56. keyword="", name="mall_resource_6")
  57. image = cv2.imread(r"E:\2025-01-16225353.png")
  58. print(O_MALL_RESOURCE_5.ocr_quantity(image))