ocr.py 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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.base.utils.utils import random_normal_distribution_int
  9. from module.logger import logger
  10. class RuleOcr(Digit, DigitCounter, Duration, Single, Full, Quantity):
  11. def __init__(self, *args, **kwargs):
  12. super().__init__(*args, **kwargs)
  13. def after_process(self, result):
  14. match self.mode:
  15. case OcrMode.FULL: return Full.after_process(self, result)
  16. case OcrMode.SINGLE: return Single.after_process(self, result)
  17. case OcrMode.DIGIT: return Digit.after_process(self, result)
  18. case OcrMode.DIGITCOUNTER: return DigitCounter.after_process(self, result)
  19. case OcrMode.DURATION: return Duration.after_process(self, result)
  20. case OcrMode.QUANTITY: return Quantity.after_process(self, result)
  21. case _: return result
  22. def ocr(self, image, keyword=None):
  23. match self.mode:
  24. case OcrMode.FULL: return Full.ocr_full(self, image, keyword)
  25. case OcrMode.SINGLE: return Single.ocr_single(self, image)
  26. case OcrMode.DIGIT: return Digit.ocr_digit(self, image)
  27. case OcrMode.DIGITCOUNTER: return DigitCounter.ocr_digit_counter(self, image)
  28. case OcrMode.DURATION: return Duration.ocr_duration(self, image)
  29. case OcrMode.QUANTITY: return Quantity.ocr_quantity(self, image)
  30. case _: return None
  31. def coord(self) -> tuple:
  32. """
  33. 获取一个区域,随机返回一个坐标
  34. :return:
  35. """
  36. area = None
  37. if self.mode == OcrMode.FULL:
  38. area = self.area
  39. else:
  40. area = self.roi
  41. # 原代码:x, y, w, h = self.area
  42. # 修改原因:20260725 , 每月活动,点击挑战的时候,经常点到切换门票模式和体力模式
  43. # OCR识别到并点击,在SINGLE模式下,识别到挑战,点击区域应该为ROI,而不是AREA
  44. # 该地方应该是误写为self.area,本地变量area没有起作用,如果是self.area,上面几句话就白写了,没起作用
  45. x, y, w, h = area
  46. x = random_normal_distribution_int(x, x + w)
  47. y = random_normal_distribution_int(y, y + h)
  48. return x, y
  49. if __name__ == "__main__":
  50. # O_MALL_RESOURCE_1 = RuleOcr(roi=(144, 7, 100, 43), area=(144, 7, 100, 43), mode="Quantity", method="Default",
  51. # keyword="", name="mall_resource_1")
  52. # O_MALL_RESOURCE_2 = RuleOcr(roi=(326, 8, 124, 39), area=(326, 8, 124, 39), mode="Quantity", method="Default",
  53. # keyword="", name="mall_resource_2")
  54. # O_MALL_RESOURCE_3 = RuleOcr(roi=(533, 9, 107, 38), area=(533, 9, 107, 38), mode="Quantity", method="Default",
  55. # keyword="", name="mall_resource_3")
  56. # O_MALL_RESOURCE_4 = RuleOcr(roi=(739, 8, 100, 39), area=(739, 8, 100, 39), mode="Quantity", method="Default",
  57. # keyword="", name="mall_resource_4")
  58. # O_MALL_RESOURCE_5 = RuleOcr(roi=(935, 11, 100, 37), area=(935, 11, 100, 37), mode="Quantity", method="Default",
  59. # keyword="", name="mall_resource_5")
  60. # O_MALL_RESOURCE_6 = RuleOcr(roi=(1129, 6, 100, 41), area=(1129, 6, 100, 41), mode="Quantity", method="Default",
  61. # keyword="", name="mall_resource_6")
  62. # image = cv2.imread(r"E:\2025-01-16225353.png")
  63. # print(O_MALL_RESOURCE_5.ocr_quantity(image))
  64. from module.atom.image import RuleImage
  65. image = cv2.imread(r"E:\Debug\MuMu-20260119-114931-173.png")
  66. I_SIDE_CHECK_SPECIAL = RuleImage(roi_front=(155,8,42,42), roi_back=(155,7,1115,90), threshold=0.7, method="Template matching", file="E:\Debug\\navbar_mall_sccales_check.png")
  67. #roi = I_SIDE_CHECK_SPECIAL.get_mall_resource_text_roi(image, threshold=0.7)
  68. rule = I_SIDE_CHECK_SPECIAL.build_mall_resource_ocr(
  69. image,
  70. threshold=0.7,
  71. name="mall_resource_1"
  72. )
  73. print(rule.ocr_quantity(image))