gif.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. @staticmethod
  32. def detach_from(image_obj: RuleImage) -> None:
  33. """Remove attached GIF behavior while preserving the image object's identity."""
  34. if 'targets' not in image_obj.__dict__:
  35. return
  36. for name, attr in RuleGif.__dict__.items():
  37. method = image_obj.__dict__.get(name)
  38. if isinstance(method, types.MethodType) and method.__func__ is attr:
  39. delattr(image_obj, name)
  40. for name in ('targets', 'appear_target', 'name'):
  41. image_obj.__dict__.pop(name, None)
  42. @property
  43. def name(self) -> str:
  44. return self.targets[0].name
  45. def __init__(self, targets: list[RuleImage]):
  46. self.targets = targets
  47. self.roi_front: list = [0, 0, 0, 0]
  48. self.roi_back: list = targets[0].roi_back
  49. self._match_init = False
  50. self.appear_target = targets[0]
  51. def pre_process(self, image):
  52. return image
  53. def search(self, image, roi: list = None, threshold: float = None) -> tuple:
  54. """
  55. :param image:
  56. :param roi:
  57. :param threshold:
  58. :return: bool
  59. 第一项是否为出现, 第二项为匹配的RuleImage
  60. """
  61. image = self.pre_process(image)
  62. #
  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(image, threshold):
  68. self.roi_front = target.roi_front
  69. self.appear_target = target
  70. return True, target
  71. return False, None
  72. def search_with_multi_scale(self, image, roi=None, threshold=None, scales=None, scale_range=(0.8, 1.1, 0.05)):
  73. image = self.pre_process(image)
  74. threshold = self.targets[0].threshold if threshold is None else threshold
  75. roi = self.targets[0].roi_back if roi is None else roi
  76. for target in self.targets:
  77. target.roi_back = roi
  78. if target.match_multi_scale(image, threshold=threshold, scales=scales, scale_range=scale_range):
  79. self.roi_front = target.roi_front
  80. self.appear_target = target
  81. return True, target
  82. return False, None
  83. def match_multi_scale(self, image, threshold: float = None, scales: list = None, scale_range: tuple = None) -> bool:
  84. return self.search_with_multi_scale(image, threshold=threshold, scales=scales, scale_range=scale_range)[0]
  85. def match(self, image, threshold: float = None) -> bool:
  86. return self.search(image, threshold=threshold)[0]
  87. def coord(self) -> tuple:
  88. x, y, w, h = self.roi_front
  89. return x + np.random.randint(0, w), y + np.random.randint(0, h)
  90. def front_center(self) -> tuple:
  91. x, y, w, h = self.roi_front
  92. return int(x + w//2), int(y + h//2)