Пример #1
0
def process_peter(calibration_path,
                  board,
                  cam_regex,
                  fisheye=False,
                  video_ext='avi'):
    videos = glob(os.path.join(calibration_path,'*.' +video_ext))
    videos = sorted(videos)

    cam_videos = defaultdict(list)
    cam_names = set()
    for vid in videos:
        name = tools.camname_from_regex(cam_regex, vid)
        cam_videos[name].append(vid)
        cam_names.add(name)
    cam_names = sorted(cam_names)
    cgroup = CameraGroup.from_names(cam_names, fisheye)

    video_list = [sorted(cam_videos[cname]) for cname in cam_names]
    if len(videos) == 0:
        print('no videos or calibration file found, continuing...')
        return

    rows_fname = os.path.join(calibration_path, 'detections.pickle')
    if os.path.exists(rows_fname):
        with open(rows_fname, 'rb') as f:
            all_rows = pickle.load(f)
    else:
        all_rows = cgroup.get_rows_videos(video_list, board)
        with open(rows_fname, 'wb') as f:
            pickle.dump(all_rows, f)

    cgroup.set_camera_sizes_videos(video_list)

    cgroup.calibrate_rows(all_rows, board,
                          init_extrinsics=True,
                          init_intrinsics=True,
                          max_nfev=100, n_iters=2,
                          n_samp_iter=100, n_samp_full=300,
                          verbose=True)
    error = cgroup.calibrate_rows(all_rows, board,
                                  init_intrinsics=False, init_extrinsics=False,
                                  max_nfev=100, n_iters=10,
                                  n_samp_iter=100, n_samp_full=1000,
                                  verbose=True)

    cgroup.metadata['adjusted'] = False
    if error is not None:
        cgroup.metadata['error'] = float(error)
    outname = os.path.join(calibration_path, 'calibration.toml')
    cgroup.dump(outname)
    print(outname)
Пример #2
0
def process_session(config, session_path):
    pipeline_calibration_videos = config['pipeline']['calibration_videos']
    pipeline_calibration_results = config['pipeline']['calibration_results']
    video_ext = config['video_extension']

    print(session_path)

    calibration_path = find_calibration_folder(config, session_path)

    if calibration_path is None:
        return

    videos = glob(
        os.path.join(calibration_path, pipeline_calibration_videos,
                     '*.' + video_ext))
    videos = sorted(videos)

    cam_videos = defaultdict(list)
    cam_names = set()
    for vid in videos:
        name = get_cam_name(config, vid)
        cam_videos[name].append(vid)
        cam_names.add(name)

    cam_names = sorted(cam_names)

    video_list = [sorted(cam_videos[cname]) for cname in cam_names]

    outname_base = 'calibration.toml'
    outdir = os.path.join(calibration_path, pipeline_calibration_results)
    outname = os.path.join(outdir, outname_base)

    print(outname)
    skip_calib = False
    init_stuff = True
    error = None

    if os.path.exists(outname):
        cgroup = CameraGroup.load(outname)
        if (not config['calibration']['animal_calibration']) or \
           ('adjusted' in cgroup.metadata and cgroup.metadata['adjusted']):
            return
        else:
            skip_calib = True
            if 'error' in cgroup.metadata:
                error = cgroup.metadata['error']
            else:
                error = None
        init_stuff = False
    elif config['calibration']['calibration_init'] is not None:
        calib_path = os.path.join(config['path'],
                                  config['calibration']['calibration_init'])
        print('loading calibration from: {}'.format(calib_path))
        cgroup = CameraGroup.load(calib_path)
        init_stuff = False
        skip_calib = len(videos) == 0
    else:
        if len(videos) == 0:
            print('no videos or calibration file found, continuing...')
            return
        cgroup = CameraGroup.from_names(cam_names,
                                        config['calibration']['fisheye'])

    board = get_calibration_board(config)

    if not skip_calib:
        rows_fname = os.path.join(outdir, 'detections.pickle')
        if os.path.exists(rows_fname):
            with open(rows_fname, 'rb') as f:
                all_rows = pickle.load(f)
        else:
            all_rows = cgroup.get_rows_videos(video_list, board)
            with open(rows_fname, 'wb') as f:
                pickle.dump(all_rows, f)

        cgroup.set_camera_sizes_videos(video_list)

        cgroup.calibrate_rows(all_rows,
                              board,
                              init_extrinsics=init_stuff,
                              init_intrinsics=init_stuff,
                              max_nfev=100,
                              n_iters=2,
                              n_samp_iter=100,
                              n_samp_full=300,
                              verbose=True)
        error = cgroup.calibrate_rows(all_rows,
                                      board,
                                      init_intrinsics=False,
                                      init_extrinsics=False,
                                      max_nfev=100,
                                      n_iters=10,
                                      n_samp_iter=100,
                                      n_samp_full=1000,
                                      verbose=True)

    cgroup.metadata['adjusted'] = False
    if error is not None:
        cgroup.metadata['error'] = float(error)
    cgroup.dump(outname)

    if config['calibration']['animal_calibration']:
        all_points, all_scores, all_cam_names = load_2d_data(
            config, calibration_path)
        imgp = process_points_for_calibration(all_points, all_scores)
        # error = cgroup.bundle_adjust(imgp, threshold=10, ftol=1e-4, loss='huber')
        cgroup = cgroup.subset_cameras_names(all_cam_names)
        error = cgroup.bundle_adjust_iter(imgp,
                                          ftol=1e-4,
                                          n_iters=10,
                                          n_samp_iter=300,
                                          n_samp_full=1000,
                                          max_nfev=500,
                                          verbose=True)
        cgroup.metadata['adjusted'] = True
        cgroup.metadata['error'] = float(error)

    cgroup.dump(outname)