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