click.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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.base.decorator import cached_property
  6. from module.logger import logger
  7. class RuleClick:
  8. def __init__(self, roi_front: tuple, roi_back: tuple) -> None:
  9. """
  10. 初始化
  11. :param roi_front:
  12. :param roi_back:
  13. """
  14. self.roi_front = roi_front
  15. self.roi_back = roi_back
  16. def coord(self) -> tuple:
  17. """
  18. 获取坐标, 从roi_front随机获取坐标
  19. :return:
  20. """
  21. x, y, w, h = self.roi_front
  22. x = np.random.randint(x, x + w)
  23. y = np.random.randint(y, y + h)
  24. return x, y
  25. def coord_more(self) -> tuple:
  26. """
  27. 从roi_back随机获取坐标
  28. :return:
  29. """
  30. x, y, w, h = self.roi_back
  31. x = np.random.randint(x, x + w)
  32. y = np.random.randint(y, y + h)
  33. return x, y
  34. @property
  35. def center(self) -> tuple:
  36. """
  37. 返回roi_front的中心坐标
  38. :return:
  39. """
  40. x, y, w, h = self.roi_front
  41. return x + w // 2, y + h // 2
  42. def move(self, x: int, y: int) -> None:
  43. """
  44. 移动roi_front, 需要限幅x是0-1280, y是0-720
  45. :param x:
  46. :param y:
  47. :return:
  48. """
  49. x, y, w, h = self.roi_front
  50. x += x
  51. y += y
  52. if x <= 0 :
  53. x = 0
  54. elif x >= 1280:
  55. x = 1280
  56. if y <= 0 :
  57. y = 0
  58. elif y >= 720:
  59. y = 720
  60. self.roi_front = x, y, w, h