Skip to content

Commit

Permalink
fix & re-run
Browse files Browse the repository at this point in the history
  • Loading branch information
ghaiszaher committed Mar 24, 2019
1 parent 1cdebc1 commit fd4f1ed
Show file tree
Hide file tree
Showing 3 changed files with 415 additions and 310 deletions.
70 changes: 35 additions & 35 deletions generate_X_y.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -19,28 +19,31 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"include_previous_frame = False\n",
"ignore_zero_output = False\n",
"frames_ahead = 0\n",
"previous_outputs = 5"
"previous_outputs = 0"
]
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['./gameplay_logs/1553422630.470564', './gameplay_logs/1553422654.9455032']"
"['./gameplay_logs/1553448946.270552',\n",
" './gameplay_logs/1553448965.3654816',\n",
" './gameplay_logs/1553449015.2570503',\n",
" './gameplay_logs/1553449071.8743048']"
]
},
"execution_count": 4,
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -53,7 +56,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 17,
"metadata": {
"scrolled": true
},
Expand All @@ -62,10 +65,14 @@
"name": "stdout",
"output_type": "stream",
"text": [
"processing folder ./gameplay_logs/1553422630.470564 ...\n",
"Done with folder ./gameplay_logs/1553422630.470564\n",
"processing folder ./gameplay_logs/1553422654.9455032 ...\n",
"Done with folder ./gameplay_logs/1553422654.9455032\n",
"processing folder ./gameplay_logs/1553448946.270552 ...\n",
"Done with folder ./gameplay_logs/1553448946.270552\n",
"processing folder ./gameplay_logs/1553448965.3654816 ...\n",
"Done with folder ./gameplay_logs/1553448965.3654816\n",
"processing folder ./gameplay_logs/1553449015.2570503 ...\n",
"Done with folder ./gameplay_logs/1553449015.2570503\n",
"processing folder ./gameplay_logs/1553449071.8743048 ...\n",
"Done with folder ./gameplay_logs/1553449071.8743048\n",
"Done generating!\n"
]
}
Expand All @@ -86,14 +93,11 @@
" X = None\n",
" y = None\n",
"\n",
"# i=-1\n",
" limit = len(game_info['images'])-45 #Skip last 45 frames (after loss)\n",
" previous_features = None\n",
"# for image_name in game_info['images']:\n",
" keys = list(game_info['images'].keys())\n",
" for i in range(len(keys)):\n",
" image_name = keys[i]\n",
"# prev_image_name = keys[i-1] if i > 0 else None\n",
" \n",
" if i>=limit:\n",
" break;\n",
Expand Down Expand Up @@ -127,11 +131,6 @@
" while j < previous_outputs and (i-j-1)>=0:\n",
" prev_x_drags[j] = game_info[\"images\"][keys[(i-j-1)]][\"prev_drag_x\"]\n",
" j+=1\n",
"# game_info[\"images\"][image_name][\"prev_drag_x\"]\n",
"# if prev_image_name is None:\n",
"# prev_x_drag = np.atleast_2d(0)\n",
"# else:\n",
"# prev_x_drag = np.atleast_2d(game_info[\"images\"][prev_image_name][\"prev_drag_x\"])\n",
" to_add = np.append(to_add, np.atleast_2d(prev_x_drags), axis=1)\n",
" \n",
" if X is None:\n",
Expand All @@ -152,37 +151,38 @@
" np.savez(f, X=X, y=y)\n",
" \n",
" print(\"Done with folder \"+folder)\n",
"# break\n",
" \n",
"print(\"Done generating!\")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0 0 0 0 0] [0 0 0 0 0]\n",
"[[0]\n",
" [0]\n",
" [0]\n",
" [0]\n",
" [0]]\n"
"[1 0 0 0 0] [1 0 0 0 0]\n",
"[[ 8]\n",
" [10]\n",
" [ 6]\n",
" [ 3]\n",
" [ 2]]\n",
"(1285, 1278)\n"
]
}
],
"source": [
"print(X[5,-5:], X[6,-5:])\n",
"print(y[0:5])"
"print(y[0:5])\n",
"print(X.shape)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 19,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -218,16 +218,16 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(1570, 1283)"
"(3677, 1278)"
]
},
"execution_count": 8,
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -238,16 +238,16 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(1570, 1)"
"(3677, 1)"
]
},
"execution_count": 9,
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
Expand Down
109 changes: 107 additions & 2 deletions run.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,108 @@
from control import jumpy_control
#!/usr/bin/env python
# encoding: utf-8

jumpy_control.test_control()
from control.jumpy_control import Jumpy, JumpyHelper
import cv2
from pynput import mouse
from pynput.mouse import Button
from threading import Thread
import time
import json
import os
import numpy as np
from keras.models import Sequential
import queue


def main():
folder = "./gameplay_logs"
with open(f"{folder}/nn_model_config.json", "r") as f:
config = json.load(f)

classifier = Sequential.from_config(config)
classifier.load_weights(f"{folder}/nn_weights.hdf5")
print(classifier.input_shape[1])
j = Jumpy()
recording, draw_positions, draw_prediction = 0, 1, 1
previous_features = None
previous_outputs = 0
i = 0
prev_x_drags = [0] * previous_outputs
q = queue.Queue()

while 1:
img = j.next_rgb()
helper = JumpyHelper(img)
if helper.is_playing_screen():

if draw_positions:
positions = helper.get_classified_positions()
for (x, y, type) in positions:
color = (255, 255, 255)
if type == "ball":
color = (255, 0, 0)
if type == "land":
color = (0, 255, 0)
if type == "dangerous":
color = (0, 0, 255)
cv2.circle(img, (x, y), 4, color, 2)

features = np.atleast_2d(helper.get_features())
to_predict = features
print("features shape", features.shape)
if previous_outputs and \
classifier.input_shape[1] == to_predict.shape[1] + previous_outputs:
to_predict = np.append(
to_predict, np.atleast_2d(prev_x_drags), axis=1)
if classifier.input_shape[1] > to_predict.shape[1]:
if previous_features is None:
previous_features = np.zeros_like(features)
to_predict = np.append(features, previous_features, axis=1)
y = classifier.predict(to_predict)
y = round(y[0, 0])
print(y)

if draw_prediction:
img = cv2.putText(img, "{}".format(y),
(50, img.shape[0]-60),
cv2.FONT_HERSHEY_DUPLEX, 0.7,
(0, 0, 0), 2, cv2.LINE_AA)

j.drag_distance(y)
previous_features = features

if previous_outputs:
del prev_x_drags[-1]
prev_x_drags = [y] + prev_x_drags

if recording:
q.put(img)

else:
previous_features = None
if previous_outputs:
prev_x_drags = [0] * previous_outputs
while not q.empty():
i += 1
item = q.get()
cv2.imwrite("images/gameplay_{}.png".format(i), item)

if helper.is_continue_screen():
j.click_no_thanks_button()
elif helper.is_main_menu_screen():
j.click_play_single_button()
elif helper.is_screboard_screen():
j.click_back_button()

cv2.imshow('jumpy', img)
k = cv2.waitKey(10) & 0xFF
if k == ord('q'):
break
if k == ord('p'):
x = helper.get_features()
print(x)


###################
if __name__ == "__main__":
main()
Loading

0 comments on commit fd4f1ed

Please sign in to comment.