lklimkiewicz commited on
Commit
ff2e72d
·
1 Parent(s): 221f92f

Add script

Browse files
Files changed (2) hide show
  1. README.md +1 -1
  2. minecraft-segmentation.py +73 -0
README.md CHANGED
@@ -1,3 +1,3 @@
1
  ---
2
  license: mit
3
- ---
 
1
  ---
2
  license: mit
3
+ ---
minecraft-segmentation.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import datasets
3
+ from datasets import DownloadManager, DatasetInfo
4
+
5
+ _DESCRIPTION = """\
6
+ A segmentation dataset for minecraft views
7
+ """
8
+ _HOMEPAGE = "https://huggingface.co/datasets/klima7/minecraft-segmentation"
9
+ _URL_BASE = "https://huggingface.co/datasets/klima7/minecraft-segmentation/resolve/main/data/"
10
+ _EXTENSION = [".png"]
11
+
12
+
13
+ class MinecraftSegmentationConfig(datasets.BuilderConfig):
14
+
15
+ def __init__(self, features, data_files, **kwargs):
16
+ super(MinecraftSegmentationConfig, self).__init__(**kwargs)
17
+ self.features = features
18
+ self.data_files = data_files
19
+
20
+
21
+ class AnimeSegmentation(datasets.GeneratorBasedBuilder):
22
+ BUILDER_CONFIGS = [
23
+ MinecraftSegmentationConfig(
24
+ name="low-seg",
25
+ description="Minecraft images taken from low altitude with segmentations maps",
26
+ features=["image", "mask"],
27
+ data_files=["low_seg.zip"]
28
+ ),
29
+ MinecraftSegmentationConfig(
30
+ name="high-seg",
31
+ description="Minecraft images taken from height altitude with segmentations maps",
32
+ features=["image", "mask"],
33
+ data_files=["high_seg.zip"]
34
+ ),
35
+ MinecraftSegmentationConfig(
36
+ name="low-mc",
37
+ description="Only minecraft images taken from low altitude, different than in seg-low",
38
+ features=["image"],
39
+ data_files=["low_mc.zip"]
40
+ ),
41
+ ]
42
+
43
+ def _info(self) -> DatasetInfo:
44
+ features = {feature: datasets.Image() for feature in self.config.features}
45
+ return datasets.DatasetInfo(
46
+ description=_DESCRIPTION,
47
+ features=datasets.Features(features),
48
+ supervised_keys=None,
49
+ homepage=_HOMEPAGE,
50
+ citation="",
51
+ )
52
+
53
+ def _split_generators(self, dl_manager: DownloadManager):
54
+ urls = [_URL_BASE + data_file for data_file in self.config.data_files]
55
+ dirs = dl_manager.download_and_extract(urls)
56
+ return [datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"dirs": dirs})]
57
+
58
+ def _generate_examples(self, dirs):
59
+ if self.config.name.split("-")[-1] == "seg":
60
+ path = dirs[0]
61
+ all_fnames = {os.path.relpath(os.path.join(root, fname), start=path)
62
+ for root, _dirs, files in os.walk(os.path.join(path, "imgs")) for fname in files}
63
+ image_fnames = sorted(fname for fname in all_fnames if os.path.splitext(fname)[1].lower() in _EXTENSION)
64
+ for image_fname in image_fnames:
65
+ yield image_fname, {"image": os.path.join(path, image_fname),
66
+ "mask": os.path.join(path, image_fname.replace("imgs", "masks"))}
67
+ else:
68
+ for path in dirs:
69
+ all_fnames = {os.path.relpath(os.path.join(root, fname), start=path)
70
+ for root, _dirs, files in os.walk(path) for fname in files}
71
+ image_fnames = sorted(fname for fname in all_fnames if os.path.splitext(fname)[1].lower() in _EXTENSION)
72
+ for image_fname in image_fnames:
73
+ yield image_fname, {"image": os.path.join(path, image_fname)}