我正在尝试玩游戏时获得输入(确切地说是《魔兽争霸III》)。由于它是RTS游戏,因此在短时间内需要大量输入。我正在尝试使用pyWinhook来获取输入,使用mss来获取屏幕截图,并使用cv进行图像处理。
我试图在获取输入后立即禁用图像处理,以减少脚本的工作量。尽管如此,它并没有太大的区别。我还尝试减少游戏中的图形细节-也没有区别。
我怀疑线程会由于大量输入而节流,而不是使用的硬件不是很好(我在具有4GB RAM的Lenovo X240上工作)。
import os
import cv2
import mss
import numpy as np
import pyWinhook as pyHook
import pythoncom
def main():
# file names for training data arrays
file_name = 'training_data.npy'
copy_file_name = 'training_data_copy.npy'
# deciding if previous file with data is saved. If yes, it is opened. If not it's created
if os.path.isfile(file_name):
print('File exists, loading previous data!')
print(os.path.realpath(file_name))
training_data = list(np.load(file_name, allow_pickle=True))
np.save(copy_file_name, training_data)
else:
print('File does not exist, starting fresh!')
training_data = []
# getting screenshot and processing it for optimal machine learning processing
def get_screen():
with mss.mss() as sct:
screen = np.array(sct.grab((0, 0, 1366, 768)))
screen = cv2.cvtColor(screen, cv2.COLOR_BGR2GRAY)
screen = cv2.resize(screen, (136, 76))
return screen
# saving data after acquiring 2500 sets of inputs and screenshots
def save_data(screen, output):
training_data.append([screen, output])
if len(training_data) % 2500 == 0:
print(len(training_data))
np.save(file_name, training_data)
print("Frames taken: " + str(len(training_data)))
# getting inputs and screen on mouse event
def OnMouseEvent(event):
screen = get_screen()
action = [event.Position, event.Wheel]
output = action
save_data(screen, output)
return True
# getting inputs and screen on keyboard event
def OnKeyboardEvent(event):
screen = get_screen()
output = event.Key
save_data(screen, output)
return True
# create a hook manager
hm = pyHook.HookManager()
# watch for all mouse events
hm.MouseAll = OnMouseEvent
hm.KeyUp = OnKeyboardEvent
# set the hook
hm.HookMouse()
hm.HookKeyboard()
# wait forever
try:
pythoncom.PumpMessages()
except KeyboardInterrupt:
pass
# looping getting data
while True:
pass
if __name__ == '__main__':
main()
我想尽可能地优化获取输入,因为目前游戏中的输入是滞后的(不适当,因为最佳输入是机器学习模块的基础)。
我完全意识到,由于硬件速度慢和每分钟的输入量过大,这可能是一个无法解决的问题,但无论如何我还是想尝试。