def generate_data(self): """生成测试数据""" print("请移动鼠标:") # 提示用户移动鼠标 data = [] x1, y1 = pyautogui.position() last_pos = [x1, y1] for i in range(self.num_samples): # 获取鼠标当前坐标 x, y = pyautogui.position() pos = [x, y]

        # 判断鼠标是否移动,如果没有移动则不记录数据
        if last_pos == pos:
            continue

        # 计算鼠标移动距离
        dx = x - last_pos[x1]
        dy = y - last_pos[y1]
        diff_time = random.randint(50, 200)  # 以毫秒为单位
        v = (dx ** 2 + dy ** 2) ** 0.5 / diff_time
        dv = v  # 假设初始速度为0
        t, dt = random.random() * 10, diff_time / 1000  # ms -> s

        # 计算水平方向上的特征
        rollback = 0
        zeros = 0
        points = 1
        rollback_points = 0

        # 计算竖直方向上的特征
        change = 1
        zero_dy = 0
        time_diff = 0

        if dx == 0:
            zeros += 1
        elif dx < 0:
            rollback = 1

        if dy == 0:
            zero_dy += 1
        elif dy < 0:
            change += 1

        if abs(dy) > 1:
            points += 1

        if rollback == 1 and dy != 0:
            rollback_points += 1

        id = i

        # 假设有20%的机器生成的轨迹
        is_machine = random.choice([True] * 20 + [False] * 80)
        label = 1 if is_machine else 0

        # 随机增加回撤操作
        if not is_machine:
            if random.choice([True] * 5 + [False] * 95):
                x, y = data[i - 1][3], data[i - 1][5]
                label = 2  # 回撤轨迹的label为2
                dv = 0  # 回撤过程中没有速度变化

        # 记录在一个列表中
        data.append([dx, change, dy, x, label, y, time_diff,
                     1 if label == 2 else 0, points, rollback_points,
                     v, dv, t, dt, zeros, zero_dy, id, label])
        sys.stdout.write('*')  # 显示正在收集数据的进度
        time.sleep(0.05)  # 休眠50毫秒,使得显示更加连续

    print("\n鼠标轨迹收集完成!")
    return dat
def generate_dataself 生成测试数据 print请移动鼠标: # 提示用户移动鼠标 data = x1 y1 = pyautoguiposition last_pos = x1 y1 for i in rangeselfnum_samples # 获取鼠标当前坐标

原文地址: https://www.cveoy.top/t/topic/ezLE 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录