mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-12-01 21:39:06 +08:00
improve metashape data import (#1605)
* improve metashape data import - support multiple sensors - support fisheye and equirectangular sensors - support multiple components with transforms - fix bug when image name has a '.' in it, excluding the prefix separator * metashape_util: fix string formatting --------- Co-authored-by: Frédéric Devernay <f-dy@users.noreply.github.com> Co-authored-by: Matthew Tancik <tancik@berkeley.edu>
This commit is contained in:
1 file changed
+77
-37
@@ -27,7 +27,7 @@ from nerfstudio.process_data.process_data_utils import CAMERA_MODELS
|
|||||||
CONSOLE = Console(width=120)
|
CONSOLE = Console(width=120)
|
||||||
|
|
||||||
|
|
||||||
def _find_distortion_param(calib_xml: ET.Element, param_name: str):
|
def _find_param(calib_xml: ET.Element, param_name: str):
|
||||||
param = calib_xml.find(param_name)
|
param = calib_xml.find(param_name)
|
||||||
if param is not None:
|
if param is not None:
|
||||||
return float(param.text) # type: ignore
|
return float(param.text) # type: ignore
|
||||||
@@ -62,34 +62,71 @@ def metashape_to_json( # pylint: disable=too-many-statements
|
|||||||
raise ValueError("No sensors found")
|
raise ValueError("No sensors found")
|
||||||
|
|
||||||
calibrated_sensors = [sensor for sensor in sensors if sensor.find("calibration")]
|
calibrated_sensors = [sensor for sensor in sensors if sensor.find("calibration")]
|
||||||
if len(calibrated_sensors) != 1:
|
if not calibrated_sensors:
|
||||||
raise ValueError("Only one sensor is supported for now")
|
raise ValueError("No calibrated sensor found in Metashape XML")
|
||||||
|
sensor_type = [s.get("type") for s in calibrated_sensors]
|
||||||
sensor = calibrated_sensors[0]
|
if sensor_type.count(sensor_type[0]) != len(sensor_type):
|
||||||
|
raise ValueError("All Metashape sensors do not have the same sensor type")
|
||||||
data = {}
|
data = {}
|
||||||
|
if sensor_type[0] == "frame":
|
||||||
|
data["camera_model"] = CAMERA_MODELS["perspective"].value
|
||||||
|
elif sensor_type[0] == "fisheye":
|
||||||
|
data["camera_model"] = CAMERA_MODELS["fisheye"].value
|
||||||
|
elif sensor_type[0] == "spherical":
|
||||||
|
data["camera_model"] = CAMERA_MODELS["equirectangular"].value
|
||||||
|
else:
|
||||||
|
# Cylindrical and RPC sensor types are not supported
|
||||||
|
raise ValueError(f"Unsupported Metashape sensor type '{sensor_type[0]}'")
|
||||||
|
|
||||||
assert sensor is not None, "Sensor not found in Metashape XML"
|
sensor_dict = {}
|
||||||
resolution = sensor.find("resolution")
|
for sensor in calibrated_sensors:
|
||||||
assert resolution is not None, "Resolution not found in Metashape xml"
|
s = {}
|
||||||
data["w"] = int(resolution.get("width")) # type: ignore
|
resolution = sensor.find("resolution")
|
||||||
data["h"] = int(resolution.get("height")) # type: ignore
|
assert resolution is not None, "Resolution not found in Metashape xml"
|
||||||
|
s["w"] = int(resolution.get("width")) # type: ignore
|
||||||
|
s["h"] = int(resolution.get("height")) # type: ignore
|
||||||
|
|
||||||
calib = sensor.find("calibration")
|
calib = sensor.find("calibration")
|
||||||
assert calib is not None, "Calibration not found in Metashape xml"
|
f = calib.find("f")
|
||||||
data["fl_x"] = float(calib.find("f").text) # type: ignore
|
if f is not None:
|
||||||
data["fl_y"] = float(calib.find("f").text) # type: ignore
|
s["fl_x"] = s["fl_y"] = float(f.text) # type: ignore
|
||||||
data["cx"] = float(calib.find("cx").text) + data["w"] / 2.0 # type: ignore
|
s["cx"] = _find_param(calib, "cx") + s["w"] / 2.0 # type: ignore
|
||||||
data["cy"] = float(calib.find("cy").text) + data["h"] / 2.0 # type: ignore
|
s["cy"] = _find_param(calib, "cy") + s["h"] / 2.0 # type: ignore
|
||||||
|
|
||||||
data["k1"] = _find_distortion_param(calib, "k1")
|
s["k1"] = _find_param(calib, "k1")
|
||||||
data["k2"] = _find_distortion_param(calib, "k2")
|
s["k2"] = _find_param(calib, "k2")
|
||||||
data["k3"] = _find_distortion_param(calib, "k3")
|
s["k3"] = _find_param(calib, "k3")
|
||||||
data["k4"] = _find_distortion_param(calib, "k4")
|
s["k4"] = _find_param(calib, "k4")
|
||||||
data["p1"] = _find_distortion_param(calib, "p1")
|
s["p1"] = _find_param(calib, "p1")
|
||||||
data["p2"] = _find_distortion_param(calib, "p2")
|
s["p2"] = _find_param(calib, "p2")
|
||||||
|
|
||||||
data["camera_model"] = CAMERA_MODELS["perspective"].value
|
sensor_dict[sensor.get("id")] = s
|
||||||
|
|
||||||
|
components = chunk.find("components")
|
||||||
|
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
|
||||||
|
|
||||||
frames = []
|
frames = []
|
||||||
cameras = chunk.find("cameras")
|
cameras = chunk.find("cameras")
|
||||||
@@ -97,33 +134,36 @@ def metashape_to_json( # pylint: disable=too-many-statements
|
|||||||
num_skipped = 0
|
num_skipped = 0
|
||||||
for camera in cameras:
|
for camera in cameras:
|
||||||
frame = {}
|
frame = {}
|
||||||
# Labels sometimes have a file extension. We remove it for consistency.
|
camera_label = camera.get("label")
|
||||||
camera_label = camera.get("label").split(".")[0] # type: ignore
|
|
||||||
if camera_label not in image_filename_map:
|
if camera_label not in image_filename_map:
|
||||||
continue
|
# Labels sometimes have a file extension. Try without the extension.
|
||||||
|
# (maybe it's just a '.' in the image name)
|
||||||
|
camera_label = camera_label.split(".")[0] # type: ignore
|
||||||
|
if camera_label not in image_filename_map:
|
||||||
|
continue
|
||||||
frame["file_path"] = image_filename_map[camera_label].as_posix()
|
frame["file_path"] = image_filename_map[camera_label].as_posix()
|
||||||
|
|
||||||
if camera.get("sensor_id") != sensor.get("id"):
|
sensor_id = camera.get("sensor_id")
|
||||||
|
if sensor_id not in sensor_dict:
|
||||||
# this should only happen when we have a sensor that doesn't have calibration
|
# this should only happen when we have a sensor that doesn't have calibration
|
||||||
if verbose:
|
if verbose:
|
||||||
CONSOLE.print(f"Missing sensor calibration for {camera.get('label')}, Skipping")
|
CONSOLE.print(f"Missing sensor calibration for {camera.get('label')}, Skipping")
|
||||||
num_skipped += 1
|
num_skipped += 1
|
||||||
continue
|
continue
|
||||||
|
# Add all sensor parameters to this frame.
|
||||||
|
frame.update(sensor_dict[sensor_id])
|
||||||
|
|
||||||
if camera.find("transform") is None:
|
if camera.find("transform") is None:
|
||||||
if verbose:
|
if verbose:
|
||||||
CONSOLE.print(f"Missing transforms data for {camera.get('label')}, Skipping")
|
CONSOLE.print(f"Missing transforms data for {camera.get('label')}, Skipping")
|
||||||
num_skipped += 1
|
num_skipped += 1
|
||||||
continue
|
continue
|
||||||
t = [float(x) for x in camera.find("transform").text.split()] # type: ignore
|
transform = np.array([float(x) for x in camera.find("transform").text.split()]).reshape((4, 4))
|
||||||
transform = np.array(
|
component_id = camera.get("component_id")
|
||||||
[
|
if component_id in component_dict:
|
||||||
[t[8], -t[9], -t[10], t[11]],
|
transform = component_dict[component_id] @ transform
|
||||||
[t[0], -t[1], -t[2], t[3]],
|
transform = transform[[2, 0, 1, 3], :]
|
||||||
[t[4], -t[5], -t[6], t[7]],
|
transform[:, 1:3] *= -1
|
||||||
[t[12], -t[13], -t[14], t[15]],
|
|
||||||
]
|
|
||||||
)
|
|
||||||
frame["transform_matrix"] = transform.tolist()
|
frame["transform_matrix"] = transform.tolist()
|
||||||
frames.append(frame)
|
frames.append(frame)
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user