click.py 1.8 KB

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