mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-11-19 22:25:44 +08:00
GaussianSplats export: allow uint8 in ply (#3111)
* GaussianSplats export: allow uint8 in ply * Ruff formatting * Add test and narrower typing * Formatting * Revert narrow typing, not supported by old python * Fix typing for old pythons
This commit is contained in:
1 parent
f046ceadf6
commit
bc9328c7ff
2 files changed
+103
-10
No files matched your search
@@ -22,6 +22,7 @@ from __future__ import annotations
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import typing
|
||||
from collections import OrderedDict
|
||||
from dataclasses import dataclass, field
|
||||
from pathlib import Path
|
||||
@@ -487,15 +488,19 @@ class ExportGaussianSplat(Exporter):
|
||||
"""Scale of the oriented bounding box along each axis."""
|
||||
|
||||
@staticmethod
|
||||
def write_ply(filename: str, count: int, map_to_tensors: OrderedDict[str, np.ndarray]):
|
||||
def write_ply(
|
||||
filename: str,
|
||||
count: int,
|
||||
map_to_tensors: typing.OrderedDict[str, np.ndarray],
|
||||
):
|
||||
"""
|
||||
Writes a PLY file with given vertex properties and their float values in the order specified by the OrderedDict.
|
||||
Writes a PLY file with given vertex properties and a tensor of float or uint8 values in the order specified by the OrderedDict.
|
||||
Note: All float values will be converted to float32 for writing.
|
||||
|
||||
Parameters:
|
||||
filename (str): The name of the file to write.
|
||||
count (int): The number of vertices to write.
|
||||
map_to_tensors (OrderedDict[str, np.ndarray]): An ordered dictionary mapping property names to numpy arrays of float values.
|
||||
map_to_tensors (OrderedDict[str, np.ndarray]): An ordered dictionary mapping property names to numpy arrays of float or uint8 values.
|
||||
Each array should be 1-dimensional and of equal length matching 'count'. Arrays should not be empty.
|
||||
"""
|
||||
|
||||
@@ -503,12 +508,14 @@ class ExportGaussianSplat(Exporter):
|
||||
if not all(len(tensor) == count for tensor in map_to_tensors.values()):
|
||||
raise ValueError("Count does not match the length of all tensors")
|
||||
|
||||
# Type check for numpy arrays of type float and non-empty
|
||||
# Type check for numpy arrays of type float or uint8 and non-empty
|
||||
if not all(
|
||||
isinstance(tensor, np.ndarray) and tensor.dtype.kind in ["f", "d"] and tensor.size > 0
|
||||
isinstance(tensor, np.ndarray)
|
||||
and (tensor.dtype.kind == "f" or tensor.dtype == np.uint8)
|
||||
and tensor.size > 0
|
||||
for tensor in map_to_tensors.values()
|
||||
):
|
||||
raise ValueError("All tensors must be numpy arrays of float type and not empty")
|
||||
raise ValueError("All tensors must be numpy arrays of float or uint8 type and not empty")
|
||||
|
||||
with open(filename, "wb") as ply_file:
|
||||
# Write PLY header
|
||||
@@ -518,17 +525,21 @@ class ExportGaussianSplat(Exporter):
|
||||
ply_file.write(f"element vertex {count}\n".encode())
|
||||
|
||||
# Write properties, in order due to OrderedDict
|
||||
for key in map_to_tensors.keys():
|
||||
ply_file.write(f"property float {key}\n".encode())
|
||||
for key, tensor in map_to_tensors.items():
|
||||
data_type = "float" if tensor.dtype.kind == "f" else "uchar"
|
||||
ply_file.write(f"property {data_type} {key}\n".encode())
|
||||
|
||||
ply_file.write(b"end_header\n")
|
||||
|
||||
# Write binary data
|
||||
# Note: If this is a perfromance bottleneck consider using numpy.hstack for efficiency improvement
|
||||
# Note: If this is a performance bottleneck consider using numpy.hstack for efficiency improvement
|
||||
for i in range(count):
|
||||
for tensor in map_to_tensors.values():
|
||||
value = tensor[i]
|
||||
ply_file.write(np.float32(value).tobytes())
|
||||
if tensor.dtype.kind == "f":
|
||||
ply_file.write(np.float32(value).tobytes())
|
||||
elif tensor.dtype == np.uint8:
|
||||
ply_file.write(value.tobytes())
|
||||
|
||||
def main(self) -> None:
|
||||
if not self.output_dir.exists():
|
||||
|
||||
Reference in new issue
Block a user