def run_all_calibration(sweep_config_json, **kwargs):
    '''
    A Python based command line utility to run calibration for ADI TOF modules

    SWEEP_CONFIG_JSON: Specify the default sweep config json file. The default options can be overriden by explictly specifying the options on the command line
    '''

    sweep_config_dict = {}
    if (sweep_config_json is not None):
        with open(sweep_config_json) as f:
            sweep_config_dict = json.load(f)

    for key, value in kwargs.items():
        if value is not None:
            sweep_config_dict[key] = kwargs[key]
        if key in sweep_config_dict:
            if sweep_config_dict[key] is None:
                print(
                    'Need to specify %s either as a command line option or in the sweep config json file'
                )
                raise SystemExit

    if sweep_config_dict['unique_id'] == None:
        unique_id = generate_unique_id()
        sweep_config_dict['unique_id'] = unique_id
    sweep_config_dict['unique_id_list'] = split_unique_id(
        sweep_config_dict['unique_id'], 4)

    ipString = ''
    if 'remote' in sweep_config_dict:
        ip = ipaddress.ip_address(sweep_config_dict['remote'])
        print('Running script for a camera connected over Ethernet at ip:', ip)
        ipString = sweep_config_dict['remote']

    #Initialize tof class
    system = tof.System()
    status = system.initialize()
    logger.info("System Initialize: " + str(status))

    # Get Camera and program firmware
    cam_handle = device.open_device2(system, ipString)
    firmware_path = sweep_config_dict['firmware_path']
    device.program_firmware2(cam_handle, firmware_path)

    logger.info("Running Calibration")
    #Initialize Dataframes
    depth_stats_calibrated_df = pd.DataFrame([], columns=['NO DATA'])
    depth_stats_df = pd.DataFrame([], columns=['NO DATA'])
    linear_offset_df = pd.DataFrame([], columns=['NO DATA'])
    metrics_df = pd.DataFrame([], columns=['NO DATA'])

    # If using rail create handle and store handle in cfg
    rail_handle = None
    if (sweep_config_dict['calib_type'] == 'Rail'
            or sweep_config_dict['verification_type'] == 'Rail'):
        rail_handle = calib.initialize_rail(sweep_config_dict)

    latest, archive = save.make_results_dir(sweep_config_dict['results_path'],
                                            sweep_config_dict['unique_id'],
                                            mode=sweep_config_dict['mode'])

    t = time.time()
    d = 0
    while d < sweep_config_dict['warmup_time']:
        d = time.time() - t

    if sweep_config_dict['calibrate']:
        if sweep_config_dict['calib_type'] == 'Sweep':
            linear_offset_df, depth_stats_df = calib.run_sweep_calibration(
                cam_handle=cam_handle,
                cfg=sweep_config_dict,
                firmware_path=firmware_path)
        elif sweep_config_dict['calib_type'] == 'Rail':
            logger.info("Running Rail Calibration")
            # Run Rail calibration and store lf files
            linear_offset_df, depth_stats_df = calib.run_rail_calibration(
                cam_handle=cam_handle,
                rail_handle=rail_handle,
                cfg=sweep_config_dict,
                firmware_path=firmware_path)

        save.save_lf_files(latest, archive, firmware_path)
        write_lf.write_linear_offset_to_lf2(firmware_path,
                                            os.path.join(latest, 'lf_files'),
                                            sweep_config_dict,
                                            linear_offset_df)

    depth_stats_calibrated_df = pd.DataFrame()
    if sweep_config_dict['verify_sweep']:
        if sweep_config_dict['verification_type'] == 'Sweep':
            logger.info("Verifying Sweep")
            _, depth_stats_calibrated_df = calib.verify_sweep(
                cam_handle, sweep_config_dict,
                os.path.join(latest, 'lf_files'))

        elif sweep_config_dict['verification_type'] == 'Rail':
            logger.info("Verifying Sweeap")
            device.program_firmware2(cam_handle,
                                     os.path.join(latest, 'lf_files'))
            #_, depth_stats_calibrated_df = calib.rail_verify(cam_handle, rail_handle, sweep_config_dict, os.path.join(latest, 'lf_files'))
            cfg = sweep_config_dict
            frame_count = cfg['frame_count']
            window = {}
            window['X'] = cfg['window_x']
            window['Y'] = cfg['window_y']

            frame = {}
            frame['height'] = cfg['frame_height']
            frame['width'] = cfg['frame_width']

            raw_frame = {}
            raw_frame['height'] = cfg['raw_frame_height']
            raw_frame['width'] = cfg['raw_frame_width']

            repeat_num_file = os.path.join(firmware_path,
                                           cfg['repeat_num_filename'])
            hpt_data_file = os.path.join(firmware_path,
                                         cfg['hpt_data_filename'])
            data_file = os.path.join(firmware_path, cfg['data_filename'])
            seq_file = os.path.join(firmware_path, cfg['seq_info_filename'])

            min_dist = cfg['rail_min_dist']
            max_dist = cfg['rail_max_dist']
            dist_step = cfg['rail_dist_step']
            depth_conv_gain = cfg['depth_conv_gain']

            sw_gain = cfg['sw_gain']
            sw_offset = cfg['sw_offset']
            depth_stats_calibrated_df = sc.rail_stats(
                min_dist, max_dist, dist_step, raw_frame, frame, frame_count,
                window, sw_gain, sw_offset, rail_handle, cam_handle)

    logger.info("Calculating Metrics")
    metrics_df = pd.DataFrame()
    #if sweep_config_dict['verify_sweep'] and sweep_config_dict['calculate_metrics']:
    #    metrics_df = metcalc.calculate_metrics(depth_stats_df, depth_stats_calibrated_df)

    logger.info("writing to calibration json files")
    cal_json = os.path.join(firmware_path, 'linear_cal.json')
    output_cal_dict = write_to_cal_json(cal_json, sweep_config_dict)

    save.save_results(latest, archive, depth_stats_df,
                      depth_stats_calibrated_df, linear_offset_df,
                      sweep_config_dict, firmware_path, metrics_df,
                      output_cal_dict)

    camera_results_path = save.get_results_path(sweep_config_json,
                                                sweep_config_dict['unique_id'])
    logger.info('camera results path %s', camera_results_path)
    return linear_offset_df
예제 #2
0
def run_all_calibration(sweep_config_json, dev_handle, unique_id, **kwargs):
    sweep_config_dict = {}
    if (sweep_config_json is not None):
        with open(sweep_config_json) as f:
            sweep_config_dict = json.load(f)

    for key, value in kwargs.items():
        if value is not None:
            sweep_config_dict[key] = kwargs[key]
        if key in sweep_config_dict:
            if sweep_config_dict[key] is None:
                print(
                    'Need to specify %s either as a command line option or in the sweep config json file'
                )
                raise SystemExit

    sweep_config_dict['unique_id'] = unique_id
    sweep_config_dict['unique_id_list'] = split_unique_id(
        sweep_config_dict['unique_id'], 4)

    firmware_path = sweep_config_dict['firmware_path']

    verify_firmware_path = os.path.join(sweep_config_dict['results_path'],
                                        sweep_config_dict['unique_id'],
                                        sweep_config_dict['verify_mode'],
                                        'latest/lf_files')

    device.program_firmware(dev_handle, firmware_path)
    logger.info("Running Sweep Calibration")

    latest, archive = save.make_results_dir(sweep_config_dict['results_path'],
                                            sweep_config_dict['unique_id'],
                                            sweep_config_dict['mode'])

    if sweep_config_dict['calibrate']:
        linear_offset_df, depth_stats_df = calib.run_sweep_calibration(
            dev_handle=dev_handle,
            cfg=sweep_config_dict,
            firmware_path=firmware_path)
        save.save_lf_files(latest, archive, firmware_path)
        write_lf.write_linear_offset_to_lf2(firmware_path,
                                            os.path.join(latest, 'lf_files'),
                                            sweep_config_dict,
                                            linear_offset_df)

    depth_stats_calibrated_df = pd.DataFrame()
    #if sweep_config_dict['verify_sweep']:
    #    logger.info("Verifying Sweep")
    #    _, depth_stats_calibrated_df = calib.verify_sweep(dev_handle, sweep_config_dict, os.path.join(latest, 'lf_files'))

    #logger.info("Calculating Metrics")
    metrics_df = pd.DataFrame()
    #if sweep_config_dict['verify_sweep'] and sweep_config_dict['calculate_metrics']:
    #    metrics_df = metcalc.calculate_metrics(depth_stats_df, depth_stats_calibrated_df)

    #test_depth_error(metrics_df, sweep_config_dict)

    logger.info("Verify depth error for previous station calibrated firmware")
    sweep_config_json_prev = glob.glob(
        os.path.join(verify_firmware_path, 'sweep*.json'))[0]
    sweep_config_dict_prev = {}
    if (sweep_config_json_prev is not None):
        with open(sweep_config_json_prev) as f:
            sweep_config_dict_prev = json.load(f)

    for key, value in kwargs.items():
        if value is not None:
            sweep_config_dict_prev[key] = kwargs[key]
        if key in sweep_config_dict_prev:
            if sweep_config_dict_prev[key] is None:
                print(
                    'Need to specify %s either as a command line option or in the sweep config json file'
                )
                raise SystemExit

    verify_depth(dev_handle, sweep_config_dict, sweep_config_dict_prev,
                 sweep_config_dict['target_distance'], verify_firmware_path,
                 50)

    logger.info("writing to calibration json files")
    cal_json = os.path.join(firmware_path, 'linear_cal.json')
    output_cal_dict = write_to_cal_json(cal_json, sweep_config_dict)

    logger.info("All tests passed. Saving results...")
    save.save_results(latest, archive, depth_stats_df,
                      depth_stats_calibrated_df, linear_offset_df,
                      sweep_config_dict, firmware_path, metrics_df,
                      output_cal_dict)

    return linear_offset_df, dev_handle, sweep_config_dict