click.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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, name: str=None) -> 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. if name:
  17. self.name = name
  18. else:
  19. self.name = 'click'
  20. def coord(self) -> tuple:
  21. """
  22. 获取坐标, 从roi_front随机获取坐标
  23. :return:
  24. """
  25. x, y, w, h = self.roi_front
  26. x = np.random.randint(x, x + w)
  27. y = np.random.randint(y, y + h)
  28. return x, y
  29. def coord_more(self) -> tuple:
  30. """
  31. 从roi_back随机获取坐标
  32. :return:
  33. """
  34. x, y, w, h = self.roi_back
  35. x = np.random.randint(x, x + w)
  36. y = np.random.randint(y, y + h)
  37. return x, y
  38. @property
  39. def center(self) -> tuple:
  40. """
  41. 返回roi_front的中心坐标
  42. :return:
  43. """
  44. x, y, w, h = self.roi_front
  45. return x + w // 2, y + h // 2
  46. def move(self, x: int, y: int) -> None:
  47. """
  48. 移动roi_front, 需要限幅x是0-1280, y是0-720
  49. :param x:
  50. :param y:
  51. :return:
  52. """
  53. x, y, w, h = self.roi_front
  54. x += x
  55. y += y
  56. if x <= 0 :
  57. x = 0
  58. elif x >= 1280:
  59. x = 1280
  60. if y <= 0 :
  61. y = 0
  62. elif y >= 720:
  63. y = 720
  64. self.roi_front = x, y, w, h