Datasets:

Modalities:
Image
Text
Formats:
parquet
Libraries:
Datasets
Dask
License:
File size: 5,559 Bytes
6a59d9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import glob
import os
import datasets
from PIL import Image
import csv


_VERSION = "2024-07-17"
_URL = f"https://github.com/DEFI-COLaF/LADaS/archive/refs/tags/{_VERSION}.tar.gz"
_HOMEPAGE = "https://github.com/DEFI-COLaF/LADaS"
_LICENSE = "CC BY 4.0"
_CITATION = """\
@misc{Clerice_Layout_Analysis_Dataset,
    author = {Clérice, Thibault and Janès, Juliette and Scheithauer, Hugo and Bénière, Sarah and Romary, Laurent and Sagot, Benoit and Bougrelle, Roxane},
    title = {{Layout Analysis Dataset with SegmOnto (LADaS)}},
    url = {https://github.com/DEFI-COLaF/LADaS}
}
"""

_CATEGORIES: list[str] = ["AdvertisementZone", "DigitizationArtefactZone", "DropCapitalZone", "FigureZone",
                          "FigureZone-FigDesc", "FigureZone-Head", "GraphicZone", "GraphicZone-Decoration",
                          "GraphicZone-FigDesc", "GraphicZone-Head", "GraphicZone-Maths", "GraphicZone-Part",
                          "GraphicZone-TextualContent", "MainZone-Date", "MainZone-Entry", "MainZone-Entry-Continued",
                          "MainZone-Form", "MainZone-Head", "MainZone-Lg", "MainZone-Lg-Continued", "MainZone-List",
                          "MainZone-List-Continued", "MainZone-Other", "MainZone-P", "MainZone-P-Continued",
                          "MainZone-Signature", "MainZone-Sp", "MainZone-Sp-Continued",
                          "MarginTextZone-ManuscriptAddendum", "MarginTextZone-Notes", "MarginTextZone-Notes-Continued",
                          "NumberingZone", "TitlePageZone", "TitlePageZone-Index", "QuireMarksZone", "RunningTitleZone",
                          "StampZone", "StampZone-Sticker", "TableZone", "TableZone-Continued", "TableZone-Head"]


class LadasConfig(datasets.BuilderConfig):
    """Builder Config for LADaS"""
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)


class LadasDataset(datasets.GeneratorBasedBuilder):
    VERSION = datasets.Version(_VERSION.replace("-", "."))
    BUILDER_CONFIGS = [
        LadasConfig(
            name="full",
            description="Full version of the dataset"
        )
    ]

    def _info(self) -> datasets.DatasetInfo:
        features = datasets.Features({
            "image_path": datasets.Value("string"),
            "year": datasets.Value("int32"),
            "dating-certainty": datasets.Value("bool"),
            "set": datasets.Value("string"),
            "image": datasets.Image(),
            "width": datasets.Value("int32"),
            "height": datasets.Value("int32"),
            "objects": datasets.Sequence(
                {
                    "bbox": datasets.Sequence(datasets.Value("float32"), length=4),
                    "category": datasets.Value("string"),
                }
            )
        })
        return datasets.DatasetInfo(
            features=features,
            homepage=_HOMEPAGE,
            citation=_CITATION,
            license=_LICENSE
        )

    def _split_generators(self, dl_manager):
        urls_to_download = _URL
        downloaded_files = dl_manager.download_and_extract(urls_to_download)
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "local_dir": downloaded_files,
                    "split": "train"
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                gen_kwargs={
                    "local_dir": downloaded_files,
                    "split": "valid"
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs={
                    "local_dir": downloaded_files,
                    "split": "test"
                },
            ),
        ]

    def _generate_examples(self, local_dir: str, split: str):
        idx = 0

        df = {}
        for file in glob.glob(os.path.join(local_dir, "*", "metadata.csv")):
            with open(file) as f:
                reader = csv.DictReader(f)
                for line in reader:
                   df[line["file"]] = line

        for file in glob.glob(os.path.join(local_dir, "*", "data", "*", split, "labels", "*.txt")):
            objects = []
            with open(file) as f:
                for line in f:
                    cls, *bbox = line.strip().split()
                    objects.append({"category": _CATEGORIES[int(cls)], "bbox": list(map(float, bbox))})

            image_path = os.path.normpath(file).split(os.sep)
            image_path = os.path.join(*image_path[:-2], "images", image_path[-1].replace(".txt", ".jpg"))
            if file.startswith("/") and not image_path.startswith("/"):
                image_path = "/" + image_path

            with open(image_path, "rb") as f:
                image_bytes = f.read()

            with Image.open(image_path) as im:
                width, height = im.size

            filename = os.path.basename(image_path)
            line = df[filename]

            yield idx, {
                "image_path": f"{line['subset']}/{filename}",
                "image": {"path": image_path, "bytes": image_bytes},
                "year": line["year"] or None,
                "dating-certainty": line["dating-certainty"],
                "set": line["subset"],
                "width": width,
                "height": height,
                "objects": objects,
            }
            idx += 1

if __name__ == "__main__":
    LadasDataset().download_and_prepare()