Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
def straight():
    print('straight')
    PressKey(W)
    ReleaseKey(A)
    ReleaseKey(D)
Ejemplo n.º 3
0
def right():
    print('right')
    PressKey(W)
    PressKey(D)
    time.sleep(0.09)
    ReleaseKey(D)
Ejemplo n.º 4
0
import time
from directkeys import PressKey, ReleaseKey, W,A,S,D

while(True):
    
    PressKey(W)
    time.sleep(3)
    ReleaseKey(W)
    time.sleep(3)
    

Ejemplo n.º 5
0
                keyPressed = True
                keyPressed_lr = True
                # player_speed += 2

    # show the frame to our screen
    frame_copy = frame.copy()
    frame_copy = cv2.rectangle(frame_copy, (0, height // 2 - windowSize // 2),
                               (width, height // 2 + windowSize // 2),
                               (255, 0, 0), 2)
    cv2.imshow("Frame", frame_copy)

    #We need to release the pressed key if none of the key is pressed else the program will keep on sending
    # the presskey command
    if not keyPressed and len(current_key_pressed) != 0:
        for key in current_key_pressed:
            ReleaseKey(key)
        current_key_pressed = set()

    #to release keys for left/right with keys of up/down remain pressed
    if not keyPressed_lr and ((A in current_key_pressed) or
                              (D in current_key_pressed)):
        if A in current_key_pressed:
            ReleaseKey(A)
            current_key_pressed.remove(A)
            # player_speed += 2

        elif D in current_key_pressed:
            ReleaseKey(D)
            current_key_pressed.remove(D)
            # player_speed -= 2
Ejemplo n.º 6
0
def left():
    PressKey(A)
    ReleaseKey(D)
    print('A')
Ejemplo n.º 7
0
def no_keys():
    ReleaseKey(A)
    ReleaseKey(D)
def left():
    PressKey(A)
    PressKey(W)
    ReleaseKey(D)
def right():
    PressKey(D)
    PressKey(W)
    # time.sleep(0.05)
    ReleaseKey(A)
Ejemplo n.º 10
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 = 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=(0, 40, GAME_WIDTH, GAME_HEIGHT + 30))
            screen = cv2.cvtColor(screen, cv2.COLOR_BGR2RGB)

            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)
Ejemplo n.º 11
0
def reverse():
    PressKey(S)
    ReleaseKey(A)
    ReleaseKey(W)
    ReleaseKey(D)
    sleep()
Ejemplo n.º 12
0
def reverse_right():
    PressKey(S)
    PressKey(D)
    ReleaseKey(W)
    ReleaseKey(A)
    sleep()
Ejemplo n.º 13
0
def reverse_left():
    PressKey(S)
    PressKey(A)
    ReleaseKey(W)
    ReleaseKey(D)
    sleep()
Ejemplo n.º 14
0
def forward_right():
    PressKey(W)
    PressKey(D)
    ReleaseKey(A)
    ReleaseKey(S)
    sleep()
Ejemplo n.º 15
0
def right():
    PressKey(D)
    PressKey(W)
    ReleaseKey(A)
    time.sleep(t_time)
    ReleaseKey(D)
Ejemplo n.º 16
0
def little_left():
    PressKey(A)
    PressKey(W)
    ReleaseKey(D)
    ReleaseKey(A)
Ejemplo n.º 17
0
def letgo():
    ReleaseKey(W)
    ReleaseKey(A)
    ReleaseKey(S)
    ReleaseKey(D)
Ejemplo n.º 18
0
def little_right():
    PressKey(D)
    PressKey(W)
    ReleaseKey(A)
    ReleaseKey(D)
Ejemplo n.º 19
0
def right():
    PressKey(D)
    ReleaseKey(A)
    print('D')
Ejemplo n.º 20
0
def slow():
    PressKey(S)
    ReleaseKey(W)
    ReleaseKey(A)
    ReleaseKey(D)
    ReleaseKey(S)
Ejemplo n.º 21
0
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)
Ejemplo n.º 22
0
def straight():
    PressKey(W)
    ReleaseKey(A)
    ReleaseKey(D)
            ):  ## finding my server's ip address
            host = str(ip)
            print(ip)
            break
    except:
        pass
s.connect((host, port))
print("CONNECTED TO SERVER")
while True:
    data = s.recv(2)
    print(str(data))
    data = str(data)
    if (data == "b'w'"):
        print("W")
        PressKey(W)
        ReleaseKey(S)
        ReleaseKey(A)
        ReleaseKey(D)
    elif (data == "b's'"):
        print("S")
        PressKey(S)
        ReleaseKey(W)
        ReleaseKey(A)
        ReleaseKey(D)
    elif (data == "b'a'"):
        print("A")
        PressKey(A)
        ReleaseKey(W)
        ReleaseKey(S)
        ReleaseKey(D)
    elif (data == "b'd'"):
Ejemplo n.º 24
0
def straight():
    PressKey(W)
    ReleaseKey(A)
    ReleaseKey(D)
    ReleaseKey(S)
    sleep()
def Waiter():
    PressKey(S)
    time.sleep(0.2)
    ReleaseKey(S)
Ejemplo n.º 26
0
def straight():
    PressKey(W)
    ReleaseKey(A)
    ReleaseKey(D)
    time.sleep(t_time)
    ReleaseKey(W)
Ejemplo n.º 27
0
def left():
    print('left')
    PressKey(W)
    PressKey(A)
    time.sleep(0.09)
    ReleaseKey(A)
Ejemplo n.º 28
0
def left():
    PressKey(A)
    PressKey(W)
    ReleaseKey(D)
    time.sleep(t_time)
    ReleaseKey(A)
Ejemplo n.º 29
0
 def release(self, key):
     ReleaseKey(key)
                (255, 255, 255), 3)
    #Bottom right region rectangle
    frame_copy = cv2.rectangle(frame_copy, (width // 2, height // 2),
                               (width, height), (255, 255, 255), 1)
    cv2.putText(frame_copy, 'Acceleration', (330, 280),
                cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 3)

    # show the frame to our screen
    cv2.imshow("Frame", frame_copy)

    #If part: We need to release the pressed key if none of the key is pressed else the program will keep on sending
    #Else part:If different keys(Only one key in each frame) are pressed in previous and current frames, then we must
    #release previous frame key, Also release the key in current frame key for smoother control
    if not keyPressed and len(current_key_pressed) != 0:
        for key in current_key_pressed:
            ReleaseKey(key)
        current_key_pressed = set()
    elif key_count == 1 and len(current_key_pressed) == 2:
        for key in current_key_pressed:
            if key_pressed != key:
                ReleaseKey(key)
        current_key_pressed = set()
        for key in current_key_pressed:
            ReleaseKey(key)
        current_key_pressed = set()

    key = cv2.waitKey(1) & 0xFF
    # if the 'q' key is pressed, stop the loop
    if key == ord("q"):
        break