cBezier.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. '''
  2. author: cbb
  3. 这个库基于贝塞尔曲线实现模拟人手动滑动的轨迹
  4. 可以用于selenium轨迹的模拟,或者生成轨迹数组用于js加密通过网站服务器后端分控检测
  5. QQ群 134064772
  6. 里面的人说话好听,个个都是人才
  7. '''
  8. import numpy as np
  9. import math
  10. import random
  11. class BezierTrajectory:
  12. @classmethod
  13. def _bztsg(cls, dataTrajectory):
  14. lengthOfdata = len(dataTrajectory)
  15. def staer(x):
  16. t = ((x - dataTrajectory[0][0]) / (dataTrajectory[-1][0] - dataTrajectory[0][0]))
  17. y = np.array([0, 0], dtype=np.float64)
  18. for s in range(len(dataTrajectory)):
  19. y += dataTrajectory[s] * ((math.factorial(lengthOfdata - 1) / (
  20. math.factorial(s) * math.factorial(lengthOfdata - 1 - s))) * math.pow(t, s) * math.pow(
  21. (1 - t), lengthOfdata - 1 - s))
  22. return y[1]
  23. return staer
  24. @classmethod
  25. def _type(cls, type, x, numberList):
  26. numberListre = []
  27. pin = (x[1] - x[0]) / numberList
  28. if type == 0:
  29. for i in range(numberList):
  30. numberListre.append(i * pin)
  31. if pin >= 0:
  32. numberListre = numberListre[::-1]
  33. elif type == 1:
  34. for i in range(numberList):
  35. numberListre.append(1 * ((i * pin) ** 2))
  36. numberListre = numberListre[::-1]
  37. elif type == 2:
  38. for i in range(numberList):
  39. numberListre.append(1 * ((i * pin - x[1]) ** 2))
  40. elif type == 3:
  41. dataTrajectory = [np.array([0,0]), np.array([(x[1]-x[0])*0.8, (x[1]-x[0])*0.6]), np.array([x[1]-x[0], 0])]
  42. fun = cls._bztsg(dataTrajectory)
  43. numberListre = [0]
  44. for i in range(1,numberList):
  45. numberListre.append(fun(i * pin) + numberListre[-1])
  46. if pin >= 0:
  47. numberListre = numberListre[::-1]
  48. numberListre = np.abs(np.array(numberListre) - max(numberListre))
  49. biaoNumberList = ((numberListre - numberListre[numberListre.argmin()]) / (
  50. numberListre[numberListre.argmax()] - numberListre[numberListre.argmin()])) * (x[1] - x[0]) + x[0]
  51. biaoNumberList[0] = x[0]
  52. biaoNumberList[-1] = x[1]
  53. return biaoNumberList
  54. @classmethod
  55. def getFun(cls, s):
  56. '''
  57. :param s: 传入P点
  58. :return: 返回公式
  59. '''
  60. dataTrajectory = []
  61. for i in s:
  62. dataTrajectory.append(np.array(i))
  63. return cls._bztsg(dataTrajectory)
  64. @classmethod
  65. def simulation(cls, start, end, le=1, deviation=0, bias=0.5):
  66. '''
  67. :param start:开始点的坐标 如 start = [0, 0]
  68. :param end:结束点的坐标 如 end = [100, 100]
  69. :param le:几阶贝塞尔曲线,越大越复杂 如 le = 4
  70. :param deviation:轨迹上下波动的范围 如 deviation = 10
  71. :param bias:波动范围的分布位置 如 bias = 0.5
  72. :return:返回一个字典equation对应该曲线的方程,P对应贝塞尔曲线的影响点
  73. '''
  74. start = np.array(start)
  75. end = np.array(end)
  76. cbb = []
  77. if le != 1:
  78. e = (1 - bias) / (le - 1)
  79. cbb = [[bias + e * i, bias + e * (i + 1)] for i in range(le - 1)]
  80. dataTrajectoryList = [start]
  81. t = random.choice([-1, 1])
  82. w = 0
  83. for i in cbb:
  84. px1 = start[0] + (end[0] - start[0]) * (random.random() * (i[1] - i[0]) + (i[0]))
  85. p = np.array([px1, cls._bztsg([start, end])(px1) + t * deviation])
  86. dataTrajectoryList.append(p)
  87. w += 1
  88. if w >= 2:
  89. w = 0
  90. t = -1 * t
  91. dataTrajectoryList.append(end)
  92. return {"equation": cls._bztsg(dataTrajectoryList), "P": np.array(dataTrajectoryList)}
  93. @classmethod
  94. def trackArray(cls, start, end, numberList, le=1, deviation=0, bias=0.5, type=0, cbb=0, yhh=10):
  95. '''
  96. :param start:开始点的坐标 如 start = [0, 0]
  97. :param end:结束点的坐标 如 end = [100, 100]
  98. :param numberList:返回的数组的轨迹点的数量 numberList = 150
  99. :param le:几阶贝塞尔曲线,越大越复杂 如 le = 4
  100. :param deviation:轨迹上下波动的范围 如 deviation = 10
  101. :param bias:波动范围的分布位置 如 bias = 0.5
  102. :param type:0表示均速滑动,1表示先慢后快,2表示先快后慢,3表示先慢中间快后慢 如 type = 1
  103. :param cbb:在终点来回摆动的次数
  104. :param yhh:在终点来回摆动的范围
  105. :return:返回一个字典trackArray对应轨迹数组,P对应贝塞尔曲线的影响点
  106. '''
  107. s = []
  108. fun = cls.simulation(start, end, le, deviation, bias)
  109. w = fun['P']
  110. fun = fun["equation"]
  111. if cbb != 0:
  112. numberListOfcbb = round(numberList*0.2/(cbb+1))
  113. numberList -= (numberListOfcbb*(cbb+1))
  114. xTrackArray = cls._type(type, [start[0], end[0]], numberList)
  115. for i in xTrackArray:
  116. s.append([i, fun(i)])
  117. dq = yhh/cbb
  118. kg = 0
  119. ends = np.copy(end)
  120. for i in range(cbb):
  121. if kg == 0:
  122. d = np.array([end[0] + (yhh-dq*i), ((end[1]-start[1])/(end[0]-start[0]))*(end[0]+(yhh-dq*i)) +(end[1]-((end[1]-start[1])/(end[0]-start[0]))*end[0]) ])
  123. kg = 1
  124. else:
  125. d = np.array([end[0] - (yhh - dq * i), ((end[1]-start[1])/(end[0]-start[0]))*(end[0]-(yhh-dq*i)) +(end[1]-((end[1]-start[1])/(end[0]-start[0]))*end[0]) ])
  126. kg = 0
  127. print(d)
  128. y = cls.trackArray(ends, d, numberListOfcbb, le=2, deviation=0, bias=0.5, type=0, cbb=0, yhh=10)
  129. s += list(y['trackArray'])
  130. ends = d
  131. y = cls.trackArray(ends, end, numberListOfcbb, le=2, deviation=0, bias=0.5, type=0, cbb=0, yhh=10)
  132. s += list(y['trackArray'])
  133. else:
  134. xTrackArray = cls._type(type, [start[0], end[0]], numberList)
  135. for i in xTrackArray:
  136. s.append([i, fun(i)])
  137. # return {"trackArray": np.array(s), "P": w}
  138. return [[int(s[0]), int(s[1])] for s in s]
  139. # print(BezierTrajectory.trackArray(start=[0, 0], end=[100, 100], numberList=50, le=2,
  140. # deviation=0, bias=0.5, type=0, cbb=0, yhh=10))