File size: 3,096 Bytes
34a03de
 
f73854b
34a03de
 
 
 
 
 
f73854b
34a03de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f73854b
34a03de
 
 
8c90c8d
34a03de
8c90c8d
f73854b
34a03de
 
 
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
import os
import datasets
from datasets import DownloadManager, DatasetInfo

_DESCRIPTION = """\
A segmentation dataset for anime character
"""
_HOMEPAGE = "https://huggingface.co/datasets/skytnt/anime-segmentation"
_URL_BASE = "https://huggingface.co/datasets/skytnt/anime-segmentation/resolve/main/data/"
_EXTENSION = [".png", ".jpg"]


class AnimeSegmentationConfig(datasets.BuilderConfig):

    def __init__(self, features, data_files, **kwargs):
        super(AnimeSegmentationConfig, self).__init__(**kwargs)
        self.features = features
        self.data_files = data_files


class AnimeSegmentation(datasets.GeneratorBasedBuilder):
    BUILDER_CONFIGS = [
        AnimeSegmentationConfig(
            name="bg",
            description="background",
            features=["image"],
            data_files=["bg-00.zip", "bg-01.zip", "bg-02.zip", "bg-03.zip", "bg-04.zip"]
        ),
        AnimeSegmentationConfig(
            name="fg",
            description="foreground",
            features=["image"],
            data_files=["fg-00.zip", "fg-01.zip", "fg-02.zip", "fg-03.zip", "fg-04.zip", "fg-05.zip"]
        ),
        AnimeSegmentationConfig(
            name="imgs-masks",
            description="real images and masks",
            features=["image", "mask"],
            data_files=["imgs-masks.zip"]
        )
    ]

    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 != "imgs-masks":
            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)}
        else:
            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"))}