davanstrien HF staff commited on
Commit
8bc1b3b
·
verified ·
1 Parent(s): b211993

Delete loading script

Browse files
Files changed (1) hide show
  1. nls_chapbook_illustrations.py +0 -207
nls_chapbook_illustrations.py DELETED
@@ -1,207 +0,0 @@
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 Illustrations"""
15
-
16
- import collections
17
- import json
18
- import os
19
- from typing import Any, Dict, List
20
- import pandas as pd
21
- import datasets
22
-
23
- _CITATION = """@inproceedings{10.1145/3476887.3476893,
24
- author = {Dutta, Abhishek and Bergel, Giles and Zisserman, Andrew},
25
- title = {Visual Analysis of Chapbooks Printed in Scotland},
26
- year = {2021},
27
- isbn = {9781450386906},
28
- publisher = {Association for Computing Machinery},
29
- address = {New York, NY, USA},
30
- url = {https://doi.org/10.1145/3476887.3476893},
31
- doi = {10.1145/3476887.3476893},
32
- abstract = {Chapbooks were short, cheap printed booklets produced in large quantities in Scotland, England, Ireland, North America and much of Europe between roughly the seventeenth and nineteenth centuries. A form of popular literature containing songs, stories, poems, games, riddles, religious writings and other content designed to appeal to a wide readership, they were frequently illustrated, particularly on their title-pages. This paper describes the visual analysis of such chapbook illustrations. We automatically extract all the illustrations contained in the National Library of Scotland Chapbooks Printed in Scotland dataset, and create a visual search engine to search this dataset using full or part-illustrations as queries. We also cluster these illustrations based on their visual content, and provide keyword-based search of the metadata associated with each publication. The visual search; clustering of illustrations based on visual content; and metadata search features enable researchers to forensically analyse the chapbooks dataset and to discover unnoticed relationships between its elements. We release all annotations and software tools described in this paper to enable reproduction of the results presented and to allow extension of the methodology described to datasets of a similar nature.},
33
- booktitle = {The 6th International Workshop on Historical Document Imaging and Processing},
34
- pages = {67–72},
35
- numpages = {6},
36
- keywords = {illustration detection, chapbooks, image search, visual grouping, printing, digital scholarship, illustration dataset},
37
- location = {Lausanne, Switzerland},
38
- series = {HIP '21}
39
- }
40
- """
41
-
42
-
43
- _DESCRIPTION = "This dataset comprises of images from chapbooks held by the National Library of Scotland and digitised and published as its Chapbooks Printed in Scotland dataset"
44
-
45
-
46
- _HOMEPAGE = "https://www.robots.ox.ac.uk/~vgg/research/chapbooks/"
47
-
48
-
49
- _LICENSE = "Public Domain Mark 1.0" # TODO confirm licence terms for annotations
50
-
51
-
52
- _IMAGES_URL = "https://nlsfoundry.s3.amazonaws.com/data/nls-data-chapbooks.zip"
53
-
54
- # TODO update url if this is merged upstream
55
- _ANNOTATIONS_URL = "https://gitlab.com/davanstrien/nls-chapbooks-illustrations/-/raw/master/data/annotations/step5-manual-verification-image-0-47329_train_coco.json"
56
-
57
-
58
- class NationalLibraryScotlandChapBooksConfig(datasets.BuilderConfig):
59
- """BuilderConfig for National Library of Scotland Chapbooks dataset."""
60
-
61
- def __init__(self, name, **kwargs):
62
- super(NationalLibraryScotlandChapBooksConfig, self).__init__(
63
- version=datasets.Version("1.0.0"),
64
- name=name,
65
- description="NLS Chapbook Illustrations",
66
- **kwargs,
67
- )
68
-
69
-
70
- class NationalLibraryScotlandChapBooks(datasets.GeneratorBasedBuilder):
71
- """National Library of Scotland Chapbooks dataset."""
72
-
73
- BUILDER_CONFIGS = [
74
- NationalLibraryScotlandChapBooksConfig("illustration-detection"),
75
- NationalLibraryScotlandChapBooksConfig("image-classification"),
76
- NationalLibraryScotlandChapBooksConfig("image-matching"),
77
- ]
78
-
79
- def _info(self):
80
- if self.config.name == "illustration-detection":
81
- features = datasets.Features(
82
- {
83
- "image_id": datasets.Value("int64"),
84
- "image": datasets.Image(),
85
- "width": datasets.Value("int32"),
86
- "height": datasets.Value("int32"),
87
- }
88
- )
89
- object_dict = {
90
- "category_id": datasets.ClassLabel(
91
- names=["early_printed_illustration"]
92
- ),
93
- "image_id": datasets.Value("string"),
94
- "id": datasets.Value("int64"),
95
- "area": datasets.Value("int64"),
96
- "bbox": datasets.Sequence(datasets.Value("float32"), length=4),
97
- "segmentation": [[datasets.Value("float32")]],
98
- "iscrowd": datasets.Value("bool"),
99
- }
100
- features["objects"] = [object_dict]
101
- if self.config.name == "image-classification":
102
- features = datasets.Features(
103
- {
104
- "image": datasets.Image(),
105
- "label": datasets.ClassLabel(
106
- num_classes=2, names=["not-illustrated", "illustrated"]
107
- ),
108
- }
109
- )
110
- if self.config.name == "image-matching":
111
- features = datasets.Features(
112
- {
113
- "image": datasets.Image(),
114
- "group-label": datasets.Value("int32"),
115
- }
116
- )
117
- return datasets.DatasetInfo(
118
- description=_DESCRIPTION,
119
- features=features,
120
- homepage=_HOMEPAGE,
121
- license=_LICENSE,
122
- citation=_CITATION,
123
- )
124
-
125
- def _split_generators(self, dl_manager):
126
- images = dl_manager.download_and_extract(_IMAGES_URL)
127
- annotations = dl_manager.download(_ANNOTATIONS_URL)
128
- image_match_annotations = dl_manager.download(
129
- "illustration-group-specifications[83].csv"
130
- )
131
- return [
132
- datasets.SplitGenerator(
133
- name=datasets.Split.TRAIN,
134
- gen_kwargs={
135
- "annotations_file": os.path.join(annotations),
136
- "image_dir": os.path.join(images, "nls-data-chapbooks"),
137
- "image_match_annotations": image_match_annotations,
138
- },
139
- )
140
- ]
141
-
142
- def _get_image_id_to_annotations_mapping(
143
- self, annotations: List[Dict]
144
- ) -> Dict[int, List[Dict[Any, Any]]]:
145
- """
146
- A helper function to build a mapping from image ids to annotations.
147
- """
148
- image_id_to_annotations = collections.defaultdict(list)
149
- for annotation in annotations:
150
- image_id_to_annotations[annotation["image_id"]].append(annotation)
151
- return image_id_to_annotations
152
-
153
- def _generate_examples(self, annotations_file, image_dir, image_match_annotations):
154
- def _image_info_to_example(image_info, image_dir):
155
- image = image_info["file_name"]
156
- return {
157
- "image_id": image_info["id"],
158
- "image": os.path.join(image_dir, image),
159
- "width": image_info["width"],
160
- "height": image_info["height"],
161
- }
162
-
163
- with open(annotations_file, encoding="utf8") as f:
164
- annotation_data = json.load(f)
165
- images = annotation_data["images"]
166
- annotations = annotation_data["annotations"]
167
- image_id_to_annotations = self._get_image_id_to_annotations_mapping(
168
- annotations
169
- )
170
-
171
- if self.config.name == "illustration-detection":
172
- for idx, image_info in enumerate(images):
173
- example = _image_info_to_example(image_info, image_dir)
174
- annotations = image_id_to_annotations[image_info["id"]]
175
- objects = []
176
- for annotation in annotations:
177
- category_id = annotation["category_id"]
178
- if category_id == 1:
179
- annotation["category_id"] = 0
180
- objects.append(annotation)
181
- example["objects"] = objects
182
- yield (idx, example)
183
- if self.config.name == "image-classification":
184
- for idx, image_info in enumerate(images):
185
- annotations = image_id_to_annotations[image_info["id"]]
186
- label = 0 if len(annotations) < 1 else 1
187
- example = {
188
- "image": os.path.join(image_dir, image_info["file_name"]),
189
- "label": label,
190
- }
191
-
192
- yield (idx, example)
193
- if self.config.name == "image-matching":
194
- df = pd.read_csv(image_match_annotations, dtype={"file_id": "string"})
195
- df = df.drop_duplicates(subset=["filename"], keep=False)
196
- df = df.set_index("filename", drop=True)
197
- mapping = df.to_dict("index")
198
- for idx, image_info in enumerate(images):
199
- filename = image_info["file_name"]
200
- match = mapping.get(filename)
201
- if match:
202
- match = match["set_id"]
203
- example = {
204
- "image": os.path.join(image_dir, image_info["file_name"]),
205
- "group-label": match,
206
- }
207
- yield (idx, example)