def play(prediction):
    with tf.Session() as sess:
        #reload trained tf model
        saver = tf.train.import_meta_graph(
            './logs/nn3_logs/canabalt_nn_100_50_20.meta')
        saver.restore(sess, tf.train.latest_checkpoint('./logs/nn3_logs'))
        sess.run(tf.global_variables_initializer())
        # initialize required variables
        start = time.time()
        dead = False
        consecutivedead = 0
        train_data = 0
        space = False
        current = np.ones(4800)
        previous1 = np.zeros(4800)
        previous2 = np.zeros(4800)
        previous3 = np.zeros(4800)
        # press enter to start the game
        print('starting to play')
        keypress.PressKey(0x1C)
        time.sleep(0.2)
        keypress.ReleaseKey(0x1C)
        while not dead:
            # grab screen and process
            screen = grab_screen(region=(0, 40, 800, 640))
            current, previous1, previous2, previous3 = process_image(
                screen), current, previous1, previous2
            savable = np.append(current, previous3)
            # prepare image for prediction
            X = savable.reshape(1, 9600)
            feed_dict = {x: X, p_input_keep: 1, p_layer_keep: 1}
            result = prediction.eval(feed_dict, session=sess)
            # check prediction for chosen action
            if result[0][1] > result[0][0]:
                if space == False:
                    keypress.PressKey(0x39)
                    space = True
                    #print('space down')
            else:
                if space == True:
                    keypress.ReleaseKey(0x39)
                    space = False
                    #print('space up')
            # check if the player has died by detecting changes in the screen. no changes == dead
            if np.array_equal(current, previous3) or np.array_equal(
                    current, previous1):
                if consecutivedead > 20:
                    print('i am dead, did i do well?')
                    dead = True
                consecutivedead += 1
            else:
                consecutivedead = 0
            # counter to later calculate fps
            train_data += 1
    # calculate run time and derivatives, and print them
    run_time = time.time() - start
    print('run took {} seconds, for {} screens, for {} seconds per screen'.
          format(run_time, train_data, (run_time / train_data)))
    #    return train_data[200:-100], run_time
    return None, run_time
def play(sess,screenloc,death):
    # initialize required variables and reset pressed buttons
    start = time.time()
    dead = False
    train_data = 0
    keypress.ReleaseKey(0x39)
    space = False   
    current = np.ones((60,80))
    previous1 = np.zeros((60,80))
    previous2 = np.zeros((60,80))
    previous3 = np.zeros((60,80))
    presult = [[0,0,0]]
    #restore the required placeholders for the feed dict
    graph = tf.get_default_graph()
    prediction = graph.get_tensor_by_name("hidden_4_dense/Relu:0")
    x = graph.get_tensor_by_name("input_data:0")
    x2 = graph.get_tensor_by_name("input_data2:0")
    layer_keep_holder =  graph.get_tensor_by_name("input_keep:0")
    # press enter to start the game
    print('starting to play')
    keypress.PressKey(0x1C)
    time.sleep(0.2)
    keypress.ReleaseKey(0x1C)
    time.sleep(0.2)
    while not dead:
        # grab screen and process             
        screen = grab_screen(region =screenloc)
        if np.array_equal(screen[:][250:280],death):
            dead = True
        current, previous1, previous2, previous3 = process_image(screen), current, previous1, previous2
        savable = np.stack([current,previous3],2)
        X = np.reshape(savable,(-1,60,80,2))
        # prepare image for prediction          
        feed_dict={x:X, x2:presult, layer_keep_holder: 1}
        result = prediction.eval(feed_dict, session=sess)
        # check prediction for chosen action
        if result[0][1] > result[0][0] :
            presult = [[1] + presult[0][0:2]]
            if space == False:
                keypress.PressKey(0x39)
                space = True
                
                #print('space down')
        else:
            presult = [[0] + presult[0][0:2]]
            if space == True:
                keypress.ReleaseKey(0x39) 
                space = False
                #print('space up')
        # counter to later calculate fps       
        train_data += 1
    # calculate run time and derivatives, and print them            
    run_time = time.time()-start       
    print('run took {} seconds, for {} screens, for {} seconds per screen'.format(run_time, train_data, (run_time/train_data)))
#    return train_data[200:-100], run_time
    return run_time
def play(estimator, deadcheck):  
    start = time.time()
    dead = False
    consecutivedead = 0
    train_data = 0
    space = False   
    current = np.zeros(4800)
    previous1 = np.zeros(4800)
    previous2 = np.zeros(4800)
    previous3 = np.zeros(4800)
    
    
    print('starting to play')
    keypress.PressKey(0x1C)
    time.sleep(0.2)
    keypress.ReleaseKey(0x1C)
    while not dead:
               
        screen = grab_screen(region =(0,40,800,640))
        current, previous1, previous2, previous3 = process_image(screen), current, previous1, previous2
        savable = np.append(current,previous3)
        X = savable.reshape(1,-1)
        
        feed_dict={"x": X}
        result = estimator.predict(feed_dict)
        
        if train_data == 0:
            print(result)
        if result == 1 : 
            if space == False:
                keypress.PressKey(0x39)
                space = True
                #print('space down')
        else:
            if space == True:
                keypress.ReleaseKey(0x39) 
                space = False
                #print('space up')
               
        train_data += 1   
        if (train_data % 1000) == 0:
            print((time.time()-start)/1000)
            start = time.time()                   
        
    run_time = time.time()-start       
        
    print('run took {} seconds, for {} screens, for {} seconds per screen'.format(run_time,
                                                                                  train_data,
                                                                                  (run_time/train_data)))
#    return train_data[200:-100], run_time
    return None, run_time
Esempio n. 4
0
def play(sess, screenloc, death, model):
    # initialize required variables and reset pressed buttons
    start = time.time()
    dead = False
    train_data = 0
    keypress.ReleaseKey(0x39)
    space = False
    current = np.ones((60, 80))
    previous1 = np.zeros((60, 80))
    previous2 = np.zeros((60, 80))
    previous3 = np.zeros((60, 80))
    presult = [[0, 0, 0]]
    # press enter to start the game
    print('starting to play')
    keypress.PressKey(0x1C)
    time.sleep(0.2)
    keypress.ReleaseKey(0x1C)
    time.sleep(0.2)
    while not dead:
        # grab screen and process
        screen = grab_screen(region=screenloc)
        if np.array_equal(screen[:][250:280], death):
            dead = True
        current, previous1, previous2, previous3 = process_image(
            screen), current, previous1, previous2
        savable = np.stack([current, previous3], 2)
        X = np.reshape(savable, (-1, 60, 80, 2))
        # prepare image for prediction
        result = model.predict(sess, X, presult)
        # check prediction for chosen action
        if result[0][1] > result[0][0]:
            presult = [[1] + presult[0][0:2]]
            if space == False:
                keypress.PressKey(0x39)
                space = True

                #print('space down')
        else:
            presult = [[0] + presult[0][0:2]]
            if space == True:
                keypress.ReleaseKey(0x39)
                space = False
                #print('space up')
        # counter to later calculate fps
        train_data += 1
    # calculate run time and derivatives, and print them
    run_time = time.time() - start
    print('run took {} seconds, for {} screens, for {} seconds per screen'.
          format(run_time, train_data, (run_time / train_data)))
    #    return train_data[200:-100], run_time
    return run_time
Esempio n. 5
0
def save_file(filename):
    time.sleep(1)

    keypress.PressKey(0x12)  #alt
    keypress.ReleaseKey(0x12)  #~alt #click file

    keypress.PressKey(0x28)  #down
    keypress.ReleaseKey(0x28)

    keypress.PressKey(0x28)  #down
    keypress.ReleaseKey(0x28)

    keypress.PressKey(0x27)  #right
    keypress.ReleaseKey(0x27)

    keypress.PressKey(0x28)  #down
    keypress.ReleaseKey(0x28)

    keypress.PressKey(0x0D)  #enter
    keypress.ReleaseKey(0x0D)  #enter

    #time.sleep(2)

    keypress.Crtla()  #select all

    time.sleep(0.2)

    keypress.type(filename + ".txt")  #type in filename

    time.sleep(0.5)

    keypress.PressKey(0x0D)  #enter
    keypress.ReleaseKey(0x0D)  #enter
def play(screenloc,death):
    
    #initialize variables
    start = time.time()
    dead = False
    train_data = []
    savable = np.ones(4800)
    previous1 = np.zeros(4800)
    previous2 = np.zeros(4800)
    previous3 = np.zeros(4800)
    
    #press enter key to start the game
    print('starting to play')
    keypress.PressKey(0x1C)
    time.sleep(0.1)
    keypress.ReleaseKey(0x1C)
    time.sleep(0.2)
    
    while not dead:
        screen = grab_screen(region = screenloc)
        if np.array_equal(screen[:][250:280],death):
            dead = True
        savable, previous1, previous2, previous3 = process_image(screen), savable, previous1, previous2
        
        #savable = process_image(screen)        
        #X = savable.reshape(1,-1)       
        
        keys = key_check()
        output = keys_to_output(keys)
        train_data.append([savable, output])                               
      
#        cv2.imshow('processed view', savable)
#        if cv2.waitKey(25) & 0xFF == ord('q'):
#            cv2.destroyAllWindows()
#            break            
    run_time = time.time()-start        
    print('run took {} seconds, for {} screens, for {} seconds per screen'.format(run_time,
                                                                                  len(train_data),
                                                                                  (run_time/len(train_data))))
    # cut of the last 250 screens as that playing resulted in death, and should not be learned by the network
    return train_data[:-250], run_time
Esempio n. 7
0
def main():
    with open('iternumber.pickle','rb') as f:
        iternumber = pickle.load(f) 
        iternumber = 2
    
#    if os.path.isfile(file_name):
#        print('file exists. loading data')
#        training_data = list(np.load(file_name))
#    else:
#        print('file does not exist. creating new file')
#        training_data = []   
    print('opening: MLPtrained_{}.pickle'.format(iternumber - 1))
    with open('MLPtrained_{}.pickle'.format(iternumber - 1 ),'rb') as f:
        mlp = pickle.load(f)    
    with open('MLPtrained_dead.pickle','rb') as f:
        deadcheck = pickle.load(f)
        
    if os.path.isfile('run_times_{}.npy'.format(iternumber -1)):
        print('run times exists. loading data')
        run_times = list(np.load('run_times_{}.npy '.format(iternumber-1)))
    else:
        run_times = []
#        
#    startdata = len(training_data)
#    startdata2 = startdata

    for i in range(4,0,-1):
        print(i)
        time.sleep(1)
        
        
    while True:   
        print('starting to play')
        keypress.PressKey(0x1C)
        time.sleep(0.2)
        keypress.ReleaseKey(0x1C)
    
        run_data, run_time = play(mlp,deadcheck)
        run_times.append(run_time)
        np.save('run_times_{}.npy'.format(iternumber-1), run_times)
Esempio n. 8
0
def play(mlp, deadcheck):
    start = time.time()
    dead = False
    consecutivedead = 0

## not saving the training data gives significant speed inmprovements. 
## replaced by a counter under the same name to preserve speed reports
#    train_data = []
    train_data = 0
    space = False
#    roundtime = time.time()
    
    current = np.zeros(4800)
    previous1 = np.zeros(4800)
    previous2 = np.zeros(4800)
    previous3 = np.zeros(4800)    

    while not dead:
               
        screen = grab_screen(region =(0,40,800,640))
        current, previous1, previous2, previous3 = process_image(screen), current, previous1, previous2
        savable = np.append(current,previous3)
        X = savable.reshape(1,-1)
        
        button = mlp.predict(X)
        
        if button == 1: 
            if space == False:
                keypress.PressKey(0x39)
                space = True
                print('space down')
        else:
            if space == True:
                keypress.ReleaseKey(0x39) 
                space = False
                print('space up')
            
#        train_data.append([savable, space])   
        train_data += 1              
        
        Xdead = current.reshape(1,-1)
        died = deadcheck.predict(Xdead)        
        if died == 1:
            consecutivedead += 1
            if consecutivedead > 50:
                dead = True
        if died == 0:
            consecutivedead = 0
        
#        timediff = time.time()-roundtime
#        if timediff < 0.023: 
#            time.sleep(0.01)
#            roundtime=time.time() 
        
    run_time = time.time()-start       
        
    print('run took {} seconds, for {} screens, for {} seconds per screen'.format(run_time,
                                                                                  train_data,
                                                                                  (run_time/train_data)))
#    return train_data[200:-100], run_time
    return None, run_time
Esempio n. 9
0
        audiobuffer = stream.read(buffer_size)
        signal = np.fromstring(audiobuffer, dtype=np.float32)

        pitch = pitch_o(signal)[0]
        confidence = pitch_o.get_confidence()
        window.makeActive()
        print("{} / {}".format(pitch, confidence))
        #print("{} / {}".format(pitch,confidence))
        for tone in pitchdict:
            if (abs((round(pitch) - tone)) < 1):
                print(pitch, tone)
                if (pitchdict[tone] == e):
                    epress = epress + 1
                    print(epress, epress % 100)
                    if (epress % 10 == 9):
                        keypress.PressKey(pitchdict[tone])
                        time.sleep(0.05)
                        keypress.ReleaseKey(pitchdict[tone])
                    break
                keypress.PressKey(pitchdict[tone])
                time.sleep(0.05)
                keypress.ReleaseKey(pitchdict[tone])
        if (round(pitch) == F2):
            keypress.moveMouse(-10, 0)
        if (round(pitch) == G2):
            keypress.moveMouse(0, 10)
        if (round(pitch) == A2):
            keypress.moveMouse(0, -10)
        if (round(pitch) == B2):
            keypress.moveMouse(10, 0)
        if (round(pitch) == leftClick):