| 1 |
""" |
| 2 |
Runs the COLMAP automatic reconstruction pipeline on the ETH3D dataset |
| 3 |
and asserts that the reconstructed model is close to the ground truth. |
| 4 |
|
| 5 |
This script is intended to be run as a CI test. It is not intended to be |
| 6 |
run manually. Instead use benchmark/reconstruction/evaluate.py. |
| 7 |
|
| 8 |
""" |
| 9 |
|
| 10 |
import argparse |
| 11 |
import logging |
| 12 |
import os |
| 13 |
import subprocess |
| 14 |
import sys |
| 15 |
import urllib.request |
| 16 |
|
| 17 |
|
| 18 |
def download_file(url: str, file_path: str, max_retries: int = 3) -> None: |
| 19 |
if os.path.exists(file_path): |
| 20 |
return |
| 21 |
logging.info(f"Downloading {url} to {file_path}") |
| 22 |
for retry in range(max_retries): |
| 23 |
try: |
| 24 |
urllib.request.urlretrieve(url, file_path) |
| 25 |
return |
| 26 |
except Exception as exc: |
| 27 |
logging.error( |
| 28 |
f"Failed to download {url} (trial={retry + 1}) " |
| 29 |
f"to {file_path} due to {exc}" |
| 30 |
) |
| 31 |
|
| 32 |
|
| 33 |
def check_small_errors_or_exit( |
| 34 |
dataset_name: str, |
| 35 |
max_rotation_error: float, |
| 36 |
max_proj_center_error: float, |
| 37 |
expected_num_images: float, |
| 38 |
errors_csv_path: str, |
| 39 |
) -> None: |
| 40 |
logging.info(f"Evaluating errors for {dataset_name}") |
| 41 |
|
| 42 |
error = False |
| 43 |
with open(errors_csv_path) as fid: |
| 44 |
num_images = 0 |
| 45 |
for line in fid: |
| 46 |
line = line.strip() |
| 47 |
if len(line) == 0 or line.startswith("#"): |
| 48 |
continue |
| 49 |
rotation_error, proj_center_error = map(float, line.split(",")) |
| 50 |
num_images += 1 |
| 51 |
if rotation_error > max_rotation_error: |
| 52 |
logging.info( |
| 53 |
"Exceeded rotation error threshold:", rotation_error |
| 54 |
) |
| 55 |
error = True |
| 56 |
if proj_center_error > max_proj_center_error: |
| 57 |
logging.info( |
| 58 |
"Exceeded projection center error threshold:", |
| 59 |
proj_center_error, |
| 60 |
) |
| 61 |
error = True |
| 62 |
|
| 63 |
if num_images != expected_num_images: |
| 64 |
logging.error("Unexpected number of images:", num_images) |
| 65 |
error = True |
| 66 |
|
| 67 |
if error: |
| 68 |
sys.exit(1) |
| 69 |
|
| 70 |
|
| 71 |
def process_dataset(args: argparse.Namespace, dataset_name: str) -> None: |
| 72 |
logging.info("Processing dataset:", dataset_name) |
| 73 |
|
| 74 |
workspace_path = os.path.join( |
| 75 |
os.path.realpath(args.workspace_path), dataset_name |
| 76 |
) |
| 77 |
os.makedirs(workspace_path, exist_ok=True) |
| 78 |
|
| 79 |
dataset_archive_path = os.path.join(workspace_path, f"{dataset_name}.7z") |
| 80 |
download_file( |
| 81 |
f"https://www.eth3d.net/data/{dataset_name}_dslr_undistorted.7z", |
| 82 |
dataset_archive_path, |
| 83 |
) |
| 84 |
|
| 85 |
subprocess.check_call( |
| 86 |
["7zz", "x", "-y", f"{dataset_name}.7z"], cwd=workspace_path |
| 87 |
) |
| 88 |
|
| 89 |
# Find undistorted parameters of first camera and |
| 90 |
# initialize all images with it. This is an approximation |
| 91 |
# because not all datasets have only a single camera. |
| 92 |
# However, it is a good enough initialization. |
| 93 |
with open( |
| 94 |
os.path.join( |
| 95 |
workspace_path, |
| 96 |
f"{dataset_name}/dslr_calibration_undistorted/cameras.txt", |
| 97 |
), |
| 98 |
) as fid: |
| 99 |
for line in fid: |
| 100 |
if not line.startswith("#"): |
| 101 |
first_camera_data = line.split() |
| 102 |
camera_model = first_camera_data[1] |
| 103 |
assert camera_model == "PINHOLE" |
| 104 |
camera_params = first_camera_data[4:] |
| 105 |
assert len(camera_params) == 4 |
| 106 |
break |
| 107 |
|
| 108 |
# Count the number of expected images in the GT. |
| 109 |
expected_num_images = 0 |
| 110 |
with open( |
| 111 |
os.path.join( |
| 112 |
workspace_path, |
| 113 |
f"{dataset_name}/dslr_calibration_undistorted/images.txt", |
| 114 |
), |
| 115 |
) as fid: |
| 116 |
for line in fid: |
| 117 |
if not line.startswith("#") and line.strip(): |
| 118 |
expected_num_images += 1 |
| 119 |
# Each image uses two consecutive lines. |
| 120 |
assert expected_num_images % 2 == 0 |
| 121 |
expected_num_images //= 2 |
| 122 |
|
| 123 |
# Run automatic reconstruction pipeline. |
| 124 |
subprocess.check_call( |
| 125 |
[ |
| 126 |
os.path.realpath(args.colmap_path), |
| 127 |
"automatic_reconstructor", |
| 128 |
"--image_path", |
| 129 |
f"{dataset_name}/images/", |
| 130 |
"--workspace_path", |
| 131 |
workspace_path, |
| 132 |
"--use_gpu", |
| 133 |
"1" if args.use_gpu else "0", |
| 134 |
"--num_threads", |
| 135 |
str(args.num_threads), |
| 136 |
"--quality", |
| 137 |
args.quality, |
| 138 |
"--camera_model", |
| 139 |
"PINHOLE", |
| 140 |
"--camera_params", |
| 141 |
",".join(camera_params), |
| 142 |
], |
| 143 |
cwd=workspace_path, |
| 144 |
) |
| 145 |
|
| 146 |
# Compare reconstructed model to GT model. |
| 147 |
subprocess.check_call( |
| 148 |
[ |
| 149 |
os.path.realpath(args.colmap_path), |
| 150 |
"model_comparer", |
| 151 |
"--input_path1", |
| 152 |
"sparse/0", |
| 153 |
"--input_path2", |
| 154 |
f"{dataset_name}/dslr_calibration_undistorted/", |
| 155 |
"--output_path", |
| 156 |
".", |
| 157 |
"--alignment_error", |
| 158 |
"proj_center", |
| 159 |
"--max_proj_center_error", |
| 160 |
str(args.max_proj_center_error), |
| 161 |
], |
| 162 |
cwd=workspace_path, |
| 163 |
) |
| 164 |
|
| 165 |
# Ensure discrepancy between reconstructed model and GT is small. |
| 166 |
check_small_errors_or_exit( |
| 167 |
dataset_name, |
| 168 |
args.max_rotation_error, |
| 169 |
args.max_proj_center_error, |
| 170 |
expected_num_images, |
| 171 |
os.path.join(workspace_path, "errors.csv"), |
| 172 |
) |
| 173 |
|
| 174 |
|
| 175 |
def parse_args() -> argparse.Namespace: |
| 176 |
parser = argparse.ArgumentParser() |
| 177 |
parser.add_argument("--dataset_names", nargs="+", required=True) |
| 178 |
parser.add_argument("--workspace_path", required=True) |
| 179 |
parser.add_argument("--colmap_path", required=True) |
| 180 |
parser.add_argument("--use_gpu", default=True, action="store_true") |
| 181 |
parser.add_argument("--use_cpu", dest="use_gpu", action="store_false") |
| 182 |
parser.add_argument("--num_threads", type=int, default=-1) |
| 183 |
parser.add_argument("--quality", default="medium") |
| 184 |
parser.add_argument("--max_rotation_error", type=float, default=1.0) |
| 185 |
parser.add_argument("--max_proj_center_error", type=float, default=0.1) |
| 186 |
return parser.parse_args() |
| 187 |
|
| 188 |
|
| 189 |
def main() -> None: |
| 190 |
args = parse_args() |
| 191 |
|
| 192 |
for dataset_name in args.dataset_names: |
| 193 |
process_dataset(args, dataset_name) |
| 194 |
|
| 195 |
|
| 196 |
if __name__ == "__main__": |
| 197 |
main() |