# Copyright 2022 The HuggingFace Datasets Authors and the current dataset script contributor. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """NYU-Depth V2.""" import os import datasets import h5py import numpy as np _CITATION = """\ @inproceedings{Silberman:ECCV12, author = {Nathan Silberman, Derek Hoiem, Pushmeet Kohli and Rob Fergus}, title = {Indoor Segmentation and Support Inference from RGBD Images}, booktitle = {ECCV}, year = {2012} } @inproceedings{icra_2019_fastdepth, author = {Wofk, Diana and Ma, Fangchang and Yang, Tien-Ju and Karaman, Sertac and Sze, Vivienne}, title = {FastDepth: Fast Monocular Depth Estimation on Embedded Systems}, booktitle = {IEEE International Conference on Robotics and Automation (ICRA)}, year = {2019} } """ _DESCRIPTION = """\ The NYU-Depth V2 data set is comprised of video sequences from a variety of indoor scenes as recorded by both the RGB and Depth cameras from the Microsoft Kinect. """ _HOMEPAGE = "https://cs.nyu.edu/~silberman/datasets/nyu_depth_v2.html" _LICENSE = "Apace 2.0 License" _URLS = { "depth_estimation": { "train/val": "http://datasets.lids.mit.edu/fastdepth/data/nyudepthv2.tar.gz", } } _IMG_EXTENSIONS = [".h5"] class NYUDepthV2(datasets.GeneratorBasedBuilder): """NYU-Depth V2 dataset.""" VERSION = datasets.Version("1.0.0") BUILDER_CONFIGS = [ datasets.BuilderConfig( name="depth_estimation", version=VERSION, description="The depth estimation variant.", ), ] DEFAULT_CONFIG_NAME = "depth_estimation" def _info(self): features = datasets.Features( {"image": datasets.Image(), "depth_map": datasets.Image()} ) return datasets.DatasetInfo( description=_DESCRIPTION, features=features, homepage=_HOMEPAGE, license=_LICENSE, citation=_CITATION, ) def _is_image_file(self, filename): # Reference: https://github.com/dwofk/fast-depth/blob/master/dataloaders/dataloader.py#L21-L23 return any(filename.endswith(extension) for extension in _IMG_EXTENSIONS) def _get_file_paths(self, dir): # Reference: https://github.com/dwofk/fast-depth/blob/master/dataloaders/dataloader.py#L31-L44 file_paths = [] dir = os.path.expanduser(dir) for target in sorted(os.listdir(dir)): d = os.path.join(dir, target) if not os.path.isdir(d): continue for root, _, fnames in sorted(os.walk(d)): for fname in sorted(fnames): if self._is_image_file(fname): path = os.path.join(root, fname) file_paths.append(path) return file_paths def _h5_loader(self, path): # Reference: https://github.com/dwofk/fast-depth/blob/master/dataloaders/dataloader.py#L8-L13 h5f = h5py.File(path, "r") rgb = np.array(h5f["rgb"]) rgb = np.transpose(rgb, (1, 2, 0)) depth = np.array(h5f["depth"]) return rgb, depth def _split_generators(self, dl_manager): urls = _URLS[self.config.name] base_path = dl_manager.download_and_extract(urls) train_data_files = self._get_file_paths( os.path.join(base_path, "nyudepthv2", "train") ) val_data_files = self._get_file_paths(os.path.join(base_path, "nyudepthv2" "val")) return [ datasets.SplitGenerator( name=datasets.Split.TRAIN, gen_kwargs={"data": train_data_files, "split": "training"}, ), datasets.SplitGenerator( name=datasets.Split.VALIDATION, gen_kwargs={"data": val_data_files, "split": "validation"}, ), ] def _generate_examples(self, filepaths): for idx, filepath in enumerate(filepaths): image, depth = self._h5_loader(filepath) yield idx, {"image": image, "depth_map": depth}