示例#1
0
    def run(self):
        # Settings instance
        s = Settings()
        # State of autopilot
        self.autopilot = False
        img_wheel = cv2.imread('steering_wheel_image.jpg', 0)
        img_wheel = cv2.cvtColor(img_wheel, cv2.COLOR_BGR2RGB)
        functions.set_image(img_wheel.copy(), self.steering_wheel_ui)
        rows, cols, _ = img_wheel.shape
        keyboard.add_hotkey('shift+w', self.hotkey_callback)
        start_time = functions.current_milli_time()
        while AutopilotThread.running:
            self.controller_thread.set_autopilot(self.autopilot)
            # Get frame of game
            frame_raw = ImageGrab.grab(bbox=functions.get_screen_bbox())
            frame = np.uint8(frame_raw)
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

            # Relevant image region for steering angle prediction
            main = frame[s.get_value(Settings.IMAGE_FRONT_BORDER_TOP):s.
                         get_value(Settings.IMAGE_FRONT_BORDER_BOTTOM),
                         s.get_value(Settings.IMAGE_FRONT_BORDER_LEFT):s.
                         get_value(Settings.IMAGE_FRONT_BORDER_RIGHT)]
            # Resize the image to the size of the neural network input layer
            image = scipy.misc.imresize(main, [66, 200]) / 255.0
            # Let the neural network predict the new steering angle
            y_eval = model.y.eval(session=self.sess,
                                  feed_dict={
                                      model.x: [image],
                                      model.keep_prob: 1.0
                                  })[0][0]
            degrees = y_eval * 180 / scipy.pi
            steering = int(round((degrees + 180) / 180 * 32768 /
                                 2))  # Value for vjoy controller

            # Set the value of the vjoy joystick to the predicted steering angle
            if self.autopilot:
                self.controller_thread.set_angle(steering)
                self.statusbar.showMessage("Autopilot active, steering: " +
                                           str(steering))
            else:
                self.statusbar.showMessage("Autopilot inactive")
            M = cv2.getRotationMatrix2D((cols / 2, rows / 2), -degrees, 1)
            dst = cv2.warpAffine(img_wheel, M, (cols, rows))
            functions.set_image(dst.copy(), self.steering_wheel_ui)
            functions.set_image(main.copy(), self.image_front_ui)
            wait_time = functions.current_milli_time() - start_time - 40
            if wait_time < 0:
                time.sleep(-wait_time / 1000)
            start_time = functions.current_milli_time()
        keyboard.clear_hotkey('shift+w')
        self.controller_thread.set_autopilot(False)
def main_logic():
    ### pen colors dark
    keyboard.add_hotkey('alt+z',
                        lambda: pen_set_color(Color.light),
                        suppress=True,
                        trigger_on_release=True)
    keyboard.add_hotkey('alt+v',
                        lambda: pen_set_color(Color.dark),
                        suppress=True,
                        trigger_on_release=True)
    keyboard.add_hotkey('alt+6',
                        lambda: pen_set_color(Color.black),
                        suppress=True,
                        trigger_on_release=True)
    ### pen colors
    keyboard.add_hotkey('alt+1',
                        lambda: pen_set_color(Color.red),
                        suppress=True,
                        trigger_on_release=True)
    keyboard.add_hotkey('alt+2',
                        lambda: pen_set_color(Color.orange),
                        suppress=True,
                        trigger_on_release=True)
    keyboard.add_hotkey('alt+3',
                        lambda: pen_set_color(Color.olive),
                        suppress=True,
                        trigger_on_release=True)
    keyboard.add_hotkey('alt+4',
                        lambda: pen_set_color(Color.green),
                        suppress=True,
                        trigger_on_release=True)
    keyboard.add_hotkey('alt+5',
                        lambda: pen_set_color(Color.blue),
                        suppress=True,
                        trigger_on_release=True)
    ### pen size
    keyboard.add_hotkey('alt+q',
                        lambda: pen_set_size(1),
                        suppress=True,
                        trigger_on_release=True)
    keyboard.add_hotkey('alt+w',
                        lambda: pen_set_size(2),
                        suppress=True,
                        trigger_on_release=True)
    keyboard.add_hotkey('alt+g',
                        lambda: pen_set_size(3),
                        suppress=True,
                        trigger_on_release=True)
    keyboard.add_hotkey('alt+h',
                        lambda: pen_set_size(4),
                        suppress=True,
                        trigger_on_release=True)
    keyboard.add_hotkey('alt+c',
                        lambda: pen_set_size(5),
                        suppress=True,
                        trigger_on_release=True)
    ### line color
    keyboard.add_hotkey('alt+b',
                        lambda: line_set_color(Color.red),
                        suppress=True,
                        trigger_on_release=True)
    keyboard.add_hotkey('alt+a',
                        lambda: line_set_color(Color.orange),
                        suppress=True,
                        trigger_on_release=True)
    keyboard.add_hotkey('alt+l',
                        lambda: line_set_color(Color.olive),
                        suppress=True,
                        trigger_on_release=True)
    keyboard.add_hotkey('alt+r',
                        lambda: line_set_color(Color.green),
                        suppress=True,
                        trigger_on_release=True)
    keyboard.add_hotkey('alt+t',
                        lambda: line_set_color(Color.blue),
                        suppress=True,
                        trigger_on_release=True)

    ### waiting for ctrl-c, control c
    try:
        while True:
            time.sleep(5)
    except KeyboardInterrupt:
        pass

    # clear keys
    keyboard.clear_hotkey('alt+z')
    keyboard.clear_hotkey('alt+v')
    keyboard.clear_hotkey('alt+6')
    keyboard.clear_hotkey('alt+1')
    keyboard.clear_hotkey('alt+2')
    keyboard.clear_hotkey('alt+3')
    keyboard.clear_hotkey('alt+4')
    keyboard.clear_hotkey('alt+5')
    keyboard.clear_hotkey('alt+q')
    keyboard.clear_hotkey('alt+w')
    keyboard.clear_hotkey('alt+g')
    keyboard.clear_hotkey('alt+h')
    keyboard.clear_hotkey('alt+c')
    keyboard.clear_hotkey('alt+b')
    keyboard.clear_hotkey('alt+a')
    keyboard.clear_hotkey('alt+l')
    keyboard.clear_hotkey('alt+t')
    keyboard.clear_hotkey('alt+r')
示例#3
0
# Xlib.error.DisplayConnectionError: Can't connect to display ":1": b'No protocol specified\n'
# temp solution: xhost +

import keyboard
import time
import pyautogui

if __name__ == '__main__':

    ### record all input keys
    # recorded: [] = keyboard.record(until='esc')
    # print(recorded)

    keyboard.add_hotkey('alt+p', lambda: pyautogui.moveRel(100, 100))
    ### waiting for ctrl-c, control c
    try:
        while True:
            time.sleep(5)
    except KeyboardInterrupt:
        pass

    keyboard.clear_hotkey('alt+p')
示例#4
0
    def run(self):
        s = Settings()
        d = Data(batch=True)

        img_id = d.get_next_fileid()
        self.recording = False

        maneuver = 0  # 0 - normal, 1 - indicator left, 2 - indicator right
        last_record = functions.current_milli_time()

        keyboard.add_hotkey('shift+w', self.record_callback, args=[d])
        while RecordingThread.running:
            # Capture the whole game
            frame_raw = ImageGrab.grab(bbox=functions.get_screen_bbox())
            frame = np.uint8(frame_raw)
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

            main = frame[s.get_value(Settings.IMAGE_FRONT_BORDER_TOP):s.
                         get_value(Settings.IMAGE_FRONT_BORDER_BOTTOM),
                         s.get_value(Settings.IMAGE_FRONT_BORDER_LEFT):s.
                         get_value(Settings.IMAGE_FRONT_BORDER_RIGHT)]
            # gray = cv2.cvtColor(main, cv2.COLOR_BGR2GRAY)
            # blur_gray = cv2.GaussianBlur(gray, (3, 3), 0)
            # edges = cv2.Canny(blur_gray, 50, 150)
            # dilated = cv2.dilate(edges, (3,3), iterations=2)

            # Resize image to save some space (height = 100px)
            ratio = main.shape[1] / main.shape[0]
            resized = cv2.resize(main, (round(ratio * 100), 100))

            # cv2.imshow('cap', dilated)
            # cv2.imshow('resized', resized)
            functions.set_image(main.copy(), self.image_front)

            axis, throttle, speed = get_steering_throttle_speed()
            if axis == 0:
                maneuver = 0
            elif axis > 0:
                maneuver = 1
            else:
                maneuver = 2
            if self.recording:
                self.statusbar.showMessage(
                    "Recording: active | Indicator: %s" %
                    functions.get_indicator(maneuver))
            else:
                self.statusbar.showMessage(
                    "Recording: inactive | Indicator: %s" %
                    functions.get_indicator(maneuver))
            # Save frame every 150ms
            if self.recording:
                cv2.imwrite("captured/%d.png" % img_id, resized)
                d.add_image("%d.png" % img_id, axis, speed, throttle, maneuver,
                            self.sequence_id)
                img_id += 1
                # at least wait 150ms
                wait_milli_time = functions.current_milli_time(
                ) - last_record - 150
                if wait_milli_time < 0:
                    time.sleep(-wait_milli_time / 1000)
                last_record = functions.current_milli_time()
            else:
                time.sleep(0.15)
        keyboard.clear_hotkey('shift+w')
        d.append()