Datasets:

artigo / artigo.py
stefanieschneider's picture
Add correct versioning
a15ad03
raw
history blame
6.92 kB
# 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"""
ZENODO_RECORD = requests.get(_URLS).json()
VERSION = datasets.Version(f"{ZENODO_RECORD['metadata']['version']}.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):
image_urls = [
file["links"]["self"]
for file in self.ZENODO_RECORD['files']
if file["type"] == "zip"
]
annotation_urls = [
file["links"]["self"]
for file in self.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(Path(image_file).name)
continue