File size: 3,345 Bytes
ff2e72d
 
 
 
 
 
 
2c663d2
ff2e72d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2c663d2
d80055f
ff2e72d
 
 
 
 
2c663d2
d80055f
ff2e72d
 
 
 
 
2c663d2
d80055f
ff2e72d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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)}