|
""" |
|
Inspired from |
|
https://huggingface.co/datasets/ydshieh/coco_dataset_script/blob/main/coco_dataset_script.py |
|
""" |
|
|
|
import json |
|
import os |
|
import datasets |
|
import collections |
|
|
|
|
|
class COCOBuilderConfig(datasets.BuilderConfig): |
|
def __init__(self, name, splits, **kwargs): |
|
super().__init__(name, **kwargs) |
|
self.splits = splits |
|
|
|
|
|
|
|
|
|
_CITATION = """\ |
|
@article{doclaynet2022, |
|
title = {DocLayNet: A Large Human-Annotated Dataset for Document-Layout Analysis}, |
|
doi = {10.1145/3534678.353904}, |
|
url = {https://arxiv.org/abs/2206.01062}, |
|
author = {Pfitzmann, Birgit and Auer, Christoph and Dolfi, Michele and Nassar, Ahmed S and Staar, Peter W J}, |
|
year = {2022} |
|
} |
|
""" |
|
|
|
|
|
|
|
_DESCRIPTION = """\ |
|
Dataset for the ICDAR 2023 Competition on Robust Layout Segmentation in Corporate Documents. |
|
""" |
|
|
|
|
|
_HOMEPAGE = "https://ds4sd.github.io/icdar23-doclaynet/" |
|
|
|
|
|
_LICENSE = "apache-2.0" |
|
|
|
|
|
|
|
|
|
|
|
_URLs = { |
|
"dev": "https://ds4sd-icdar23-doclaynet-competition.s3.eu-de.cloud-object-storage.appdomain.cloud/dev-dataset-public.zip", |
|
"test": "https://ds4sd-icdar23-doclaynet-competition.s3.eu-de.cloud-object-storage.appdomain.cloud/competition-dataset-public.zip" |
|
} |
|
|
|
|
|
class COCODataset(datasets.GeneratorBasedBuilder): |
|
"""An example dataset script to work with the local (downloaded) COCO dataset""" |
|
|
|
VERSION = datasets.Version("1.0.0") |
|
|
|
BUILDER_CONFIG_CLASS = COCOBuilderConfig |
|
BUILDER_CONFIGS = [ |
|
COCOBuilderConfig(name="2023.01", splits=["dev", "test"]), |
|
] |
|
DEFAULT_CONFIG_NAME = "2023.01" |
|
|
|
def _info(self): |
|
features = datasets.Features( |
|
{ |
|
"image_id": datasets.Value("int64"), |
|
"image": datasets.Image(), |
|
"width": datasets.Value("int32"), |
|
"height": datasets.Value("int32"), |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
) |
|
object_dict = { |
|
"category_id": datasets.ClassLabel( |
|
names=[ |
|
"Caption", |
|
"Footnote", |
|
"Formula", |
|
"List-item", |
|
"Page-footer", |
|
"Page-header", |
|
"Picture", |
|
"Section-header", |
|
"Table", |
|
"Text", |
|
"Title", |
|
] |
|
), |
|
"image_id": datasets.Value("string"), |
|
"id": datasets.Value("int64"), |
|
"area": datasets.Value("int64"), |
|
"bbox": datasets.Sequence(datasets.Value("float32"), length=4), |
|
"segmentation": [[datasets.Value("float32")]], |
|
"iscrowd": datasets.Value("bool"), |
|
"precedence": datasets.Value("int32"), |
|
} |
|
|
|
|
|
return datasets.DatasetInfo( |
|
|
|
description=_DESCRIPTION, |
|
|
|
features=features, |
|
|
|
|
|
|
|
supervised_keys=None, |
|
|
|
homepage=_HOMEPAGE, |
|
|
|
license=_LICENSE, |
|
|
|
citation=_CITATION, |
|
) |
|
|
|
def _split_generators(self, dl_manager): |
|
"""Returns SplitGenerators.""" |
|
archive_path = dl_manager.download_and_extract(_URLs) |
|
splits = [] |
|
for split in self.config.splits: |
|
if split in ["val", "valid", "validation", "dev"]: |
|
dataset = datasets.SplitGenerator( |
|
name=datasets.Split.VALIDATION, |
|
|
|
gen_kwargs={ |
|
"json_path": os.path.join( |
|
archive_path["dev"], "coco.json" |
|
), |
|
"image_dir": os.path.join(archive_path["dev"], "PNG"), |
|
"split": "dev", |
|
}, |
|
) |
|
elif split == "test": |
|
dataset = datasets.SplitGenerator( |
|
name=datasets.Split.TEST, |
|
|
|
gen_kwargs={ |
|
"json_path": os.path.join( |
|
archive_path["test"], "coco.json" |
|
), |
|
"image_dir": os.path.join(archive_path["test"], "PNG"), |
|
"split": "test", |
|
}, |
|
) |
|
else: |
|
continue |
|
|
|
splits.append(dataset) |
|
return splits |
|
|
|
def _generate_examples( |
|
|
|
self, |
|
json_path, |
|
image_dir, |
|
split, |
|
): |
|
"""Yields examples as (key, example) tuples.""" |
|
|
|
|
|
def _image_info_to_example(image_info, image_dir): |
|
image = image_info["file_name"] |
|
return { |
|
"image_id": image_info["id"], |
|
"image": os.path.join(image_dir, image), |
|
"width": image_info["width"], |
|
"height": image_info["height"], |
|
|
|
|
|
|
|
|
|
} |
|
|
|
with open(json_path, encoding="utf8") as f: |
|
annotation_data = json.load(f) |
|
images = annotation_data["images"] |
|
|
|
|
|
|
|
|
|
|
|
for idx, image_info in enumerate(images): |
|
example = _image_info_to_example(image_info, image_dir) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
yield idx, example |
|
|