mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-12-02 05:48:57 +08:00
cache in cache dir, no longer edit json file
This commit is contained in:
1 parent
5df7c95542
commit
9c3dcbbf5a
3 files changed
+40
-29
No files matched your search
@@ -16,7 +16,6 @@
|
|||||||
Depth dataset.
|
Depth dataset.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import os
|
|
||||||
from typing import Dict
|
from typing import Dict
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
@@ -48,46 +47,58 @@ class DepthDataset(InputDataset):
|
|||||||
"depth_filenames" not in dataparser_outputs.metadata.keys()
|
"depth_filenames" not in dataparser_outputs.metadata.keys()
|
||||||
or dataparser_outputs.metadata["depth_filenames"] is None
|
or dataparser_outputs.metadata["depth_filenames"] is None
|
||||||
):
|
):
|
||||||
depth_paths = []
|
cache = dataparser_outputs.image_filenames[0].parent / "depths.npy"
|
||||||
tranforms = self._find_transform(dataparser_outputs.image_filenames[0])
|
if cache.exists():
|
||||||
assert tranforms is not None, "Could not find transforms.json"
|
# load all the depths
|
||||||
data = dataparser_outputs.image_filenames[0].parent
|
self.depths = np.load(cache)
|
||||||
meta = json.load(open(tranforms, "r"))
|
self.depths = torch.from_numpy(self.depths).to(device)
|
||||||
frames = meta["frames"]
|
else:
|
||||||
filenames = [data / frames[j]["file_path"].split("/")[-1] for j in range(len(frames))]
|
depth_tensors = []
|
||||||
os.makedirs(dataparser_outputs.image_filenames[0].parent / "depth", exist_ok=True)
|
tranforms = self._find_transform(dataparser_outputs.image_filenames[0])
|
||||||
repo = "isl-org/ZoeDepth"
|
data = dataparser_outputs.image_filenames[0].parent
|
||||||
self.zoe = torch.compile(torch.hub.load(repo, "ZoeD_NK", pretrained=True).to(device))
|
if tranforms is not None:
|
||||||
|
meta = json.load(open(tranforms, "r"))
|
||||||
|
frames = meta["frames"]
|
||||||
|
filenames = [data / frames[j]["file_path"].split("/")[-1] for j in range(len(frames))]
|
||||||
|
else:
|
||||||
|
meta = None
|
||||||
|
frames = None
|
||||||
|
filenames = dataparser_outputs.image_filenames
|
||||||
|
|
||||||
for i in track(range(len(filenames)), description="Generating depth images"):
|
repo = "isl-org/ZoeDepth"
|
||||||
image_filename = filenames[i]
|
self.zoe = torch.compile(torch.hub.load(repo, "ZoeD_NK", pretrained=True).to(device))
|
||||||
pil_image = Image.open(image_filename)
|
|
||||||
image = np.array(pil_image, dtype="uint8") # shape is (h, w) or (h, w, 3 or 4)
|
|
||||||
if len(image.shape) == 2:
|
|
||||||
image = image[:, :, None].repeat(3, axis=2)
|
|
||||||
image = torch.from_numpy(image.astype("float32") / 255.0)
|
|
||||||
|
|
||||||
with torch.no_grad():
|
for i in track(range(len(filenames)), description="Generating depth images"):
|
||||||
image = torch.permute(image, (2, 0, 1)).unsqueeze(0).to(device)
|
image_filename = filenames[i]
|
||||||
depth_numpy = self.zoe.infer(image).squeeze().unsqueeze(-1).cpu().numpy()
|
pil_image = Image.open(image_filename)
|
||||||
|
image = np.array(pil_image, dtype="uint8") # shape is (h, w) or (h, w, 3 or 4)
|
||||||
|
if len(image.shape) == 2:
|
||||||
|
image = image[:, :, None].repeat(3, axis=2)
|
||||||
|
image = torch.from_numpy(image.astype("float32") / 255.0)
|
||||||
|
|
||||||
depth_paths.append(image_filename.parent / "depth" / f"depth{i}.npy")
|
with torch.no_grad():
|
||||||
meta["frames"][i]["depth_file_path"] = str(
|
image = torch.permute(image, (2, 0, 1)).unsqueeze(0).to(device)
|
||||||
Path(meta["frames"][i]["file_path"]).parent / "depth" / f"depth{i}.npy"
|
if image.shape[1] == 4:
|
||||||
)
|
image = image[:, :3, :, :]
|
||||||
np.save(depth_paths[-1], depth_numpy)
|
depth_tensor = self.zoe.infer(image).squeeze().unsqueeze(-1)
|
||||||
|
|
||||||
json.dump(meta, open(tranforms, "w"))
|
depth_tensors.append(depth_tensor)
|
||||||
|
|
||||||
dataparser_outputs.metadata["depth_filenames"] = depth_paths
|
self.depths = torch.stack(depth_tensors)
|
||||||
self.metadata["depth_filenames"] = depth_paths
|
np.save(cache, self.depths.cpu().numpy())
|
||||||
|
|
||||||
|
dataparser_outputs.metadata["depth_filenames"] = None
|
||||||
dataparser_outputs.metadata["depth_unit_scale_factor"] = 1.0
|
dataparser_outputs.metadata["depth_unit_scale_factor"] = 1.0
|
||||||
|
self.metadata["depth_filenames"] = None
|
||||||
self.metadata["depth_unit_scale_factor"] = 1.0
|
self.metadata["depth_unit_scale_factor"] = 1.0
|
||||||
|
|
||||||
self.depth_filenames = self.metadata["depth_filenames"]
|
self.depth_filenames = self.metadata["depth_filenames"]
|
||||||
self.depth_unit_scale_factor = self.metadata["depth_unit_scale_factor"]
|
self.depth_unit_scale_factor = self.metadata["depth_unit_scale_factor"]
|
||||||
|
|
||||||
def get_metadata(self, data: Dict) -> Dict:
|
def get_metadata(self, data: Dict) -> Dict:
|
||||||
|
if self.depth_filenames is None:
|
||||||
|
return {"depth_image": self.depths[data["image_idx"]]}
|
||||||
|
|
||||||
filepath = self.depth_filenames[data["image_idx"]]
|
filepath = self.depth_filenames[data["image_idx"]]
|
||||||
height = int(self._dataparser_outputs.cameras.height[data["image_idx"]])
|
height = int(self._dataparser_outputs.cameras.height[data["image_idx"]])
|
||||||
width = int(self._dataparser_outputs.cameras.width[data["image_idx"]])
|
width = int(self._dataparser_outputs.cameras.width[data["image_idx"]])
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Reference in new issue
Block a user