基于图像处理和tensorflow实现GTA5的车辆自动驾驶——第二节获取游戏窗口
代码已放到码云
直接上代码了
import numpy as np
from PIL import ImageGrab
import cv2
import time
def screen_record():
last_time = time.time()
while True:
# 800x600 windowed mode for GTA 5, at the top left position of your main screen.
# 40 px accounts for title bar.
printscreen = np.array(ImageGrab.grab(bbox=(0, 40, 800, 640)))
print('loop took {} seconds'.format(time.time() - last_time))
last_time = time.time()
cv2.imshow('window', cv2.cvtColor(printscreen, cv2.COLOR_BGR2RGB))
if cv2.waitKey(25) & 0xFF == ord('q'):
cv2.destroyAllWindows()
break
screen_record()
实现效果
需要把游戏调整窗口化分辨率为:800*600.放到屏幕左上角,当然也可以不放(需要修改代码,printscreen = np.array(ImageGrab.grab(bbox=(0, 40, 800, 640)))
这个部分)
左边为游戏界面,右边为opencv获取到的界面,帧数还是比较高的(1帧能玩,2帧流畅)