Datasets:
Commit
•
e55356f
1
Parent(s):
1f6615b
Upload nls_chapbook_images.py
Browse files- nls_chapbook_images.py +175 -0
nls_chapbook_images.py
ADDED
@@ -0,0 +1,175 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright 2022 Daniel van Strien.
|
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 |
+
"""NLS Chapbook Images"""
|
15 |
+
|
16 |
+
import collections
|
17 |
+
import json
|
18 |
+
import os
|
19 |
+
from typing import Any, Dict, List
|
20 |
+
|
21 |
+
import datasets
|
22 |
+
|
23 |
+
|
24 |
+
_CITATION = "TODO"
|
25 |
+
|
26 |
+
|
27 |
+
_DESCRIPTION = "TODO"
|
28 |
+
|
29 |
+
|
30 |
+
_HOMEPAGE = "TODO"
|
31 |
+
|
32 |
+
|
33 |
+
_LICENSE = "Public Domain Mark 1.0" # TODO confirm licence terms for annotations
|
34 |
+
|
35 |
+
|
36 |
+
_IMAGES_URL = "https://nlsfoundry.s3.amazonaws.com/data/nls-data-chapbooks.zip"
|
37 |
+
|
38 |
+
# TODO update url if this is merged upstream
|
39 |
+
_ANNOTATIONS_URL = "https://gitlab.com/davanstrien/nls-chapbooks-illustrations/-/raw/master/data/annotations/step5-manual-verification-image-0-47329_train_coco.json"
|
40 |
+
|
41 |
+
|
42 |
+
class NationalLibraryScotlandChapBooksConfig(datasets.BuilderConfig):
|
43 |
+
"""BuilderConfig for National Library of Scotland Chapbooks dataset."""
|
44 |
+
|
45 |
+
def __init__(self, name, **kwargs):
|
46 |
+
super(NationalLibraryScotlandChapBooksConfig, self).__init__(
|
47 |
+
version=datasets.Version("1.0.0"),
|
48 |
+
name=name,
|
49 |
+
description="TODO",
|
50 |
+
**kwargs,
|
51 |
+
)
|
52 |
+
|
53 |
+
|
54 |
+
class NationalLibraryScotlandChapBooks(datasets.GeneratorBasedBuilder):
|
55 |
+
"""National Library of Scotland Chapbooks dataset."""
|
56 |
+
|
57 |
+
BUILDER_CONFIGS = [
|
58 |
+
NationalLibraryScotlandChapBooksConfig("illustration_detection"),
|
59 |
+
NationalLibraryScotlandChapBooksConfig("image_classification"),
|
60 |
+
]
|
61 |
+
|
62 |
+
def _info(self):
|
63 |
+
if self.config.name == "illustration_detection":
|
64 |
+
features = datasets.Features(
|
65 |
+
{
|
66 |
+
"image_id": datasets.Value("int64"),
|
67 |
+
"image": datasets.Image(),
|
68 |
+
"width": datasets.Value("int32"),
|
69 |
+
"height": datasets.Value("int32"),
|
70 |
+
"url": datasets.Value("string"),
|
71 |
+
"date_captured": datasets.Value("string"),
|
72 |
+
}
|
73 |
+
)
|
74 |
+
object_dict = {
|
75 |
+
"category_id": datasets.ClassLabel(
|
76 |
+
names=["early_printed_illustration"]
|
77 |
+
),
|
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 == "image_classification":
|
87 |
+
features = datasets.Features(
|
88 |
+
{
|
89 |
+
"image": datasets.Image(),
|
90 |
+
"label": datasets.ClassLabel(
|
91 |
+
num_classes=2, names=["not-illustrated", "illustrated"]
|
92 |
+
),
|
93 |
+
}
|
94 |
+
)
|
95 |
+
return datasets.DatasetInfo(
|
96 |
+
description=_DESCRIPTION,
|
97 |
+
features=features,
|
98 |
+
homepage=_HOMEPAGE,
|
99 |
+
license=_LICENSE,
|
100 |
+
citation=_CITATION,
|
101 |
+
)
|
102 |
+
|
103 |
+
def _split_generators(self, dl_manager):
|
104 |
+
images = dl_manager.download_and_extract(_IMAGES_URL)
|
105 |
+
annotations = dl_manager.download(_ANNOTATIONS_URL)
|
106 |
+
return [
|
107 |
+
datasets.SplitGenerator(
|
108 |
+
name=datasets.Split.TRAIN,
|
109 |
+
gen_kwargs={
|
110 |
+
"annotations_file": os.path.join(annotations),
|
111 |
+
"image_dir": os.path.join(images, "nls-data-chapbooks"),
|
112 |
+
},
|
113 |
+
)
|
114 |
+
]
|
115 |
+
|
116 |
+
def _get_image_id_to_annotations_mapping(
|
117 |
+
self, annotations: List[Dict]
|
118 |
+
) -> Dict[int, List[Dict[Any, Any]]]:
|
119 |
+
"""
|
120 |
+
A helper function to build a mapping from image ids to annotations.
|
121 |
+
"""
|
122 |
+
image_id_to_annotations = collections.defaultdict(list)
|
123 |
+
for annotation in annotations:
|
124 |
+
image_id_to_annotations[annotation["image_id"]].append(annotation)
|
125 |
+
return image_id_to_annotations
|
126 |
+
|
127 |
+
def _generate_examples(self, annotations_file, image_dir):
|
128 |
+
def _image_info_to_example(image_info, image_dir):
|
129 |
+
image = image_info["file_name"]
|
130 |
+
return {
|
131 |
+
"image_id": image_info["id"],
|
132 |
+
"image": os.path.join(image_dir, image),
|
133 |
+
"width": image_info["width"],
|
134 |
+
"height": image_info["height"],
|
135 |
+
"url": image_info.get("url"),
|
136 |
+
"date_captured": image_info["date_captured"],
|
137 |
+
}
|
138 |
+
|
139 |
+
with open(annotations_file, encoding="utf8") as f:
|
140 |
+
annotation_data = json.load(f)
|
141 |
+
images = annotation_data["images"]
|
142 |
+
annotations = annotation_data["annotations"]
|
143 |
+
|
144 |
+
image_id_to_annotations = self._get_image_id_to_annotations_mapping(
|
145 |
+
annotations
|
146 |
+
)
|
147 |
+
if self.config.name == "illustration_detection":
|
148 |
+
for idx, image_info in enumerate(images):
|
149 |
+
example = _image_info_to_example(
|
150 |
+
image_info,
|
151 |
+
image_dir,
|
152 |
+
)
|
153 |
+
annotations = image_id_to_annotations[image_info["id"]]
|
154 |
+
objects = []
|
155 |
+
for annot in annotations:
|
156 |
+
category_id = annot["category_id"]
|
157 |
+
if category_id == 1:
|
158 |
+
annot["category_id"] = 0
|
159 |
+
object_ = annot
|
160 |
+
objects.append(object_)
|
161 |
+
example["objects"] = objects
|
162 |
+
yield idx, example
|
163 |
+
if self.config.name == "image_classification":
|
164 |
+
for idx, image_info in enumerate(images):
|
165 |
+
example = _image_info_to_example(image_info, image_dir)
|
166 |
+
annotations = image_id_to_annotations[image_info["id"]]
|
167 |
+
if len(annotations) < 1:
|
168 |
+
label = 0
|
169 |
+
else:
|
170 |
+
label = 1
|
171 |
+
example = {
|
172 |
+
"image": os.path.join(image_dir, image_info["file_name"]),
|
173 |
+
"label": label,
|
174 |
+
}
|
175 |
+
yield idx, example
|