click.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # github https://github.com/runhey
  4. import random
  5. import numpy as np
  6. from module.base.decorator import cached_property
  7. from module.base.utils.utils import random_normal_distribution_int
  8. from module.logger import logger
  9. class RuleClick:
  10. def __init__(self, roi_front: tuple, roi_back: tuple, name: str = None) -> None:
  11. """
  12. 初始化
  13. :param roi_front:
  14. :param roi_back:
  15. """
  16. self.roi_front = roi_front
  17. self.roi_back = roi_back
  18. if name:
  19. self.name = name
  20. else:
  21. self.name = 'click'
  22. def coord(self) -> tuple:
  23. """
  24. 获取坐标, 从roi_front随机获取坐标
  25. :return:
  26. """
  27. x, y, w, h = self.roi_front
  28. x = random_normal_distribution_int(x, x + w)
  29. y = random_normal_distribution_int(y, y + h)
  30. return x, y
  31. def coord_more(self) -> tuple:
  32. """
  33. 从roi_back随机获取坐标
  34. :return:
  35. """
  36. x, y, w, h = self.roi_back
  37. x = random_normal_distribution_int(x, x + w)
  38. y = random_normal_distribution_int(y, y + h)
  39. return x, y
  40. @property
  41. def center(self) -> tuple:
  42. """
  43. 返回roi_front的中心坐标
  44. :return:
  45. """
  46. x, y, w, h = self.roi_front
  47. return x + w // 2, y + h // 2
  48. def move(self, x: int, y: int) -> None:
  49. """
  50. 移动roi_front, 需要限幅x是0-1280, y是0-720
  51. :param x:
  52. :param y:
  53. :return:
  54. """
  55. x, y, w, h = self.roi_front
  56. x += x
  57. y += y
  58. if x <= 0 :
  59. x = 0
  60. elif x >= 1280:
  61. x = 1280
  62. if y <= 0 :
  63. y = 0
  64. elif y >= 720:
  65. y = 720
  66. self.roi_front = x, y, w, h
  67. class RuleClickExclude(RuleClick):
  68. """Randomly click within the screen while excluding other click regions."""
  69. SCREEN_WIDTH = 1280
  70. SCREEN_HEIGHT = 720
  71. def __init__(self, clicks, strategy: str | None = None, distribution: str | None = None,
  72. max_attempts: int = 100, name: str = 'exclude_random_click') -> None:
  73. """
  74. 初始化排除区域随机点击规则。
  75. :param clicks: 需要排除的 RuleClick,或 RuleClick 序列;使用其 roi_back 作为排除区域
  76. :param strategy: 取点策略,None 时在初始化时随机选择 rejection 或 complement
  77. :param distribution: 坐标分布,None 时在初始化时随机选择 uniform 或 normal
  78. :param max_attempts: 拒绝采样的最大尝试次数,耗尽后自动使用补集矩形采样
  79. :param name: 点击规则名称,用于 BaseTask.click 的间隔计时器
  80. :raises TypeError: clicks 不是 RuleClick 或 RuleClick 序列
  81. :raises ValueError: strategy、distribution 或 max_attempts 参数无效,或排除区域覆盖整个屏幕
  82. """
  83. super().__init__((0, 0, self.SCREEN_WIDTH, self.SCREEN_HEIGHT),
  84. (0, 0, self.SCREEN_WIDTH, self.SCREEN_HEIGHT), name=name)
  85. if strategy is None:
  86. strategy = random.choice(('rejection', 'complement'))
  87. if distribution is None:
  88. distribution = random.choice(('uniform', 'normal'))
  89. if strategy not in ('rejection', 'complement'):
  90. raise ValueError("strategy must be 'rejection' or 'complement'")
  91. if distribution not in ('uniform', 'normal'):
  92. raise ValueError("distribution must be 'uniform' or 'normal'")
  93. if max_attempts < 1:
  94. raise ValueError('max_attempts must be positive')
  95. if isinstance(clicks, RuleClick):
  96. clicks = (clicks,)
  97. self.clicks = tuple(clicks)
  98. if not all(isinstance(click, RuleClick) for click in self.clicks):
  99. raise TypeError('clicks must contain RuleClick instances')
  100. self.strategy = strategy
  101. self.distribution = distribution
  102. self.max_attempts = max_attempts
  103. self._excluded = tuple(
  104. rect for click in self.clicks
  105. for rect in (self._clip_roi(click.roi_back),)
  106. if rect is not None
  107. )
  108. self._allowed = self._build_complement()
  109. if not self._allowed:
  110. raise ValueError('Excluded regions cover the whole screen')
  111. def __str__(self):
  112. click_names = ', '.join(click.name for click in self.clicks) or 'none'
  113. return (
  114. f'{self.name}(strategy={self.strategy}, '
  115. f'distribution={self.distribution}, clicks=[{click_names}])'
  116. )
  117. __repr__ = __str__
  118. @classmethod
  119. def _clip_roi(cls, roi):
  120. x, y, w, h = roi
  121. left, top = max(0, x), max(0, y)
  122. right, bottom = min(cls.SCREEN_WIDTH, x + w), min(cls.SCREEN_HEIGHT, y + h)
  123. if left >= right or top >= bottom:
  124. return None
  125. return left, top, right - left, bottom - top
  126. def _is_excluded(self, x, y):
  127. return any(left <= x < left + width and top <= y < top + height
  128. for left, top, width, height in self._excluded)
  129. def _build_complement(self):
  130. x_edges = {0, self.SCREEN_WIDTH}
  131. y_edges = {0, self.SCREEN_HEIGHT}
  132. for x, y, width, height in self._excluded:
  133. x_edges.update((x, x + width))
  134. y_edges.update((y, y + height))
  135. x_edges, y_edges = sorted(x_edges), sorted(y_edges)
  136. allowed = []
  137. for x1, x2 in zip(x_edges, x_edges[1:]):
  138. for y1, y2 in zip(y_edges, y_edges[1:]):
  139. if not self._is_excluded(x1, y1):
  140. allowed.append((x1, y1, x2 - x1, y2 - y1))
  141. return allowed
  142. def _sample_axis(self, start, size, distribution):
  143. if size == 1:
  144. return start
  145. if distribution == 'uniform':
  146. return random.randrange(start, start + size)
  147. return max(start, min(start + size - 1,
  148. random_normal_distribution_int(start, start + size)))
  149. def _sample_point(self, rect, distribution):
  150. x, y, width, height = rect
  151. return self._sample_axis(x, width, distribution), self._sample_axis(y, height, distribution)
  152. def _coord_rejection(self, distribution):
  153. for _ in range(self.max_attempts):
  154. point = self._sample_point((0, 0, self.SCREEN_WIDTH, self.SCREEN_HEIGHT), distribution)
  155. if not self._is_excluded(*point):
  156. return point
  157. return self._coord_complement(distribution)
  158. def _coord_complement(self, distribution):
  159. total = sum(width * height for _, _, width, height in self._allowed)
  160. offset = random.randrange(total)
  161. for rect in self._allowed:
  162. area = rect[2] * rect[3]
  163. if offset < area:
  164. return self._sample_point(rect, distribution)
  165. offset -= area
  166. raise RuntimeError('Unable to select a click region')
  167. def coord(self) -> tuple:
  168. if self.strategy == 'rejection':
  169. return self._coord_rejection(self.distribution)
  170. return self._coord_complement(self.distribution)