python - 如何在Python中记录xbox/游戏 handle Controller 的状态?

标签 python input xbox gamepad

我需要知道在特定时间Xbox Controller 的所有按钮的值。原因是我正在为神经网络构建训练集,并且我试图同时拍摄屏幕快照并拍摄 Controller 状态的“快照”。请注意,我能够成功地对该项目的键盘版本执行此操作,但 xbox Controller 给我带来了困难。

我尝试的是创建按钮和值的字典,并在每次收到来自 Controller 的事件时更新字典。然后我将图像和字典保存为训练数据的实例。然而,输入最终与图像根本不同步。我认为该问题可能与用于读取 Controller 的包之一中的线程或子进程有关,但我不够熟练,不知道如何修复它。

下面是我的代码。

from inputs import get_gamepad
import time
import cv2
import numpy as np
from mss.windows import MSS as mss

#Only track relevant inputs
gp_state = {#'ABS_HAT0X' : 0, #-1 to 1
             #'ABS_HAT0Y' : 0, #-1 to 1
             #'ABS_RX' : 0, #-32768 to 32767
             #'ABS_RY' : 0, #-32768 to 32767
             'ABS_RZ' : 0, #0 to 255
             'ABS_X' : 0, #-32768 to 32767
             'ABS_Y' : 0, #-32768 to 32767
             #'ABS_Z' : 0, #0 to 255
             'BTN_EAST' : 0,
             'BTN_NORTH' : 0,
             #'BTN_SELECT' : 0,
             'BTN_SOUTH' : 0,
             #'BTN_START' : 0,
             #'BTN_THUMBL' : 0,
             #'BTN_THUMBR' : 0,
             'BTN_TL' : 0,
             'BTN_TR' : 0,
             'BTN_WEST' : 0,
             #'SYN_REPORT' : 0,
             }

dead_zone = 7500

def screen_record(): 
    last_time = time.time()
    while(True):
        # 800x600 windowed mode
        printscreen =  np.array(ImageGrab.grab(bbox=(0,40,800,640)))
        last_time = time.time()
        cv2.imshow('window',cv2.cvtColor(printscreen, cv2.COLOR_BGR2RGB))
        if cv2.waitKey(25) & 0xFF == ord('q'):
            cv2.destroyAllWindows()
            break

def process_img(image):
    original_image = image
    processed_img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    contrast = 1
    brightness = 0
    out = cv2.addWeighted(processed_img, contrast, processed_img, 0, brightness)
    return out

def main():

    #Give myself time to switch windows
    #Screen should be in top left
    for _ in range(4):
        time.sleep(1)

    controller_input = np.zeros(5)
    training_data = []
    training_files = 0

    with mss() as sct:
        while True:
            #Get screen and display
            bbox = (150,240,650,490)
            screen =  np.array(sct.grab(bbox))
            new_screen = process_img(screen)
            cv2.imshow('window', new_screen)
            new_screen = cv2.resize(new_screen, (100,50))

            #Map events to dictionary
            events = get_gamepad()
            for event in events:
                gp_state[event.code] = event.state

            #Set to zero if in dead zone
            if abs(gp_state['ABS_X']) < dead_zone:
                gp_state['ABS_X'] = 0 

            if abs(gp_state['ABS_Y']) < dead_zone:
                gp_state['ABS_Y'] = 0 

            #Set values to be between 0 and 1.
            controller_input[0] = (gp_state['ABS_X'] + 32768) / (32767 + 32768)
            controller_input[1] = gp_state['ABS_RZ'] / 255
            controller_input[2] = gp_state['BTN_SOUTH']
            controller_input[3] = gp_state['BTN_EAST']
            controller_input[4] = gp_state['BTN_TR']


            record = gp_state['BTN_NORTH'] #Record while holding y button
            if record:
                training_data.append(np.array([new_screen, controller_input]))
                print(controller_input)
                time.sleep(1)

            if len(training_data) % 500 == 0 and record:
                filename = f"training_data/rlb_XBOXtrain_{time.time()}.npy"
                np.save(filename, training_data)
                training_files += 1
                print(f"Trained {training_files} files!")
                training_data = []


            if cv2.waitKey(25) & 0xFF == ord('q'):
                cv2.destroyAllWindows()
                break

main()

我觉得我让这条路变得比需要的更加困难。但是有没有更简单的方法来获取 Controller 在某个时间点的状态?

请注意,我找到了一些适用于 Linux 的解决方案,但我在 Windows 10 中运行。以下是 Linux 解决方案的示例: https://github.com/FRC4564/Xbox

最佳答案

TensorKart 项目已经解决了这个问题:https://github.com/kevinhughes27/TensorKart/blob/master/utils.py

关于python - 如何在Python中记录xbox/游戏 handle Controller 的状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53803729/

相关文章:

C++:在空格字符后获取字符而不是返回回车

api - 访问 xbox 音乐 API

python - python中字典中的变量

python - numpy 数组列出转换问题

java - 如何通过System.in获取输入?

javascript - 输入框乘以2的结果出现在窗口中而不是div中

augmented-reality - 是否可以在没有 Xbox 360 的情况下开发 Kinect 传感器?

xbox360 - 获取用户 Xbox 成就列表?

python - 如何为所有模型添加相同的保存方法

python - 配置 : error: Python interpreter is too old