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(): 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 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)
import time import cv2 from grabscreen import grab_screen # import numpy as np # final_image = np.zeros((80, 60, 3), dtype=np.int) time.sleep(3) for i in range(10): # screen = grab_screen(region=(590, 485, 635, 480 + 30)) screen = grab_screen(region=(0, 0, 640, 480 + 40)) screen = cv2.cvtColor(screen, cv2.COLOR_BGR2RGB) speed_image = screen[485:510, 590:635] screen_cropped = screen[75:465, 10:630] screen_cropped_resized = cv2.resize(screen_cropped, (62, 39), 0, 0, 0, cv2.INTER_AREA) speed_image_resized = cv2.resize(speed_image, (18, 10), 0, 0, 0, cv2.INTER_AREA) combined = screen_cropped_resized combined[-len(speed_image_resized):, int( len(screen_cropped_resized[1]) / 2 - len(speed_image_resized[1]) / 2):int( len(screen_cropped_resized[1]) / 2 + len(speed_image_resized[1]) / 2)] = speed_image_resized cv2.imwrite("whole_screen-{}.png".format(i), screen) cv2.imwrite("speed-{}.png".format(i), speed_image) cv2.imwrite("cropped-{}.png".format(i), screen_cropped)
#TLD Models Parameters TLD_MODEL_SIZE = (120, 60, 3) MODEL_NAME = "TLD.model" TRAFFIC_LIGHT = 9 lightClasses = ["Red", "Yellow", "Green"] #Model parameters tldModel = TLDModel() tldModel.load_weights(MODEL_NAME) lightClass = None index = 2000 while(True): #Capture screen and convert to RGB screen = grab_screen(SCREEN_REGION) screen = cv2.cvtColor(screen, cv2.COLOR_BGR2RGB) #Detect objects using YOLO boxes, classes, scores = processYolo(screen) #Classify traffic light colour (if there are any) if(classes is not None and TRAFFIC_LIGHT in classes): tlImage, tlPosition = extractLaneTrafficLight(screen, boxes, classes, scores) prediction = tldModel.predict(np.expand_dims(tlImage, axis=0))[0] lightClass = np.argmax(prediction) highlightSeenTrafficLight(screen, tlPosition) else: lightClass = None #Uncomment to show ALL boxes on screen
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)
stats = [] L_num = 0 while True: st_time = time.time() loop_time_1 = time.time() # DONE: smarter logic about bomb location/placement to kill enemy and bomb timing # bomb route clipping? (to avoid trying to go to somewhere that will be unavailable because of bomb(s)) # if((L_num%4)!=1): # getting the window mode screen screen = grab_screen(region=(anchorHWidthTopLeft, anchorHeightTopLeft, anchorWidthBotRight, anchorHeightBotRight)) # pixels characters 2 # screen = cv2.resize(screen, (int(WIDTH / 2), int(HEIGTH / 2))) # screen = cv2.resize(screen, (int(WIDTH / 8), int(HEIGTH / 8))) loop_time_11 = time.time() # run a color convert: screen = cv2.cvtColor(screen, cv2.COLOR_BGR2RGB) # ret, screen = camera.read() loop_time_111 = time.time() getPlayerPosition = GetPlayerPosition(screen, 1)
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))
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
# ## Helper code 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) # Size, in inches, of the output images. IMAGE_SIZE = (12, 8) with detection_graph.as_default(): with tf.Session(graph=detection_graph) as sess: while True: #screen = cv2.resize(grab_screen(region=(0,40,1280,745)), (WIDTH,HEIGHT)) screen = cv2.resize(grab_screen(region=(0, 40, 1280, 745)), (1000, 650)) image_np = cv2.cvtColor(screen, cv2.COLOR_BGR2RGB) # Expand dimensions since the model expects images to have shape: [1, None, None, 3] image_np_expanded = np.expand_dims(image_np, axis=0) image_tensor = detection_graph.get_tensor_by_name('image_tensor:0') # Each box represents a part of the image where a particular object was detected. boxes = detection_graph.get_tensor_by_name('detection_boxes:0') # Each score represent how level of confidence for each of the objects. # Score is shown on the result image, together with the class label. 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') # Actual detection. (boxes, scores, classes, num_detections) = sess.run(
def toward_realm(win_location): pyautogui.keyDown('z') pyautogui.keyUp('z') pyautogui.keyDown('w') realm_location = cv2.imread('realm.png', 0) location_aq = False #got realm location w, h = realm_location.shape is_stuck = 0 win_location = (int(win_location[0]+(win_location[2] - win_location[0])*0.75), win_location[1], win_location[2], int(win_location[1]+(win_location[3] - win_location[1])*0.33)) #win_location = (win_location[0]+20, win_location[1]+20, win_location[2]-20, win_location[3]-20) #add temp counter = 0 screen = np.array(grab_screen(region=win_location), dtype='uint8') frame = cv2.cvtColor(screen, cv2.COLOR_BGR2GRAY) center = (int(frame.shape[0]/2), int(frame.shape[1]/2)) while frame.any(): #print(frame.any()) res = cv2.matchTemplate(frame,realm_location, cv2.TM_SQDIFF) min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res) min_val = min_val/10000000 #print(min_val) if min_val < 0.7 and not location_aq: #location_aq = realm_location_get(screen, center, 0) pyautogui.keyUp('w') print('location detected..') location_aq = True elif location_aq: if counter%15==0: seed = random.randrange(0,100) realm_location_get(screen, center, seed, True) counter+=1 else: if is_stuck%100 == 0: print('im stuck') stuck_direction = int((is_stuck/100)) if stuck_direction > 0: if (stuck_direction)%2==0: for i in range(stuck_direction): pyautogui.keyDown('a') pyautogui.keyUp('a') else: for i in range(stuck_direction): pyautogui.keyDown('d') pyautogui.keyUp('d') is_stuck+=1 top_left = min_loc bottom_right = (top_left[0] + w, top_left[1] + h) cv2.rectangle(frame,top_left, bottom_right, 255, 2) #cv2.imshow('output', frame) if cv2.waitKey(25) & 0xFF == ord('q'): cv2.destroyAllWindows() break screen = np.array(grab_screen(region=win_location), dtype='uint8') frame = cv2.cvtColor(screen, cv2.COLOR_BGR2GRAY)
count_down(0) print(win_location) next_image = cv2.imread('next.png', 0) pet_yard = cv2.imread('petyard.png', 0) #toward_realm(win_location) #cv2.namedWindow('output') #cv2.setMouseCallback('output',mouse_cb) nexus = False nexusing = False tp=False check=False while True: screen = np.array(grab_screen(region=win_location), dtype='uint8') frame = cv2.cvtColor(screen, cv2.COLOR_BGR2RGB) game_win, map_win, fame_level, hp_level, mana_level = process_game_frame(frame) if not nexus: hp = get_hp(hp_level) mana = get_mana(mana_level) print('HP:{} mana:{}'.format(hp, mana)) if hp < 30: if check_special(hp_level): print("Entering Dongen") watch_done(win_location) time.sleep(2) else:
def initial_population(pred): if (pred == 1): model = neural_network_model() model.load_weights('my_model_weights.h5') # [OBS, MOVES] training_data = [] # all scores: scores = [] # just the scores that met our threshold: accepted_scores = [] # iterate through however many games we want: for game in range(initial_games): print(game) score = 0 # moves specifically from this environment: game_memory = [] # previous observation that we saw prev_observation = [] # for each frame in 200 PressKey(0x20) ReleaseKey(0x20) for frame in range(goal_steps): # choose random action (0 or 1) observation = grab_screen(region=(0, 0, 1919, 1079)) observation = cv2.cvtColor(observation, cv2.COLOR_BGR2GRAY) observation = cv2.resize(observation, (256, 144)) done = IsDone() reward = 1 action = 0 if (pred == 1): observation = observation.reshape(-1, HEIGHT, WIDTH, 1) action = np.argmax(model.predict(observation)) #print(model.predict(observation)) print(action) if action == 1: PressKey(0x26) ReleaseKey(0x26) if action == 2: PressKey(0x28) ReleaseKey(0x28) elif (pred == 2): if win32api.GetAsyncKeyState(win32con.VK_UP) != 0: action = 1 if win32api.GetAsyncKeyState(win32con.VK_DOWN) != 0: action = 2 if win32api.GetAsyncKeyState(0x51) != 0: done = True else: action = random.randrange(0, 2) if action == 1: PressKey(0x26) ReleaseKey(0x26) if action == 2: PressKey(0x28) ReleaseKey(0x28) if len(prev_observation) > 0: game_memory.append([prev_observation, action]) prev_observation = observation score += reward if done: print(score) time.sleep(2) break if frame == goal_steps: break if score >= score_requirement: accepted_scores.append(score) for data in game_memory: training_data.append([data[0], action]) scores.append(score) if training_data: try: prev_data = list(np.load('saved.npy', allow_pickle=True)) prev_data.append(training_data) except Exception as e: print(e) # just in case you wanted to reference later training_data_save = np.array(training_data) np.save('saved.npy', training_data_save) # some stats here, to further illustrate the neural network magic! print('Average accepted score:', mean(accepted_scores)) print('Median score for accepted scores:', median(accepted_scores)) print(Counter(accepted_scores)) return training_data
# from grabscreen import grab_screen # screen = cv2.resize(grab_screen(region=(0,40,1280,745)), (800,450)) # image_np = cv2.cvtColor(screen, cv2.COLOR_BGR2RGB) # show_inference(detection_model, image_np) # from grabscreen import grab_screen import cv2 import time last_time = time.time() while True: screen = cv2.resize(grab_screen(region=(0, 40, 1000, 700)), (800, 450)) image_np = cv2.cvtColor(screen, cv2.COLOR_BGR2RGB) output_dict = run_inference_for_single_image(detection_model, image_np) vis_util.visualize_boxes_and_labels_on_image_array( image_np, output_dict['detection_boxes'], output_dict['detection_classes'], output_dict['detection_scores'], category_index, instance_masks=output_dict.get('detection_masks_reframed', None), use_normalized_coordinates=True, line_thickness=8) cv2.imshow('window', image_np)
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!!!') intI = 0 while(True): if(paused == True): time.sleep(1) paused = False # DONE: find the proper anchor while paused==False: print("========================") # print("another frame") last_time = time.time() if (keyboard.is_pressed('n') == True): paused = True # getting the window mode screen screen = grab_screen(region=(anchorHWidthTopLeft,anchorHeightTopLeft, anchorWidthBotRight,anchorHeightBotRight)) last_time = time.time() # pixels characters 2 # resize to something a bit more acceptable for a CNN screen = cv2.resize(screen, (int(WIDTH/2), int(HEIGTH/2))) # run a color convert: screen = cv2.cvtColor(screen, cv2.COLOR_BGR2RGB) # which key is pressed key = what_keys_is_pressed() print("key:"+str(key)) output = key if(key==-1): print("if(key==-1):") else: if(1): intI = intI + 1 print("intI:"+str(intI)) training_data.append([screen,output]) if len(training_data) % 100 == 0: print("len(training_data):" + str(len(training_data))) if len(training_data) == 500: np.save(file_name, training_data) print('SAVED') training_data = [] starting_value += 1 file_name = './phase-1/training_data-{}.npy'.format(starting_value) #print('loop took {} seconds'.format(time.time()-last_time)) last_time = time.time() # print(type(screen)) # too see what is captured cv2.imshow('screen',cv2.cvtColor(screen, cv2.COLOR_BGR2RGB)) if cv2.waitKey(25) & 0xFF == ord('t'): cv2.destroyAllWindows() break
obstacle_height = 0 should_crouch = False jumpdist = 175 is_crouching = False MAX_SPEED_TIME = 118 DINO_WALKING_HEIGHT = 110 while (True): leftest = 1000 if (time.time() - second >= 1 and time.time() - start_time < MAX_SPEED_TIME): jumpdist += 1.7 second = time.time() # jump_dist = 170 + (time.time() - start_time) *1.80 pts = [] scr = grab_screen(region=(75, 250, 750, 450)) # scr = cv2.cvtColor(scr, cv2.COLOR_BGR2RGB) scr_gray = cv2.cvtColor(scr, cv2.COLOR_BGR2GRAY) res_dino = cv2.matchTemplate(scr_gray, dino, cv2.TM_CCOEFF_NORMED) threshold = 0.8 loc_dino = np.where(res_dino >= threshold) for pt in zip(*loc_dino[::-1]): cv2.rectangle(scr, pt, (pt[0] + w_dino, pt[1] + h_dino + 9), (50, 205, 50), 1) dinoX = pt[0] + w_dino dinoH = pt[1] is_crouching = False for cactus in cacti: res = cv2.matchTemplate(scr_gray, cactus, cv2.TM_CCOEFF_NORMED)
return masked_img LD = Lane_Detector() while (True): tic = time.time() # First Try - using PIL ImageGrab method - gives less frame rate # orig_screengrab = np.array(ImageGrab.grab(bbox=(0,40,640,520))) # Grabbing + Displaying takes around 0.15 secs per frame of size(480,640) # Capture Region based on resolution # 40px to allow for the window title bar, bottom = 480 + 40 = 520 capture_region = (0, 40, 640, 520) orig_screengrab = grab_screen(capture_region) # Grabbing + Displaying takes around 0.03 secs per frame of size(480,640) mask_birdeye, mask_1ch, lane_masked_image, roi_extracted_image, orig_image = LD.process_screengrab( orig_screengrab, capture_region) #print(processed_screengrab.shape) cv2.imshow('Original ScreeGrab', cv2.cvtColor(orig_screengrab, cv2.COLOR_RGB2BGR)) cv2.imshow('Region of Interest Extracted', cv2.cvtColor(roi_extracted_image, cv2.COLOR_RGB2BGR)) cv2.imshow('Lane Masked Image', cv2.cvtColor(lane_masked_image, cv2.COLOR_RGB2BGR)) cv2.imshow('Binary Mask', mask_1ch) cv2.imshow('Mask after Bird Eye Transform', mask_birdeye)
ReleaseKey(D) def slow_ya_roll(): ReleaseKey(W) ReleaseKey(A) ReleaseKey(D) for i in list(range(4))[::-1]: print(i+1) time.sleep(1) last_time = time.time() while True: screen = grab_screen(region=(0,40,800,640)) print('Frame took {} seconds'.format(time.time()-last_time)) last_time = time.time() new_screen,original_image, m1, m2 = process_img(screen) #cv2.imshow('window', new_screen) cv2.imshow('window2',cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB)) if m1 < 0 and m2 < 0: right() elif m1 > 0 and m2 > 0: left() else: straight() #cv2.imshow('window',cv2.cvtColor(screen, cv2.COLOR_BGR2RGB)) if cv2.waitKey(25) & 0xFF == ord('q'):
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 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) stop = False print('STARTING!!!') intI = 0 # supposed to be at least not a dead player at the start numbersOFDeathInLastSeconds = 0 p1scoreAS, p1killAS, p1deathAS = getScoreKillsDeaths() # AS: At Start print("at start p1scoreAS, p1killAS, p1deathAS", p1scoreAS, p1killAS, p1deathAS) while(True): print("========================") numberOfKeys = len(all_keys) print("numberOfKeys",numberOfKeys) roundEnded = False i = 0 # screenshotTaken = [] # keyIssued = [] if(stop == True): break if(starting_value > 100): exit() game_data = [] while(numbersOFDeathInLastSeconds==1): # 1 or >1 if a players in the lasts seconds tmpOffset = int(0x455C9C) print("tmpOffset", '%s' % hex(tmpOffset)) ReadProcessMemory(ph, c.c_void_p(tmpOffset), buff, bufferSize, c.byref(bytesRead)) numbersOFDeathInLastSeconds = unpack('I', buff)[0] print("numbersOFDeathInLastSeconds", numbersOFDeathInLastSeconds) while(roundEnded == False): # choosedKey = random.randint(numberOfKeys) choosedKey = random.randint(300000) % 6 print("choosedKey",choosedKey) # getting the window mode screen screen = grab_screen(region=(anchorHWidthTopLeft, anchorHeightTopLeft, anchorWidthBotRight, anchorHeightBotRight)) # pixels characters 2 # resize to something a bit more acceptable for a CNN screen = cv2.resize(screen, (int(WIDTH / 2), int(HEIGTH / 2))) # run a color convert: screen = cv2.cvtColor(screen, cv2.COLOR_BGR2RGB) timePress = ( 0.05 + 0.01 * random.randint(7) ) / SPEEDHACK_SPEED # e = [1,0,0,0,0,0] # d = [0,1,0,0,0,0] # s = [0,0,1,0,0,0] # f = [0,0,0,1,0,0] # ctrl = [0,0,0,0,1,0] # none = [0,0,0,0,0,1] # all_keys = [e,d,s,f,ctrl,none] if(choosedKey == 0): keyboard.press('e') time.sleep(timePress) keyboard.release('e') if(choosedKey == 1): keyboard.press('d') time.sleep(timePress) keyboard.release('d') if(choosedKey == 2): keyboard.press('s') time.sleep(timePress) keyboard.release('s') if(choosedKey == 3): keyboard.press('f') time.sleep(timePress) keyboard.release('f') if(choosedKey == 4): keyboard.press('ctrl') time.sleep(timePress) keyboard.release('ctrl') if (choosedKey == 5): print("none") # debugging to test before firing the script for hours # time.sleep(timePress) if (keyboard.is_pressed('p') == True): stop = True keyboard.release('ctrl') keyboard.release('e') keyboard.release('d') keyboard.release('s') keyboard.release('f') break # screenshotTaken.append(screen) # keyIssued.append(choosedKey) game_data.append([screen, choosedKey]) # TODO: check when player2 is dead # TODO: check for variable set to one in memory # when the round is restarting/ending # 1 if a players in the lasts seconds tmpOffset = int(0x4440B0) print("tmpOffset", '%s' % hex(tmpOffset)) ReadProcessMemory(ph, c.c_void_p(tmpOffset), buff, bufferSize, c.byref(bytesRead)) aplayerDown = unpack('I', buff)[0] print("playerDown", aplayerDown) # 1 or >1 if a players in the lasts seconds tmpOffset = int(0x455C9C) print("tmpOffset", '%s' % hex(tmpOffset)) ReadProcessMemory(ph, c.c_void_p(tmpOffset), buff, bufferSize, c.byref(bytesRead)) numbersOFDeathInLastSeconds = unpack('I', buff)[0] print("numbersOFDeathInLastSeconds", numbersOFDeathInLastSeconds) if(numbersOFDeathInLastSeconds!=0): p1scoreNew, p1killNew, p1deathNew = getScoreKillsDeaths() print("p1scoreNew, p1killNew, p1deathNew", p1scoreNew, p1killNew, p1deathNew) # drop the current capture if p1 and p2 died if(numbersOFDeathInLastSeconds==1): print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>") roundEnded = True # Mean the controlled player kill count changed if(p1killNew!=p1killAS): file_name = './phase-1-bruteforce/training_data-{}.npy'.format(starting_value) np.save(file_name, game_data) print('SAVED') starting_value += 1 keyboard.press('tab') time.sleep(0.01/SPEEDHACK_SPEED) keyboard.release('tab') time.sleep(0.01/SPEEDHACK_SPEED) # updating infos about the game state, a break is required to work properly p1killAS = p1killNew p1kdeathAS = p1deathNew p1scoreAS = p1killNew pass break else: print("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<") # # p1 and p2 are dead (including both killed themself) # if(numbersOFDeathInLastSeconds>1): # roundEnded = True # file_name = './phase-1-bruteforce/training_data-{}.npy'.format(starting_value) # np.save(file_name, game_data) # print('SAVED') # starting_value += 1 # # keyboard.press('tab') # time.sleep(0.01/SPEEDHACK_SPEED) # keyboard.release('tab') # time.sleep(0.01/SPEEDHACK_SPEED) # # # updating infos about the game state, a break is required to work properly # p1killAS = p1killNew # p1kdeathAS = p1deathNew # p1scoreAS = p1killNew # # break # stop the recording if it is too long (and kill the player ?) if(i == 50000): roundEnded = True break i+=1
screen = get_screen() obj_locations = get_objects_locations(screen) #Get center tuples ball_center = tuple(np.round(obj_locations[:2]).astype(int)) bar1_center = tuple(np.round(obj_locations[2:4]).astype(int)) bar2_center = tuple(np.round(obj_locations[4:6]).astype(int)) #Draw object locations on the image cv2.circle(screen, ball_center, 5, 128, -1) cv2.circle(screen, bar1_center, 5, 128, -1) cv2.circle(screen, bar2_center, 5, 128, -1) return screen, obj_locations if __name__ == "__main__": print("Fit pong screen into the window:") while True: screen = grab_screen(region=(650,300,950,600)) cv2.imshow("Capture Screen", screen) #Pass next frame every 25ms #Exit when 'q' is pressed if cv2.waitKey(25) == ord('q'): cv2.destroyAllWindows() break
window_size = (320, 104, 704, 448) #384,344 192,172 96,86 blood_window = (60, 91, 280, 562) for i in list(range(wait_time))[::-1]: print(i + 1) time.sleep(1) last_time = time.time() while (True): #printscreen = np.array(ImageGrab.grab(bbox=(window_size))) #printscreen_numpy = np.array(printscreen_pil.getdata(),dtype='uint8')\ #.reshape((printscreen_pil.size[1],printscreen_pil.size[0],3)) #pil格式耗时太长 screen_gray = cv2.cvtColor(grabscreen.grab_screen(blood_window), cv2.COLOR_BGR2GRAY) #灰度图像收集 # screen_reshape = cv2.resize(screen_gray,(96,86)) self_blood = self_blood_count(screen_gray) boss_blood = boss_blood_count(screen_gray) cv2.imshow('window1', screen_gray) #cv2.imshow('window3',printscreen) #cv2.imshow('window2',screen_reshape) #测试时间用 print('loop took {} seconds'.format(time.time() - last_time)) last_time = time.time() if cv2.waitKey(5) & 0xFF == ord('q'): break
ReleaseKey(D) def slow_ya_roll(): ReleaseKey(W) ReleaseKey(A) ReleaseKey(D) for i in list(range(4))[::-1]: print(i + 1) time.sleep(1) last_time = time.time() while True: screen = grab_screen(region=(0, 40, 800, 640)) print('Frame took {} seconds'.format(time.time() - last_time)) last_time = time.time() new_screen, original_image, m1, m2 = process_img(screen) #cv2.imshow('window', new_screen) cv2.imshow('window2', cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB)) if m1 < 0 and m2 < 0: right() elif m1 > 0 and m2 > 0: left() else: straight() #cv2.imshow('window',cv2.cvtColor(screen, cv2.COLOR_BGR2RGB)) if cv2.waitKey(25) & 0xFF == ord('q'):
click_point.append(x/capturing_resize[0]); click_point.append(y/capturing_resize[1]); for i in range(len(detected_items)): if isPointInsideBbox(click_point, detected_items[i]) == True: print("Inside") add_tracker(image_np,detected_items[i]) cv2.namedWindow('window') cv2.setMouseCallback('window',track_selected) with detection_graph.as_default(): with tf.Session(graph=detection_graph) as sess: while True: state_right = win32api.GetKeyState(0x02) if state_right == 0 or state_right == 1: screen = cv2.resize(grab_screen(region=(capturing_coordinates)), (capturing_resize)) image_np = cv2.cvtColor(screen, cv2.COLOR_BGR2RGB) # Expand dimensions since the model expects images to have shape: [1, None, None, 3] image_np_expanded = np.expand_dims(image_np, axis=0) image_tensor = detection_graph.get_tensor_by_name('image_tensor:0') # Each box represents a part of the image where a particular object was detected. boxes = detection_graph.get_tensor_by_name('detection_boxes:0') # Each score represent how level of confidence for each of the objects. # Score is shown on the result image, together with the class label. 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') # Actual detection. (boxes, scores, classes, num_detections) = sess.run( [boxes, scores, classes, num_detections], feed_dict={image_tensor: image_np_expanded})
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: screen = cv2.resize(grab_screen(region=(0,40,800,640)), (800,640)) image_np = cv2.cvtColor(screen, 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),
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
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, 2000)): train_data_collect(reduced_view, instant_move) frame = 0 cv2.imshow('rv', reduced_view) if cv2.waitKey(1) == 27: break print(moves_count, " ( ", len(X_train[0]), " ) ") cv2.destroyAllWindows() cv2.waitKey(1) save_train_data("second_batch")
#ReleaseKey(A) def right(): PressKey(D) ReleaseKey(A) ReleaseKey(W) #ReleaseKey(D) for i in list(range(4))[::-1]: print(i+1) time.sleep(1) while True: screen = grab_screen(region=(0,40,800,640)) print('Frame took {} seconds'.format(time.time()-last_time)) last_time = time.time() new_screen,original_image, m1, m2 = process_img(screen) #cv2.imshow('window', new_screen) cv2.imshow('window2',cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB)) if m1 < 0 and m2 < 0: right() elif m1 > 0 and m2 > 0: left() else: straight() sys.path.append("..")
conf_thres = 0.3 iou_thres = 0.2 classes = None agnostic_nms = True names = [ 'hero', 'small_map', "monster", 'money', 'material', 'door', 'BOSS', 'box', 'options' ] colors = [[random.randint(0, 255) for _ in range(3)] for _ in range(len(names))] if half: model.half() # to FP16 while (True): if not paused: img0 = grab_screen(window_size) print('loop took {} seconds'.format(time.time() - last_time)) last_time = time.time() img0 = cv2.cvtColor(img0, cv2.COLOR_BGRA2BGR) # Padded resize img = letterbox(img0, new_shape=img_size)[0] # Convert img = img[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB, to 3x416x416 img = np.ascontiguousarray(img) img = torch.from_numpy(img).to(device).unsqueeze(0) img = img.half() if half else img.float() # uint8 to fp16/32 img /= 255.0 # 0 - 255 to 0.0 - 1.0 t1 = time_synchronized()
model = load_model_weights('v2_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) right_view = reduced_view[:, 0:shape[1] - margin] left_view = reduced_view[:, margin:] cv2.imshow('frame', reduced_view) right_view = right_view.reshape(1, right_view.shape[0], right_view.shape[1], 1) left_view = left_view.reshape(1, left_view.shape[0], left_view.shape[1], 1) instant_move = key_check() res = model.predict({ 'right_input': right_view,
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(): ''' 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)
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 get_state(): state = grab_screen(region = capture_region) state = cv2.cvtColor(state, cv2.COLOR_BGR2GRAY) state = cv2.resize(state,(reshape_size[1],reshape_size[0])) #state = state.reshape(reshape_size[0],reshape_size[1],1) return state
v = 0 move = [0, 0, 0] isLeftPressedKey = False isRightPressedKey = False isSpacePressedKey = False window_upleft = None window_bottomright = None while (True): keys = key_check() if 'T' in keys or 't' in keys: break screen = grab_screen(region=(0, 0, 1300, 850)) screen = cv2.cvtColor(screen, cv2.COLOR_BGR2GRAY) if window_upleft is None: cv2.imwrite('s.png', screen) screen = cv2.imread('s.png', 0) marca = cv2.imread("marca.png", 0) result = cv2.matchTemplate(screen, marca, cv2.TM_CCOEFF_NORMED) origin = np.unravel_index(result.argmax(), result.shape) print('origin', origin) window_upleft = [origin[0] + 45, origin[1]] print('window_upleft', window_upleft) marca_fim = cv2.imread("marca_fim.png", 0) result = cv2.matchTemplate(screen, marca_fim, cv2.TM_CCOEFF_NORMED) position_right = np.unravel_index(result.argmax(), result.shape)
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)