File size: 4,724 Bytes
ffd62d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
07a01fd
ffd62d6
 
 
 
 
 
 
 
 
 
 
 
 
 
07a01fd
ffd62d6
 
07a01fd
ffd62d6
 
 
 
 
 
 
 
07a01fd
ffd62d6
 
 
 
07a01fd
ffd62d6
 
 
 
 
 
07a01fd
ffd62d6
 
07a01fd
 
 
 
 
ffd62d6
956b3b5
 
 
6893772
956b3b5
6893772
 
 
 
ffd62d6
07a01fd
956b3b5
ffd62d6
 
956b3b5
ffd62d6
 
07a01fd
 
ffd62d6
 
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import json

import datasets


_CITATION = """\
@InProceedings{huggingface:dataset,
title = {Graffiti},
author={UR
},
year={2023}
}
"""

_DESCRIPTION = """\
Graffiti dataset taken from https://www.graffiti.org/ and https://www.graffiti-database.com/.
"""

_HOMEPAGE = "https://huggingface.co/datasets/artificialhoney/graffiti"

_LICENSE = "Apache License 2.0"

_VERSION = "0.1.0"

_SOURCES = [
    "graffiti.org",
    "graffiti-database.com"
]


class GraffitiConfig(datasets.BuilderConfig):
    """BuilderConfig for Graffiti."""

    def __init__(self, **kwargs):
        """BuilderConfig for Graffiti.
        Args:
          **kwargs: keyword arguments forwarded to super.
        """
        super(GraffitiConfig, self).__init__(**kwargs)


class Graffiti(datasets.GeneratorBasedBuilder):
    """Graffiti dataset taken from https://www.graffiti.org/ and https://www.graffiti-database.com/."""

    BUILDER_CONFIG_CLASS = GraffitiConfig

    BUILDER_CONFIGS = [
        GraffitiConfig(
            name="default",
        ),
    ]

    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features(
                {
                    "image": datasets.Image(),
                    "conditioning_image": datasets.Image(),
                    "text": datasets.Value("string")
                }
            ),
            homepage=_HOMEPAGE,
            license=_LICENSE,
            citation=_CITATION,
            version=_VERSION,
            task_templates=[],
        )

    def _split_generators(self, dl_manager):
        """Returns SplitGenerators."""
        images = []
        metadata = []
        conditioning = []
        for source in _SOURCES:
            images.append(dl_manager.iter_archive(dl_manager.download("./data/{0}/images.tar.gz".format(source))))
            conditioning.append(dl_manager.iter_archive(dl_manager.download("./data/{0}/conditioning.tar.gz".format(source))))
            metadata.append(dl_manager.download("./data/{0}/metadata.jsonl".format(source)))
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                # These kwargs will be passed to _generate_examples
                gen_kwargs={
                    "images": images,
                    "metadata": metadata,
                    "conditioning": conditioning
                },
            )
        ]

    def _generate_examples(self, metadata, images, conditioning):
        idx = 0
        for index, meta in enumerate(metadata):
            m = []
            with open(meta, encoding="utf-8") as f:
                for row in f:
                    m.append(json.loads(row))
            c = iter(conditioning[index])
            for file_path, file_obj in images[index]:
                data = [x for x in m if file_path.endswith(x["file"])][0]

                conditioning_file = next(c)
                conditioning_file_path = conditioning_file[0]
                conditioning_file_obj = conditioning_file[1]

                text = data["caption"]
                if data["palette"] != None:
                    colors = []
                    for color in data["palette"]:
                        if color[2] in colors or "grey" in color[2]:
                            continue
                        colors.append(color[2])
                    if len(colors) > 0:
                        text += ", in the colors "
                        text += " and ".join(colors)
                if data["artist"] != None:
                    # text += ", with text " + data["artist"]
                    text += ", by " + data["artist"]
                if data["city"] != None:
                    text += ", located in " + data["city"]

                yield idx, {
                        "image": {"path": file_path, "bytes": file_obj.read()},
                        "conditioning_image": {"path": conditioning_file_path, "bytes": conditioning_file_obj.read()},
                        "text": text,
                    }
                idx+=1