From ee35703db86f4b9ff32e849ac660455695495c0e Mon Sep 17 00:00:00 2001 From: terrance Date: Fri, 12 May 2023 18:48:58 +0000 Subject: [PATCH] undo process_data change --- nerfstudio/process_data/colmap_utils.py | 9 +- nerfstudio/process_data/hloc_utils.py | 2 +- nerfstudio/process_data/metashape_utils.py | 90 ++++++++----------- nerfstudio/process_data/process_data_utils.py | 6 +- 4 files changed, 48 insertions(+), 59 deletions(-) diff --git a/nerfstudio/process_data/colmap_utils.py b/nerfstudio/process_data/colmap_utils.py index 690080771..f4b9e9b03 100644 --- a/nerfstudio/process_data/colmap_utils.py +++ b/nerfstudio/process_data/colmap_utils.py @@ -13,12 +13,12 @@ # limitations under the License. """ -Tools supporting the execution of COLMAP and preparation of COLMAP-based datasets for nerfstudio training. +Tools supporting the execution of COLMAP and preparation of COLMAP-based datasets for nerstudio training. """ import json from pathlib import Path -from typing import Any, Dict, Literal, Optional +from typing import Any, Dict, Optional import appdirs import cv2 @@ -27,6 +27,7 @@ import requests import torch from rich.console import Console from rich.progress import track +from typing_extensions import Literal # TODO(1480) use pycolmap instead of colmap_parsing_utils # import pycolmap @@ -114,7 +115,9 @@ def run_colmap( colmap_version = get_colmap_version(colmap_cmd) colmap_database_path = colmap_dir / "database.db" - colmap_database_path.unlink(missing_ok=True) + if colmap_database_path.exists(): + # Can't use missing_ok argument because of Python 3.7 compatibility. + colmap_database_path.unlink() # Feature extraction feature_extractor_cmd = [ diff --git a/nerfstudio/process_data/hloc_utils.py b/nerfstudio/process_data/hloc_utils.py index eda02cae1..f04f6c32c 100644 --- a/nerfstudio/process_data/hloc_utils.py +++ b/nerfstudio/process_data/hloc_utils.py @@ -21,9 +21,9 @@ Requires hloc module from : https://github.com/cvg/Hierarchical-Localization import sys from pathlib import Path -from typing import Literal from rich.console import Console +from typing_extensions import Literal from nerfstudio.process_data.process_data_utils import CameraModel diff --git a/nerfstudio/process_data/metashape_utils.py b/nerfstudio/process_data/metashape_utils.py index 414a31ff8..0e4a7593e 100644 --- a/nerfstudio/process_data/metashape_utils.py +++ b/nerfstudio/process_data/metashape_utils.py @@ -61,17 +61,12 @@ def metashape_to_json( # pylint: disable=too-many-statements if sensors is None: raise ValueError("No sensors found") - calibrated_sensors = [ - sensor for sensor in sensors if sensor.get("type") == "spherical" or sensor.find("calibration") - ] + calibrated_sensors = [sensor for sensor in sensors if sensor.find("calibration")] if not calibrated_sensors: raise ValueError("No calibrated sensor found in Metashape XML") sensor_type = [s.get("type") for s in calibrated_sensors] if sensor_type.count(sensor_type[0]) != len(sensor_type): - raise ValueError( - "All Metashape sensors do not have the same sensor type. " - "nerfstudio does not support per-frame camera_model." - ) + raise ValueError("All Metashape sensors do not have the same sensor type") data = {} if sensor_type[0] == "frame": data["camera_model"] = CAMERA_MODELS["perspective"].value @@ -92,54 +87,46 @@ def metashape_to_json( # pylint: disable=too-many-statements s["h"] = int(resolution.get("height")) # type: ignore calib = sensor.find("calibration") - if calib is None: - assert sensor_type[0] == "spherical", "Only spherical sensors should have no intrinsics" - s["fl_x"] = s["w"] / 2.0 - s["fl_y"] = s["h"] - s["cx"] = s["w"] / 2.0 - s["cy"] = s["h"] / 2.0 - else: - f = calib.find("f") - assert f is not None, "Focal length not found in Metashape xml" + f = calib.find("f") + if f is not None: s["fl_x"] = s["fl_y"] = float(f.text) # type: ignore - s["cx"] = _find_param(calib, "cx") + s["w"] / 2.0 # type: ignore - s["cy"] = _find_param(calib, "cy") + s["h"] / 2.0 # type: ignore + s["cx"] = _find_param(calib, "cx") + s["w"] / 2.0 # type: ignore + s["cy"] = _find_param(calib, "cy") + s["h"] / 2.0 # type: ignore - s["k1"] = _find_param(calib, "k1") - s["k2"] = _find_param(calib, "k2") - s["k3"] = _find_param(calib, "k3") - s["k4"] = _find_param(calib, "k4") - s["p1"] = _find_param(calib, "p1") - s["p2"] = _find_param(calib, "p2") + s["k1"] = _find_param(calib, "k1") + s["k2"] = _find_param(calib, "k2") + s["k3"] = _find_param(calib, "k3") + s["k4"] = _find_param(calib, "k4") + s["p1"] = _find_param(calib, "p1") + s["p2"] = _find_param(calib, "p2") sensor_dict[sensor.get("id")] = s components = chunk.find("components") - if components is not None: - component_dict = {} - for component in components: - transform = component.find("transform") - if transform is not None: - rotation = transform.find("rotation") - if rotation is None: - r = np.eye(3) - else: - r = np.array([float(x) for x in rotation.text.split()]).reshape((3, 3)) - translation = transform.find("translation") - if translation is None: - t = np.zeros(3) - else: - t = np.array([float(x) for x in translation.text.split()]) - scale = transform.find("scale") - if scale is None: - s = 1.0 - else: - s = float(scale.text) + component_dict = {} + for component in components: + transform = component.find("transform") + if transform is not None: + rotation = transform.find("rotation") + if rotation is None: + r = np.eye(3) + else: + r = np.array([float(x) for x in rotation.text.split()]).reshape((3, 3)) + translation = transform.find("translation") + if translation is None: + t = np.zeros(3) + else: + t = np.array([float(x) for x in translation.text.split()]) + scale = transform.find("scale") + if scale is None: + s = 1.0 + else: + s = float(scale.text) - m = np.eye(4) - m[:3, :3] = r - m[:3, 3] = t / s - component_dict[component.get("id")] = m + m = np.eye(4) + m[:3, :3] = r + m[:3, 3] = t / s + component_dict[component.get("id")] = m frames = [] cameras = chunk.find("cameras") @@ -172,10 +159,9 @@ def metashape_to_json( # pylint: disable=too-many-statements num_skipped += 1 continue transform = np.array([float(x) for x in camera.find("transform").text.split()]).reshape((4, 4)) - if components is not None: - component_id = camera.get("component_id") - if component_id in component_dict: - transform = component_dict[component_id] @ transform + component_id = camera.get("component_id") + if component_id in component_dict: + transform = component_dict[component_id] @ transform transform = transform[[2, 0, 1, 3], :] transform[:, 1:3] *= -1 frame["transform_matrix"] = transform.tolist() diff --git a/nerfstudio/process_data/process_data_utils.py b/nerfstudio/process_data/process_data_utils.py index 74a981826..a977df8b2 100644 --- a/nerfstudio/process_data/process_data_utils.py +++ b/nerfstudio/process_data/process_data_utils.py @@ -20,11 +20,12 @@ import shutil import sys from enum import Enum from pathlib import Path -from typing import List, Literal, Optional, OrderedDict, Tuple +from typing import List, Optional, Tuple import cv2 import numpy as np from rich.console import Console +from typing_extensions import Literal, OrderedDict from nerfstudio.utils.rich_utils import status from nerfstudio.utils.scripts import run_command @@ -38,13 +39,12 @@ class CameraModel(Enum): OPENCV = "OPENCV" OPENCV_FISHEYE = "OPENCV_FISHEYE" - EQUIRECTANGULAR = "EQUIRECTANGULAR" CAMERA_MODELS = { "perspective": CameraModel.OPENCV, "fisheye": CameraModel.OPENCV_FISHEYE, - "equirectangular": CameraModel.EQUIRECTANGULAR, + "equirectangular": CameraModel.OPENCV, }