Beispiel #1
0
def add_cameras_to_database(sensors: kapture.Sensors,
                            database: COLMAPDatabase) -> Dict[str, int]:
    """
    Add the kapture cameras to the colmap database.

    :param sensors: list af kapture cameras
    :param database: colmap database.
    :return: dictionary of kapture camera identifier -> colmap camera identifier
    """
    assert isinstance(sensors, kapture.Sensors)
    colmap_camera_ids = {}
    cameras = {
        cam_id: camera
        for cam_id, camera in sensors.items()
        if isinstance(camera, kapture.Camera)
    }

    for cam_id, cam in cameras.items():
        col_cam_id, width, height, params, prior_focal_length = get_colmap_camera(
            cam)
        colmap_camera_ids[cam_id] = database.add_camera(
            col_cam_id,
            width,
            height,
            params,
            prior_focal_length=prior_focal_length)

    database.commit()
    return colmap_camera_ids
Beispiel #2
0
def export_to_colmap_cameras_txt(colmap_cameras_filepath: str,
                                 sensors: kapture.Sensors,
                                 colmap_camera_ids: Dict[str, int]) -> None:
    """
    Exports to colmap reconstruction file "cameras.txt".

    :param colmap_cameras_filepath: path to colmap file "cameras.txt" to be writen.
    :param sensors: sensors to be exported
    :param colmap_camera_ids: gives the correspondences between kapture camera id and colmap camera id
    """
    assert path.basename(colmap_cameras_filepath) == 'cameras.txt'
    assert isinstance(colmap_camera_ids, dict)
    assert isinstance(sensors, kapture.Sensors)
    cameras = {
        cam_id: cam
        for cam_id, cam in sensors.items() if isinstance(cam, kapture.Camera)
    }

    # cameras.txt
    cameras_colmap_header = '# Sensor list with one line of data per camera:\n' \
                            '#   CAMERA_ID, MODEL, WIDTH, HEIGHT, PARAMS[]\n' \
                            '# NB cameras : {}\n'.format(len(cameras))

    converted_cameras = [(cam_id, get_colmap_camera(cam))
                         for cam_id, cam in cameras.items()]
    with open(colmap_cameras_filepath, 'w') as fid:
        fid.write(cameras_colmap_header)
        lines = (' '.join(
            [str(colmap_camera_ids[cam_id]), CAMERA_MODEL_NAMES[col_cam_id]] +
            [str(int(width)), str(int(height))] +
            [str(int(v)) if v.is_integer() else str(v) for v in params])
                 for cam_id, (col_cam_id, width, height, params,
                              prior_focal_length) in converted_cameras
                 if cam_id in colmap_camera_ids)
        fid.write('\n'.join(lines))
Beispiel #3
0
def get_sensors_mapping(sensors: kapture.Sensors,
                        offset: int = 0) -> Dict[str, str]:
    """
    Creates list of sensor names,identifiers

    :param sensors: list of sensor definitions
    :param offset: optional offset for the identifier numbers
    :return: mapping of sensor names to identifiers
    """
    return {
        k: f'sensor{v}'
        for k, v in zip(sensors.keys(), range(offset, offset + len(sensors)))
    }
Beispiel #4
0
def sensors_to_file(filepath: str, sensors: kapture.Sensors) -> None:
    """
    Writes the sensors to CSV file.

    :param filepath: input file path
    :param sensors: input sensors
    """
    assert (isinstance(sensors, kapture.Sensors))
    header = '# sensor_id, name, sensor_type, [sensor_params]+'
    table = ([sensor_id] + sensor_to_list(sensor)
             for sensor_id, sensor in sensors.items())

    os.makedirs(path.dirname(filepath), exist_ok=True)
    with open(filepath, 'w') as file:
        table_to_file(file, table, header=header)