gif.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # github https://github.com/runhey
  4. import types
  5. import numpy as np
  6. from module.atom.image import RuleImage
  7. class RuleGif:
  8. # 大部分实现同RuleImage 的接口
  9. @staticmethod
  10. def attach_to(image_obj: RuleImage, targets: list[RuleImage]) -> None:
  11. """
  12. 把GIF的多帧匹配行为原地挂载到一个RuleImage实例上
  13. 与replace_img的原地换属性同思路: 不换对象、不换引用,
  14. 持有该对象引用的Page/寻路会立即生效
  15. :param image_obj: 被挂载的RuleImage实例
  16. :param targets: 多帧RuleImage, 顺序即匹配优先级
  17. """
  18. # GIF状态
  19. image_obj.targets = targets
  20. image_obj.roi_front = [0, 0, 0, 0]
  21. image_obj.roi_back = targets[0].roi_back
  22. image_obj.appear_target = targets[0]
  23. image_obj.name = targets[0].name
  24. image_obj._match_init = False
  25. # 方法级替换: 把RuleGif的所有公开方法绑定到该实例上
  26. for name, attr in RuleGif.__dict__.items():
  27. if name.startswith('_'):
  28. continue
  29. if isinstance(attr, types.FunctionType):
  30. setattr(image_obj, name, types.MethodType(attr, image_obj))
  31. @property
  32. def name(self) -> str:
  33. return self.targets[0].name
  34. def __init__(self, targets: list[RuleImage]):
  35. self.targets = targets
  36. self.roi_front: list = [0, 0, 0, 0]
  37. self.roi_back: list = targets[0].roi_back
  38. self._match_init = False
  39. self.appear_target = targets[0]
  40. def pre_process(self, image):
  41. return image
  42. def search(self, image, roi: list = None, threshold: float = None) -> tuple:
  43. """
  44. :param image:
  45. :param roi:
  46. :param threshold:
  47. :return: bool
  48. 第一项是否为出现, 第二项为匹配的RuleImage
  49. """
  50. image = self.pre_process(image)
  51. #
  52. threshold = self.targets[0].threshold if threshold is None else threshold
  53. roi = self.targets[0].roi_back if roi is None else roi
  54. for target in self.targets:
  55. target.roi_back = roi
  56. if target.match(image, threshold):
  57. self.roi_front = target.roi_front
  58. self.appear_target = target
  59. return True, target
  60. return False, None
  61. def search_with_multi_scale(self, image, roi=None, threshold=None, scales=None, scale_range=(0.8, 1.1, 0.05)):
  62. image = self.pre_process(image)
  63. threshold = self.targets[0].threshold if threshold is None else threshold
  64. roi = self.targets[0].roi_back if roi is None else roi
  65. for target in self.targets:
  66. target.roi_back = roi
  67. if target.match_multi_scale(image, threshold=threshold, scales=scales, scale_range=scale_range):
  68. self.roi_front = target.roi_front
  69. self.appear_target = target
  70. return True, target
  71. return False, None
  72. def match_multi_scale(self, image, threshold: float = None, scales: list = None, scale_range: tuple = None) -> bool:
  73. return self.search_with_multi_scale(image, threshold=threshold, scales=scales, scale_range=scale_range)[0]
  74. def match(self, image, threshold: float = None) -> bool:
  75. return self.search(image, threshold=threshold)[0]
  76. def coord(self) -> tuple:
  77. x, y, w, h = self.roi_front
  78. return x + np.random.randint(0, w), y + np.random.randint(0, h)
  79. def front_center(self) -> tuple:
  80. x, y, w, h = self.roi_front
  81. return int(x + w//2), int(y + h//2)