def main(): for i in list(range(4))[::-1]: print(i+1) time.sleep(1) paused = False while(True): if not paused: # 800x600 windowed mode screen = grab_screen(region=(0,40,800,640)) last_time = time.time() screen = cv2.cvtColor(screen, cv2.COLOR_BGR2GRAY) screen = cv2.resize(screen, (160,120)) # resize to something a bit more acceptable for a CNN keys = key_check() output = keys_to_output(keys) training_data.append([screen,output]) if len(training_data) % 1000 == 0: print(len(training_data)) np.save(file_name,training_data) keys = key_check() if 'T' in keys: if paused: paused = False print('unpaused!') time.sleep(1) else: print('Pausing!') paused = True time.sleep(1)
def main(file_name, starting_value): file_name = file_name starting_value = starting_value training_data = [] for i in list(range(4))[::-1]: print(i+1) time.sleep(1) last_time = time.time() paused = False print('STARTING!!!') while(True): if not paused: # windowed mode, this is 1920x1080, but you can change this to suit whatever res you're running. screen = grab_screen(region=(0,40,1920,1120)) last_time = time.time() # resize to something a bit more acceptable for a CNN screen = cv2.resize(screen, (480,270)) # run a color convert: screen = cv2.cvtColor(screen, cv2.COLOR_BGR2RGB) keys = key_check() output = keys_to_output(keys) training_data.append([screen,output]) #print('loop took {} seconds'.format(time.time()-last_time)) last_time = time.time() ## cv2.imshow('window',cv2.resize(screen,(640,360))) ## if cv2.waitKey(25) & 0xFF == ord('q'): ## cv2.destroyAllWindows() ## break if len(training_data) % 100 == 0: print(len(training_data)) if len(training_data) == 500: np.save(file_name,training_data) print('SAVED') training_data = [] starting_value += 1 file_name = 'training_data-{}.npy'.format(starting_value) keys = key_check() if 'T' in keys: if paused: paused = False print('unpaused!') time.sleep(1) else: print('Pausing!') paused = True time.sleep(1)
def main(): for i in list(range(4))[::-1]: print(i+1) time.sleep(1) last_time = time.time() paused = False print('STARTING!!!') while(True): if not paused: # 800x600 windowed mode screen = grab_screen(region=(0,40,1920,1120)) last_time = time.time() # resize to something a bit more acceptable for a CNN screen = cv2.resize(screen, (160,90)) # run a color convert: screen = cv2.cvtColor(screen, cv2.COLOR_BGR2RGB) keys = key_check() output = keys_to_output(keys) training_data.append([screen,output]) #print('loop took {} seconds'.format(time.time()-last_time)) last_time = time.time() ## cv2.imshow('window',cv2.resize(screen,(640,360))) ## if cv2.waitKey(25) & 0xFF == ord('q'): ## cv2.destroyAllWindows() ## break if len(training_data) % 1000 == 0: print(len(training_data)) if len(training_data) == 4000: np.save(file_name,training_data) for i in range(25): print('DONE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!') break keys = key_check() if 'T' in keys: if paused: paused = False print('unpaused!') time.sleep(1) else: print('Pausing!') paused = True time.sleep(1)
def main(): last_time = time.time() for i in list(range(4))[::-1]: print(i+1) time.sleep(1) paused = False while(True): if not paused: # 800x600 windowed mode screen = grab_screen(region=(0,40,800,640)) print('loop took {} seconds'.format(time.time()-last_time)) last_time = time.time() screen = cv2.cvtColor(screen, cv2.COLOR_BGR2GRAY) screen = cv2.resize(screen, (160,120)) prediction = model.predict([screen.reshape(160,120,1)])[0] print(prediction) if np.argmax(prediction) == np.argmax(w): straight() elif np.argmax(prediction) == np.argmax(s): reverse() if np.argmax(prediction) == np.argmax(a): left() if np.argmax(prediction) == np.argmax(d): right() if np.argmax(prediction) == np.argmax(wa): forward_left() if np.argmax(prediction) == np.argmax(wd): forward_right() if np.argmax(prediction) == np.argmax(sa): reverse_left() if np.argmax(prediction) == np.argmax(sd): reverse_right() if np.argmax(prediction) == np.argmax(nk): no_keys() keys = key_check() # p pauses game and can get annoying. if 'T' in keys: if paused: paused = False time.sleep(1) else: paused = True ReleaseKey(A) ReleaseKey(W) ReleaseKey(D) time.sleep(1)
def test(game, model, n_games, verbose=1): # Train # Reseting the win counter win_cnt = 0 # We want to keep track of the progress of the AI over time, so we save its win count history win_hist = [] # Epochs is the number of games we play for e in range(n_games): # Resetting the game game.reset() game_over = False # get tensorflow running first to acquire cudnn handle input_t = game.observe() if e == 0: paused = True print('Training is paused. Press p once game is loaded and is ready to be played.') else: paused = False while not game_over: if not paused: # The learner is acting on the last observed game screen # input_t is a vector containing representing the game screen input_tm1 = input_t """ We want to avoid that the learner settles on a local minimum. Imagine you are eating in an exotic restaurant. After some experimentation you find that Penang Curry with fried Tempeh tastes well. From this day on, you are settled, and the only Asian food you are eating is Penang Curry. How can your friends convince you that there is better Asian food? It's simple: Sometimes, they just don't let you choose but order something random from the menu. Maybe you'll like it. The chance that your friends order for you is epsilon """ # Choose yourself # q contains the expected rewards for the actions q = model.predict(input_tm1) # We pick the action with the highest expected reward print('q values=' + str(q[0])) action = np.argmax(q[0]) # apply action, get rewards and new state input_t, reward, game_over = game.act(action) # If we managed to catch the fruit we add 1 to our win counter if reward == 1: win_cnt += 1 """ The experiences < s, a, r, s’ > we make during gameplay are our training data. Here we first save the last experience, and then load a batch of experiences to train our model """ # menu control keys = key_check() if 'P' in keys: if paused: paused = False print('unpaused!') time.sleep(1) else: print('Pausing!') paused = True time.sleep(1) elif 'O' in keys: print('Quitting!') return if verbose > 0: print("Game {:03d}/{:03d} | Win count {}".format(e, n_games, win_cnt)) win_hist.append(win_cnt) return win_hist
t = 0 if not os.path.exists(r'./images'): os.mkdir(r'./images') for i in list(range(8))[::-1]: print(i + 1) time.sleep(1) while (True): # 640x480 windowed mode screen = grab_screen(region=(0, 0, 640, 480)) screen = cv2.cvtColor(screen, cv2.COLOR_BGR2GRAY) screen = cv2.resize(screen, (64, 64)) # resize to something a bit more acceptable for a CNN keys = key_check() if 'up' in keys: if c < 1000: #1000 denotes the no of images for jump action cv2.imwrite('./images/frame_{0}.jpg'.format(x), screen) csv.write('1\n') print('jump dino') x += 1 c += 1 elif 'down' in keys: if d < 25: #25 denotes the no of images for duck action cv2.imwrite('./images/frame_{0}.jpg'.format(x), screen) csv.write('2\n') print('duck') x += 1 d += 1 else:
print("Starting !!!") time.sleep(1) print("1") time.sleep(1) print("2") time.sleep(1) print("3") time.sleep(1) print("4") print("GO go go go RECORDING STARTED!!!!") frame = 0 skip = 2 while (True): current_view = grab_screen([5, 20, 800, 600]) reduced_view = cv2.resize(current_view, (shape[1], shape[0]), interpolation=cv2.INTER_AREA) instant_move = key_check() frame += 1 if (frame % skip == 0 and verify_differnce(instant_move, 5000)): train_data_collect(reduced_view, instant_move) frame = 0 cv2.imshow('rv', reduced_view) if cv2.waitKey(1) == 27: break print(" ( ", len(X_train[0]), " ) ", moves_count) cv2.destroyAllWindows() cv2.waitKey(1) save_train_data("third_batch")
def main(): j = g = 0 paused = True printed = True print('FILE START!') i = 1 j = True g = 0 Flag = False s_time = time.time() LR_time = 4.0 while (True): keys = key_check() ## if j: ## frame_file_name = 'training_img-{}-data.npy'.format(i) ## lane_file_name = 'training_lane-{}-data.npy'.format(i) ## LRF_file_name = 'training_LRF-{}-data.npy'.format(i) ## j = False ## ## if not (os.path.isfile(frame_file_name)): ## lane_training_data = [] ## LRF_training_data = [] ## frame_training_data = [] frame = cv2.cvtColor(grab_screen(region=(10, 27, 809, 626)), cv2.COLOR_BGR2RGB) vertices_frame = np.array([[0, 600], [0, 349], [221, 293], [543, 288], [800, 331], [800, 600]], np.int32) ## mask_frame = roi(road_frame, [vertices_frame]) mask_frame = roi(frame, [vertices_frame]) hsv = cv2.cvtColor(mask_frame, cv2.COLOR_RGB2HSV) hsv = cv2.GaussianBlur(hsv, (5, 5), 75, 75) ## DY_ROAD_CLR_VERTICES_L = np.array([[61, 578], [94, 442], [198, 425], [177, 582]], np.int32) ## DY_ROAD_CLR_ROI_L = roi(hsv, [DY_ROAD_CLR_VERTICES_L]) ## DY_ROAD_CLR_VERTICES_R = np.array([[697, 579], [622, 429], [697, 433], [791, 549]], np.int32) ## DY_ROAD_CLR_ROI_R = roi(hsv, [DY_ROAD_CLR_VERTICES_R]) ## DY_ROAD_CLR_ROI = cv2.bitwise_or(DY_ROAD_CLR_ROI_L, DY_ROAD_CLR_ROI_R) ## new_road_colors(DY_ROAD_CLR_VERTICES_L, DY_ROAD_CLR_VERTICES_R, hsv) LEFT = color_road1(hsv, 27, 415, 0, 358, 123, 319, 370, 289, 303, 319, 237, 327) RIGHT = color_road1(hsv, 452, 289, 676, 308, 797, 334, 791, 405, 529, 318, 490, 317) FORWARD = color_road0(hsv, 303, 319, 370, 289, 452, 289, 490, 317) L = cv2.cvtColor(LEFT, cv2.COLOR_BGR2GRAY) R = cv2.cvtColor(RIGHT, cv2.COLOR_BGR2GRAY) F = cv2.cvtColor(FORWARD, cv2.COLOR_BGR2GRAY) if not paused: if s_time >= 1: Flag, LR_time = LRF_drive(L, R, F, Flag, LR_time, 2.0) s_time = time.time() LRF = cv2.bitwise_or(LEFT, RIGHT) LRF = cv2.bitwise_or(LRF, FORWARD) ## LANES = process_img(frame, vertices_frame, hsv) cv2.imshow('LRF', LRF) ## frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) ## LRF_gray = cv2.cvtColor(LRF, cv2.COLOR_BGR2GRAY) ## ## MIN_SIZE = 120 ## MAX_SIZE = 160 ## ## frame_resize = cv2.resize(frame_gray, (MAX_SIZE, MIN_SIZE)) ## LANES_resize = cv2.resize(LANES, (MAX_SIZE, MIN_SIZE)) ## LRF_resize = cv2.resize(LRF_gray, (MAX_SIZE, MIN_SIZE)) ## ## if printed: ## print('frame_resize: ', frame_resize.shape, 'type: ', type(frame_resize[0][0])) ## print('LANES_resize: ', LANES_resize.shape, 'type: ', type(LANES_resize[0][0])) ## print('LRF_resize: ', LRF_resize.shape, 'type: ', type(LRF_resize[0][0])) ## printed = False ## ## cv2.imshow('FRAME_resize', frame_resize) ## cv2.imshow('LANES_resize', LANES_resize) ## cv2.imshow('LRF_resize', LRF_resize) ## keyswad = key_check() ## output = keys_to_output(keyswad) ## ## frame_training_data.append( [frame_resize, output]) ## lane_training_data.append( [LANES_resize, output]) ## LRF_training_data.append( [LRF_resize, output]) ## #### if len(frame_training_data) % 500 == 0: ## print(len(frame_training_data)) ## np.save(frame_file_name, frame_training_data) ## g += 1 ## ## if len(lane_training_data) % 500 == 0: ## print(len(lane_training_data)) ## np.save(lane_file_name, lane_training_data) ## g += 1 ## ## if len(LRF_training_data) % 500 == 0: ## print(len(LRF_training_data)) ## np.save(LRF_file_name, LRF_training_data) ## g += 1 ## ## if g == 3: ## j = True ## i += 1 ## print('NEXT FILE! i: ', i) ## ## if i > 10: ## print('FILE ENDS!') ## cv2.destroyAllWindows() ## break if cv2.waitKey(25) & ('T' in key_check()): if paused: paused = False print('START!!!') time.sleep(1) else: slow_ya_roll() print('PAUSE!!!') paused = True if cv2.waitKey(25) & ('Q' in key_check()): print('FILE ENDS!') cv2.destroyAllWindows() break
def main(): last_time = time.time() for i in list(range(4))[::-1]: print(i + 1) time.sleep(1) paused = False mode_choice = 0 screen = grab_screen(region=(0, 40, GAME_WIDTH, GAME_HEIGHT + 40)) screen = cv2.cvtColor(screen, cv2.COLOR_BGR2RGB) prev = cv2.resize(screen, (WIDTH, HEIGHT)) t_minus = prev t_now = prev t_plus = prev while (True): if not paused: screen = grab_screen(region=(0, 40, GAME_WIDTH, GAME_HEIGHT + 40)) screen = cv2.cvtColor(screen, cv2.COLOR_BGR2RGB) last_time = time.time() screen = cv2.resize(screen, (WIDTH, HEIGHT)) delta_count_last = motion_detection(t_minus, t_now, t_plus, screen) t_minus = t_now t_now = t_plus t_plus = screen t_plus = cv2.blur(t_plus, (4, 4)) prediction = model.predict([screen.reshape(WIDTH, HEIGHT, 3)])[0] prediction = np.array(prediction) * np.array( [4.5, 0.1, 0.1, 0.1, 1.8, 1.8, 0.5, 0.5, 0.2]) mode_choice = np.argmax(prediction) if mode_choice == 0: straight() choice_picked = 'straight' elif mode_choice == 1: reverse() choice_picked = 'reverse' elif mode_choice == 2: left() choice_picked = 'left' elif mode_choice == 3: right() choice_picked = 'right' elif mode_choice == 4: forward_left() choice_picked = 'forward+left' elif mode_choice == 5: forward_right() choice_picked = 'forward+right' elif mode_choice == 6: reverse_left() choice_picked = 'reverse+left' elif mode_choice == 7: reverse_right() choice_picked = 'reverse+right' elif mode_choice == 8: no_keys() choice_picked = 'nokeys' motion_log.append(delta_count) motion_avg = round(mean(motion_log), 3) print('loop took {} seconds. Motion: {}. Choice: {}'.format( round(time.time() - last_time, 3), motion_avg, choice_picked)) if motion_avg < motion_req and len(motion_log) >= log_len: print( 'WERE PROBABLY STUCK FFS, initiating some evasive maneuvers.' ) # 0 = reverse straight, turn left out # 1 = reverse straight, turn right out # 2 = reverse left, turn right out # 3 = reverse right, turn left out quick_choice = random.randrange(0, 4) if quick_choice == 0: reverse() time.sleep(random.uniform(1, 2)) forward_left() time.sleep(random.uniform(1, 2)) elif quick_choice == 1: reverse() time.sleep(random.uniform(1, 2)) forward_right() time.sleep(random.uniform(1, 2)) elif quick_choice == 2: reverse_left() time.sleep(random.uniform(1, 2)) forward_right() time.sleep(random.uniform(1, 2)) elif quick_choice == 3: reverse_right() time.sleep(random.uniform(1, 2)) forward_left() time.sleep(random.uniform(1, 2)) for i in range(log_len - 2): del motion_log[0] keys = key_check() # p pauses game and can get annoying. if 'T' in keys: if paused: paused = False time.sleep(1) else: paused = True ReleaseKey(A) ReleaseKey(W) ReleaseKey(D) time.sleep(1)
def main(file_name, starting_value): file_name = file_name starting_value = starting_value training_data = [] for i in list(range(4))[::-1]: print(i + 1) time.sleep(1) last_time = time.time() paused = False print('STARTING!!!') while (True): if not paused: screen = grab_screen(region=(0, 40, GAME_WIDTH, GAME_HEIGHT + 30)) last_time = time.time() # resize to something a bit more acceptable for a CNN screen = cv2.resize(screen, (WIDTH, HEIGHT)) # run a color convert: screen = cv2.cvtColor(screen, cv2.COLOR_BGR2RGB) # Show what is captured # cv2.imshow('window', screen) keys = key_check() output = keys_to_output(keys) training_data.append([screen, output]) # Show training data array # print(training_data) print('loop took {} seconds'.format(time.time() - last_time)) last_time = time.time() ## cv2.imshow('window',cv2.resize(screen,(640,360))) ## if cv2.waitKey(25) & 0xFF == ord('q'): ## cv2.destroyAllWindows() ## break if len(training_data) % 100 == 0: print(len(training_data)) if len(training_data) == 500: np.save(file_name, training_data) file_name = os.path.join( os.getcwd(), 'training', '{}/{}_{}-{}.npy'.format(train_no, train_no, training_type, starting_value)) print('=' * 80) print('SAVED', file_name) print('=' * 80) training_data = [] starting_value += 1 keys = key_check() if 'T' in keys: if paused: paused = False print('unpaused!') time.sleep(1) else: print('Pausing!') paused = True time.sleep(1)
def main(): last_time = time.time() for i in list(range(4))[::-1]: print(i + 1) time.sleep(1) paused = False mode_choice = 0 screen = grab_screen(region = (0, 40, GAME_WIDTH, GAME_HEIGHT + 40)) screen = cv2.cvtColor(screen, cv2.COLOR_BGR2RGB) prev = cv2.resize(screen, (WIDTH,HEIGHT)) t_minus, t_now, t_plus = prev, prev, prev while(True): if not paused: screen = grab_screen(region = (0, 40, GAME_WIDTH, GAME_HEIGHT + 40)) screen = cv2.cvtColor(screen, cv2.COLOR_BGR2RGB) last_time = time.time() screen = cv2.resize(screen, (WIDTH,HEIGHT)) delta_count_last = motion_detection(t_minus, t_now, t_plus) t_minus = t_now t_now = t_plus t_plus = screen t_plus = cv2.blur(t_plus, (4, 4)) prediction = model.predict([screen.reshape(WIDTH,HEIGHT, 3)])[0] prediction = np.array(prediction) * np.array([4.5, 0.1, 0.1, 0.1, 1.8, 1.8, 0.5, 0.5, 0.2]) mode_choice = np.argmax(prediction) if mode_choice == 0: straight() choice_picked = 'straight' elif mode_choice == 1: reverse() choice_picked = 'reverse' elif mode_choice == 2: left() choice_picked = 'left' elif mode_choice == 3: right() choice_picked = 'right' elif mode_choice == 4: forward_left() choice_picked = 'forward+left' elif mode_choice == 5: forward_right() choice_picked = 'forward+right' elif mode_choice == 6: reverse_left() choice_picked = 'reverse+left' elif mode_choice == 7: reverse_right() choice_picked = 'reverse+right' elif mode_choice == 8: no_keys() choice_picked = 'nokeys' motion_log.append(delta_count) motion_avg = round(mean(motion_log), 3) print('loop took {0} seconds. Motion: {1}. Choice: {2}'.format(round(time.time() - last_time, 3), motion_avg, choice_picked)) if motion_avg < motion_req and len(motion_log) >= log_len: print('WERE PROBABLY STUCK FFS, initiating some evasive maneuvers.') # 0 = reverse straight, turn left out # 1 = reverse straight, turn right out # 2 = reverse left, turn right out # 3 = reverse right, turn left out quick_choice = random.randrange(0, 4) if quick_choice == 0: reverse() time.sleep(random.uniform(1, 2)) forward_left() time.sleep(random.uniform(1, 2)) elif quick_choice == 1: reverse() time.sleep(random.uniform(1, 2)) forward_right() time.sleep(random.uniform(1, 2)) elif quick_choice == 2: reverse_left() time.sleep(random.uniform(1, 2)) forward_right() time.sleep(random.uniform(1, 2)) elif quick_choice == 3: reverse_right() time.sleep(random.uniform(1, 2)) forward_left() time.sleep(random.uniform(1, 2)) for i in range(log_len - 2): del motion_log[0] keys = key_check() # p pauses game and can get annoying. if 'T' in keys: if paused: paused = False time.sleep(1) else: paused = True ReleaseKey(A) ReleaseKey(W) ReleaseKey(D) time.sleep(1)
def get_keys_t(): global keys while (g_gather_keys): keys = key_check() sleep(0.1)
def main(file_name): framethread = getFrameThread(8, 30, 800, 600, "TrackMania Nations Forever").start() # file_index = 1 training_directory = os.path.abspath( r"E:\Trackmania Data\training_data_new") # while True: # file_name = 'training_data-{}.npy'.format(file_index) # if os.path.isfile(os.path.join(training_directory, file_name)): # print('File exists, moving along', file_index) # file_index += 1 # else: # print('File does not exist, starting fresh!', file_index) # break loop_times = [] training_data = [] paused = True _ = key_check() print("Press t to start") driving_keys = ("W", "A", "S", "D") last_time = time.time() while True: # main loop keys = key_check() if 'T' in keys: if paused: paused = False print("Unpausing") time.sleep(1 / 2) last_time = time.time() else: print("Pausing") paused = True time.sleep(1 / 2) elif 'O' and 'P' in keys and paused: print("Saving and Stopping") # file_name = 'training_data-{}.npy'.format(file_index) np.save(os.path.join(training_directory, file_name), training_data) print(stats(loop_times)) break elif 'M' in keys and paused: print(stats(loop_times)) time.sleep(1 / 2) correct_driving_keys = True if not paused: for key in keys: if key not in driving_keys: correct_driving_keys = False if len(keys) <= 2 and correct_driving_keys: screenshot, velocity = create_screenshot(framethread) training_data.append([screenshot, velocity, keys]) if len(training_data) % 500 == 0: print(len(training_data)) new_time = time.time() current_loop_time = new_time - last_time fixed_loop_time = 0.035 difference = fixed_loop_time - current_loop_time if difference > 0: time.sleep(difference) # loop_times.append(current_loop_time) loop_times.append(time.time() - last_time) # actual loop times last_time = new_time framethread.stopNow() with open(os.path.join(training_directory, "stats.txt"), "w") as text_file: text_file.write(stats(loop_times))
def eval_net(dirname='ski-race', dropout=False, weight_decay=False, large_decay=False, epoch=9): (means, stds) = load_normalization_stats(dirname=dirname) dx_key_dict = get_key_dict() one_hot_dict = load_one_hot_dict(dirname=dirname) device = torch.device('cuda:0') cudnn.benchmark = True cudnn.enabled = True model = load_resnet50(use_custom=True, dropout=dropout, dirname=dirname) model = model.to(device) optimizer = torch.optim.Adam(model.parameters()) load_model(model, dirname=dirname) #load_checkpoint(model, optimizer, dirname=dirname, \ # weight_decay=weight_decay, dropout=dropout, # epoch=epoch, large_decay=large_decay) model.eval() torch.set_grad_enabled(False) normalization_counts = form_normalization_tensor(dirname=dirname) normalization_counts = normalization_counts.to(device) # time.sleep(5) # print('Done sleeping!')attawwawawtw paused = False keys = key_check() prev_key = '' start_time = time.time() frames_analyzed = 0 while True: if not paused: # initial_time = time.time() # torch.cuda.synchronize() screen = read_screen(means, stds) screen = screen.to(device) # torch.cuda.synchronize() # screen_time = time.time() # torch.cuda.synchronize() screen = screen[None] pred = model(screen) # torch.cuda.synchronize() # pred_time = time.time() # torch.cuda.synchronize() one_hot = prediction_to_one_hot(pred, normalization_counts) one_hot.cpu() prev_key = act_on_prediction(one_hot.tolist(), one_hot_dict, prev_key, dx_key_dict) # final_time = time.time() # print('Processing of single frame took ', final_time - initial_time) # print('\t Screen read, process, and transfer time: ', screen_time - initial_time) # print('\t Inference time: ', pred_time - screen_time) # print('\t Time to act: ', final_time - pred_time) else: if 'X' in keys: break if 'T' in keys: ReleaseAllKeys(dx_key_dict) if paused: paused = False print('unpaused!') time.sleep(0.1) else: print('Pausing!') paused = True time.sleep(0.1) if not paused: frames_analyzed += 1 if (frames_analyzed % 10 == 9): print('Last ten frames averaged ', 10 / (time.time() - start_time), ' fps') start_time = time.time() keys = key_check()
def main(): for i in list(range(4))[::-1]: print(i + 1) time.sleep(1) paused = False mode_choice = 0 test_data = [] test_data_2 = [] test_data_3 = [] while (True): if not paused: if len(test_data) == 0: screen = grab_screen(region=(0, 40, GAME_WIDTH, GAME_HEIGHT + 40)) screen = cv2.cvtColor(screen, cv2.COLOR_BGR2RGB) screen = cv2.resize(screen, (WIDTH, HEIGHT)) keys = key_check() output = keys_to_output(keys) test_data = [screen, output] elif len(test_data_2) == 0: screen = grab_screen(region=(0, 40, GAME_WIDTH, GAME_HEIGHT + 40)) screen = cv2.cvtColor(screen, cv2.COLOR_BGR2RGB) screen = cv2.resize(screen, (WIDTH, HEIGHT)) keys = key_check() output = keys_to_output(keys) test_data_2 = test_data test_data = [screen, output] elif len(test_data_3) == 0: screen = grab_screen(region=(0, 40, GAME_WIDTH, GAME_HEIGHT + 40)) screen = cv2.cvtColor(screen, cv2.COLOR_BGR2RGB) screen = cv2.resize(screen, (WIDTH, HEIGHT)) keys = key_check() output = keys_to_output(keys) test_data_3 = test_data_2 test_data_2 = test_data test_data = [screen, output] else: screen = grab_screen(region=(0, 40, GAME_WIDTH, GAME_HEIGHT + 40)) screen = cv2.cvtColor(screen, cv2.COLOR_BGR2RGB) screen = cv2.resize(screen, (WIDTH, HEIGHT)) img1 = screen img2 = test_data[0] img3 = test_data_2[0] img4 = test_data_3[0] last_input = test_data[1] _2nd_last_input = test_data_2[1] _3rd_last_input = test_data_3[1] font = cv2.FONT_HERSHEY_SIMPLEX last = [] if last_input == [1, 0, 0, 0, 0, 0, 0, 0, 0]: last = 'W' elif last_input == [0, 1, 0, 0, 0, 0, 0, 0, 0]: last = 'SS' elif last_input == [0, 0, 1, 0, 0, 0, 0, 0, 0]: last = 'AAA' elif last_input == [0, 0, 0, 1, 0, 0, 0, 0, 0]: last = 'DDDD' elif last_input == [0, 0, 0, 0, 1, 0, 0, 0, 0]: last = 'WAWAWA' elif last_input == [0, 0, 0, 0, 0, 1, 0, 0, 0]: last = 'WDWDWDWD' elif last_input == [0, 0, 0, 0, 0, 0, 1, 0, 0]: last = 'SASASASASA' elif last_input == [0, 0, 0, 0, 0, 0, 0, 1, 0]: last = 'SDSDSDSDSDSD' elif last_input == [0, 0, 0, 0, 0, 0, 0, 0, 1]: last = 'NKNKNKNKNKNKNK' cv2.putText(img2, str(last), (10, 10), font, 1, (0, 0, 255), 3, cv2.LINE_AA) if _2nd_last_input == [1, 0, 0, 0, 0, 0, 0, 0, 0]: last = 'W' elif _2nd_last_input == [0, 1, 0, 0, 0, 0, 0, 0, 0]: last = 'SS' elif _2nd_last_input == [0, 0, 1, 0, 0, 0, 0, 0, 0]: last = 'AAA' elif _2nd_last_input == [0, 0, 0, 1, 0, 0, 0, 0, 0]: last = 'DDDD' elif _2nd_last_input == [0, 0, 0, 0, 1, 0, 0, 0, 0]: last = 'WAWAWA' elif _2nd_last_input == [0, 0, 0, 0, 0, 1, 0, 0, 0]: last = 'WDWDWDWD' elif _2nd_last_input == [0, 0, 0, 0, 0, 0, 1, 0, 0]: last = 'SASASASASA' elif _2nd_last_input == [0, 0, 0, 0, 0, 0, 0, 1, 0]: last = 'SDSDSDSDSDSD' elif _2nd_last_input == [0, 0, 0, 0, 0, 0, 0, 0, 1]: last = 'NKNKNKNKNKNKNK' cv2.putText(img3, str(last), (10, 10), font, 1, (0, 0, 255), 3, cv2.LINE_AA) if _3rd_last_input == [1, 0, 0, 0, 0, 0, 0, 0, 0]: last = 'W' elif _3rd_last_input == [0, 1, 0, 0, 0, 0, 0, 0, 0]: last = 'SS' elif _3rd_last_input == [0, 0, 1, 0, 0, 0, 0, 0, 0]: last = 'AAA' elif _3rd_last_input == [0, 0, 0, 1, 0, 0, 0, 0, 0]: last = 'DDDD' elif _3rd_last_input == [0, 0, 0, 0, 1, 0, 0, 0, 0]: last = 'WAWAWA' elif _3rd_last_input == [0, 0, 0, 0, 0, 1, 0, 0, 0]: last = 'WDWDWDWD' elif _3rd_last_input == [0, 0, 0, 0, 0, 0, 1, 0, 0]: last = 'SASASASASA' elif _3rd_last_input == [0, 0, 0, 0, 0, 0, 0, 1, 0]: last = 'SDSDSDSDSDSD' elif _3rd_last_input == [0, 0, 0, 0, 0, 0, 0, 0, 1]: last = 'NKNKNKNKNKNKNK' cv2.putText(img4, str(last), (10, 10), font, 1, (0, 0, 255), 3, cv2.LINE_AA) vis0 = np.concatenate((img1, img2), axis=1) vis1 = np.concatenate((img3, img4), axis=1) final_vis = np.concatenate((vis0, vis1), axis=0) final_vis = cv2.resize(final_vis, (480, 270)) prediction = model.predict( [final_vis.reshape(WIDTH, HEIGHT, 3)])[0] prediction = np.array( prediction) # * np.array([1.12, 1, 1, 1, 1, 1, 1, 1, 0.2]) print(prediction) # div = 4 # wplus = prediction[0] + (prediction[4] / div) + (prediction[5] / div) # print(wplus) # splus = prediction[1] + (prediction[6] / div) + (prediction[7] / div) # print(splus) # aplus = prediction[2] + (prediction[6] / div) + (prediction[4] / div) # print(aplus) # dplus = prediction[3] + (prediction[5] / div) + (prediction[7] / div) # print(dplus) # waplus = prediction[4] + (prediction[0] / div) + (prediction[2] / div) # print(waplus) # wdplus = prediction[5] + (prediction[0] / div) + (prediction[3] / div) # print(wdplus) # saplus = prediction[6] + (prediction[2] / div) + (prediction[1] / div) # print(saplus) # sdplus = prediction[7] + (prediction[1] / div) + (prediction[3] / div) # print(sdplus) # # mode_choice_v2 = np.argmax([wplus, splus, aplus, dplus, waplus, wdplus, saplus, sdplus]) # print(mode_choice_v2) mode_choice = np.argmax(prediction) print(mode_choice) if mode_choice == 0: straight() choice_picked = 'straight' print(choice_picked) elif mode_choice == 1: reverse() choice_picked = 'reverse' print(choice_picked) elif mode_choice == 2: left() choice_picked = 'left' print(choice_picked) elif mode_choice == 3: right() choice_picked = 'right' print(choice_picked) elif mode_choice == 4: forward_left() choice_picked = 'forward+left' print(choice_picked) elif mode_choice == 5: forward_right() choice_picked = 'forward+right' print(choice_picked) elif mode_choice == 6: reverse_left() choice_picked = 'reverse+left' print(choice_picked) elif mode_choice == 7: reverse_right() choice_picked = 'reverse+right' print(choice_picked) elif mode_choice == 8: no_keys() choice_picked = 'nokeys' print(choice_picked) keys = key_check() output = keys_to_output(keys) test_data_3 = test_data_2 test_data_2 = test_data test_data = [screen, output] keys = key_check() if 'T' in keys: if paused: paused = False time.sleep(1) print("Unpaused") else: paused = True ReleaseKey(A) ReleaseKey(W) ReleaseKey(D) time.sleep(1) print("Paused")
def train(game, model, epochs,num_of_games, verbose=1): # Train # Reseting the win counter mouse=Controller() # We want to keep track of the progress of the AI over time, so we save its win count history loss_hist = [] average_points=[] current_step=0 for ga in range(num_of_games): time.sleep(1) reward_count=0 #os.system('TASKKILL /F /IM zed.exe') os.startfile('C:/Users/naman/Desktop/zed.exe') #win_cnt = 0 game.reset() game_over = False print('Training paused! will begin in 10 seconds') time.sleep(10) loss=0. average_value=0 for e in range(1,epochs+1): current_step+=1 epsilon = 0.01 + 0.99*exp(-1.*current_step*0.001) # get tensorflow running first to acquire cudnn handle input_t = game.observe() if e==1: time.sleep(1) # if e == 0: # paused = True # print('Training is paused. Press N once game is loaded and is ready to be played.') # else: # paused = False #while not game_over: #if not paused: # The learner is acting on the last observed game screen # input_t is a vector containing representing the game screen input_tm1 = input_t n=np.random.rand() if n <= epsilon: # Eat something random from the menu action = int(np.random.randint(0, num_actions, size=1)) #print('random action') else: # Choose yourself # q contains the expected rewards for the actions q = model.predict(input_tm1) # We pick the action with the highest expected reward action = np.argmax(q[0]) #print('optimal action') # apply action, get rewards and new state input_t, reward, game_over = game.act(action) print("ingame_reward",reward) if reward==0.2 or reward==1: reward_count+=1 average_value+=reward_count/e # If we managed to catch the fruit we add 1 to our win counter # if reward == 1: # win_cnt += 1 """ The experiences < s, a, r, s’ > we make during gameplay are our training data. Here we first save the last experience, and then load a batch of experiences to train our model """ # store experience exp_replay.remember([input_tm1, action, reward, input_t], game_over) # Load batch of experiences inputs, targets = exp_replay.get_batch(model, batch_size=batch_size) # train model on experiences batch_loss = model.train_on_batch(inputs, targets) print('batch loss',batch_loss) loss += batch_loss # menu control keys = key_check() # if 'N' in keys: # if paused: # paused = False # print('unpaused!') # time.sleep(1) # else: # print('Pausing!') # paused = True # time.sleep(1) if 'O' in keys: print('Quitting!') return print("Step {:03d}/{:03d}".format(e,epochs)) if game_over: print('game over! restarting') mouse.position=(1280,280) mouse.click(Button.left,1) break if verbose > 0: print("Epoch {:03d}/{:03d} | Loss {:.4f}".format(ga, num_of_games, loss)) #print('Epoch: {}, Loss: {}, Accuracy: {}'.format(e+1,loss,win_cnt/e*100)) loss_hist.append(loss) average_points.append(average_value) mouse.position=(1280,280) mouse.click(Button.left,1) save_model(model) return loss_hist,average_points
def control_bot(game,epochs, model): # Train win_cnt = 0 loss_cnt = 0 matka = 0 loss = 0 # We want to keep track of the progress of the AI over time, so we save its win count history win_hist = [] game_over = False # Epochs is the number of games we play for e in range(epochs): # epsilon = 4 / ((e + 1) ** (1 / 2)) epsilon = 0.1 # Resetting the game game.reset() game_over = False # get tensorflow running first to acquire cudnn handle input_t = game.observe() if e == 0: paused = True print('Training is paused. Press p once game is loaded and is ready to be played.') else: paused = False while not game_over: if not paused: # The learner is acting on the last observed game screen # input_t is a vector containing representing the game screen input_tm1 = input_t #shape output of VGG feature extractor (1,7,7,512) if random.choice([1,2,3,4,5,6,7,8,9,10]) in [1,5,10]: print("inside random") action = int(np.random.randint(0, num_actions, size=1)) else: print("inside predict") q = model.predict(input_tm1) print(q.shape) action = np.argmax(q[0]) print(q[0]) input_t, reward, game_over = game.act(action) print("reward calculated - " + str(reward)) exp_replay.remember([input_tm1, action, reward, input_t], game_over) # Load batch of experiences inputs, targets = exp_replay.get_batch(model, batch_size=32) print(targets) # train model on experiences batch_loss = model.train_on_batch(inputs, targets) # print(loss) loss += batch_loss keys = key_check() if 'P' in keys: if paused: paused = False print('unpaused!') time.sleep(1) else: print('Pausing!') paused = True time.sleep(1) elif 'O' in keys: print('Quitting!') return save_model(model) win_hist.append(win_cnt)
def main(): last_time = time.time() for i in list(range(4))[::-1]: print(i + 1) time.sleep(1) paused = False L = 0 R = 0 S = 0 while (True): if not paused: # 800x600 windowed mode #screen = np.array(ImageGrab.grab(bbox=(0,40,800,640))) screen = get_screen() print('loop took {} seconds'.format(time.time() - last_time)) last_time = time.time() screen = cv2.cvtColor(screen, cv2.COLOR_BGR2GRAY) screen = cv2.resize(screen, (WIDTH, HEIGHT)) screen = screen.astype('float32') screen /= 255 plt.imshow(screen) prediction = model.predict([screen.reshape(1, HEIGHT, WIDTH, 1)])[0] turn_thresh = 0.5 fwd_thresh = 0.70 if prediction[1] > fwd_thresh: S += 1 straight() elif prediction[0] > turn_thresh: L += 1 left() elif prediction[2] > turn_thresh: R += 1 right() else: straight() print('{} L, {} S, {} R'.format(L, S, R)) keys = key_check() # p pauses game and can get annoying. if 'T' in keys: if paused: print("Begynner igjen") paused = False time.sleep(1) else: print("Pauser") paused = True ReleaseKey(A) ReleaseKey(W) ReleaseKey(D) time.sleep(1) if 'R' in keys: L = 0 R = 0 S = 0
import pyautogui import time import getkeys KEY_FOR_BUILD = "E" print("Starting in...") for i in reversed(range(4)): print(i) time.sleep(1) while True: keys = getkeys.key_check() if KEY_FOR_BUILD in keys: pyautogui.click()
def main(): print("Loaded model: " + MODEL_NAME) # 4 second countdown for i in list(range(4))[::-1]: print(i+1) time.sleep(1) last_time = time.time() time_per_frame = 0 iterations = 0 paused = False while(True): if not paused: screen = grab_screen(region=(0, 0, 640, 480)) screen = cv2.cv2.cvtColor(screen, cv2.COLOR_BGR2GRAY) screen = cv2.resize(screen, (64, 48)) cv2.namedWindow('input screen') cv2.moveWindow('input screen', 800,30) cv2.imshow('input screen', screen) # taking game frame as input and predicting a one-hot array at that exact frame img = np.array(screen).reshape(-1,64,48,1) # acnet # x2 = np.array(screen) # resnet and densenet # img = np.repeat(x2[..., np.newaxis], 3, -1).reshape(-1, 64, 48, 3) # resnet and densenet pred = model.predict(np.array(img)) y = list(np.around(pred[0])) print(y) if y == [0, 1, 0]: straight() elif y == [0, 0, 1]: right() elif y == [1, 0, 0]: left() # press G to pause the script keys = key_check() if 'G' in keys: if paused: paused = False time.sleep(1) else: paused = True ReleaseKey(A) ReleaseKey(W) ReleaseKey(D) time.sleep(1) iterations+=1 time_per_frame += float(format(time.time() - last_time)) last_time = time.time() # focus opencv window and press Q to quit if (cv2.waitKey(25) & 0xFF == ord('q')): stop() cv2.destroyAllWindows() print('Average FPS:', iterations/time_per_frame) break
def main(): angle = 0x4000 last_time = time.time() for i in list(range(4))[::-1]: print(i + 1) time.sleep(1) paused = False mode_choice = 0 screen = grab_screen(region=(0, 0, GAME_WIDTH, GAME_HEIGHT)) screen = screen[664:714, 104:154] screen = cv2.cvtColor(screen, cv2.COLOR_BGR2RGB) screen = cv2.resize(screen, (WIDTH, HEIGHT)) # cv2.imshow('img',screen) # check if capture is correct # if cv2.waitKey(0) & 0xFF == ord('q'): # cv2.destroyAllWindows() while (True): if not paused: screen = grab_screen(region=(0, 0, GAME_WIDTH, GAME_HEIGHT)) screen = screen[664:714, 104:154] hsv = cv2.cvtColor(screen, cv2.COLOR_RGB2HSV) lower_blue = np.array([134, 0, 0]) upper_blue = np.array([137, 255, 255]) mask = cv2.inRange(hsv, lower_blue, upper_blue) screen = cv2.bitwise_and(screen, screen, mask=mask) screen = cv2.cvtColor(screen, cv2.COLOR_BGR2RGB) screen = cv2.resize(screen, (WIDTH, HEIGHT)) last_time = time.time() screen = screen.reshape((1, ) + screen.shape) prediction = model.predict(screen, batch_size=None, verbose=0, steps=None) print(prediction) if model_type == 'binary': if prediction[0][0] < 0.5: mode_choice = 0 else: mode_choice = 1 if mode_choice == 0: left() elif mode_choice == 1: right() else: print("Unknown command") else: prediction = np.argmax(prediction) predictions.append(prediction) predictions.popleft() lefts = predictions.count(0) + predictions.count(3) rights = predictions.count(1) + predictions.count(4) # print(predictions) print(prediction) if prediction == 0 or prediction == 3: angle = 0x0 elif prediction == 1 or prediction == 4: angle = 0x8000 else: angle = 0x4000 j.set_axis(pyvjoy.HID_USAGE_X, angle) j.set_axis(pyvjoy.HID_USAGE_RZ, 0x5000) time.sleep(0.01) j.set_axis(pyvjoy.HID_USAGE_X, 0x4000) # if prediction == 0: # left() # elif prediction == 1: # right() # elif prediction == 2: # straight() # elif prediction == 3: # forward_left() # elif prediction == 4: # forward_right() # else: # print("wat") keys = key_check() # p pauses game and can get annoying. if 'T' in keys: if paused: paused = False time.sleep(1) else: paused = True j.set_axis(pyvjoy.HID_USAGE_RZ, 0x4000) # Throttle j.set_axis(pyvjoy.HID_USAGE_X, 0x4000) # Steering ReleaseKey(A) ReleaseKey(W) ReleaseKey(D) time.sleep(1)
def main(): last_time = time.time() for i in list(range(5))[::-1]: print(i + 1) time.sleep(1) paused = False while (True): if not paused: # 800x600 windowed mode screen = grab_screen(region=(window_size)) print('loop took {} seconds'.format(time.time() - last_time)) last_time = time.time() screen = cv2.cvtColor(screen, cv2.COLOR_BGR2GRAY) screen = cv2.resize(screen, (WIDTH, HEIGHT)) prediction = model.predict([screen.reshape(WIDTH, HEIGHT, 1)])[0] print(prediction) print("np后的预测:", np.argmax(prediction)) if np.argmax(prediction) == np.argmax(w): print("前") directkeys.go_forward() elif np.argmax(prediction) == np.argmax(a): print("左") directkeys.go_left() elif np.argmax(prediction) == np.argmax(s): print("后") directkeys.go_back() elif np.argmax(prediction) == np.argmax(d): print("右") directkeys.go_right() elif np.argmax(prediction) == np.argmax(q): print("蓝拳") directkeys.blue_fist() elif np.argmax(prediction) == np.argmax(e): print("红刀") directkeys.red_trigger() elif np.argmax(prediction) == np.argmax(j): print("j键,刀平A") directkeys.blade() elif np.argmax(prediction) == np.argmax(k): print("ctrl") directkeys.ctrl() elif np.argmax(prediction) == np.argmax(l): print("l键,枪平a") directkeys.gun() elif np.argmax(prediction) == np.argmax(p): print("shift") directkeys.shift() elif np.argmax(prediction) == np.argmax(jump): print("跳跃") directkeys.jump() elif np.argmax(prediction) == np.argmax(none): print("不动") keys = key_check() if 'Y' in keys: if paused: paused = False time.sleep(1) else: paused = True time.sleep(1) break
def main(): filepath = r'models\vgg16_2048_512_dropout0_Adagrad_lanes.h5' paused = False model_speed = tf.keras.models.load_model('model_num_v3.h5') model = tf.keras.models.load_model(filepath=filepath) for i in list(range(5))[::-1]: print(i + 1) time.sleep(1) while True: if not paused: screen = grab_screen_rgb(640, 34, 1920, 834) screen_speed = grab_screen_rgb(1594, 558, 1670, 576) screen = cv2.resize(screen, (WIDTH, HEIGHT)) screen[:18, :76, :] = screen_speed # image1, image2, image3 = process_image(screen) image1 = process_image(screen) speed = model_speed.predict( np.expand_dims(screen[0:18, 14:34, :], axis=0)) cv2.imshow('1', image1) # cv2.imshow('2', image2) # cv2.imshow('3', image3) cv2.waitKey(1) # screen = np.reshape(screen, (-1, HEIGHT, WIDTH, 3)) image1 = np.expand_dims(image1, axis=0) # image2 = np.expand_dims(image2, axis=0) # image3 = np.expand_dims(image3, axis=0) # prediction = model.predict({"image1": image1, "image2": image2, "image3": image3, "speed": speed}) # prediction = model.predict({"image1": image1, "speed": speed}) * [1, 1, 1, 1, 1, 1, 1, 1, 1] prediction = model.predict(image1) * [1, 0.1, 1, 1, 1, 1, 1, 1, 1] mode_choice = np.argmax(prediction) if mode_choice == 0: straight() choice_picked = 'straight' elif mode_choice == 1: reverse() choice_picked = 'reverse' elif mode_choice == 2: left() choice_picked = 'left' time.sleep(0.095) ReleaseKey(A) elif mode_choice == 3: right() choice_picked = 'right' time.sleep(0.095) ReleaseKey(D) elif mode_choice == 4: forward_left() choice_picked = 'forward+left' time.sleep(0.07) ReleaseKey(A) ReleaseKey(W) elif mode_choice == 5: forward_right() choice_picked = 'forward+right' time.sleep(0.07) ReleaseKey(D) ReleaseKey(W) elif mode_choice == 6: reverse_left() choice_picked = 'reverse+left' elif mode_choice == 7: reverse_right() choice_picked = 'reverse+right' else: no_keys() choice_picked = 'no_keys' np.set_printoptions(formatter={'float': '{: 0.2f}'.format}) print(prediction, choice_picked, ' speed:', speed) keys = key_check() if 'T' in keys: if paused: paused = False print('unpaused!') time.sleep(1) else: paused = True print('Pausing!') ReleaseKey(A) ReleaseKey(W) ReleaseKey(D) ReleaseKey(S) time.sleep(1)
def main(): # last_time = time.time() for i in list(range(4))[::-1]: print(i+1) time.sleep(1) paused = False NUM_CLASSES = 90 detection_graph = tf.Graph() with detection_graph.as_default(): od_graph_def = tf.GraphDef() with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid: serialized_graph = fid.read() od_graph_def.ParseFromString(serialized_graph) tf.import_graph_def(od_graph_def, name='') label_map = label_map_util.load_labelmap(PATH_TO_LABELS) categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True) category_index = label_map_util.create_category_index(categories) def load_image_into_numpy_array(image): (im_width, im_height) = image.size return np.array(image.getdata()).reshape( (im_height, im_width, 3)).astype(np.uint8) IMAGE_SIZE = (12, 8) with detection_graph.as_default(): with tf.Session(graph=detection_graph) as sess: while(True): if not paused: # 800x600 windowed mode #screen = np.array(ImageGrab.grab(bbox=(0,40,800,640))) screen = grab_screen(region=(0,40,800,640)) screen1 = cv2.resize(screen, (800,640)) image_np = cv2.cvtColor(screen1, cv2.COLOR_BGR2RGB) image_np_expanded = np.expand_dims(image_np, axis=0) image_tensor = detection_graph.get_tensor_by_name('image_tensor:0') boxes = detection_graph.get_tensor_by_name('detection_boxes:0') scores = detection_graph.get_tensor_by_name('detection_scores:0') classes = detection_graph.get_tensor_by_name('detection_classes:0') num_detections = detection_graph.get_tensor_by_name('num_detections:0') (boxes, scores, classes, num_detections) = sess.run( [boxes, scores, classes, num_detections], feed_dict={image_tensor: image_np_expanded}) vis_util.visualize_boxes_and_labels_on_image_array( image_np, np.squeeze(boxes), np.squeeze(classes).astype(np.int32), np.squeeze(scores), category_index, use_normalized_coordinates=True, line_thickness=8) cv2.imshow('window',image_np) # print('loop took {} seconds'.format(time.time()-last_time)) # last_time = time.time() screen = cv2.cvtColor(screen, cv2.COLOR_BGR2GRAY) screen = cv2.resize(screen, (160,90)) prediction = model.predict([screen.reshape(160,90,1)])[0] print(prediction) turn_thresh = .75 fwd_thresh = 0.6 if prediction[1] > fwd_thresh: straight() # print("!") elif prediction[0] > turn_thresh: left() # print("<") elif prediction[2] > turn_thresh and prediction[2] < 0.95 : right() # print(">") else: straight() # print("!!!") keys = key_check() # p pauses game and can get annoying. if 'T' in keys: if paused: paused = False time.sleep(1) else: paused = True ReleaseKey(A) ReleaseKey(W) ReleaseKey(D) time.sleep(1) if cv2.waitKey(25) & 0xFF == ord('q'): cv2.destroyAllWindows() break
def main(): ################################################################################################### upperBound_s1 = np.array( [200, 150, 255] ) # upper and lower bound for the color detection (the way I came up with to find the contour of the green rectangle) lowerBound_s1 = np.array([130, 0, 85]) upperBound_fish = np.array([50, 255, 197]) lowerBound_fish = np.array([20, 215, 147]) x_center_calibration_value = 10 # Makes the x coordinate of the center of the fish and of the rectangle to be in the right place x = 105 # If I need to translate the areas of interest easily and equally y = 75 file_name = 'Data\\training_data.npy' if os.path.isfile(file_name): print("Training file exists, loading previos data!") training_data = list(np.load(file_name)) else: print("Training file does not exist, starting fresh!") training_data = [] frame_file = 'Data\\frames.npy' if os.path.isfile(frame_file): print("Frames file exists, loading previos data!") frames = list(np.load(frame_file)) else: print("Frames file does not exist, starting fresh!") frames = [] ################################################################################################### region_template = cv2.imread('Images\\fishing region 3.png') region_template_gray = cv2.cvtColor(region_template, cv2.COLOR_BGR2GRAY) wr, hr = region_template_gray.shape[::-1] was_fishing = False last_time = time.time() c_pressed = 0 while True: screen = np.array( ImageGrab.grab(bbox=(0, 40, 1280, 760))) # gets what is happening on the screen #print('Frame took {} ms'.format(np.round((time.time()-last_time)*1000, 2))) #print('FPS: ', np.round(1/(time.time()-last_time), 1)) last_time = time.time() fishing, green_bar_window, floor_height = fishing_region( screen, region_template_gray, wr, hr) if fishing: contour, green_bar_height, lowest_point = process_img( screen, green_bar_window ) # process every frame (would be nice if it could process every 5 or so frames, so the process becomes faster). fish_detected, fish_height, searching_nemo = fish(green_bar_window) d_rect_fish = fish_height - green_bar_height # if result is + : fish is below the green bar, if result is - : fish is above the green bar d_rect_floor = floor_height - lowest_point # always + keys = key_check() c_pressed = keys_to_output(keys) data = [ d_rect_fish, d_rect_floor, c_pressed ] # example c pressed: [231, 456, 1]. c not pressed: [231, 456, 0] training_data.append(data) was_fishing = True #print("G-L:", lowest_point) #print("FLoor", floor_height) #print("R/Fish:\t", data[0], "R/Floor:\t", data[1], "C pressed:\t", data[2]) if not fishing and was_fishing: if len(frames) == 0: #print('list of frames is new') frames.append(len(training_data)) #print(len(training_data)) print("Frames analysed:\t", len(training_data)) np.save(frame_file, frames) #print(frames) print("Saving...") np.save(file_name, training_data) was_fishing = False else: frame = len(training_data) - sum(frames) frames.append(frame) print("Frames analysed:\t", frames[-1]) np.save(frame_file, frames) #print(frames) #print(len(training_data)) print("Saving...") np.save(file_name, training_data) was_fishing = False #cv2.imshow('RGB Region',cv2.cvtColor(green_bar_window, cv2.COLOR_BGR2RGB)) if cv2.waitKey(25) & 0xFF == ord('q'): # To close the windows cv2.destroyAllWindows() break
def thatsover(): for i in list(range(2))[::-1]: print(i + 1) time.sleep(1) paused = False while (True): last_time = time.time() if not paused: # 640*480 windowed mode screen = grabscreen.grab_screen(region=(0, 40, 640, 480)) screen, lines = collect_data.process_img(screen) screen = cv2.resize(screen, (320, 240)) # screen = np.array(screen).reshape(320,240) # screen.reshape(-1,320,240,1) cv2.imshow('window', screen) if cv2.waitKey(25) & 0xFF == ord('q'): cv2.destroyAllWindows() break prediction = model.predict([screen.reshape(320, 240, 1)])[0] b = np.array([]) print(a) for i in range(9): if prediction[i] <= 0.4: b = np.append(b, [0]) print("1") elif prediction[i] >= 0.4: b = np.append(b, [1]) print("0") print(b) b = b.tolist() if b == collect_data.w: directkeys.w() print("w") elif b == collect_data.a: directkeys.a() print("a") elif b == collect_data.s: directkeys.s() print("s") elif b == collect_data.d: directkeys.d() print("d") elif b == collect_data.nk: directkeys.nokey() print("nokey") elif b == collect_data.sa: directkeys.sa() print("sa") elif b == collect_data.sd: directkeys.sd() print("sd") elif b == collect_data.wa: directkeys.wa() print("wa") elif b == collect_data.wd: directkeys.wd() print("wd") else: print("something wrong") keys = getkeys.key_check() if 'T' in keys: if paused: paused = False print('unpaused!') time.sleep(1) else: print('Pausing!') paused = True time.sleep(1) print('Loop took {} seconds'.format(time.time() - last_time))
#! "D:\anaconda\python.exe """ Created on Sun Oct 4 12:46:42 2020 @author: Max """ import time from getkeys import key_check from data_collection import get_one_hot, keys_to_output one_hot = get_one_hot('wing_suit', add_shift=True) valid_keys = list(one_hot.values()) n_keys = 0 while n_keys < 1000: key = key_check() output = keys_to_output(key, one_hot) if output in valid_keys: print('valid output: ', output) else: print('invalid output: ', output) time.sleep(0.01)
def main(): last_time = time.time() vj.open() time.sleep(1) for i in list(range(4))[::-1]: print(i + 1) time.sleep(1) model_name = 'steer_augmentation.h5' model = load_model(model_name) # Load Yolo net = cv2.dnn.readNet("yolov3-tiny.weights", "yolov3-tiny.cfg") classes = [] with open("coco.names.txt", "r") as f: classes = [line.strip() for line in f.readlines()] layer_names = net.getLayerNames() confidence_threshold = 0.6 paused = False while (True): if not paused: '''-------------Screenshot for YOLO and CNN------------------------''' screen = grab_screen( region=(0, 40, 800, 640)) # take screen shot of the screen img = screen #make a copy for YOLOv3 window_width = 800 img = img[:, round(window_width / 4):round(window_width * 3 / 4)] img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) height, width, channels = img.shape #window_width = width print("FPS: ", 1 / (time.time() - last_time)) #print FPS last_time = time.time() '''-------------------resize and reshape the input image for CNN----------------''' screen = cv2.cvtColor(screen, cv2.COLOR_BGR2GRAY) screen = cv2.resize(screen, (160, 120)) prediction = model.predict([screen.reshape(-1, 160, 120, 1)])[0] print(prediction) steering_angle = prediction[0] #if steering_angle > 0.20 or steering_angle <-0.20: #steering_angle = steering_angle*1.5 throttle = prediction[1] brake = 0 if throttle >= 0: throttle = throttle / 2 else: throttle = -0.25 #-----------------YOLO IMPLEMENTATION--------------------------------- blob = cv2.dnn.blobFromImage(img, 0.00392, (416, 416), (0, 0, 0), True, crop=False) net.setInput(blob) outs = net.forward(output_layers) # class_ids = [] confidences = [] boxes = [] for out in outs: for detection in out: scores = detection[5:] class_id = np.argmax(scores) confidence = scores[class_id] if confidence > confidence_threshold: # Object detected center_x = int(detection[0] * width) center_y = int(detection[1] * height) w = int(detection[2] * width) h = int(detection[3] * height) # Rectangle coordinates x = int(center_x - w / 2) y = int(center_y - h / 2) boxes.append([x, y, w, h]) confidences.append(float(confidence)) class_ids.append(class_id) indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4) for i in range(len(boxes)): if i in indexes: if (classes[class_ids[i]] == 'car' or classes[class_ids[i]] == 'truck' or classes[class_ids[i]] == 'person' or classes[class_ids[i]] == 'motorbike' or classes[class_ids[i]] == 'bus' or classes[class_ids[i]] == 'train' or classes[class_ids[i]] == 'stop sign' ): # person bicycle car motorbike bus train stop sign #PressKey(S) x, y, w, h = boxes[i] diag_len = np.sqrt((w * w) + (h * h)) #print (diag_len) if diag_len >= 150: # Detected object is too close so STOP print("STOP") throttle = -1 steering_angle = 0 brake = 1 elif diag_len >= 50 and diag_len < 150: # Detected object is near by so SLOW DOWN print("SLOW") if throttle >= 0: throttle = throttle / 2 else: throttle = throttle - ((1 + throttle) / 2) else: #Detected object is far print("JUST DRIVE") if throttle >= 0: throttle = throttle / 2 else: throttle = throttle - ((1 + throttle) / 2) setJoy_Steer_Throttle_Brake(steering_angle, throttle, brake) time.sleep(0.0001) keys = key_check() # p pauses game and can get annoying. if 'T' in keys: if paused: paused = False time.sleep(1) else: paused = True steering_angle = 0 throttle = -1 brake = 0 setJoy_Steer_Throttle_Brake(steering_angle, throttle, brake) time.sleep(1)
def main(): last_time = time.time() for i in list(range(4))[::-1]: print(i + 1) time.sleep(1) paused = False mode_choice = 0 screen = grab_screen(region=(0, 40, GAME_WIDTH, GAME_HEIGHT + 30)) screen = cv2.cvtColor(screen, cv2.COLOR_BGR2RGB) prev = cv2.resize(screen, (WIDTH, HEIGHT)) t_minus = prev t_now = prev t_plus = prev while (True): if not paused: screen = grab_screen(region=(200, 250, 200 + GAME_WIDTH, 250 + GAME_HEIGHT)) screen = cv2.cvtColor(screen, cv2.COLOR_BGR2RGB) # cv2.imshow('window', screen) # if cv2.waitKey(25) & 0xFF == ord('q'): # cv2.destroyAllWindows() # break last_time = time.time() screen = cv2.resize(screen, (WIDTH, HEIGHT)) # print(last_time) # delta_count_last = motion_detection(t_minus, t_now, t_plus) # t_minus = t_now # t_now = t_plus # t_plus = screen # t_plus = cv2.blur(t_plus,(4,4)) prediction = model.predict([screen.reshape(WIDTH, HEIGHT, 3)])[0] # print("%.5f" % (prediction)) # prediction = np.array(prediction) * np.array([4.5, 0.1, 0.1, 0.1, 1.8, 1.8, 0.5, 0.5, 0.2]) # prediction = np.array(prediction) * np.array([1.0, 0.1, 0.1, 0.1, 1.0, 1.0, 0.5, 0.5, 1.0]) prediction = np.array(prediction) * weights # Print confidence level for each output print("w: %.5f" % (prediction[0])) print("s: %.5f" % (prediction[1])) print("a: %.5f" % (prediction[2])) print("d: %.5f" % (prediction[3])) print("wa: %.5f" % (prediction[4])) print("wd: %.5f" % (prediction[5])) print("sa: %.5f" % (prediction[6])) print("sd: %.5f" % (prediction[7])) print("nk: %.5f" % (prediction[8])) # print('prediction: {}'.format(prediction[1])) mode_choice = np.argmax(prediction) # print(mode_choice) if mode_choice == 0: straight() choice_picked = 'straight' elif mode_choice == 1: reverse() choice_picked = 'reverse' elif mode_choice == 2: left() choice_picked = 'left' elif mode_choice == 3: right() choice_picked = 'right' elif mode_choice == 4: forward_left() choice_picked = 'forward+left' elif mode_choice == 5: forward_right() choice_picked = 'forward+right' elif mode_choice == 6: reverse_left() choice_picked = 'reverse+left' elif mode_choice == 7: reverse_right() choice_picked = 'reverse+right' elif mode_choice == 8: no_keys() choice_picked = 'nokeys' # motion_log.append(delta_count) # motion_avg = round(mean(motion_log),3) # print('loop took {} seconds. Motion: {}. Choice: {}'.format( round(time.time()-last_time, 3) , motion_avg, choice_picked)) print('loop took {} seconds. Choice: {}'.format( round(time.time() - last_time, 3), choice_picked)) # if motion_avg < motion_req and len(motion_log) >= log_len: # print('WERE PROBABLY STUCK FFS, initiating some evasive maneuvers.') # # 0 = reverse straight, turn left out # # 1 = reverse straight, turn right out # # 2 = reverse left, turn right out # # 3 = reverse right, turn left out # quick_choice = random.randrange(0,4) # if quick_choice == 0: # reverse() # time.sleep(random.uniform(1,2)) # forward_left() # time.sleep(random.uniform(1,2)) # elif quick_choice == 1: # reverse() # time.sleep(random.uniform(1,2)) # forward_right() # time.sleep(random.uniform(1,2)) # elif quick_choice == 2: # reverse_left() # time.sleep(random.uniform(1,2)) # forward_right() # time.sleep(random.uniform(1,2)) # elif quick_choice == 3: # reverse_right() # time.sleep(random.uniform(1,2)) # forward_left() # time.sleep(random.uniform(1,2)) # for i in range(log_len-2): # del motion_log[0] keys = key_check() # p pauses game and can get annoying. if 'T' in keys: if paused: paused = False print('Paused') time.sleep(1) else: paused = True ReleaseKey(A) ReleaseKey(W) ReleaseKey(D) time.sleep(1)
def main(): ''' with the z axis, your %s are out of 32,786 with the x and y, your %s are out of 16393 ...so left = 16393 - (some % of 16393) ... right = 16393 + (some % of 16393) ''' ################ XYRANGE = 16393 ZRANGE = 32786 wAxisX = 16393 wAxisY = 16393 wAxisZ = 0 wAxisXRot = 16393 wAxisYRot = 16393 wAxisZRot = 0 last_time = time.time() for i in list(range(4))[::-1]: print(i+1) time.sleep(1) #how_long_since_move = 0 paused = False mode_choice = 0 screen = grab_screen(region=(0,40,GAME_WIDTH,GAME_HEIGHT+40)) screen = cv2.cvtColor(screen, cv2.COLOR_BGR2RGB) prev = cv2.resize(screen, (160,90)) t_minus = prev t_now = prev t_plus = prev while(True): if not paused: screen = grab_screen(region=(0,40,GAME_WIDTH,GAME_HEIGHT+40)) screen = cv2.cvtColor(screen, cv2.COLOR_BGR2RGB) last_time = time.time() screen = cv2.resize(screen, (160,90)) delta_view = delta_images(t_minus, t_now, t_plus) retval, delta_view = cv2.threshold(delta_view, 16, 255, 3) cv2.normalize(delta_view, delta_view, 0, 255, cv2.NORM_MINMAX) img_count_view = cv2.cvtColor(delta_view, cv2.COLOR_RGB2GRAY) delta_count = cv2.countNonZero(img_count_view) dst = cv2.addWeighted(screen,1.0, delta_view,0.6,0) now=time.time() delta_count_last = delta_count t_minus = t_now t_now = t_plus t_plus = screen t_plus = cv2.blur(t_plus,(4,4)) o_prediction = model.predict([screen.reshape(160,90,3)])[0] # w s a d wa wd sa sd nk prediction = np.array(o_prediction) * np.array([4.5, 0.1, 0.1, 0.1, 1.8, 1.8, 0.5, 0.5, 0.2]) ## w s a d wa wd sa sd nk joy_choices = np.array(o_prediction) * np.array([4.5, 2.0, 1.0, 1.0, 1.8, 1.8, 1.0, 1.0, 1.0]) # could in theory be a negative. # w s sa sd nk throttle = joy_choices[0] - joy_choices[1] - joy_choices[6] - joy_choices[7] - joy_choices[8] # - is left.. .+ is right. (16393 + (-/+ up to 16393)) # a wa sa d wd sd turn = (-1*joy_choices[2]) +(-1*joy_choices[4]) +(-1*joy_choices[6]) + joy_choices[3] + joy_choices[5] + joy_choices[7] if throttle < -1 : throttle = -1 elif throttle > 1 : throttle = 1 if turn < -1 : turn = -1 elif turn > 1 : turn = 1 motion_log.append(delta_count) motion_avg = round(mean(motion_log),3) fps = 1 / round(time.time()-last_time, 3) if throttle > 0: vj.open() joystickPosition = vj.generateJoystickPosition(wAxisZ=int(ZRANGE*throttle),wAxisX=int(XYRANGE + (turn*XYRANGE))) vj.update(joystickPosition) time.sleep(0.001) vj.close() print('FPS {}. Motion: {}. ThumbXaxis: {}. Throttle: {}. Brake: {}'.format(fps , motion_avg, int(XYRANGE + (turn*XYRANGE)), int(ZRANGE*throttle),0)) else: vj.open() joystickPosition = vj.generateJoystickPosition(wAxisZRot=int(-1*(ZRANGE*throttle)),wAxisX=int(XYRANGE + (turn*XYRANGE))) vj.update(joystickPosition) time.sleep(0.001) vj.close() print('FPS {}. Motion: {}. ThumbXaxis: {}. Throttle: {}. Brake: {}'.format(fps , motion_avg, int(XYRANGE + (turn*XYRANGE)), 0, int(-1*(ZRANGE*throttle)))) mode_choice = np.argmax(prediction) if motion_avg < motion_req and len(motion_log) >= log_len: print('WERE PROBABLY STUCK FFS, initiating some evasive maneuvers.') # 0 = reverse straight, turn left out # 1 = reverse straight, turn right out # 2 = reverse left, turn right out # 3 = reverse right, turn left out quick_choice = random.randrange(0,4) if quick_choice == 0: reverse() time.sleep(random.uniform(1,2)) forward_left() time.sleep(random.uniform(1,2)) elif quick_choice == 1: reverse() time.sleep(random.uniform(1,2)) forward_right() time.sleep(random.uniform(1,2)) elif quick_choice == 2: reverse_left() time.sleep(random.uniform(1,2)) forward_right() time.sleep(random.uniform(1,2)) elif quick_choice == 3: reverse_right() time.sleep(random.uniform(1,2)) forward_left() time.sleep(random.uniform(1,2)) for i in range(log_len-2): del motion_log[0] keys = key_check() # p pauses game and can get annoying. if 'T' in keys: ultimate_release() if paused: paused = False time.sleep(1) else: paused = True ReleaseKey(A) ReleaseKey(W) ReleaseKey(D) time.sleep(1)
window_size = (76,110,876,570) if os.path.isfile(file_name): print("file exists , loading previous data") training_data = list(np.load(file_name,allow_pickle=True)) else: print("file don't exists , create new one") training_data = [] for i in list(range(wait_time))[::-1]: print(i+1) time.sleep(1) last_time = time.time() while(True): output_key = getkeys.get_key(getkeys.key_check())#按键收集 if output_key == [1,1,1,1,1,1,1,1,1,1,1,1]: print(len(training_data)) np.save(file_name,training_data) break screen_gray = cv2.cvtColor(grabscreen.grab_screen(window_size),cv2.COLOR_BGR2GRAY)#灰度图像收集 screen_reshape = cv2.resize(screen_gray,(200,145)) training_data.append([screen_reshape,output_key]) if len(training_data) % 1500 == 0: print(len(training_data)) np.save(file_name,training_data) cv2.imshow('window1',screen_reshape)
def readChat(link='https://www.youtube.com/live_chat?is_popout=1&v=0Ku4f56pj-U'): global readDelay,keyStack,paused,refreshRate,browser,root,isExit #Parameters msgID = [0] actualMsg = "Null" gameLoop = 0 newMsg = [] newMsgId = 0 #Loading the page browser.get(link) print("\n\n[!] Script is Paused By Default\n- To Resume the script Press '1','2' and '3' at the same time\n- To Quit Press '4','5','6' at the same time\n") print(">> Script Paused <<") #Main Loop while True: gameLoop += 1 if not paused: #Extracting Latest chat message try: msgs = browser.find_elements_by_css_selector('yt-live-chat-text-message-renderer') newMsg = msgs[len(msgs)-1] actualMsg = deEmojify(newMsg.find_element_by_id('message').text) newMsgId = newMsg.id except : pass print(actualMsg) #Differentiating new message if newMsgId != msgID[len(msgID)-1]: msgID.append(newMsgId) #Processing The Keys processKey(actualMsg,gameLoop,readDelay) #Processing the Key Stack for index,keyData in enumerate(keyStack): if keyData[1] >= gameLoop: PressKey(keyData[0]) else: PressKey(keyData[0]) ReleaseKey(keyData[0]) keyStack.remove(keyData) #For Popular live-streamers :P time.sleep(readDelay) else: time.sleep(readDelay) #Pausing The Loop #To pause the script press 1,2,3,4 all together keys = key_check() if '1' in keys and '2' in keys and '3' in keys: paused = not paused if paused: print(">> Script Paused <<") if not paused: print(">> Script Unpaused <<") elif '4' in keys and '5' in keys and '6' in keys: isExit = True #Saving From overflow if len(msgID) % refreshRate == 0: msgID = [0] browser.get(link) print("--------------- Refreshed --------------") if isExit: closeBrowser() break
model = load_model_weights('v1_nfs_ai') print("Starting !!!") time.sleep(1) print("1") time.sleep(1) print("2") time.sleep(1) print("3") time.sleep(1) print("4") print("GO go go go !!!!") while (True): current_view = grab_screen([0, 0, 800, 600]) reduced_view = cv2.resize(current_view, (shape[1], shape[0]), interpolation=cv2.INTER_AREA) reduced_view = reduced_view.reshape(1, shape[0], shape[1], 1) instant_move = key_check() res = model.predict(reduced_view) move = move_decode[np.argmax(res[0])] print("press ", move, " keycheck :", key_check()) cv2.imshow('frame', current_view) key_press[move]() if cv2.waitKey(1) == 27: break cv2.destroyAllWindows() cv2.waitKey(1) #save_model(model , "v1_testing")
def main(): last_time = time.time() for i in list(range(4))[::-1]: print(i + 1) time.sleep(1) paused = False while (True): if not paused: # 800x600 windowed mode # screen = np.array(ImageGrab.grab(bbox=(0,40,800,640))) img = grab_screen(region=GRAB_REGION) print('loop took {} seconds'.format(time.time() - last_time)) last_time = time.time() img = bit_mask(roi(cv2.resize(img, (WIDTH, HEIGHT)), ROI_VERTICES)) prediction = model.predict([img.reshape(WIDTH, HEIGHT, 1)])[0] print(prediction) #time.sleep(1) mx, my, press = prediction mx = int(mx * kx + cx) my = int(my * ky + cy) if mx > max_x: mx = max_x if mx < min_x: mx = min_x if my > max_y: my = max_y if my < min_y: my = min_y if press > 0: mouse_down(mx, my) else: mouse_up(mx, my) cv2.imshow('window', img) if cv2.waitKey(25) & 0xFF == ord('q'): cv2.destroyAllWindows() break keys = key_check() # p pauses game and can get annoying. if 'R' in keys: mouse_up(cx, cy) mouse_down(cx, cy) time.sleep(0.1) mouse_up(cx, cy) time.sleep(0.1) if 'T' in keys: if paused: paused = False time.sleep(1) else: mx = int(0 * kx + cx) my = int(0 * ky + cy) if mx > max_x: mx = max_x if mx < min_x: mx = min_x if my > max_y: my = max_y if my < min_y: my = min_y mouse_up(mx, my) paused = True time.sleep(1)
def main(): last_time = time.time() for i in list(range(4))[::-1]: print(i + 1) time.sleep(1) paused = False while (True): if not paused: # 800x600 windowed mode #screen = np.array(ImageGrab.grab(bbox=(0,40,800,640))) screen = grab_screen(region=(0, 40, 800, 640)) print('loop took {} seconds'.format(time.time() - last_time)) last_time = time.time() screen = cv2.cvtColor(screen, cv2.COLOR_BGR2GRAY) screen = cv2.resize(screen, (WIDTH, HEIGHT)) inp = np.reshape(screen, (1, 1, WIDTH, HEIGHT)) print(inp.shape) #inp = [screen.reshape(WIDTH,HEIGHT,1)] prediction = model.forward(torch.cuda.FloatTensor(inp))[0] print(prediction) turn_thresh = .75 fwd_thresh = 0.70 slow_tresh = 0.8 if prediction[1] > fwd_thresh: straight() elif prediction[0] > turn_thresh: left() elif prediction[2] > turn_thresh: right() elif prediction[3] > slow_tresh: slow() else: straight() keys = key_check() # p pauses game and can get annoying. if 'T' in keys: if paused: paused = False time.sleep(1) else: paused = True ReleaseKey(A) ReleaseKey(W) ReleaseKey(D) time.sleep(1)
def main(): for i in list(range(4))[::-1]: print(i + 1) time.sleep(1) paused = False mode_choice = 0 while (True): if not paused: screen = grab_screen(region=(0, 40, GAME_WIDTH, GAME_HEIGHT + 40)) screen = cv2.cvtColor(screen, cv2.COLOR_BGR2RGB) screen = cv2.resize(screen, (WIDTH, HEIGHT)) prediction = model.predict([screen.reshape(WIDTH, HEIGHT, 3)])[0] prediction = np.array( prediction) #* np.array([1.12, 1, 1, 1, 1, 1, 1, 1, 0.2]) print(prediction) # div = 4 # wplus = prediction[0] + (prediction[4] / div) + (prediction[5] / div) # print(wplus) # splus = prediction[1] + (prediction[6] / div) + (prediction[7] / div) # print(splus) # aplus = prediction[2] + (prediction[6] / div) + (prediction[4] / div) # print(aplus) # dplus = prediction[3] + (prediction[5] / div) + (prediction[7] / div) # print(dplus) # waplus = prediction[4] + (prediction[0] / div) + (prediction[2] / div) # print(waplus) # wdplus = prediction[5] + (prediction[0] / div) + (prediction[3] / div) # print(wdplus) # saplus = prediction[6] + (prediction[2] / div) + (prediction[1] / div) # print(saplus) # sdplus = prediction[7] + (prediction[1] / div) + (prediction[3] / div) # print(sdplus) # # mode_choice_v2 = np.argmax([wplus, splus, aplus, dplus, waplus, wdplus, saplus, sdplus]) # print(mode_choice_v2) mode_choice = np.argmax(prediction) print(mode_choice) if mode_choice == 0: straight() choice_picked = 'straight' print(choice_picked) elif mode_choice == 1: reverse() choice_picked = 'reverse' print(choice_picked) elif mode_choice == 2: left() choice_picked = 'left' print(choice_picked) elif mode_choice == 3: right() choice_picked = 'right' print(choice_picked) elif mode_choice == 4: forward_left() choice_picked = 'forward+left' print(choice_picked) elif mode_choice == 5: forward_right() choice_picked = 'forward+right' print(choice_picked) elif mode_choice == 6: reverse_left() choice_picked = 'reverse+left' print(choice_picked) elif mode_choice == 7: reverse_right() choice_picked = 'reverse+right' print(choice_picked) elif mode_choice == 8: no_keys() choice_picked = 'nokeys' print(choice_picked) keys = key_check() # p pauses game and can get annoying. if 'T' in keys: if paused: paused = False time.sleep(1) print("Unpaused") else: paused = True ReleaseKey(A) ReleaseKey(W) ReleaseKey(D) time.sleep(1) print("Paused")