Datasets:

File size: 6,873 Bytes
4e0faec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# 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.

"""Script for reading the dataset of the 'ARTigo: Social Image Tagging' project."""

import glob
import json
import datasets
import requests

from PIL import Image
from pathlib import Path

_CITATION = """\
@dataset{bry_et_al_artigo,
    author       = {Bry, François and
                    Kohle, Hubertus and
                    Krefeld, Thomas and 
                    Riepl, Christian and 
                    Schneider, Stefanie and 
                    Schön, Gerhard and 
                    Schulz, Klaus},
    title        = {{ARTigo}: Social Image Tagging (Aggregated Data)},
    publisher    = {Zenodo},
    doi          = {10.5281/zenodo.8202331},
    url          = {https://doi.org/10.5281/zenodo.8202331}}
"""

_DESCRIPTION = """\
ARTigo (https://www.artigo.org/) is a Citizen Science project that has been jointly developed at the Institute for Art History and the Institute for Informatics at Ludwig Maximilian University of Munich since 2010. It enables participants to engage in the tagging of artworks, thus fostering knowledge accumulation and democratizing access to a traditionally elitist field. ARTigo is built as an interactive web application that offers Games With a Purpose: in them, players are presented with an image – and then challenged to communicate with one another using visual or textual annotations within a given time. Through this playful approach, the project aims to inspire greater appreciation for art and draw new audiences to museums and archives. It streamlines the discoverability of art-historical images, while promoting inclusivity, effective communication, and collaborative research practices. The project’s data are freely available to the wider research community for novel scientific investigations.
"""

_HOMEPAGE = "https://doi.org/10.5281/zenodo.8202331"

_LICENSE = "Creative Commons Attribution Share Alike 4.0 International (CC BY-SA 4.0)"

_URLS = "https://zenodo.org/api/records/8202331"


logger = datasets.utils.logging.get_logger(__name__)


def create_annotations_dict(annotations_file):
    annotations = {}

    with open(annotations_file, encoding="utf-8") as annotations_obj:
        for annotation_row in annotations_obj:
            annotation_data = json.loads(annotation_row, strict=False)
            for tag in annotation_data["tags"]:
                if not tag.get("regions"):
                    tag["regions"] = None
            annotations[annotation_data["hash_id"]] = annotation_data

    return annotations


class ARTigo(datasets.GeneratorBasedBuilder):
    """Dataset of the 'ARTigo: Social Image Tagging' project"""

    VERSION = datasets.Version("1.0.0")

    def _info(self):
        features = datasets.Features(
            {
                "id": datasets.Value("int64"),
                "hash_id": datasets.Value("string"),
                "titles": datasets.Sequence(
                    {
                        "id": datasets.Value("int64"),
                        "name": datasets.Value("string"),
                    }
                ),
                "creators": datasets.Sequence(
                    {
                        "id": datasets.Value("int64"),
                        "name": datasets.Value("string"),
                    }
                ),
                "location": datasets.Value("string"),
                "institution": datasets.Value("string"),
                "source": {
                    "id": datasets.Value("int64"),
                    "name": datasets.Value("string"),
                    "url": datasets.Value("string"),
                },
                "path": datasets.Value("string"),
                "tags": datasets.Sequence(
                    {
                        "id": datasets.Value("int64"),
                        "name": datasets.Value("string"),
                        "language": datasets.Value("string"),
                        "count": datasets.Value("int64"),
                        "regions": datasets.Sequence(
                            {
                                "x": datasets.Value("float64"),
                                "y": datasets.Value("float64"),
                                "width": datasets.Value("float64"),
                                "height": datasets.Value("float64"),
                            }
                        ),
                    }
                ),
                "image": datasets.Image(),
            }
        )

        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,
            homepage=_HOMEPAGE,
            license=_LICENSE,
            citation=_CITATION,
        )
    
    def _split_generators(self, dl_manager):
        zenodo_record = requests.get(_URLS).json()

        image_urls = [
            file["links"]["self"]
            for file in zenodo_record['files']
            if file["type"] == "zip"
        ]
        annotation_urls = [
            file["links"]["self"]
            for file in zenodo_record['files']
            if file["type"] == "jsonl"
        ]

        image_directories = dl_manager.download_and_extract(image_urls)
        annotations_file = dl_manager.download(annotation_urls)[0]
        
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "image_directories": image_directories,
                    "annotations_file": annotations_file,
                },
            ),
        ]

    def _generate_examples(self, image_directories, annotations_file):
        annotations = create_annotations_dict(annotations_file)

        for image_directory in image_directories:
            for image_file in glob.glob(f"{image_directory}/**/*.jpg", recursive=True):
                with Image.open(image_file) as image:
                    try:
                        hash_id, _ = Path(image_file).name.split('.', 1)
                        image_data = annotations[hash_id]
                        image_data["image"] = image

                        yield image_data["id"], image_data
                    except Exception:
                        logger.warn(image_file.name)

                        continue