Exemplo n.º 1
0
def drive(model):
    global gamepad
    gamepad = XInputDevice(1)
    gamepad.PlugIn()

    # last_time = time.time()  # to measure the number of frames
    close = False  # to exit execution
    pause = True  # to pause execution

    print("Press T to start driving")
    while not close:
        while not pause:
            # apply the preprocessing
            image, speed, direct = img_process("Grand Theft Auto V")
            radar = cv2.cvtColor(image[194:230, 5:65, :],
                                 cv2.COLOR_RGB2BGR)[:, :, 1:3]
            image = preprocess(image)

            # predict steering angle for the image
            controls = model.predict(
                [np.array([image]), np.array([radar])], batch_size=1)

            if speed < 60:
                throttle = 0.4
                controls = [[controls[0][0], throttle]]
            else:
                throttle = 0.0
                controls = [[controls[0][0], throttle]]

            # set the gamepad values
            set_gamepad(controls)
            # print("Steering: {0:.2f}".format(controls[0][0]))

            # print('Main loop took {} seconds'.format(time.time() - last_time))
            # last_time = time.time()

            keys = key_check()
            if 'T' in keys:
                pause = True
                # release gamepad keys
                gamepad.SetTrigger('L', 0)
                gamepad.SetTrigger('R', 0)
                gamepad.SetAxis('X', 0)
                print('Paused. To exit the program press Z.')
                time.sleep(0.5)

        keys = key_check()
        if 'T' in keys:
            pause = False
            print('Unpaused')
            time.sleep(1)
        elif 'Z' in keys:
            close = True
            print('Closing the program.')
            gamepad.UnPlug()
def main():

    while True:
        screen, resized, speed, direct = img_process("Grand Theft Auto V")
        radar = cv2.cvtColor(resized[206:226, 25:45, :], cv2.COLOR_RGB2BGR)[:, :, 2:3]
        resized = preprocess(resized)
        left_line_color = [0, 255, 0]
        right_line_color = [0, 255, 0]

        yolo_screen, color_detected, obj_distance = yolo_detection(screen, direct)
        cv2.imshow("Driving-mode", yolo_screen)
        cv2.waitKey(1)
Exemplo n.º 3
0
def main():
    numbers = []
    record = False
    i = 0
    empty = 0

    while True:
        time.sleep(0.2)

        keys = key_check()
        if 'T' in keys:
            record = not record
        elif 'Z' in keys:
            np.save('digits.npy', numbers)

        image = grab_screen("Grand Theft Auto V")

        # speed
        screen, resized, speed, direct = img_process("Grand Theft Auto V")

        vis = image[567:576, 18:28, :]
        print(direct, speed)
        vis = cv2.cvtColor(vis, cv2.COLOR_RGB2GRAY)
        # ret, vis = cv2.threshold(vis, 140, 255, cv2.THRESH_BINARY_INV)
        # ret, vis = cv2.threshold(vis, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
        vis = cv2.adaptiveThreshold(vis, 255, cv2.ADAPTIVE_THRESH_MEAN_C,
                                    cv2.THRESH_BINARY, 7, -5)

        if record:
            i += 1
            print(i)
            numbers.append(vis[:, :5])  # num1
            numbers.append(vis[:, 7:12])  # num2
            if empty % 5 == 0:
                numbers.append(vis[:, -5:])  # num3
            empty += 1

        vis = cv2.resize(vis, None, fx=10, fy=10)
        cv2.imshow("Frame", vis)
        key = cv2.waitKey(1) & 0xFF
        # cv2.waitKey()

        # if the `q` key was pressed, break from the loop
        if key == ord("q"):
            break
Exemplo n.º 4
0
def drive(model):
    global gamepad
    gamepad = XInputDevice(1)
    gamepad.PlugIn()

    # last_time = time.time()  # to measure the number of frames
    close = False  # to exit execution
    pause = True  # to pause execution
    throttle = 0
    left_line_max = 80
    right_line_max = 680

    print("Press T to start driving")
    while not close:
        while not pause:
            # apply the preprocessing
            screen, resized, speed, direct = img_process("Grand Theft Auto V")
            radar = cv2.cvtColor(resized[206:226, 25:45, :],
                                 cv2.COLOR_RGB2BGR)[:, :, 2:3]
            resized = preprocess(resized)
            left_line_color = [0, 255, 0]
            right_line_color = [0, 255, 0]

            # predict steering angle for the image
            # original + radar (small) + speed
            controls = model.predict(
                [np.array([resized]),
                 np.array([radar]),
                 np.array([speed])],
                batch_size=1)
            # check that the car is following lane
            lane, stop_line = detect_lane(screen)

            # adjusting speed
            if speed < 35:
                throttle = 0.4
            elif speed > 40:
                throttle = 0.0

            # adjusting steering angle
            if lane[0] and lane[0][0] > left_line_max:
                controls[0][0] = 0.27
                left_line_color = [0, 0, 255]
            elif lane[1] and lane[1][0] < right_line_max:
                controls[0][0] = -0.27
                right_line_color = [0, 0, 255]

            # set the gamepad values
            set_gamepad([[controls[0][0], throttle]])
            # print("Steering: {0:.2f}".format(controls[0][0]))

            # for yolo detection
            # screen = np.array(screen, dtype=np.uint8)
            # yolo_detection(tfnet, screen, speed, controls, gamepad, direct)
            # cv2.imshow('frame', screen)
            # if cv2.waitKey(1) & 0xFF == ord('q'):
            #     break

            screen[280:-130, :, :] = draw_lane(screen[280:-130, :, :], lane,
                                               stop_line, left_line_color,
                                               right_line_color)
            cv2.imshow("Frame", screen)
            cv2.waitKey(1)

            if direct == Direct.NONE:
                cv2.destroyAllWindows()
                print("Arrived at destination.")
                stop()
                pause = True

            # print('Main loop took {} seconds'.format(time.time() - last_time))
            # last_time = time.time()

            keys = key_check()
            if 'T' in keys:
                cv2.destroyAllWindows()
                pause = True
                # release gamepad keys
                stop()
                print('Paused. To exit the program press Z.')
                time.sleep(0.5)

        keys = key_check()
        if 'T' in keys:
            pause = False
            print('Unpaused')
            time.sleep(1)
        elif 'Z' in keys:
            close = True
            print('Closing the program.')
            gamepad.UnPlug()
Exemplo n.º 5
0
def drive(model):
    global gamepad
    gamepad = XInputDevice(1)
    gamepad.PlugIn()

    last_time = time.time()  # to measure the number of frames
    close = False  # to exit execution
    pause = False  # to pause execution
    stop = False  # to stop the car
    throttle = 0
    left_line_max = 75
    right_line_max = 670

    print("Press T to start driving")

    while not close:
        yolo_screen, resized, speed, direct = img_process("Grand Theft Auto V")
        ##        cv2.imshow("Driving-mode", yolo_screen)
        ##        cv2.waitKey(1)

        while not pause:
            # apply the preprocessing
            screen, resized, speed, direct = img_process("Grand Theft Auto V")
            radar = cv2.cvtColor(resized[206:226, 25:45, :],
                                 cv2.COLOR_RGB2BGR)[:, :, 2:3]
            resized = preprocess(resized)
            left_line_color = [0, 255, 0]
            right_line_color = [0, 255, 0]

            # predict steering angle for the image
            # original + radar (small) + speed
            controls = model.predict(
                [np.array([resized]),
                 np.array([radar]),
                 np.array([speed])],
                batch_size=1)
            # check that the car is following lane
            lane, stop_line = detect_lane(screen)
            # detect objects
            yolo_screen, color_detected, obj_distance = yolo_detection(
                screen, direct)

            if not stop:
                # adjusting speed
                if speed < 65:
                    throttle = 0.8
                elif speed > 70:
                    throttle = 0.0

                if 0 <= obj_distance <= 0.6:
                    if speed < 5:
                        throttle = 0
                    else:
                        throttle = -0.7 if obj_distance <= 0.4 else -0.3

                elif color_detected == "Red":
                    if stop_line:
                        if speed < 5:
                            throttle = 0
                        elif 0 <= stop_line[0][1] <= 50:
                            throttle = -0.5
                        elif 50 < stop_line[0][1] <= 120:
                            throttle = -1
                    # else:
                    #     throttle = -0.5
            elif speed > 5:
                throttle = -1
            else:
                throttle = 0
                cv2.destroyAllWindows()
                pause = True

            # adjusting steering angle
            if lane[0] and lane[0][0] > left_line_max:
                if abs(controls[0][0]) < 0.27:
                    controls[0][0] = 0.27
                    left_line_color = [0, 0, 255]
            elif lane[1] and lane[1][0] < right_line_max:
                if abs(controls[0][0]) < 0.27:
                    controls[0][0] = -0.27
                    right_line_color = [0, 0, 255]

            # set the gamepad values
            set_gamepad([[controls[0][0], throttle]])

            # print('Main loop took {} seconds'.format(time.time() - last_time))
            # last_time = time.time()

            screen[280:-130, :, :] = draw_lane(screen[280:-130, :, :], lane,
                                               stop_line, left_line_color,
                                               right_line_color)
            ##            cv2.imshow("Driving-mode",yolo_screen)
            ##            cv2.waitKey(1)

            if direct == 6:
                print("Arrived at destination.")
                stop = True

            print('Main loop took {} seconds'.format(time.time() - last_time))
            last_time = time.time()

            keys = key_check()
            if 'T' in keys:
                cv2.destroyAllWindows()
                pause = True
                # release gamepad keys
                set_gamepad([[0, 0]])
                print('Paused. To exit the program press Z.')
                time.sleep(0.5)

        keys = key_check()
        if 'T' in keys:
            pause = False
            stop = False
            print('Unpaused')
            time.sleep(1)
        elif 'Z' in keys:
            cv2.destroyAllWindows()
            close = True
            print('Closing the program.')
            gamepad.UnPlug()
def main():
    # initialize gamepad
    gamepad = Gamepad()
    gamepad.open()

    # last_time = time.time()   # to measure the number of frames
    alert_time = time.time()  # to signal about exceeding speed limit
    close = False  # to exit execution
    pause = True  # to pause execution
    session = 0  # number of frames recorded in one session
    training_img = []  # lists for storing training data
    controls = []
    metrics = []

    print("Press RB on your gamepad to start recording")
    while not close:
        while not pause:
            # read throttle and steering values from the gamepad
            throttle, steering = gamepad.get_state()
            # get screen, speed and direction
            ignore, screen, speed, direction = img_process("Grand Theft Auto V")

            training_img.append(screen)
            controls.append([throttle, steering])
            metrics.append([speed, direction])
            session += 1

            if speed > 60 and time.time() - alert_time > 1:
                winsound.PlaySound('.\\resources\\alert.wav', winsound.SND_ASYNC)
                alert_time = time.time()

            # save the data every 30 iterations
            if len(training_img) % 30 == 0:
                # print("-" * 30 + "Saving" + "-" * 30)
                threading.Thread(target=save, args=(training_img, controls, metrics)).start()
                training_img = []
                controls = []
                metrics = []

            time.sleep(0.015)  # in order to slow down fps
            # print('Main loop took {} seconds'.format(time.time() - last_time))
            # last_time = time.time()

            if gamepad.get_RB():
                pause = True
                print('Paused. Save the last 15 seconds?')

                keys = key_check()
                while ('Y' not in keys) and ('N' not in keys):
                    keys = key_check()

                if 'N' in keys:
                    delete(session)
                    training_img = []
                    controls = []
                    metrics = []
                    print('Deleted.')
                else:
                    print('Saved.')

                print('To exit the program press LB.')
                session = 0
                time.sleep(0.5)

        if gamepad.get_RB():
            pause = False
            print('Unpaused')
            time.sleep(1)
        elif gamepad.get_LB():
            gamepad.close()
            close = True
            print('Saving data and closing the program.')
            save(training_img, controls, metrics)

    data_file.close()
def drive():
    global gamepad
    gamepad = XInputDevice(1)
    gamepad.PlugIn()

    # last_time = time.time()  # to measure the number of frames
    close = False  # to exit execution
    pause = True  # to pause execution
    stop = False  # to stop the car
    throttle = 0
    left_line_max = 75
    right_line_max = 670

    print("Press T to start driving")

    while not close:
        yolo_screen, resized, speed, direct = img_process("Grand Theft Auto V")
        cv2.imshow("Driving-mode", yolo_screen)
        cv2.waitKey(1)

        while not pause:
            # apply the preprocessing
            screen, resized, speed, direct = img_process("Grand Theft Auto V")
            radar = cv2.cvtColor(resized[206:226, 25:45, :],
                                 cv2.COLOR_RGB2BGR)[:, :, 2:3]
            resized = preprocess(resized)
            left_line_color = [0, 255, 0]
            right_line_color = [0, 255, 0]

            # predict steering angle for the image
            # original + radar (small) + speed
            # check that the car is following lane
            lane, stop_line = detect_lane(screen)
            # detect objects

            print(speed, "#", direct)

            if not stop:
                # adjusting speed
                if speed < 20:
                    throttle = 0.3
                elif speed > 25:
                    throttle = 0.0

            elif speed > 5:
                throttle = -1
            else:
                throttle = 0
                cv2.destroyAllWindows()
                pause = True

            # adjusting steering angle
            if lane[0] and lane[0][0] > left_line_max:
                left_line_color = [0, 0, 255]
            elif lane[1] and lane[1][0] < right_line_max:
                right_line_color = [0, 0, 255]

            # set the gamepad values

            # print('Main loop took {} seconds'.format(time.time() - last_time))
            # last_time = time.time()

            screen[280:-130, :, :] = draw_lane(screen[280:-130, :, :], lane,
                                               stop_line, left_line_color,
                                               right_line_color)
            cv2.imshow("Driving-mode", yolo_screen)
            cv2.waitKey(1)

            # print('Main loop took {} seconds'.format(time.time() - last_time))
            # last_time = time.time()

            keys = key_check()
            if 'T' in keys:
                cv2.destroyAllWindows()
                pause = True
                # release gamepad keys
                set_gamepad([[0, 0]])
                print('Paused. To exit the program press Z.')
                time.sleep(0.5)

        keys = key_check()
        if 'T' in keys:
            pause = False
            stop = False
            print('Unpaused')
            time.sleep(1)
        elif 'Z' in keys:
            cv2.destroyAllWindows()
            close = True
            print('Closing the program.')
            gamepad.UnPlug()