Esempio n. 1
0
class CortexBasic(threading.Thread):
    def __init__(self, update_interval_ms, oculus, corti, controller):

        # Logger
        self.logger = logging.getLogger(__name__)
        logging.basicConfig(
            format='%(asctime)s %(module)s %(levelname)s: %(message)s',
            datefmt='%m/%d/%Y %I:%M:%S %p',
            level=logging.INFO)
        self.logger.setLevel(logging.INFO)

        self.oculus = oculus
        self.retina = Retina()

        self.retina.fil_hsv_l[2] = 180
        self.retina.fil_hsv_u[1] = 100

        self.thread_name = "Cortex"
        threading.Thread.__init__(self, name=self.thread_name)

        self.enabled = False
        self.update_interval_ms = update_interval_ms

        self.angles = [None, None, None]
        self.midpoints = [None, None, None]

    def process_frame(self):

        # Setting the current frame
        self.retina.frame = self.oculus.get_frame()

        # Detecting lines
        if self.retina.frame is not None:
            self.angles, self.midpoints = self.retina.process()

    def enable(self):

        self.enabled = True

    def disable(self):

        self.enabled = False

    def run(self):

        while True:

            if self.enabled == True:
                self.process_frame()

            time.sleep(self.update_interval_ms / 1000)
Esempio n. 2
0
    def __init__(self, update_interval_ms, oculus, corti, controller):

        # Logger
        self.logger = logging.getLogger(__name__)
        logging.basicConfig(
            format='%(asctime)s %(module)s %(levelname)s: %(message)s',
            datefmt='%m/%d/%Y %I:%M:%S %p',
            level=logging.INFO)
        self.logger.setLevel(logging.INFO)

        self.oculus = oculus
        self.retina = Retina()

        self.retina.fil_hsv_l[2] = 180
        self.retina.fil_hsv_u[1] = 100

        self.thread_name = "Cortex"
        threading.Thread.__init__(self, name=self.thread_name)

        self.enabled = False
        self.update_interval_ms = update_interval_ms

        self.angles = [None, None, None]
        self.midpoints = [None, None, None]
Esempio n. 3
0
        self.enabled = True

    def disable(self):

        self.enabled = False

    def run(self):

        while True:

            if self.enabled == True:
                self.process_frame()

            time.sleep(self.update_interval_ms / 1000)


# ------------------------------------------------------------------------------
#                                 SAMPLE CODE
# ------------------------------------------------------------------------------

if __name__ == '__main__':

    retina = Retina()

    retina.load_npy(
        file_name=
        '/Users/arnavgupta/car_data/raw_npy/oculus-2019-06-16 20;49;28.264824.npy'
    )
    retina.test_line_detection()
Esempio n. 4
0
    def __init__(self, update_interval_ms, oculus, corti, drive, mode="simulation"):

        """
        Constructor

        :param update_interval_ms: Thread execution period
        :param oculus: Interface to vision systems
        :param corti: Interface to inertial measurement systems
        :param controller: Interface to user rf controller module
        """

        # Logger
        self.logger = logging.getLogger(__name__)
        logging.basicConfig(
            format='%(asctime)s %(module)s %(levelname)s: %(message)s',
            datefmt='%m/%d/%Y %I:%M:%S %p',
            level=logging.INFO)
        self.logger.setLevel(logging.INFO)

        # External vehicle interfaces
        self.retina = Retina()
        self.corti = corti
        self.drive = drive
        self.oculus = oculus

        # Lap History
        self.lap_history = LapHistory(memory_size = 5)

        # Thread configuration
        self.thread_name = "Cortex"
        threading.Thread.__init__(self, name=self.thread_name)

        # Thread parameters
        self.enabled = False
        self.update_interval_ms = update_interval_ms

        # Observation feature existence
        self.observation_space = dict()
        self.observation_space['left_lane_present'] = None
        self.observation_space['right_lane_present'] = None
        self.observation_space['splitter_present'] = None
        self.observation_space['vehicle_offroad'] = None

        # Position Observations
        self.observation_space['left_lane_position'] = None
        self.observation_space['right_lane_position'] = None
        self.observation_space['splitter_position'] = None
        self.observation_space['vehicle_position'] = None

        # Angle observations
        self.observation_space['left_lane_angle'] = None
        self.observation_space['right_lane_angle'] = None
        self.observation_space['splitter_angle'] = None
        self.observation_space['vehicle_angle'] = None

        # Observation space acceleration
        self.observation_space['x_acceleration'] = 0
        self.observation_space['y_acceleration'] = 0
        self.observation_space['z_acceleration'] = 0

        # Observation space user controls
        self.observation_space['user_throttle'] = None
        self.observation_space['user_steering'] = None
        self.observation_space['terminal'] = 0

        # Reward
        self.reward = 0

        # Training mode
        self.mode = "IMITATION"

        # Offroad State Machine
        self.offroad_sm = []

        # State counter
        self.state_counter = 0
Esempio n. 5
0
class CortexAdvanced(threading.Thread):

    """
    Cortex provides perception via vision and inertial systems
    """

    def __init__(self, update_interval_ms, oculus, corti, drive, mode="simulation"):

        """
        Constructor

        :param update_interval_ms: Thread execution period
        :param oculus: Interface to vision systems
        :param corti: Interface to inertial measurement systems
        :param controller: Interface to user rf controller module
        """

        # Logger
        self.logger = logging.getLogger(__name__)
        logging.basicConfig(
            format='%(asctime)s %(module)s %(levelname)s: %(message)s',
            datefmt='%m/%d/%Y %I:%M:%S %p',
            level=logging.INFO)
        self.logger.setLevel(logging.INFO)

        # External vehicle interfaces
        self.retina = Retina()
        self.corti = corti
        self.drive = drive
        self.oculus = oculus

        # Lap History
        self.lap_history = LapHistory(memory_size = 5)

        # Thread configuration
        self.thread_name = "Cortex"
        threading.Thread.__init__(self, name=self.thread_name)

        # Thread parameters
        self.enabled = False
        self.update_interval_ms = update_interval_ms

        # Observation feature existence
        self.observation_space = dict()
        self.observation_space['left_lane_present'] = None
        self.observation_space['right_lane_present'] = None
        self.observation_space['splitter_present'] = None
        self.observation_space['vehicle_offroad'] = None

        # Position Observations
        self.observation_space['left_lane_position'] = None
        self.observation_space['right_lane_position'] = None
        self.observation_space['splitter_position'] = None
        self.observation_space['vehicle_position'] = None

        # Angle observations
        self.observation_space['left_lane_angle'] = None
        self.observation_space['right_lane_angle'] = None
        self.observation_space['splitter_angle'] = None
        self.observation_space['vehicle_angle'] = None

        # Observation space acceleration
        self.observation_space['x_acceleration'] = 0
        self.observation_space['y_acceleration'] = 0
        self.observation_space['z_acceleration'] = 0

        # Observation space user controls
        self.observation_space['user_throttle'] = None
        self.observation_space['user_steering'] = None
        self.observation_space['terminal'] = 0

        # Reward
        self.reward = 0

        # Training mode
        self.mode = "IMITATION"

        # Offroad State Machine
        self.offroad_sm = []

        # State counter
        self.state_counter = 0

    def get_state(self):

        """
        Getting the observation space
        """

        # Setting the current frame
        self.retina.frame = self.oculus.get_frame()

        # Making a prediction of what is expected in the next step
        # Retina compares this prediction to its computed value
        if len(self.lap_history.lap) > 0:
            self.retina.prediction = self.lap_history.predict()

        self.retina.process()

        # # Detecting lines
        # try:
        #
        #     road =
        #
        #     # Adding the current snapshot to the track history
        #     self.lap_history.add_road_snapshot(road)
        #
        #     self.observation_space['left_lane_present'] = road.left_lane.present
        #     self.observation_space['right_lane_present'] = road.right_lane.present
        #     self.observation_space['splitter_present'] = road.splitter.present
        #     self.observation_space['vehicle_offroad'] = road.vehicle.offroad
        #
        #     self.observation_space['left_lane_position'] = road.left_lane.midpoint
        #     self.observation_space['right_lane_position'] = road.right_lane.midpoint
        #     self.observation_space['splitter_position'] = road.splitter.midpoint
        #     self.observation_space['vehicle_position'] = road.vehicle.position
        #
        #     self.observation_space['left_lane_angle'] = road.left_lane.angle
        #     self.observation_space['right_lane_angle'] = road.right_lane.angle
        #     self.observation_space['splitter_angle'] = road.splitter.angle
        #     self.observation_space['vehicle_angle'] = road.vehicle.angle
        #
        #     self.observation_space['x_acceleration'] = self.corti.get_frame()
        #     self.observation_space['y_acceleration'] = 0
        #     self.observation_space['z_acceleration'] = 0
        #
        #     self.observation_space['user_throttle'], self.observation_space['user_steering'] = self.drive.get_frame()
        #
        #     self.state_counter += 1
        #
        #     if (self.state_counter % 20 == 0) or (self.observation_space['vehicle_offroad']):
        #         self.observation_space['terminal'] = 1
        #     else:
        #         self.observation_space['terminal'] = 0
        #
        # except Exception as e:
        #     print(e)


    def compute_reward(self, cerebellum_thr, cerebellum_str):

        """
        Computes the reward given the training mode, throttle and steering

        :param mode: Imitation vs Reinforcement
        :param cerebellum_thr: Machine computed throttle
        :param cerebellum_str: Machine computed steerting
        :return: Returns the reward
        """

        if self.mode == "IMITATION":


            user_throttle , user_steering = self.drive.get_frame()

            x = user_throttle - cerebellum_thr
            throttle_reward = 5*self.gaussian_function(x, 0.25, 0)

            y = user_steering - cerebellum_str
            steering_reward = 5*self.gaussian_function(y, 0.25, 0)

            self.reward = throttle_reward*steering_reward

        elif self.mode == "REINFORCEMENT":
            self.reward = 0

        if self.reward < 0:
            print("impossible negative reward")

        return self.reward

    def gaussian_function(self, x, sigma, mu):

        """
        Gaussian function used in computing reward

        :param sigma: std
        :param mu: mean
        :return: value of gaussian function at x
        """

        return np.exp(-0.5 * (((x - mu) / sigma) ** 2))

    def enable(self):

        """
        Enables the cortex thread
        """

        self.enabled = True

    def disable(self):

        """
        Disables the cortex thread
        """

        self.enabled = False

    def set_mode(self, mode):

        """
        Setting the training mode
        """

        self.mode = mode

    def offroad(self):

        if len(self.offroad_sm) == 5:
            del self.offroad_sm[0]

        self.offroad_sm.append([self.observation_space['splitter_present'], self.observation_space['left_lane_present'], self.observation_space['right_lane_present'], self.observation_space['offroad']])

        offroad = True

        left = False
        right = False

        for i in self.offroad_sm:
            if i[0]:
                offroad = False
            elif i[1]:
                left = True
            elif i[2]:
                right = True

        if offroad:
            if left and right:
                offroad = False
            if left and not right:
                if self.observation_space['left_lane_position'] < 0.5:
                    offroad = False
            if right and not left:
                if self.observation_space['right_lane_position'] > -0.5:
                    offroad = False

        self.observation_space['offroad'] = offroad

    def vectorize_state(self):

        """
        Vectorizing the environment state into a form readable by the neural network
        """

        self.vectorized_state = []

        if self.observation_space['left_lane_present']:
            self.vectorized_state.append(1)
        else:
            self.vectorized_state.append(0)

        if self.observation_space['right_lane_present']:
            self.vectorized_state.append(1)
        else:
            self.vectorized_state.append(0)

        if self.observation_space['splitter_present']:
            self.vectorized_state.append(1)
        else:
            self.vectorized_state.append(0)

        if self.observation_space['vehicle_offroad']:
            self.vectorized_state.append(1)
        else:
            self.vectorized_state.append(0)

        #TODO: How does this -1 affect the relu in the neural network?

        try:
            self.vectorized_state.append(self.observation_space['left_lane_position']/128)
        except:
            self.vectorized_state.append(-1)

        try:
            self.vectorized_state.append(self.observation_space['right_lane_position']/128)
        except:
            self.vectorized_state.append(-1)

        try:
            self.vectorized_state.append(self.observation_space['splitter_position']/128)
        except:
            self.vectorized_state.append(-1)

        try:
            self.vectorized_state.append(self.observation_space['vehicle_position']/128)
        except:
            self.vectorized_state.append(-1)

        try:
            self.vectorized_state.append(self.observation_space['left_lane_angle']/180+0.5)
        except:
            self.vectorized_state.append(-1)

        try:
            self.vectorized_state.append(self.observation_space['right_lane_angle']/180+0.5)
        except:
            self.vectorized_state.append(-1)

        try:
            self.vectorized_state.append(self.observation_space['splitter_angle']/180+0.5)
        except:
            self.vectorized_state.append(-1)

        try:
            self.vectorized_state.append(self.observation_space['vehicle_angle']/180+0.5)
        except:
            self.vectorized_state.append(-1)

        self.vectorized_state.append(self.observation_space['x_acceleration'][0]/10)
        self.vectorized_state.append(self.observation_space['y_acceleration']/10)
        self.vectorized_state.append(self.observation_space['z_acceleration']/10)

        return np.array(self.vectorized_state)

    def get_raw_state(self):

        return self.retina.frame

    def run(self):

        """
        Cortex thread
        """

        #TODO: Add time tracking (needed for terminal)

        while True:

            if self.enabled == True:

                self.get_state()

            time.sleep(self.update_interval_ms / 1000)
Esempio n. 6
0
    def init_retina(self):

        self.retina = Retina()
Esempio n. 7
0
class Simulator(threading.Thread):

    UI_HEIGHT = 600
    UI_WIDTH = 900

    IMG_WIDTH = 400
    IMG_HEIGHT = 200

    INITAL_IMG_INDEX = 24

    ADJ_MAG = 5

    TYPE = 'lane'  # 'splitter'

    def __init__(self):

        # Logger
        logger = logging.getLogger(__name__)
        logging.basicConfig(
            format='%(asctime)s %(module)s %(levelname)s: %(message)s',
            datefmt='%m/%d/%Y %I:%M:%S %p',
            level=logging.INFO)
        logger.setLevel(logging.DEBUG)

        self.video_active = False
        self.apply_retina = True

        self.init_ui()

        # self.init_recall()
        # self.init_retina()

        # self.init_img_ctrls()

        # self.img_index = self.INITAL_IMG_INDEX
        # self.change_img(self.img_index)
        # self.update_img()

    def init_ui(self):

        self.ui = tk.Tk()
        self.ui.resizable(width=False, height=False)
        self.ui.geometry("{}x{}".format(self.UI_WIDTH, self.UI_HEIGHT))

        self.canvas = tk.Canvas(self.ui,
                                width=self.IMG_WIDTH,
                                height=self.IMG_HEIGHT)
        self.canvas.place(x=10, y=10)

        self.canvas_img = self.canvas.create_image((10, 10),
                                                   image=(),
                                                   anchor='nw')

        self.canvas.bind("<Button 1>", self.mouse_click_event)

    def init_img_ctrls(self):

        self.next = tk.Button(self.ui,
                              text="  Next  ",
                              command=lambda: self.next_img())
        self.next.config(width=8)
        self.next.place(x=15, y=280)

        self.previous = tk.Button(self.ui,
                                  text="Previous",
                                  command=lambda: self.previous_img())
        self.previous.config(width=8)
        self.previous.place(x=120, y=280)

        self.start = tk.Button(self.ui,
                               text="Play",
                               command=lambda: self.start_video())
        self.start.config(width=8)
        self.start.place(x=15, y=310)

        self.stop = tk.Button(self.ui,
                              text="Stop",
                              command=lambda: self.stop_video())
        self.stop.config(width=8)
        self.stop.place(x=120, y=310)

        self.fil_l_rgb1_increase = tk.Button(
            self.ui,
            text="Inc R L Fil",
            command=lambda: self.adjust_lower_fil(
                index=0, vector="increase", mag=self.ADJ_MAG))
        self.fil_l_rgb1_increase.config(width=8)
        self.fil_l_rgb1_increase.place(x=320, y=20)

        self.fil_l_rgb2_increase = tk.Button(
            self.ui,
            text="Inc G L Fil",
            command=lambda: self.adjust_lower_fil(
                index=1, vector="increase", mag=self.ADJ_MAG))
        self.fil_l_rgb2_increase.config(width=8)
        self.fil_l_rgb2_increase.place(x=320, y=50)

        self.fil_l_rgb3_increase = tk.Button(
            self.ui,
            text="Inc B L Fil",
            command=lambda: self.adjust_lower_fil(
                index=2, vector="increase", mag=self.ADJ_MAG))
        self.fil_l_rgb3_increase.config(width=8)
        self.fil_l_rgb3_increase.place(x=320, y=80)

        self.fil_l_rgb1_decrease = tk.Button(
            self.ui,
            text="Dec R L Fil",
            command=lambda: self.adjust_lower_fil(
                index=0, vector="decrease", mag=self.ADJ_MAG))
        self.fil_l_rgb1_decrease.config(width=8)
        self.fil_l_rgb1_decrease.place(x=420, y=20)

        self.fil_l_rgb2_decrease = tk.Button(
            self.ui,
            text="Dec G L Fil",
            command=lambda: self.adjust_lower_fil(
                index=1, vector="decrease", mag=self.ADJ_MAG))
        self.fil_l_rgb2_decrease.config(width=8)
        self.fil_l_rgb2_decrease.place(x=420, y=50)

        self.fil_l_rgb3_decrease = tk.Button(
            self.ui,
            text="Dec B L Fil",
            command=lambda: self.adjust_lower_fil(
                index=2, vector="decrease", mag=self.ADJ_MAG))
        self.fil_l_rgb3_decrease.config(width=8)
        self.fil_l_rgb3_decrease.place(x=420, y=80)

        self.fil_u_rgb1_increase = tk.Button(
            self.ui,
            text="Inc R U Fil",
            command=lambda: self.adjust_upper_fil(
                index=0, vector="increase", mag=self.ADJ_MAG))
        self.fil_u_rgb1_increase.config(width=8)
        self.fil_u_rgb1_increase.place(x=320, y=120)

        self.fil_u_rgb2_increase = tk.Button(
            self.ui,
            text="Inc G U Fil",
            command=lambda: self.adjust_upper_fil(
                index=1, vector="increase", mag=self.ADJ_MAG))
        self.fil_u_rgb2_increase.config(width=8)
        self.fil_u_rgb2_increase.place(x=320, y=150)

        self.fil_u_rgb3_increase = tk.Button(
            self.ui,
            text="Inc B U Fil",
            command=lambda: self.adjust_upper_fil(
                index=2, vector="increase", mag=self.ADJ_MAG))
        self.fil_u_rgb3_increase.config(width=8)
        self.fil_u_rgb3_increase.place(x=320, y=180)

        self.fil_u_rgb1_decrease = tk.Button(
            self.ui,
            text="Dec R U Fil",
            command=lambda: self.adjust_upper_fil(
                index=0, vector="decrease", mag=self.ADJ_MAG))
        self.fil_u_rgb1_decrease.config(width=8)
        self.fil_u_rgb1_decrease.place(x=420, y=120)

        self.fil_u_rgb2_decrease = tk.Button(
            self.ui,
            text="Dec G U Fil",
            command=lambda: self.adjust_upper_fil(
                index=1, vector="decrease", mag=self.ADJ_MAG))
        self.fil_u_rgb2_decrease.config(width=8)
        self.fil_u_rgb2_decrease.place(x=420, y=150)

        self.fil_u_rgb3_decrease = tk.Button(
            self.ui,
            text="Dec B U Fil",
            command=lambda: self.adjust_upper_fil(
                index=2, vector="decrease", mag=self.ADJ_MAG))
        self.fil_u_rgb3_decrease.config(width=8)
        self.fil_u_rgb3_decrease.place(x=420, y=180)

        self.fil_l_hsv1_increase = tk.Button(
            self.ui,
            text="Inc H L Fil",
            command=lambda: self.adjust_lower_hsv_fil(
                index=0, vector="increase", mag=self.ADJ_MAG))
        self.fil_l_hsv1_increase.config(width=8)
        self.fil_l_hsv1_increase.place(x=520, y=20)

        self.fil_l_hsv2_increase = tk.Button(
            self.ui,
            text="Inc S L Fil",
            command=lambda: self.adjust_lower_hsv_fil(
                index=1, vector="increase", mag=self.ADJ_MAG))
        self.fil_l_hsv2_increase.config(width=8)
        self.fil_l_hsv2_increase.place(x=520, y=50)

        self.fil_l_hsv3_increase = tk.Button(
            self.ui,
            text="Inc V L Fil",
            command=lambda: self.adjust_lower_hsv_fil(
                index=2, vector="increase", mag=self.ADJ_MAG))
        self.fil_l_hsv3_increase.config(width=8)
        self.fil_l_hsv3_increase.place(x=520, y=80)

        self.fil_l_hsv1_decrease = tk.Button(
            self.ui,
            text="Dec H L Fil",
            command=lambda: self.adjust_lower_hsv_fil(
                index=0, vector="decrease", mag=self.ADJ_MAG))
        self.fil_l_hsv1_decrease.config(width=8)
        self.fil_l_hsv1_decrease.place(x=620, y=20)

        self.fil_l_hsv2_decrease = tk.Button(
            self.ui,
            text="Dec S L Fil",
            command=lambda: self.adjust_lower_hsv_fil(
                index=1, vector="decrease", mag=self.ADJ_MAG))
        self.fil_l_hsv2_decrease.config(width=8)
        self.fil_l_hsv2_decrease.place(x=620, y=50)

        self.fil_l_hsv3_decrease = tk.Button(
            self.ui,
            text="Dec V L Fil",
            command=lambda: self.adjust_lower_hsv_fil(
                index=2, vector="decrease", mag=self.ADJ_MAG))
        self.fil_l_hsv3_decrease.config(width=8)
        self.fil_l_hsv3_decrease.place(x=620, y=80)

        self.fil_u_hsv1_increase = tk.Button(
            self.ui,
            text="Inc H U Fil",
            command=lambda: self.adjust_upper_hsv_fil(
                index=0, vector="increase", mag=self.ADJ_MAG))
        self.fil_u_hsv1_increase.config(width=8)
        self.fil_u_hsv1_increase.place(x=520, y=120)

        self.fil_u_hsv2_increase = tk.Button(
            self.ui,
            text="Inc S U Fil",
            command=lambda: self.adjust_upper_hsv_fil(
                index=1, vector="increase", mag=self.ADJ_MAG))
        self.fil_u_hsv2_increase.config(width=8)
        self.fil_u_hsv2_increase.place(x=520, y=150)

        self.fil_u_hsv3_increase = tk.Button(
            self.ui,
            text="Inc V U Fil",
            command=lambda: self.adjust_upper_hsv_fil(
                index=2, vector="increase", mag=self.ADJ_MAG))
        self.fil_u_hsv3_increase.config(width=8)
        self.fil_u_hsv3_increase.place(x=520, y=180)

        self.fil_u_hsv1_decrease = tk.Button(
            self.ui,
            text="Dec H U Fil",
            command=lambda: self.adjust_upper_hsv_fil(
                index=0, vector="decrease", mag=self.ADJ_MAG))
        self.fil_u_hsv1_decrease.config(width=8)
        self.fil_u_hsv1_decrease.place(x=620, y=120)

        self.fil_u_hsv2_decrease = tk.Button(
            self.ui,
            text="Dec S U Fil",
            command=lambda: self.adjust_upper_hsv_fil(
                index=1, vector="decrease", mag=self.ADJ_MAG))
        self.fil_u_hsv2_decrease.config(width=8)
        self.fil_u_hsv2_decrease.place(x=620, y=150)

        self.fil_u_hsv3_decrease = tk.Button(
            self.ui,
            text="Dec V U Fil",
            command=lambda: self.adjust_upper_hsv_fil(
                index=2, vector="decrease", mag=self.ADJ_MAG))
        self.fil_u_hsv3_decrease.config(width=8)
        self.fil_u_hsv3_decrease.place(x=620, y=180)

        self.display_lanes = tk.Button(self.ui,
                                       text="Display Lanes",
                                       command=lambda: self.enable_lanes())
        self.display_lanes.config(width=8)
        self.display_lanes.place(x=320, y=220)

        self.remove_lanes = tk.Button(self.ui,
                                      text="Remove Lanes",
                                      command=lambda: self.disable_lanes())
        self.remove_lanes.config(width=8)
        self.remove_lanes.place(x=420, y=220)

        self.enable_vision = tk.Button(self.ui,
                                       text="Enable Vision",
                                       command=lambda: self.enable_retina())
        self.enable_vision.config(width=8)
        self.enable_vision.place(x=320, y=260)

        self.disable_vision = tk.Button(self.ui,
                                        text="Disable Vision",
                                        command=lambda: self.disable_retina())
        self.disable_vision.config(width=8)
        self.disable_vision.place(x=420, y=260)

        self.show_rgb = tk.Button(self.ui,
                                  text="RGB",
                                  command=lambda: self.enable_RGB())
        self.show_rgb.config(width=8)
        self.show_rgb.place(x=320, y=300)

        self.show_hsv = tk.Button(self.ui,
                                  text="HSV",
                                  command=lambda: self.enable_HSV())
        self.show_hsv.config(width=8)
        self.show_hsv.place(x=420, y=300)

    def init_vision_ctrls(self):

        self.recalibrate = tk.Button(
            self.ui,
            text="Recalibrate",
            command=lambda: print("Run button pressed"))
        self.recalibrate.config(width=9)
        self.recalibrate.place(x=475, y=80)

    def init_recall(self):

        self.recall = Recall(
            "/Users/arnavgupta/car_data/raw_npy/oculus-2019-06-29 18;29;43.996328.npy"
        )
        self.recall.load()

    # ------------------------- Retina Integration -----------------------------

    def init_retina(self):

        self.retina = Retina()

        # self.retina.fil_hsv_l[2] = 180
        # self.retina.fil_hsv_u[1] = 160

    # ---------------------------- Video Thread --------------------------------

    def start_video(self):

        self.video_active = True
        self.video = threading.Thread(target=self.video_thread, args=())
        self.video.start()

    def stop_video(self):

        self.video_active = False

    def video_thread(self):

        while self.video_active:

            self.img_index += 1
            if self.img_index >= self.recall.num_frames:
                self.img_index = 0

            self.change_img(self.img_index)

            time.sleep(0.25)

    # ----------------------------- Calibration --------------------------------

    def adjust_lower_fil(self, index, vector="increase", mag=5):
        for i in range(3):
            if vector == "increase":
                if self.retina.fil_rgb_l[i][index] < 255:
                    self.retina.fil_rgb_l[i][index] += mag
            elif vector == "decrease":
                if self.retina.fil_rgb_l[i][index] > 0:
                    self.retina.fil_rgb_l[i][index] -= mag

        # self.retina.set_calibration(self.TYPE, self.retina.fil_rgb_l, self.retina.fil_rgb_u)

        logger.info(
            "Lower RGB Filter: {} Upper RGB Filter: {} Lower HSV Filter: {} Upper HSV Filter: {}"
            .format(self.retina.fil_rgb_l, self.retina.fil_rgb_u,
                    self.retina.fil_hsv_l, self.retina.fil_hsv_u))

        self.change_img(self.img_index)

    def adjust_upper_fil(self, index, vector="increase", mag=5):
        for i in range(3):
            if vector == "increase":
                if self.retina.fil_rgb_u[i][index] < 255:
                    self.retina.fil_rgb_u[i][index] += mag
            elif vector == "decrease":
                if self.retina.fil_rgb_u[i][index] > 0:
                    self.retina.fil_rgb_u[i][index] -= mag

        # self.retina.set_calibration(self.TYPE, self.retina.fil_rgb_l, self.retina.fil_rgb_u)

        logger.info(
            "Lower RGB Filter: {} Upper RGB Filter: {} Lower HSV Filter: {} Upper HSV Filter: {}"
            .format(self.retina.fil_rgb_l, self.retina.fil_rgb_u,
                    self.retina.fil_hsv_l, self.retina.fil_hsv_u))

        self.change_img(self.img_index)

    def adjust_lower_hsv_fil(self, index, vector="increase", mag=5):

        if vector == "increase":
            if self.retina.fil_hsv_l[index] < 255:
                self.retina.fil_hsv_l[index] += mag
        elif vector == "decrease":
            if self.retina.fil_hsv_l[index] > 0:
                self.retina.fil_hsv_l[index] -= mag

        # self.retina.set_calibration(self.TYPE, self.retina.fil_hsv_l, self.retina.fil_hsv_u)

        logger.info(
            "Lower RGB Filter: {} Upper RGB Filter: {} Lower HSV Filter: {} Upper HSV Filter: {}"
            .format(self.retina.fil_rgb_l, self.retina.fil_rgb_u,
                    self.retina.fil_hsv_l, self.retina.fil_hsv_u))

        self.change_img(self.img_index)

    def adjust_upper_hsv_fil(self, index, vector="increase", mag=5):

        if vector == "increase":
            if self.retina.fil_hsv_u[index] < 255:
                self.retina.fil_hsv_u[index] += mag
        elif vector == "decrease":
            if self.retina.fil_hsv_u[index] > 0:
                self.retina.fil_hsv_u[index] -= mag

        # self.retina.set_calibration(self.TYPE, self.retina.fil_hsv_l, self.retina.fil_hsv_u)

        logger.info(
            "Lower RGB Filter: {} Upper RGB Filter: {} Lower HSV Filter: {} Upper HSV Filter: {}"
            .format(self.retina.fil_rgb_l, self.retina.fil_rgb_u,
                    self.retina.fil_hsv_l, self.retina.fil_hsv_u))

        self.change_img(self.img_index)

    def enable_lanes(self):

        self.retina.enable_lines = True
        self.change_img(self.img_index)

    def disable_lanes(self):

        self.retina.enable_lines = False
        self.change_img(self.img_index)

    def enable_retina(self):

        self.apply_retina = True
        self.change_img(self.img_index)

    def disable_retina(self):

        self.apply_retina = False
        self.change_img(self.img_index)

    def enable_RGB(self):

        self.retina.mode = 'RGB'
        self.change_img(self.img_index)

    def enable_HSV(self):

        self.retina.mode = 'HSV'
        self.change_img(self.img_index)

    # ---------------------------- Image Change --------------------------------

    def next_img(self):

        if self.img_index == self.recall.num_frames - 1:
            logger.info("Last image reached")
        else:
            self.img_index += 1
            self.change_img(self.img_index)

    def previous_img(self):

        if self.img_index == 0:
            logger.info("First image reached")
        else:
            self.img_index -= 1
            self.change_img(self.img_index)

    def change_img(self, img_index):

        self.get_image(img_index)

        if self.apply_retina == False:

            self.raw = self.raw[40:80:, :]
            self.img = ImageTk.PhotoImage(self.resize_im(self.raw))
            self.update_img()
            logger.info("Image {} opened (Retina applied: {})".format(
                self.img_index, self.apply_retina))

        elif self.apply_retina == True:

            self.retina.frame = self.raw
            theta, rho = self.retina.process()
            if self.retina.mode == "RGB":
                self.processed = self.retina.rgb_frame
            else:
                self.processed = self.retina.frame
            print(theta, rho)
            self.img = ImageTk.PhotoImage(self.resize_im(self.processed))

            self.update_img()
            logger.info("Image {} opened (Retina applied: {})".format(
                self.img_index, self.apply_retina))

    # -------------------------- Image Utilities -------------------------------

    def get_image(self, image_num):

        self.raw = self.recall.frames[image_num]
        self.img = ImageTk.PhotoImage(self.resize_im(self.raw))

    def update_img(self):

        self.canvas.itemconfigure(self.canvas_img, image=self.img)

    def resize_im(self, im):
        im = self.recall.rgb_to_img(im)
        return im.resize((128 * 2, 40 * 2), Image.NEAREST)
        # return im

    # ---------------------------- GUI Startup ---------------------------------

    def run(self):

        self.ui.mainloop()

    # ---------------------------- Pixel Info ----------------------------------

    def mouse_click_event(self, event_origin):

        global x0, y0
        x0 = event_origin.x
        y0 = event_origin.y
        print("Pixel: ({},{}) Shape:({}) RGB: ({})".format(
            x0, y0, self.raw.shape, self.raw[y0][x0]))