Skip to content

Commit

Permalink
added 3D points to test
Browse files Browse the repository at this point in the history
  • Loading branch information
CardiacMangoes committed Jan 19, 2024
1 parent 46d1735 commit f8ce76e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
20 changes: 11 additions & 9 deletions nerfstudio/data/dataparsers/nerfstudio_dataparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,12 +321,12 @@ def _generate_dataparser_outputs(self, split="train"):

# _generate_dataparser_outputs might be called more than once so we check if we already loaded the point cloud
try:
self.loaded_points
self.prompted_user
except AttributeError:
self.loaded_points = False
self.prompted_user = False

# Load 3D points
if self.config.load_3D_points and not self.loaded_points:
if self.config.load_3D_points:
colmap_path = self.config.data / "colmap/sparse/0"

if "ply_file_path" in meta:
Expand All @@ -336,9 +336,10 @@ def _generate_dataparser_outputs(self, split="train"):
from rich.prompt import Confirm

# check if user wants to make a point cloud from colmap points
self.create_pc = Confirm.ask(
"load_3D_points is true, but the dataset was processed with an outdated ns-process-data that didn't convert colmap points to .ply! Update the colmap dataset automatically?"
)
if not self.prompted_user:
self.create_pc = Confirm.ask(
"load_3D_points is true, but the dataset was processed with an outdated ns-process-data that didn't convert colmap points to .ply! Update the colmap dataset automatically?"
)

if self.create_pc:
import json
Expand All @@ -358,9 +359,10 @@ def _generate_dataparser_outputs(self, split="train"):
else:
ply_file_path = None
else:
CONSOLE.print(
"[bold yellow]Warning: load_3D_points set to true but no point cloud found. gaussian-splatting models will use random point cloud initialization."
)
if not self.prompted_user:
CONSOLE.print(
"[bold yellow]Warning: load_3D_points set to true but no point cloud found. gaussian-splatting models will use random point cloud initialization."
)
ply_file_path = None

if ply_file_path:
Expand Down
10 changes: 6 additions & 4 deletions nerfstudio/process_data/colmap_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ def colmap_to_json(
camera_mask_path: Optional[Path] = None,
image_id_to_depth_path: Optional[Dict[int, Path]] = None,
image_rename_map: Optional[Dict[str, str]] = None,
ply_filename="sparse_pc.ply",
) -> int:
"""Converts COLMAP's cameras.bin and images.bin to a JSON file.
Expand Down Expand Up @@ -462,8 +463,8 @@ def colmap_to_json(
out["applied_transform"] = applied_transform.tolist()

# create ply from colmap
create_ply_from_colmap(recon_dir, output_dir)
out["ply_file_path"] = "sparse_pc.ply"
assert ply_filename.endswith(".ply"), f"ply_filename: {ply_filename} does not end with '.ply'"
out["ply_file_path"] = ply_filename

with open(output_dir / "transforms.json", "w", encoding="utf-8") as f:
json.dump(out, f, indent=4)
Expand Down Expand Up @@ -645,10 +646,11 @@ def get_matching_summary(num_initial_frames: int, num_matched_frames: int) -> st
return f"[bold green]COLMAP found poses for {num_matched_frames / num_initial_frames * 100:.2f}% of the images."


def create_ply_from_colmap(recon_dir, output_dir):
def create_ply_from_colmap(filename: str, recon_dir: Path, output_dir: Path):
"""Writes a ply file from colmap.
Args:
filename: file name for .ply
recon_dir: Directory to grab colmap points
output_dir: Directory to output .ply
"""
Expand All @@ -665,7 +667,7 @@ def create_ply_from_colmap(recon_dir, output_dir):
points3D_rgb = torch.from_numpy(np.array([p.rgb for p in colmap_points.values()], dtype=np.uint8))

# write ply
with open(output_dir / "sparse_pc.ply", "w") as f:
with open(output_dir / filename, "w") as f:
# Header
f.write("ply\n")
f.write("format ascii 1.0\n")
Expand Down
14 changes: 14 additions & 0 deletions tests/process_data/test_process_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
qvec2rotmat,
write_cameras_binary,
write_images_binary,
write_points3D_binary,
)
from nerfstudio.process_data.images_to_nerfstudio_dataset import ImagesToNerfstudioDataset

Expand Down Expand Up @@ -50,6 +51,19 @@ def test_process_images_skip_colmap(tmp_path: Path):
{1: Camera(1, "OPENCV", width, height, [110, 110, 50, 75, 0, 0, 0, 0, 0, 0])},
sparse_path / "cameras.bin",
)
write_points3D_binary(
{
1: Point3D(
id=1,
xyz=np.array(0, 0, 0),
rgb=np.array(0, 0, 0),
error=np.array(0),
image_ids=np.array(1),
point2D_idxs=np.array([]),
),
},
sparse_path / "points3D.bin",
)
frames = {}
num_frames = 10
qvecs = random_quaternion(num_frames)
Expand Down

0 comments on commit f8ce76e

Please sign in to comment.