labels = [i.strip() for i in labels]



print('depthai.__version__ == %s' % depthai.__version__)
print('depthai.__dev_version__ == %s' % depthai.__dev_version__)



if not depthai.init_device(cmd_file):
    print("Error initializing device. Try to reset it.")
    exit(1)



print('Available streams: ' + str(depthai.get_available_steams()))

# Do not modify the default values in the config Dict below directly. Instead, use the `-co` argument when running this script.
config = {
    # Possible streams:
    # ['left', 'right','previewout', 'metaout', 'depth_sipp', 'disparity', 'depth_color_h']
    # If "left" is used, it must be in the first position.
    # To test depth use:
    #'streams': [{'name': 'depth_sipp', "max_fps": 12.0}, {'name': 'previewout', "max_fps": 12.0}, ],
    #'streams': [{'name': 'previewout', "max_fps": 3.0}, {'name': 'depth_mm_h', "max_fps": 3.0}],
    'streams': [{'name': 'previewout', "max_fps": fps}, {'name': 'metaout', "max_fps": 2.0}],
    #'streams': ['metaout', 'previewout'],
    'depth':
    {
        'calibration_file': consts.resource_paths.calib_fpath,
        # 'type': 'median',
    def setup():

        global pitch_pid_modifier
        global rotate_pid_modifier
        global t_start = time()
        global time_start = time()
        global frame_count = {}
        global frame_count_prev = {}
        global bus 
        global slave_address 
        global i2c_cmd

        self.arg_setup()

        # imshow_debug = False
        if self.args['imshow_debug']:
            self.imshow_debug = True

        self.timeout_time = self.tmout() 

        pitch_pid_modifier  = self.args['pitch_pid_modify']
    
        rotate_pid_modifier = self.args['rotate_pid_modify']

        if self.args['i2c_off']:
            self.communication_on = False
        else:
            self.communication_on = True

        trackbars_on = False
        if self.args['create_trackbars']:
            self.trackbars_on = True
            trackbars_on = True
            self.imshow_debug = True

        # cmd_file = consts.resource_paths.device_cmd_fpath
        if self.args['dev_debug']:
            self.cmd_file = ''
            print('depthai will not load cmd file into device.')

        with open(consts.resource_paths.blob_labels_fpath) as fp:
            self.labels = fp.readlines()
            self.labels = [i.strip() for i in labels]

        print('depthai.__version__ == %s' % depthai.__version__)
        print('depthai.__dev_version__ == %s' % depthai.__dev_version__)

                
        if not depthai.init_device(self.cmd_file):
            print("Error initializing device. Try to reset it.")
            exit(1)


        print('Available streams: ' + str(depthai.get_available_steams()))


        if self.args['config_overwrite'] is not None:
            config = utils.merge(self.args['config_overwrite'],self.config)
            print("Merged Pipeline config with overwrite",config)

        if 'depth_sipp' in config['streams'] and ('depth_color_h' in config['streams'] or 'depth_mm_h' in config['streams']):
            print('ERROR: depth_sipp is mutually exclusive with depth_color_h')
            exit(2)
            # del config["streams"][config['streams'].index('depth_sipp')]

        self.stream_names = [stream if isinstance(stream, str) else stream['name'] for stream in config['streams']]

        # create the pipeline, here is the first connection with the device
        self.p = depthai.create_pipeline(self.config=config)

        if self.p is None:
            print('Pipeline is not created.')
            exit(2)


        # t_start = time()
        # time_start = time()
        # frame_count = {}
        # frame_count_prev = {}
        for s in self.stream_names:
            frame_count[s] = 0
            frame_count_prev[s] = 0

        self.entries_prev = []


        ##################    I2C COMMUNICATION SETUP    ####################
        if self.communication_on:
            self.bus = smbus.SMBus(1)

        ################## ADDED FOR COLOR DETECTION CWM ####################
        if self.imshow_debug:
            cv2.namedWindow('g_image')
            cv2.namedWindow('r_image')

        # if self.trackbars_on:
        #     cv2.namedWindow('r1_sliders')
        #     cv2.namedWindow('r2_sliders')
        #     cv2.namedWindow('g_sliders')
        
        if trackbars_on:
            cv2.namedWindow('r1_sliders')
            cv2.namedWindow('r2_sliders')
            cv2.namedWindow('g_sliders')
    
        # white blank image
        self.blank_image = 255 * np.ones(shape=[10, 256, 3], dtype=np.uint8)
        self.thrs=50




        #cv2.createTrackbar('Hue', 'image', 80, 179, nothing)
        #cv2.createTrackbar('Sat', 'image', 127, 255, nothing)
        #cv2.createTrackbar('Val', 'image', 222, 255, nothing)

        if trackbars_on:
            
            cv2.createTrackbar('filterThresh', 'r1_sliders', self.thresholdValue, 100, nothing)
            cv2.createTrackbar('r1LowHue', 'r1_sliders', self.r1LowHue, 179, nothing)
            cv2.createTrackbar('r1LowSat', 'r1_sliders', self.r1LowSat, 255, nothing)
            cv2.createTrackbar('r1LowVal', 'r1_sliders', self.r1LowVal, 255, nothing)
            cv2.createTrackbar('r1UpHue', 'r1_sliders', self.r1UpHue, 179, nothing)
            cv2.createTrackbar('r1UpSat', 'r1_sliders', self.r1UpSat, 255, nothing)
            cv2.createTrackbar('r1UpVal', 'r1_sliders', self.r1UpVal, 255, nothing)
            cv2.createTrackbar('r2LowHue', 'r2_sliders', self.r2LowHue, 179, nothing)
            cv2.createTrackbar('r2LowSat', 'r2_sliders', self.r2LowSat, 255, nothing)
            cv2.createTrackbar('r2LowVal', 'r2_sliders', self.r2LowVal, 255, nothing)
            cv2.createTrackbar('r2UpHue', 'r2_sliders', self.r2UpHue, 179, nothing)
            cv2.createTrackbar('r2UpSat', 'r2_sliders', self.r2UpSat, 255, nothing)
            cv2.createTrackbar('r2UpVal', 'r2_sliders', self.r2UpVal, 255, nothing)
            cv2.createTrackbar('gLowHue', 'g_sliders', self.gLowHue, 179, nothing)
            cv2.createTrackbar('gLowSat', 'g_sliders', self.gLowSat, 255, nothing)
            cv2.createTrackbar('gLowVal', 'g_sliders', self.gLowVal, 255, nothing)
            cv2.createTrackbar('gUpHue', 'g_sliders', self.gUpHue, 179, nothing)
            cv2.createTrackbar('gUpSat', 'g_sliders', self.gUpSat, 255, nothing)
            cv2.createTrackbar('gUpVal', 'g_sliders', self.gUpVal, 255, nothing)


        ## red ball mask areas
        #red_mask_1 = cv2.inRange(im_hsv, (0, 120, 70), (10, 255, 255))
        #red_mask_2 = cv2.inRange(im_hsv, (170, 120, 70), (179, 255, 255)) 

        #lower_red1 = np.array([0, 120, 100])
        #upper_red1 = np.array([10, 255, 255])
        #lower_red2 = np.array([170, 120, 100])
        #upper_red2 = np.array([179, 255, 255])

        #green mask area centered around 
        # (80, 127, 222)
        #green_mask = cv2.inRange(im_hsv, (55, 120, 70), (105, 255, 255)) 
        #    lower_green = np.array([40, 10, 200])
        #    upper_green = np.array([120, 245, 255])

        #lower_green = np.array([55, 120, 70])
        #upper_green = np.array([105, 255, 255])


        #sets how much to blur
        self.filt=39
        self.exitNow=0
        self.pause=0