Skip to content

Instantly share code, notes, and snippets.

@Jimut123
Last active September 19, 2019 13:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jimut123/6fcc90002d82f14f0dfa679f1c8e2f58 to your computer and use it in GitHub Desktop.
Save Jimut123/6fcc90002d82f14f0dfa679f1c8e2f58 to your computer and use it in GitHub Desktop.
from keras.models import load_model
import numpy as np
import cv2
import time
import pyscreenshot as ImageGrab
import pyautogui
#loading the model
model = load_model('Pong_Thu May 2 08_33_34 2019.h5')
def main():
for i in list(range(8))[::-1]:
print(i+1)
time.sleep(1)
while(True):
# 640x480 windowed mode
screen=ImageGrab.grab(bbox=(390,290,990,695))
screen = cv2.cvtColor(np.float32(screen), cv2.COLOR_BGR2GRAY)
screen = cv2.resize(screen,(64,64))
screen = screen[np.newaxis,:,:,np.newaxis]
screen = np.array(screen)
# model prediction
y_prob = model.predict(screen)
prediction = y_prob.argmax(axis=-1)
if prediction == 1:
# up
pyautogui.keyUp('down')
pyautogui.keyDown('up')
print('Up')
elif prediction == 0:
pyautogui.keyUp('down')
pyautogui.keyUp('up')
print('Nothing')
# do nothing
elif prediction == 2:
pyautogui.keyUp('up')
time.sleep(.5)
pyautogui.keyDown('down')
print('Down')
# down
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment