Upload anime_segmentation.py
Browse files- anime_segmentation.py +76 -0
anime_segmentation.py
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import datasets
|
3 |
+
import PIL
|
4 |
+
from datasets import DownloadManager, DatasetInfo, load_dataset
|
5 |
+
|
6 |
+
_DESCRIPTION = """\
|
7 |
+
A segmentation dataset for anime character
|
8 |
+
"""
|
9 |
+
_HOMEPAGE = "https://huggingface.co/datasets/skytnt/anime-segmentation"
|
10 |
+
_URL_BASE = "https://huggingface.co/datasets/skytnt/anime-segmentation/resolve/main/data/"
|
11 |
+
|
12 |
+
|
13 |
+
class AnimeSegmentationConfig(datasets.BuilderConfig):
|
14 |
+
|
15 |
+
def __init__(self, features, data_files, **kwargs):
|
16 |
+
super(AnimeSegmentationConfig, self).__init__(**kwargs)
|
17 |
+
self.features = features
|
18 |
+
self.data_files = data_files
|
19 |
+
|
20 |
+
|
21 |
+
class AnimeSegmentation(datasets.GeneratorBasedBuilder):
|
22 |
+
BUILDER_CONFIGS = [
|
23 |
+
AnimeSegmentationConfig(
|
24 |
+
name="bg",
|
25 |
+
description="background",
|
26 |
+
features=["image"],
|
27 |
+
data_files=["bg-00.zip", "bg-01.zip", "bg-02.zip", "bg-03.zip", "bg-04.zip"]
|
28 |
+
),
|
29 |
+
AnimeSegmentationConfig(
|
30 |
+
name="fg",
|
31 |
+
description="foreground",
|
32 |
+
features=["image"],
|
33 |
+
data_files=["fg-00.zip", "fg-01.zip", "fg-02.zip", "fg-03.zip", "fg-04.zip", "fg-05.zip"]
|
34 |
+
),
|
35 |
+
AnimeSegmentationConfig(
|
36 |
+
name="imgs-masks",
|
37 |
+
description="real images and masks",
|
38 |
+
features=["image", "mask"],
|
39 |
+
data_files=["imgs-masks.zip"]
|
40 |
+
)
|
41 |
+
]
|
42 |
+
|
43 |
+
def _info(self) -> DatasetInfo:
|
44 |
+
|
45 |
+
features = {feature: datasets.Image() for feature in self.config.features}
|
46 |
+
return datasets.DatasetInfo(
|
47 |
+
description=_DESCRIPTION,
|
48 |
+
features=datasets.Features(features),
|
49 |
+
supervised_keys=None,
|
50 |
+
homepage=_HOMEPAGE,
|
51 |
+
citation="",
|
52 |
+
)
|
53 |
+
|
54 |
+
def _split_generators(self, dl_manager: DownloadManager):
|
55 |
+
urls = [_URL_BASE + data_file for data_file in self.config.data_files]
|
56 |
+
dirs = dl_manager.download_and_extract(urls)
|
57 |
+
return [datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"dirs": dirs})]
|
58 |
+
|
59 |
+
def _generate_examples(self, dirs):
|
60 |
+
if self.config.name != "imgs-masks":
|
61 |
+
for path in dirs:
|
62 |
+
all_fnames = {os.path.relpath(os.path.join(root, fname), start=path)
|
63 |
+
for root, _dirs, files in os.walk(path) for fname in files}
|
64 |
+
image_fnames = sorted(fname for fname in all_fnames
|
65 |
+
if os.path.splitext(fname)[1].lower() in PIL.Image.EXTENSION)
|
66 |
+
for image_fname in image_fnames:
|
67 |
+
yield image_fname, {"image": os.path.join(path, image_fname)}
|
68 |
+
else:
|
69 |
+
path = dirs[0]
|
70 |
+
all_fnames = {os.path.relpath(os.path.join(root, fname), start=path)
|
71 |
+
for root, _dirs, files in os.walk(path) for fname in files}
|
72 |
+
image_fnames = sorted(fname for fname in all_fnames if fname.startswith(
|
73 |
+
"imgs") and os.path.splitext(fname)[1].lower() in PIL.Image.EXTENSION)
|
74 |
+
for image_fname in image_fnames:
|
75 |
+
yield image_fname, {"image": os.path.join(path, image_fname),
|
76 |
+
"mask": os.path.join(path, image_fname.replace("imgs", "masks"))}
|