early_printed_books_font_detection / early_printed_books_font_detection.py
davanstrien's picture
davanstrien HF staff
draft loading script
c39e419
raw
history blame
4.8 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.
"""Dataset of illustrated and non illustrated 19th Century newspaper ads."""
import os
from pathlib import Path
import datasets
import requests
from PIL import Image
_CITATION = """\
@dataset{seuret_mathias_2019_3366686,
author = {Seuret, Mathias and
Limbach, Saskia and
Weichselbaumer, Nikolaus and
Maier, Andreas and
Christlein, Vincent},
title = {{Dataset of Pages from Early Printed Books with
Multiple Font Groups}},
month = aug,
year = 2019,
publisher = {Zenodo},
version = {1.0.0},
doi = {10.5281/zenodo.3366686},
url = {https://doi.org/10.5281/zenodo.3366686}
}
"""
_DESCRIPTION = """\
This dataset is composed of photos of various resolution of 35'623 pages of printed books dating from the 15th to the 18th century. Each page has been attributed by experts from one to five labels corresponding to the font groups used in the text, with two extra-classes for non-textual content and fonts not present in the following list: Antiqua, Bastarda, Fraktur, Gotico Antiqua, Greek, Hebrew, Italic, Rotunda, Schwabacher, and Textura.
"""
_HOMEPAGE = "https://doi.org/10.5281/zenodo.3366686"
_LICENSE = "Creative Commons Attribution Non Commercial Share Alike 4.0 International"
ZENDO_REPO_ID = 3366686
ZENODO_API_URL = f"https://zenodo.org/api/records/{ZENDO_REPO_ID}"
class EarlyBookFonts(datasets.GeneratorBasedBuilder):
"""Early printed book fonts detection dataset"""
VERSION = datasets.Version("1.1.0")
def _info(self):
features = datasets.Features(
{
"image": datasets.Image(),
"labels": datasets.Sequence(
datasets.ClassLabel(
names=[
"greek",
"antiqua",
"other_font",
"not_a_font",
"italic",
"rotunda",
"textura",
"fraktur",
"schwabacher",
"hebrew",
"bastarda",
"gotico_antiqua",
]
)
),
}
)
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=features,
homepage=_HOMEPAGE,
license=_LICENSE,
citation=_CITATION,
)
def _split_generators(self, dl_manager):
zenodo_record = requests.get(ZENODO_API_URL).json()
urls = sorted(
file["links"]["self"]
for file in zenodo_record["files"]
if file["type"] == "zip"
)
*image_urls, label_url = urls
config = datasets.DownloadConfig()
labels = dl_manager.download_and_extract(label_url)
images = dl_manager.download_and_extract(image_urls)
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={"images": images, "labels": os.path.join(labels), "split": "training"},
),
datasets.SplitGenerator(
name=datasets.Split.TEST,
gen_kwargs={"images": images, "labels": os.path.join(labels), "split": "test"},
),
]
def _generate_examples(self, images, labels, split):
mapping = {}
for directory in images:
for file in Path(directory).rglob("*"):
mapping["/".join(file.parts[-2:])] = file
with open(f"labels-{split}.csv", 'r') as label_csv:
for id_, row in enumerate(label_csv.readlines()):
filename, *labels = row.split(",")
labels = [label.strip("\n") for label in labels]
labels = [label for label in labels if label != '-']
filename = mapping[filename]
image = Image.open(filename)
yield id_, {"image": image, "labels": labels}