| 1 |
# Copyright (c), ETH Zurich and UNC Chapel Hill. |
| 2 |
# All rights reserved. |
| 3 |
# |
| 4 |
# Redistribution and use in source and binary forms, with or without |
| 5 |
# modification, are permitted provided that the following conditions are met: |
| 6 |
# |
| 7 |
# * Redistributions of source code must retain the above copyright |
| 8 |
# notice, this list of conditions and the following disclaimer. |
| 9 |
# |
| 10 |
# * Redistributions in binary form must reproduce the above copyright |
| 11 |
# notice, this list of conditions and the following disclaimer in the |
| 12 |
# documentation and/or other materials provided with the distribution. |
| 13 |
# |
| 14 |
# * Neither the name of ETH Zurich and UNC Chapel Hill nor the names of |
| 15 |
# its contributors may be used to endorse or promote products derived |
| 16 |
# from this software without specific prior written permission. |
| 17 |
# |
| 18 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 |
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 |
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
| 22 |
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 |
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 |
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 |
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 |
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 |
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 |
# POSSIBILITY OF SUCH DAMAGE. |
| 29 |
|
| 30 |
|
| 31 |
import numpy as np |
| 32 |
from PIL import Image |
| 33 |
|
| 34 |
import pycolmap |
| 35 |
|
| 36 |
from .utils import Dataset, SceneInfo |
| 37 |
|
| 38 |
|
| 39 |
class DatasetBlendedMVS(Dataset): |
| 40 |
@property |
| 41 |
def position_accuracy_gt(self): |
| 42 |
return 0.001 |
| 43 |
|
| 44 |
@property |
| 45 |
def supports_covisibility_filtering(self) -> bool: |
| 46 |
return True |
| 47 |
|
| 48 |
def list_scenes(self): |
| 49 |
scene_infos = [] |
| 50 |
for category_path in (self.data_path / "blended-mvs").iterdir(): |
| 51 |
if not category_path.is_dir() or ( |
| 52 |
self.categories and category_path.name not in self.categories |
| 53 |
): |
| 54 |
continue |
| 55 |
|
| 56 |
category = category_path.name |
| 57 |
|
| 58 |
for scene_path in sorted(category_path.iterdir()): |
| 59 |
if not scene_path.is_dir(): |
| 60 |
continue |
| 61 |
|
| 62 |
scene = scene_path.name |
| 63 |
if self.scenes and scene not in self.scenes: |
| 64 |
continue |
| 65 |
|
| 66 |
workspace_path = ( |
| 67 |
self.run_path |
| 68 |
/ self.run_name |
| 69 |
/ "blended-mvs" |
| 70 |
/ category |
| 71 |
/ scene |
| 72 |
) |
| 73 |
image_path = scene_path / "blended_images" |
| 74 |
image_list_path = scene_path / "images.txt" |
| 75 |
num_images = 0 |
| 76 |
with open(image_list_path, "w") as fid: |
| 77 |
for filepath in sorted(image_path.iterdir()): |
| 78 |
image_name = str(filepath.name) |
| 79 |
if ( |
| 80 |
image_name.endswith(".jpg") |
| 81 |
and "masked" not in image_name |
| 82 |
): |
| 83 |
fid.write(image_name + "\n") |
| 84 |
num_images += 1 |
| 85 |
|
| 86 |
sparse_gt_path = scene_path / "sparse_gt" |
| 87 |
colmap_extra_args = ["--image_list_path", image_list_path] |
| 88 |
|
| 89 |
scene_info = SceneInfo( |
| 90 |
dataset="blended-mvs", |
| 91 |
category=category, |
| 92 |
scene=scene, |
| 93 |
num_images=num_images, |
| 94 |
workspace_path=workspace_path, |
| 95 |
image_path=image_path, |
| 96 |
sparse_gt_path=sparse_gt_path, |
| 97 |
has_camera_priors=True, |
| 98 |
colmap_extra_args=colmap_extra_args, |
| 99 |
) |
| 100 |
|
| 101 |
scene_infos.append(scene_info) |
| 102 |
|
| 103 |
return scene_infos |
| 104 |
|
| 105 |
def prepare_scene(self, scene_info): |
| 106 |
if scene_info.sparse_gt_path.exists(): |
| 107 |
return |
| 108 |
|
| 109 |
scene_path = scene_info.image_path.parent |
| 110 |
|
| 111 |
sparse_gt = pycolmap.Reconstruction() |
| 112 |
for i, filepath in enumerate(sorted((scene_path / "cams").iterdir())): |
| 113 |
filename = str(filepath.name) |
| 114 |
if not filename.endswith("_cam.txt"): |
| 115 |
continue |
| 116 |
image_name = filename[:-8] + ".jpg" |
| 117 |
width, height = Image.open( |
| 118 |
scene_path / "blended_images" / image_name |
| 119 |
).size[:2] |
| 120 |
with open(filepath, encoding="ascii") as fid: |
| 121 |
lines = list(map(lambda b: b.strip(), fid.readlines())) |
| 122 |
extrinsic = np.fromstring( |
| 123 |
" ".join(lines[1:4]), |
| 124 |
count=12, |
| 125 |
sep=" ", |
| 126 |
).reshape(3, 4) |
| 127 |
intrinsic = np.fromstring( |
| 128 |
" ".join(lines[7:10]), |
| 129 |
count=9, |
| 130 |
sep=" ", |
| 131 |
).reshape(3, 3) |
| 132 |
camera = pycolmap.Camera( |
| 133 |
camera_id=i, |
| 134 |
model=pycolmap.CameraModelId.PINHOLE, |
| 135 |
width=width, |
| 136 |
height=height, |
| 137 |
params=intrinsic[(0, 1, 0, 1), (0, 1, 2, 2)], |
| 138 |
) |
| 139 |
rig = pycolmap.Rig(rig_id=i) |
| 140 |
rig.add_ref_sensor(camera.sensor_id) |
| 141 |
image = pycolmap.Image( |
| 142 |
image_id=i, |
| 143 |
camera_id=i, |
| 144 |
name=image_name, |
| 145 |
) |
| 146 |
image.frame_id = i |
| 147 |
frame = pycolmap.Frame(frame_id=i) |
| 148 |
frame.rig_id = i |
| 149 |
frame.add_data_id(image.data_id) |
| 150 |
frame.rig_from_world = pycolmap.Rigid3d(extrinsic) |
| 151 |
sparse_gt.add_camera(camera) |
| 152 |
sparse_gt.add_rig(rig) |
| 153 |
sparse_gt.add_frame(frame) |
| 154 |
sparse_gt.add_image(image) |
| 155 |
|
| 156 |
scene_info.sparse_gt_path.mkdir(exist_ok=True) |
| 157 |
sparse_gt.write(scene_info.sparse_gt_path) |