【问题标题】:Make mouse movements follow windmouse algorithm使鼠标移动遵循风鼠算法
【发布时间】:2022-10-24 13:09:19
【问题描述】:

不确定我是否在问正确的问题,但是否有可能让 python 使用 pyautogui/selenium 动作/或者最好是遵循此算法的所有鼠标移动来移动我的鼠标?对于我想要制作的机器人,我想让我的鼠标移动尽可能真实。如果可能的话,我将如何去做?我在这里找到了算法:https://web.archive.org/web/20210621155859/https://ben.land/post/2021/04/25/windmouse-human-mouse-movement/

import numpy as np
sqrt3 = np.sqrt(3)
sqrt5 = np.sqrt(5)

def wind_mouse(start_x, start_y, dest_x, dest_y, G_0=9, W_0=3, M_0=15, D_0=12, move_mouse=lambda x,y: None):
    '''
    WindMouse algorithm. Calls the move_mouse kwarg with each new step.
    Released under the terms of the GPLv3 license.
    G_0 - magnitude of the gravitational fornce
    W_0 - magnitude of the wind force fluctuations
    M_0 - maximum step size (velocity clip threshold)
    D_0 - distance where wind behavior changes from random to damped
    '''
    current_x,current_y = start_x,start_y
    v_x = v_y = W_x = W_y = 0
    while (dist:=np.hypot(dest_x-start_x,dest_y-start_y)) >= 1:
        W_mag = min(W_0, dist)
        if dist >= D_0:
            W_x = W_x/sqrt3 + (2*np.random.random()-1)*W_mag/sqrt5
            W_y = W_y/sqrt3 + (2*np.random.random()-1)*W_mag/sqrt5
        else:
            W_x /= sqrt3
            W_y /= sqrt3
            if M_0 < 3:
                M_0 = np.random.random()*3 + 3
            else:
                M_0 /= sqrt5
        v_x += W_x + G_0*(dest_x-start_x)/dist
        v_y += W_y + G_0*(dest_y-start_y)/dist
        v_mag = np.hypot(v_x, v_y)
        if v_mag > M_0:
            v_clip = M_0/2 + np.random.random()*M_0/2
            v_x = (v_x/v_mag) * v_clip
            v_y = (v_y/v_mag) * v_clip
        start_x += v_x
        start_y += v_y
        move_x = int(np.round(start_x))
        move_y = int(np.round(start_y))
        if current_x != move_x or current_y != move_y:
            #This should wait for the mouse polling interval
            move_mouse(current_x:=move_x,current_y:=move_y)
    return current_x,current_y

【问题讨论】:

    标签: python selenium pyautogui


    【解决方案1】:

    您找到的算法将使人类像鼠标一样移动,而不是在屏幕上物理移动鼠标,它只会帮助您在屏幕上绘制移动。

    为了帮助您查看情节,您可以在作者/程序员使用 Python matplotlib 的同一博客中查看。

    import matplotlib.pyplot as plt
    fig = plt.figure(figsize=[13,13])
    plt.axis('off')
    for y in np.linspace(-200,200,25):
        points = []
        wind_mouse(0,y,500,y,move_mouse=lambda x,y: points.append([x,y]))
        points = np.asarray(points)
        plt.plot(*points.T)
    plt.xlim(-50,550)
    plt.ylim(-250,250)
    

    这段代码只是缺少一个 plt.show() ,它将在您的屏幕上显示该图。只需添加,

    plt.show()
    

    最后,你很高兴。

    现在对于第二部分,如何获得像鼠标一样的物理人类运动。为此,您可以使用 Python 包 pyHM。要安装,只需在 shell 中运行此命令

    pip install pyHM
    

    然后编写此脚本,以在屏幕上物理移动鼠标。

    from pyHM import mouse
    destination_x = 1000
    destination_y = 500
    mouse.move(destination_x, destination_y)
    

    您可能会收到 scipy.random.randit 的错误,如果您收到此错误,请告诉我,我也会帮助您解决这个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-31
      • 1970-01-01
      • 1970-01-01
      • 2011-09-29
      • 1970-01-01
      相关资源
      最近更新 更多