Beispiel #1
0
def main():
    scale = 16000.0  # Vjoy stuff.
    vj.open()
    while (True):

        #speed = Get_Speed(clf) # Get current speed. , # I'll leave the speed cap deactivated as a default
        #Cap_Speed(speed) # Make sure we aren't going too fast. , # I'll leave the speed cap deactivated as a default

        screen = grab_screen(region=ROI)  #Screenshot part of window.
        screen = np.array(screen)  #Make screenshot into array.
        screen = cv2.resize(screen, (120, 60))  #Resize to a 120x60 pic.
        pic_array = screen.reshape(60, 120, 3)

        prediction = model.predict({'input_pic': [pic_array]})[0]

        steer = (prediction[2] - prediction[0])**3
        setJoy(steer + 0.024, 0, scale)  # +0.024 Helps fix joystick offset.
    vj.close()
Beispiel #2
0
def main():
    print("vJoy Opening")
    vj.open()
    time.sleep(0.5)

    # place_waypoints()

    print("Scanning Memory")
    scanner = HeapScanner(0x034B0000)
    wp1d_addr = scanner.scan_memory("2.1 m")
    wp1a_addr = scanner.scan_memory("-54.91")
    wp2a_addr = scanner.scan_memory("+99.91")

    print('Creating pipelines')
    # GRIP generated pipelines for cones and mobile goals
    cone_pipeline = ConePipeline()
    rmg_pipeline = RedMobileGoalPipeline()
    bmg_pipeline = BlueMobileGoalPipeline()

    print('Running pipeline')
    last_time = time.time()
    while True:
        have_frame, frame = grab_screen()
        if have_frame:
            # GRIP frame processing
            cone_pipeline.process(frame)
            rmg_pipeline.process(frame)
            bmg_pipeline.process(frame)

            # Final processing (coloring, etc.)
            images = [
                frame,
                extra_processing_cones(cone_pipeline),
                extra_processing_red_mobile_goals(rmg_pipeline),
                extra_processing_blue_mobile_goals(bmg_pipeline),
                blank_image.copy()
            ]

            for i in range(len(images)):
                images[i] = cv2.resize(images[i],
                                       None,
                                       fx=0.6,
                                       fy=0.6,
                                       interpolation=cv2.INTER_AREA)

            r = num_from_distance_string(scanner.read_memory(wp1d_addr, 6))
            theta = num_from_angle_string(scanner.read_memory(wp2a_addr, 6))
            wp1_t = num_from_angle_string(scanner.read_memory(wp1a_addr, 6))
            if r is not None and theta is not None and wp1_t is not None:
                theta = radians(theta)
                x = r * fabs(mp.cos(theta))
                y = x * mp.tan(theta)

            # To write to joystick call set_joy
            # set_joy(xval, yval)

            # Show all filters in 4x4 grid
            cv2.imshow(
                'Filters',
                np.hstack([
                    np.vstack([np.hstack(images[0:2]),
                               np.hstack(images[2:4])]),
                    np.vstack([images[4], blank_image_scaled])
                ]))
            if cv2.waitKey(10) == 27:
                print('Capture closed')
                cv2.destroyAllWindows()
                cleanup_gui_helper()
                vj.close()
                return
            else:
                print("fps: ", int(1 / (time.time() - last_time)))
                last_time = time.time()
from vjoy import vj, setJoy
import time

SCALE = 16000.0
TRIMMER = 0.025

print("Openning vJoy...")
vj.open()
time.sleep(1)

class SteeringController():
    def __init__(self, keyboardHook):
        # joystick axis position
        self.xPos = 0.0
        self.yPos = 0.0

        # mouse control properties
        self.neutralPosition = 1920 / 2
        self.sensitivity = 800
        self.currentPosition = 0

        # if the keyboard overrides the network
        self.keyboardActive = False

        # bind listener to keyboard
        keyboardHook.addTapListener(self.keyboardTap)

    def updateJoystick(self):
        #print("Steering: " + str(self.xPos))
        setJoy(self.xPos + TRIMMER, self.yPos, SCALE)
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)