alexjc commited on
Commit
59762fc
1 Parent(s): b414e68

Loader script based on td01.

Browse files
Files changed (1) hide show
  1. td02_urban-surface-textures.py +104 -0
td02_urban-surface-textures.py ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright (c) 2022, texture.design.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ import json
16
+
17
+ import datasets
18
+
19
+
20
+ DESCRIPTION = """\
21
+ Multi-photo texture captures in outdoor nature scenes focusing on the ground. Each set contains variations of the same texture theme.
22
+ """
23
+
24
+ REPO_PREFIX = "https://huggingface.co/datasets/texturedesign/td02_natural-ground-textures"
25
+ DOWNLOAD_PREFIX = REPO_PREFIX + "/resolve/main"
26
+
27
+ INDEX_URLS = {
28
+ "train": REPO_PREFIX + "/raw/main/train/metadata.jsonl",
29
+ "test": REPO_PREFIX + "/raw/main/test/metadata.jsonl",
30
+ }
31
+
32
+
33
+ class NaturalGroundTextures(datasets.GeneratorBasedBuilder):
34
+
35
+ VERSION = datasets.Version("0.1.0")
36
+
37
+ BUILDER_CONFIGS = [
38
+ datasets.BuilderConfig(name="JXL@4K", version=VERSION, description="The original resolution dataset."),
39
+ datasets.BuilderConfig(name="JXL@2K", version=VERSION, description="Half-resolution dataset."),
40
+ datasets.BuilderConfig(name="JXL@1K", version=VERSION, description="Quarter-resolution dataset."),
41
+ datasets.BuilderConfig(name="PNG@1K", version=VERSION, description="Fallback version of the dataset."),
42
+ ]
43
+
44
+ DEFAULT_CONFIG_NAME = "JXL@2K"
45
+
46
+ def _info(self):
47
+ return datasets.DatasetInfo(
48
+ description=DESCRIPTION,
49
+ citation="",
50
+ homepage="https://huggingface.co/texturedesign",
51
+ license="cc-by-nc-4.0",
52
+ features=datasets.Features(
53
+ {
54
+ "image": datasets.Image(),
55
+ "set": datasets.Value("uint8"),
56
+ }
57
+ )
58
+ )
59
+
60
+ def _split_generators(self, dl_manager):
61
+ data_dir = dl_manager.download(INDEX_URLS)
62
+ train_lines = [json.loads(l) for l in open(data_dir["train"], "r", encoding="utf-8").readlines()]
63
+ test_lines = [json.loads(l) for l in open(data_dir["test"], "r", encoding="utf-8").readlines()]
64
+
65
+ def _get_file_url(row, split):
66
+ file_name = row['file_name']
67
+ if self.config.name[:3] == "PNG":
68
+ file_name = file_name.replace('.jxl', '.png')
69
+ return (DOWNLOAD_PREFIX + '/' + split + '/' + file_name)
70
+
71
+ train_files = dl_manager.download([_get_file_url(row, split='train') for row in train_lines])
72
+ test_files = dl_manager.download([_get_file_url(row, split='test') for row in test_lines])
73
+
74
+ return [
75
+ datasets.SplitGenerator(datasets.Split.TRAIN, dict(lines=train_lines, filenames=train_files)),
76
+ datasets.SplitGenerator(datasets.Split.TEST, dict(lines=test_lines, filenames=test_files)),
77
+ ]
78
+
79
+ def _generate_examples(self, lines, filenames):
80
+ try:
81
+ if self.config.name[:3] == "JXL":
82
+ from jxlpy import JXLImagePlugin
83
+ import PIL.Image
84
+ except (ImportError, ModuleNotFoundError):
85
+ logger = datasets.logging.get_logger()
86
+ logger.critical('\n\n\nERROR: Please install `jxlpy` from PyPI to use JPEG-XL images.\n')
87
+ raise SystemExit
88
+
89
+ for key, (data, filename) in enumerate(zip(lines, filenames)):
90
+ # Load the images from disk with the correct format.
91
+ image = PIL.Image.open(filename, formats=[self.config.name[:3].lower()])
92
+ sz = image.size
93
+
94
+ # PNG files only available at quarter resolution.
95
+ if self.config.name[:3] == "PNG":
96
+ sz = (sz*4, sz*4)
97
+
98
+ # Resize the images as specified by the user.
99
+ if self.config.name[-2:] == "1K":
100
+ image = image.resize(size=(sz[0]//4, sz[1]//4), resample=PIL.Image.Resampling.LANCZOS)
101
+ elif self.config.name[-2:] == "2K":
102
+ image = image.resize(size=(sz[0]//2, sz[1]//2), resample=PIL.Image.Resampling.LANCZOS)
103
+
104
+ yield key, {"image": image, "set": data["set"]}