Esempio n. 1
0
def right():
    if random.randrange(0, 3) == 1:
        PressKey(W)
    else:
        ReleaseKey(W)
    PressKey(D)
    ReleaseKey(A)
    ReleaseKey(S)
Esempio n. 2
0
def stop():
    ##    if random.randrange(4) == 2:
    ##        ReleaseKey(W)
    ##    else:
    PressKey(S)
    ReleaseKey(A)
    ReleaseKey(D)
    ReleaseKey(W)
Esempio n. 3
0
def no_keys():
    if random.randrange(0, 3) == 1:
        PressKey(W)
    else:
        ReleaseKey(W)
    ReleaseKey(A)
    ReleaseKey(S)
    ReleaseKey(D)
Esempio n. 4
0
def left():
    PressKey(W)
    PressKey(A)
    # ReleaseKey(W)
    ReleaseKey(D)
    ReleaseKey(S)
    # ReleaseKey(A)
    time.sleep(t_time)
    ReleaseKey(A)
Esempio n. 5
0
def right():
    PressKey(W)
    PressKey(D)
    ReleaseKey(A)
    ReleaseKey(S)
    # ReleaseKey(W)
    # ReleaseKey(D)
    time.sleep(t_time)
    ReleaseKey(D)
Esempio n. 6
0
def main():

    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    sess = tf.Session(config=config)

    # Train model
    loaded_model = load()
    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.resize(screen, (299, 299))

            prediction = loaded_model.predict([screen.reshape(1, 299, 299, 3)])
            print(prediction)

            turn_thresh = .75
            fwd_thresh = 0.70

            if prediction[0][1] > fwd_thresh:
                straight()
            elif prediction[0][0] > turn_thresh:
                left()
            elif prediction[0][2] > turn_thresh:
                right()
            elif prediction[0][3] > turn_thresh:
                stop()
            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)
                ReleaseKey(S)
                time.sleep(1)
Esempio n. 7
0
def dir_move_paddle(ftr_pong, paddle, up, down):
    '''logic to move paddle based on ball pos and paddle pos'''
    if (ftr_pong[1] - paddle[1]) > 50: # add random to try and get offence length of paddle
        #print('down')
        ReleaseKey(up)
        PressKey(down)
    elif (ftr_pong[1] - paddle[1]) < -50:
        #print('up')
        ReleaseKey(down)
        PressKey(up)
    else:
        #print('hold')
        ReleaseKey(down)
        ReleaseKey(up)
Esempio n. 8
0
def simple_move_paddle(pong, paddle, up, down):
    '''logic to move paddle based on ball pos and paddle pos'''
    if (pong[1] - paddle[1]) > 15:
        #print('down')
        ReleaseKey(up)
        PressKey(down)
    elif (pong[1] - paddle[1]) < -15:
        #print('up')
        ReleaseKey(down)
        PressKey(up)
    else:
        #print('hold')
        ReleaseKey(down)
        ReleaseKey(up)
def simple_decision_with_slow_down_and_slow_turn(m1,
                                                 m2,
                                                 slow_prob=0.2,
                                                 leave_turn_prob=0.3):
    """Releasing turn keys after tapping to turn keys according to a probability"""
    if m1 < 0 and m2 < 0:
        right()
        random_num = np.random.rand()
        if random_num < leave_turn_prob:
            ReleaseKey(D)

    elif m1 > 0 and m2 > 0:
        left()
        random_num = np.random.rand()
        if random_num < leave_turn_prob:
            ReleaseKey(A)

    else:
        random_num = np.random.rand()
        if random_num > slow_prob:
            straight()
        else:
            slow_down()
Esempio n. 10
0
def left():
    PressKey(A)
    ReleaseKey(W)
    ReleaseKey(D)
    time.sleep(0.1)
    ReleaseKey(A)
Esempio n. 11
0
def no_key():
    ReleaseKey(W)
    ReleaseKey(S)
    ReleaseKey(A)
    ReleaseKey(D)
Esempio n. 12
0
def backward():
    PressKey(S)
    ReleaseKey(W)
    ReleaseKey(A)
    ReleaseKey(D)
Esempio n. 13
0
def forward():
    PressKey(W)
    ReleaseKey(A)
    ReleaseKey(D)
    ReleaseKey(S)
Esempio n. 14
0
def reverse_right():
    PressKey(S)
    PressKey(D)
    ReleaseKey(W)
    ReleaseKey(A)
Esempio n. 15
0
def right():
    PressKey(D)
    ReleaseKey(A)
    ReleaseKey(W)
    ReleaseKey(D)
Esempio n. 16
0
def straight():
    PressKey(W)
    ReleaseKey(A)
    ReleaseKey(D)
def main():
    bbox = {"top": 0, "left": 0, "width": width, "height": height}

    sct = mss()

    # vertices for region of interest
    # from left bottom and clockwise
    vertices = np.array([[0, height - 1], [0, height * 5 // 8],
                         [width * 2 // 5, height // 2],
                         [width * 3 // 5, height // 2],
                         [width - 1, height * 5 // 8], [width - 1,
                                                        height - 1]])

    paused = False

    # mainloop
    while True:
        if not paused:
            # get screen
            sct_img = sct.grab(bbox)
            screen = np.array(sct_img)

            # process screen
            processed, original = process_image(screen, vertices)

            # find lines
            lines = cv2.HoughLinesP(processed, 1, np.pi / 180, 150,
                                    np.array([]), 20, 15)

            # find lanes
            m1, m2, l1, l2 = 0, 0, 0, 0  # default values
            ret = get_2_lanes(original, lines)
            if ret:
                (l1, l2, m1, m2) = ret
            lanes = [l1, l2]

            # simple decision
            #simple_decision(m1, m2)
            #simple_decision_with_slow_down(m1, m2, 0.3)
            simple_decision_with_slow_down_and_slow_turn(m1, m2, 0.4, 0.2)

            # draw lines and lanes
            #draw_lines(processed, lines)
            #draw_lines(original, lanes, color=[0, 255, 0], thickness=20)

            #cv2.imshow("processed", processed)
            #cv2.imshow("original", original)

            #if cv2.waitKey(1) & 0xFF == ord("q"):
            #cv2.destroyAllWindows()
            #break

        # keys and user control
        keys = get_pressed()

        if "P" in keys:
            # pause or continue
            if paused:
                paused = False
                time.sleep(1)
            else:
                paused = True
                ReleaseKey(A)
                ReleaseKey(W)
                ReleaseKey(D)
                time.sleep(1)
        elif "B" in keys:
            # close the app
            ReleaseKey(A)
            ReleaseKey(W)
            ReleaseKey(D)
            break
Esempio n. 18
0
def reverse_left():
    PressKey(S)
    PressKey(A)
    ReleaseKey(W)
    ReleaseKey(D)
Esempio n. 19
0
def forward_right():
    PressKey(W)
    PressKey(D)
    ReleaseKey(A)
    ReleaseKey(S)
Esempio n. 20
0
def forward_left():
    PressKey(W)
    PressKey(A)
    ReleaseKey(D)
    ReleaseKey(S)
Esempio n. 21
0
def reverse():
    PressKey(S)
    ReleaseKey(A)
    ReleaseKey(W)
    ReleaseKey(D)
Esempio n. 22
0
def right():
    PressKey(D)
    ReleaseKey(A)
    ReleaseKey(W)
    time.sleep(0.1)
    ReleaseKey(D)
Esempio n. 23
0
def calmdown():
    ReleaseKey(W)
    ReleaseKey(A)
    ReleaseKey(D)
def slow_down():
    ReleaseKey(W)
    ReleaseKey(A)
    ReleaseKey(D)
Esempio n. 25
0
def left():
    PressKey(A)
    ReleaseKey(W)
    ReleaseKey(D)
    ReleaseKey(A)
Esempio n. 26
0
def main():
    bbox = {"top": 0, "left": 0, "width": width, "height": height}

    sct = mss()

    last_time = time.time()
    paused = False

    # mainloop
    while True:
        if not paused:
            # get screen
            sct_img = sct.grab(bbox)
            screen = np.array(sct_img)

            # image to grayscale
            screen = cv2.cvtColor(screen, cv2.COLOR_BGR2GRAY)

            ## crop black borders of the game screen
            # thresh black parts
            _, thresh = cv2.threshold(screen, 1, 255, cv2.THRESH_BINARY)
            # find contours
            contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL,
                                           cv2.CHAIN_APPROX_SIMPLE)
            if contours:
                cnt = contours[0]
                x, y, w, h = cv2.boundingRect(cnt)
                # crop non-black part of the image
                screen = screen[y:y + h, x:x + w]

            # resize image
            screen = cv2.resize(screen, (SIZE, SIZE))

            # normalize image
            screen = (screen - screen.min()) / max(
                (screen.max() - screen.min()), 0.001)

            ## uncomment below line to see your FPS
            #print("Frame took {} seconds".format(time.time()-last_time))
            last_time = time.time()

            # prediction
            prediction = model.predict(screen.reshape(1, SIZE, SIZE, 1))[0]
            confidence = prediction.max()

            # interpret prediction to move
            #pred_to_move(prediction)
            pred_to_move_with_slow_down(prediction, 0.4)

        # keys
        keys = get_pressed()

        # user control
        if "P" in keys:
            # Pause or continue
            if paused:
                paused = False
                time.sleep(1)
            else:
                paused = True
                ReleaseKey(A)
                ReleaseKey(W)
                ReleaseKey(D)
                ReleaseKey(S)
                time.sleep(1)
        elif "B" in keys:
            # Break the loop
            ReleaseKey(A)
            ReleaseKey(W)
            ReleaseKey(D)
            ReleaseKey(S)
            break
Esempio n. 27
0
def slow_ya_roll():
    ReleaseKey(W)
    ReleaseKey(A)
    ReleaseKey(D)
Esempio n. 28
0
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 = grabscreen(region=(0, 40, GAME_WIDTH, GAME_HEIGHT))
    screen = cv2.cvtColor(screen, cv2.COLOR_BGR2RGB)

    t_minus = screen
    t_now = screen
    t_plus = screen

    while (True):

        if not paused:
            screen = grabscreen(region=(0, 40, GAME_WIDTH, GAME_HEIGHT + 40))
            screen = cv2.cvtColor(screen, cv2.COLOR_BGR2RGB)
            print(screen.shape)
            last_time = time.time()

            delta_count = 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(1, HEIGHT, WIDTH, 3))[0]
            prediction = np.array(prediction)# * np.array([1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.2])
            second = np.argpartition(a, -2)[1]
            mode_choice = np.argmax(prediction)
            print('Choice: {}'.format(mode_choice))
            print('Second: {}'.format(second))
            print(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)