import io from PIL import Image from datasets import GeneratorBasedBuilder, DatasetInfo, Features, SplitGenerator, Value, Array2D, Split import datasets import OpenEXR import Imath import numpy as np import h5py class RGBSemanticDepthDataset(GeneratorBasedBuilder): def _info(self): return DatasetInfo( features=Features({ "left_rgb": datasets.Image(), "right_rgb": datasets.Image(), "left_seg": datasets.Image(), "left_depth": datasets.Image(), "right_depth": datasets.Image(), }) ) def _h5_loader(self, bytes_stream): # Reference: https://github.com/dwofk/fast-depth/blob/master/dataloaders/dataloader.py#L8-L13 f = io.BytesIO(bytes_stream) h5f = h5py.File(f, "r") left_rgb = self._read_jpg(h5f['rgb_left']) right_rgb = self._read_jpg(h5f['rgb_right']) left_seg = h5f['seg_left'] left_depth = h5f['depth_left'].astype(np.float32) right_depth = h5f['depth_right'].astype(np.float32) print(left_rgb.shape, left_depth.shape, left_seg.shape) return left_rgb, right_rgb, left_seg, left_depth, right_depth def _read_jpg(self, bytes_stream): return Image.open(io.BytesIO(bytes_stream)) def _split_generators(self, dl_manager): archives = dl_manager.download({"train":["data/images_1729368304.063240.tar"]}) return [ SplitGenerator( name=Split.TRAIN, gen_kwargs={ "archives": [dl_manager.iter_archive(archive) for archive in archives["train"]], }, ), ] def _generate_examples(self, archives): for archive in archives: for path, file in archive: print(path) left_rgb, right_rgb, left_seg, left_depth, right_depth = self._h5_loader(file.read()) yield path, { "left_rgb": left_rgb, "right_rgb": right_rgb, "left_seg": left_seg, "left_depth": left_depth, "right_depth": right_depth, }