minecraft-segmentation / minecraft-segmentation.py
klima7
Replace stub datasets with real
d80055f
import os
import datasets
from datasets import DownloadManager, DatasetInfo
_DESCRIPTION = """\
A segmentation dataset for minecraft views
"""
_VERSION = '0.1.0'
_HOMEPAGE = "https://huggingface.co/datasets/klima7/minecraft-segmentation"
_URL_BASE = "https://huggingface.co/datasets/klima7/minecraft-segmentation/resolve/main/data/"
_EXTENSION = [".png"]
class MinecraftSegmentationConfig(datasets.BuilderConfig):
def __init__(self, features, data_files, **kwargs):
super(MinecraftSegmentationConfig, self).__init__(**kwargs)
self.features = features
self.data_files = data_files
class AnimeSegmentation(datasets.GeneratorBasedBuilder):
BUILDER_CONFIGS = [
MinecraftSegmentationConfig(
name="low-seg",
description="Minecraft images taken from low altitude with segmentations maps",
features=["image", "mask"],
data_files=["low_seg.zip"],
version=datasets.Version("1.0.0")
),
MinecraftSegmentationConfig(
name="high-seg",
description="Minecraft images taken from height altitude with segmentations maps",
features=["image", "mask"],
data_files=["high_seg.zip"],
version=datasets.Version("1.0.0")
),
MinecraftSegmentationConfig(
name="low-mc",
description="Only minecraft images taken from low altitude, different than in seg-low",
features=["image"],
data_files=["low_mc.zip"],
version=datasets.Version("1.0.0")
),
]
def _info(self) -> DatasetInfo:
features = {feature: datasets.Image() for feature in self.config.features}
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=datasets.Features(features),
supervised_keys=None,
homepage=_HOMEPAGE,
citation="",
)
def _split_generators(self, dl_manager: DownloadManager):
urls = [_URL_BASE + data_file for data_file in self.config.data_files]
dirs = dl_manager.download_and_extract(urls)
return [datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"dirs": dirs})]
def _generate_examples(self, dirs):
if self.config.name.split("-")[-1] == "seg":
path = dirs[0]
all_fnames = {os.path.relpath(os.path.join(root, fname), start=path)
for root, _dirs, files in os.walk(os.path.join(path, "imgs")) for fname in files}
image_fnames = sorted(fname for fname in all_fnames if os.path.splitext(fname)[1].lower() in _EXTENSION)
for image_fname in image_fnames:
yield image_fname, {"image": os.path.join(path, image_fname),
"mask": os.path.join(path, image_fname.replace("imgs", "masks"))}
else:
for path in dirs:
all_fnames = {os.path.relpath(os.path.join(root, fname), start=path)
for root, _dirs, files in os.walk(path) for fname in files}
image_fnames = sorted(fname for fname in all_fnames if os.path.splitext(fname)[1].lower() in _EXTENSION)
for image_fname in image_fnames:
yield image_fname, {"image": os.path.join(path, image_fname)}