Untitled

mail@pastecode.io avatar
unknown
plain_text
25 days ago
2.6 kB
4
Indexable
Never
class Colmap:
    def __init__(self, image_path, database_path, sfm_output_path, sfm_output_txt_path):
        self.image_path = image_path
        self.database_path = database_path
        self.sfm_output_path = sfm_output_path
        self.sfm_output_txt_path = sfm_output_txt_path

    def run_feature_extractor(self):
        subprocess.run([
            "colmap", "feature_extractor",
            "--database_path", self.database_path,
            "--image_path", self.image_path
        ], check=True)

    def run_exhaustive_matcher(self):
        subprocess.run([
            "colmap", "exhaustive_matcher",
            "--database_path", self.database_path
        ], check=True)

    def run_mapper(self):
        os.makedirs(self.sfm_output_path, exist_ok=True)
        subprocess.run([
            "colmap", "mapper",
            "--database_path", self.database_path,
            "--image_path", self.image_path,
            "--output_path", self.sfm_output_path
        ], check=True)

    def run_model_converter(self):
        os.makedirs(self.sfm_output_txt_path, exist_ok=True)
        subprocess.run([
            "colmap", "model_converter",
            "--input_path", os.path.join(self.sfm_output_path, "0"),
            "--output_path", self.sfm_output_txt_path,
            "--output_type", "TXT"
        ], check=True)

    def read_cameras(self, file_path):
        with open(file_path, 'r') as file:
            lines = file.readlines()[3:]
        cameras = {}
        for line in lines:
            elems = line.split()
            camera_id = int(elems[0])
            model = elems[1]
            width = int(elems[2])
            height = int(elems[3])
            params = np.array(list(map(float, elems[4:])))
            cameras[camera_id] = {
                'model': model,
                'width': width,
                'height': height,
                'params': params
            }
        return cameras

    def read_images(self, file_path):
        with open(file_path, 'r') as file:
            lines = file.readlines()[4:]
        images = {}
        for i in range(0, len(lines), 2):
            elems = lines[i].split()
            image_id = int(elems[0])
            qvec = np.array(list(map(float, elems[1:5])))
            tvec = np.array(list(map(float, elems[5:8])))
            camera_id = int(elems[8])
            name = elems[9]
            images[image_id] = {
                'qvec': qvec,
                'tvec': tvec,
                'camera_id': camera_id,
                'name': name
            }
        return images
Leave a Comment