gif.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. from module.atom.image import RuleImage
  6. class RuleGif:
  7. # 大部分实现同RuleImage 的接口
  8. @property
  9. def name(self) -> str:
  10. return self.appear_target.name
  11. def __init__(self, targets: list[RuleImage]):
  12. self.targets = targets
  13. self.roi_front: list = [0, 0, 0, 0]
  14. self.appear_target = targets[0]
  15. def pre_process(self, image):
  16. return image
  17. def search(self, image, roi: list = None, threshold: float = None) -> tuple:
  18. """
  19. :param image:
  20. :param roi:
  21. :param threshold:
  22. :return: bool
  23. 第一项是否为出现, 第二项为匹配的RuleImage
  24. """
  25. image = self.pre_process(image)
  26. #
  27. threshold = self.targets[0].threshold if threshold is None else threshold
  28. roi = self.targets[0].roi_back if roi is None else roi
  29. for target in self.targets:
  30. target.roi_back = roi
  31. if target.match(image, threshold):
  32. self.roi_front = target.roi_front
  33. self.appear_target = target
  34. return True, target
  35. return False, None
  36. def match(self, image, threshold: float = None) -> bool:
  37. return self.search(image, threshold=threshold)[0]
  38. def coord(self) -> tuple:
  39. x, y, w, h = self.roi_front
  40. return x + np.random.randint(0, w), y + np.random.randint(0, h)
  41. def front_center(self) -> tuple:
  42. x, y, w, h = self.roi_front
  43. return int(x + w//2), int(y + h//2)