Skip to content

Commit

Permalink
High Score 144 , Need More Data and Better Algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
vigneshhari committed Jul 17, 2018
1 parent 1cd9049 commit 786445a
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 26 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# Don't track content of these folders
screens/
1/
temp/
12 changes: 8 additions & 4 deletions helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
from PIL import Image


for i in range(0,50):
im = Image.open("screenshot-{}.png".format(i)) # Can be many different formats.
pix = im.load()
print sum(pix[330,291]),
import pandas

l = [1,2,3,4]
ll = [5,6,7,8]


df = pandas.DataFrame(data={"col1": l, "col2": ll})
df.to_csv("file.csv", sep=',',index=False)
10 changes: 6 additions & 4 deletions image_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
import numpy as np

from PIL import Image

import keyboard
import cv2

def mouse_callback(event, x, y, flags, params):
global thresh_img
print x , y , thresh_img[y,x]


img = cv2.imread('screens/screenshot-213.png',0)
img = cv2.imread('screens/screenshot-22.png',0)

img = img[ 169 : 400 , 90: ]
img = img[ 168 : 400 , 105: ]

print img[119,315]

Expand All @@ -27,9 +27,11 @@ def mouse_callback(event, x, y, flags, params):

cv2.setMouseCallback('image', mouse_callback)



cv2.imshow("image" , thresh_img)


cv2.imshow("image" , thresh_img)


cv2.waitKey(0)
55 changes: 42 additions & 13 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,60 @@

import numpy as np

from PIL import Image

import keyboard
import cv2

driver = webdriver.Chrome()
import pandas

import os

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--no-sandbox')
driver = webdriver.Chrome(chrome_options=chrome_options)

driver.get("chrome://dino")
elem = driver.find_element_by_id("t")
space = 0
variable = 0

batch = 1

image = []
action = []

try:
# Create target Directory
os.mkdir(str(batch))
except :
print "Folder Exists"



while True:
driver.save_screenshot("screens/screenshot-{}.png".format(variable))
img = cv2.imread("screens/screenshot-{}.png".format(variable))
if( elem.get_attribute("class") == "offline" ):continue
if(driver.execute_script("return Runner.instance_.playing;") == False ):continue

img = img[ 169 : 400 , 90: ]
driver.save_screenshot("{}/screenshot-{}.png".format(batch,variable))
img = cv2.imread("{}/screenshot-{}.png".format(batch , variable))

ret,thresh_img = cv2.threshold(img,127,255,cv2.THRESH_BINARY)
img = img[ 168 : 400 , 105: ]

ret,thresh_img = cv2.threshold(img,127,255,cv2.THRESH_BINARY)

if(keyboard.is_pressed("space")):space = 1
if(keyboard.is_pressed("esc")):break

print thresh_img[119,291]
print variable

cv2.imwrite("screens/screenshot-{}-converted.png".format(variable), thresh_img)
cv2.imwrite("{}/train-{}.png".format(batch,variable), thresh_img)

image.append("{}/train-{}.png".format(batch,variable))
action.append(space)
space = 0

if sum(thresh_img[119,291]) == 0 :
elem.send_keys(Keys.SPACE)
print "Sent Key"
variable+=1

df = pandas.DataFrame(data={"image": image, "action": action})
df.to_csv("1/out.csv", sep=',',index=False)

driver.close()
116 changes: 111 additions & 5 deletions model.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,118 @@

import tensorflow as tf
import csv
from PIL import Image
import numpy as np
import PIL.ImageOps
import cv2
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

# First Layer
files = []
action = []

X = tf.Placeholder( size = (None , 768))
Y = tf.Placeholder( size = (None ))
with open("1/out.csv" , "r") as f :
data = csv.reader(f, delimiter=',')
for i in data:
if(i[0] == 1):
action.append(0)
else:
action.append(1)
files.append(i[1])


def parse_function(filename,basewidth):
img = Image.open(filename).convert('L')
img = PIL.ImageOps.invert(img)
wpercent = (basewidth/float(img.size[0]))
hsize = int((float(img.size[1])*float(wpercent)))
img = img.resize((basewidth,hsize), Image.ANTIALIAS)
return img

# layer 1

W1 = tf.Variable(type=tf.float32 )
x_v = [np.array(parse_function(i,200)).flatten() for i in files ]
y_v = action

print "Loaded all Images"

print "Starting Training on NN ( Tensorflow ) "

'''
Using a Neural Network with No Hidden Layers with 55 input layers with 200 values each. giving a total of 55 * 200 Values.
The weight dimentions are 200 * 1 , input is 1 * 200
Output is single neuron ( 1 - Neuron Fired -- Jump ) else ( Leave )
'''

print sum(x_v[0])

# Parameters
#learning_rate = .00000000001
learning_rate = .00000001
training_epochs = 100
batch_size = 100
display_step = 1

tf.set_random_seed(777)
x = tf.placeholder(tf.float32, [None, 11000])
y = tf.placeholder(tf.float32, [None])

W = tf.Variable(tf.random_normal([11000, 1]))
b = tf.Variable(tf.random_normal([1]))

pred = tf.nn.sigmoid(tf.matmul(x, W) + b )


#cost = tf.reduce_mean(-1 * (tf.reduce_sum(y*tf.log(pred) + tf.reduce_sum( (y-1)*tf.log(pred -1) ) )))
cost = tf.reduce_mean(-1 * (tf.reduce_sum(y*tf.log(pred + 1e-30 )) ) )

optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)


correct_prediction = tf.equal(tf.cast(tf.argmax(pred, 1),"float64"),tf.cast(y , "float64") )
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float32"))

init = tf.global_variables_initializer()

with tf.Session() as sess:
sess.run(init)

for epoch in range(training_epochs):

_, c , w = sess.run([optimizer, cost, W], feed_dict={x: x_v,y: y_v})
print sum(w)
if (epoch+1) % display_step == 0:
print("Epoch:", '%04d' % (epoch+1), "cost=", "{}".format(c))

print(sess.run([accuracy , pred] ,feed_dict={x: x_v,y: y_v }))


# Start Selenium Program

import math

driver = webdriver.Chrome()

driver.get("chrome://dino")
elem = driver.find_element_by_id("t")

variable = 1
while True:
if( elem.get_attribute("class") == "offline" ):continue
if(driver.execute_script("return Runner.instance_.playing;") == False ):continue

driver.save_screenshot("temp/screenshot-{}.png".format(variable))
img = cv2.imread("temp/screenshot-{}.png".format( variable))

img = img[ 168 : 400 , 105: ]

ret,thresh_img = cv2.threshold(img,127,255,cv2.THRESH_BINARY)
cv2.imwrite("temp/train-1.png", thresh_img)

value = parse_function("temp/train-1.png",200)

if(round(sess.run(pred , feed_dict={x : [np.array(value).flatten()]})) == 0) :
elem.send_keys(Keys.SPACE)

0 comments on commit 786445a

Please sign in to comment.