Debian Codegraph @

python/examples/example.py

raw
1 """
2 An example for running incremental SfM on images with the pycolmap interface.
3 """
4
5 import shutil
6 import urllib.request
7 import zipfile
8 from pathlib import Path
9
10 import enlighten
11
12 import pycolmap
13 from pycolmap import logging
14
15
16 def incremental_mapping_with_pbar(
17 database_path: Path, image_path: Path, sfm_path: Path
18 ) -> dict[int, pycolmap.Reconstruction]:
19 with pycolmap.Database.open(database_path) as database:
20 num_images = database.num_images()
21 with enlighten.Manager() as manager:
22 with manager.counter(
23 total=num_images, desc="Images registered:"
24 ) as pbar:
25 pbar.update(0, force=True)
26 reconstructions = pycolmap.incremental_mapping(
27 database_path,
28 image_path,
29 sfm_path,
30 initial_image_pair_callback=lambda: pbar.update(2),
31 next_image_callback=lambda: pbar.update(1),
32 )
33 return reconstructions
34
35
36 def run() -> None:
37 output_path = Path("example/")
38 image_path = output_path / "Fountain/images"
39 database_path = output_path / "database.db"
40 sfm_path = output_path / "sfm"
41
42 output_path.mkdir(exist_ok=True)
43 # The log filename is postfixed with the execution timestamp.
44 logging.set_log_destination(logging.INFO, output_path / "INFO.log.")
45
46 data_url = "https://cvg-data.inf.ethz.ch/local-feature-evaluation-schoenberger2017/Strecha-Fountain.zip"
47 if not image_path.exists():
48 logging.info("Downloading the data.")
49 zip_path = output_path / "data.zip"
50 urllib.request.urlretrieve(data_url, zip_path)
51 with zipfile.ZipFile(zip_path, "r") as fid:
52 fid.extractall(output_path)
53 logging.info(f"Data extracted to {output_path}.")
54
55 if database_path.exists():
56 database_path.unlink()
57 pycolmap.set_random_seed(0)
58 pycolmap.extract_features(database_path, image_path)
59 pycolmap.match_exhaustive(database_path)
60
61 if sfm_path.exists():
62 shutil.rmtree(sfm_path)
63 sfm_path.mkdir(exist_ok=True)
64
65 recs = incremental_mapping_with_pbar(database_path, image_path, sfm_path)
66 # alternatively, use:
67 # import custom_incremental_pipeline
68 # recs = custom_incremental_pipeline.main(
69 # database_path, image_path, sfm_path
70 # )
71 for idx, rec in recs.items():
72 logging.info(f"#{idx} {rec.summary()}")
73
74
75 if __name__ == "__main__":
76 run()