Last active
September 19, 2019 13:32
-
-
Save Jimut123/6fcc90002d82f14f0dfa679f1c8e2f58 to your computer and use it in GitHub Desktop.
CNN AI's testing file: blog(https://jimut123.github.io/blogs/cnn_games_ai.html)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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