In [1]:
import sys
sys.path.append('../')
In [2]:
from europilot.screen import stream_local_game_screen
from europilot.screen import Box
from europilot.joystick import LinuxVirtualJoystick
In [1]:
%matplotlib inline
import matplotlib.pyplot as plt
import os
import numpy as np
from PIL import Image
from time import time
import keras
from keras.models import load_model
from keras.preprocessing import image
parent_path = os.path.dirname(os.getcwd())
model_path = os.path.join(parent_path, 'model')
# multiply by constant to undo normalization
OUTPUT_NORMALIZATION = 655.35
In [4]:
# limit GPU memory usage
import tensorflow as tf
from keras.backend.tensorflow_backend import set_session
config = tf.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.5
set_session(tf.Session(config=config))
load pretrained model
In [2]:
%ls $model_path
In [5]:
model = load_model(os.path.join(model_path, 'v5-PilotNet_v2-009-0.67221.h5'))
Get sample image, to find out the performance of our model
In [6]:
%ls ../sample/img/raw
In [7]:
sample_img = Image.open(os.path.join('../sample/img/raw', '9d0c3c2b_2017_07_27_14_55_08_16.jpg')).convert('RGB')
In [8]:
front_coord = (289,167, 851, 508)
sample_img_front = sample_img.crop(front_coord)
In [9]:
plt.imshow(sample_img_front)
Out[9]:
In [10]:
sample_arr = image.img_to_array(sample_img_front)
sample_arr = np.reshape(sample_arr, (1,) + sample_arr.shape)
In [13]:
model.predict(sample_arr, batch_size = 1)
start = time()
for i in range(100):
model.predict(sample_arr, batch_size = 1)
end = time()
fps = 100. / (end - start)
print("fps: %f" % fps)
Be aware that the performance of the model can affect the performance of the overall program, since it may not react quickly enough to the changing environment.
Get the coordinates for the game screen. The values may be different per game setting.
In [14]:
x1, y1 = (68, 54)
x2, y2 = (x1 + 1024, y1 + 768)
box = Box(x1, y1, x2, y2)
In [15]:
joy = LinuxVirtualJoystick()
As seen below, a virtual device can now be detected.
In [16]:
!cat /proc/bus/input/devices
Set up game loop
In [17]:
# values computed from dataset sample.
def normalize(img):
img[:,:,0] -= 89.5761
img[:,:,0] /= 58.4214
img[:,:,1] -= 97.5966
img[:,:,1] /= 61.7917
img[:,:,2] -= 88.3135
img[:,:,2] /= 68.2043
return img
def get_angle(predict):
angle = predict[0][0]
angle *= OUTPUT_NORMALIZATION
return int(angle)
In [ ]:
streamer = stream_local_game_screen(box=box, default_fps=60)
while True:
image_data = next(streamer)
im = Image.fromarray(image_data)
img_front = im.crop(front_coord)
arr = image.img_to_array(img_front)
arr = normalize(arr)
arr = np.reshape(arr, (1,) + arr.shape)
angle = get_angle(model.predict(arr, batch_size = 1))
joy.emit(angle)
In [ ]: