gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 01:21:22 24 4
gpt4 key购买 nike

我需要知道在特定时间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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com