Datasets:

Modalities:
Image
Text
ArXiv:
Libraries:
Datasets
License:
davanstrien HF staff commited on
Commit
db6b423
1 Parent(s): ee9040e

Create new file

Browse files
yalta_ai_segmonto_manuscript_dataset.py ADDED
@@ -0,0 +1,243 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2022 The HuggingFace Datasets Authors and the current dataset script contributor.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ """Script for reading 'Object Detection for Chess Pieces' dataset."""
15
+
16
+
17
+ import os
18
+ from glob import glob
19
+
20
+ import datasets
21
+ from PIL import Image
22
+
23
+ _CITATION = """\
24
+ @dataset{clerice_thibault_2022_6827706,
25
+ author = {Clérice, Thibault},
26
+ title = {YALTAi: Tabular Dataset},
27
+ month = jul,
28
+ year = 2022,
29
+ publisher = {Zenodo},
30
+ version = {1.0.0},
31
+ doi = {10.5281/zenodo.6827706},
32
+ url = {https://doi.org/10.5281/zenodo.6827706}
33
+ }
34
+ """
35
+
36
+ _DESCRIPTION = """Yalt AI Tabular Dataset"""
37
+
38
+ _HOMEPAGE = "https://doi.org/10.5281/zenodo.6827706"
39
+
40
+ _LICENSE = "Creative Commons Attribution 4.0 International"
41
+
42
+ _URL = "https://zenodo.org/record/6814770/files/yaltai-segmonto-dataset.zip?download=1"
43
+
44
+ _CATEGORIES = ['DamageZone', 'DigitizationArtefactZone', 'DropCapitalZone', 'GraphicZone', 'MainZone',
45
+ 'MarginTextZone', 'MusicZone', 'NumberingZone', 'QuireMarksZone', 'RunningTitleZone', 'SealZone',
46
+ 'StampZone', 'TableZone', 'TitlePageZone']
47
+
48
+ class YaltAiTabularDatasetConfig(datasets.BuilderConfig):
49
+ """BuilderConfig for YaltAiTabularDataset."""
50
+
51
+ def __init__(self, name, **kwargs):
52
+ """BuilderConfig for YaltAiTabularDataset."""
53
+ super(YaltAiTabularDatasetConfig, self).__init__(
54
+ version=datasets.Version("1.0.0"), name=name, description=None, **kwargs
55
+ )
56
+
57
+
58
+ class YaltAiTabularDataset(datasets.GeneratorBasedBuilder):
59
+ """Object Detection for historic manuscripts"""
60
+
61
+ BUILDER_CONFIGS = [
62
+ YaltAiTabularDatasetConfig("YOLO"),
63
+ YaltAiTabularDatasetConfig("COCO"),
64
+ ]
65
+
66
+ def _info(self):
67
+ if self.config.name == "COCO":
68
+ features = datasets.Features(
69
+ {
70
+ "image_id": datasets.Value("int64"),
71
+ "image": datasets.Image(),
72
+ "width": datasets.Value("int32"),
73
+ "height": datasets.Value("int32"),
74
+ }
75
+ )
76
+ object_dict = {
77
+ "category_id": datasets.ClassLabel(names=_CATEGORIES),
78
+ "image_id": datasets.Value("string"),
79
+ "id": datasets.Value("int64"),
80
+ "area": datasets.Value("int64"),
81
+ "bbox": datasets.Sequence(datasets.Value("float32"), length=4),
82
+ "segmentation": [[datasets.Value("float32")]],
83
+ "iscrowd": datasets.Value("bool"),
84
+ }
85
+ features["objects"] = [object_dict]
86
+ if self.config.name == "YOLO":
87
+ features = datasets.Features(
88
+ {
89
+ "image": datasets.Image(),
90
+ "objects": datasets.Sequence(
91
+ {
92
+ "label": datasets.ClassLabel(names=_CATEGORIES),
93
+ "bbox": datasets.Sequence(
94
+ datasets.Value("int32"), length=4
95
+ ),
96
+ }
97
+ ),
98
+ }
99
+ )
100
+ return datasets.DatasetInfo(
101
+ features=features,
102
+ supervised_keys=None,
103
+ description=_DESCRIPTION,
104
+ homepage=_HOMEPAGE,
105
+ license=_LICENSE,
106
+ citation=_CITATION,
107
+ )
108
+
109
+ def _split_generators(self, dl_manager):
110
+ data_dir = dl_manager.download_and_extract(_URL)
111
+ return [
112
+ datasets.SplitGenerator(
113
+ name=datasets.Split.TRAIN,
114
+ gen_kwargs={
115
+ "data_dir": os.path.join(data_dir, "yaltai-segmonto-dataset", "train")
116
+ },
117
+ ),
118
+ datasets.SplitGenerator(
119
+ name=datasets.Split.VALIDATION,
120
+ gen_kwargs={"data_dir": os.path.join(data_dir, "yaltai-segmonto-dataset", "val")},
121
+ ),
122
+ datasets.SplitGenerator(
123
+ name=datasets.Split.TEST,
124
+ gen_kwargs={
125
+ "data_dir": os.path.join(data_dir, "yaltai-segmonto-dataset", "test")
126
+ },
127
+ ),
128
+ ]
129
+
130
+ def _generate_examples(self, data_dir):
131
+ def create_annotation_from_yolo_format(
132
+ min_x,
133
+ min_y,
134
+ width,
135
+ height,
136
+ image_id,
137
+ category_id,
138
+ annotation_id,
139
+ segmentation=False,
140
+ ):
141
+ bbox = (float(min_x), float(min_y), float(width), float(height))
142
+ area = width * height
143
+ max_x = min_x + width
144
+ max_y = min_y + height
145
+ if segmentation:
146
+ seg = [[min_x, min_y, max_x, min_y, max_x, max_y, min_x, max_y]]
147
+ else:
148
+ seg = []
149
+ return {
150
+ "id": annotation_id,
151
+ "image_id": image_id,
152
+ "bbox": bbox,
153
+ "area": area,
154
+ "iscrowd": 0,
155
+ "category_id": category_id,
156
+ "segmentation": seg,
157
+ }
158
+
159
+ image_dir = os.path.join(data_dir, "images")
160
+ label_dir = os.path.join(data_dir, "labels")
161
+ image_paths = sorted(glob(f"{image_dir}/*.jpg"))
162
+ label_paths = sorted(glob(f"{label_dir}/*.txt"))
163
+ if self.config.name == "COCO":
164
+ for idx, (image_path, label_path) in enumerate(
165
+ zip(image_paths, label_paths)
166
+ ):
167
+ image_id = idx
168
+ annotations = []
169
+ image = Image.open(image_path) # Possibly conver to RGB?
170
+ w, h = image.size
171
+ with open(label_path, "r") as f:
172
+ lines = f.readlines()
173
+ for line in lines:
174
+ line = line.strip().split()
175
+ category_id = line[0]
176
+ x_center = float(line[1])
177
+ y_center = float(line[2])
178
+ width = float(line[3])
179
+ height = float(line[4])
180
+
181
+ float_x_center = w * x_center
182
+ float_y_center = h * y_center
183
+ float_width = w * width
184
+ float_height = h * height
185
+
186
+ min_x = int(float_x_center - float_width / 2)
187
+ min_y = int(float_y_center - float_height / 2)
188
+ width = int(float_width)
189
+ height = int(float_height)
190
+
191
+ annotation = create_annotation_from_yolo_format(
192
+ min_x,
193
+ min_y,
194
+ width,
195
+ height,
196
+ image_id,
197
+ category_id,
198
+ image_id,
199
+ )
200
+ annotations.append(annotation)
201
+
202
+ example = {
203
+ "image_id": image_id,
204
+ "image": image,
205
+ "width": w,
206
+ "height": h,
207
+ "objects": annotations,
208
+ }
209
+ yield idx, example
210
+ if self.config.name == "YOLO":
211
+ for idx, (image_path, label_path) in enumerate(
212
+ zip(image_paths, label_paths)
213
+ ):
214
+ im = Image.open(image_path)
215
+ width, height = im.size
216
+ image_id = idx
217
+ annotations = []
218
+ with open(label_path, "r") as f:
219
+ lines = f.readlines()
220
+ objects = []
221
+ for line in lines:
222
+ line = line.strip().split()
223
+ bbox_class = int(line[0])
224
+ bbox_xcenter = int(float(line[1]) * width)
225
+ bbox_ycenter = int(float(line[2]) * height)
226
+ bbox_width = int(float(line[3]) * width)
227
+ bbox_height = int(float(line[4]) * height)
228
+ objects.append(
229
+ {
230
+ "label": bbox_class,
231
+ "bbox": [
232
+ bbox_xcenter,
233
+ bbox_ycenter,
234
+ bbox_width,
235
+ bbox_height,
236
+ ],
237
+ }
238
+ )
239
+
240
+ yield idx, {
241
+ "image": image,
242
+ "objects": objects,
243
+ }