|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | """NLS Chapbook Images""" | 
					
						
						|  |  | 
					
						
						|  | import collections | 
					
						
						|  | import json | 
					
						
						|  | import os | 
					
						
						|  | from typing import Any, Dict, List | 
					
						
						|  |  | 
					
						
						|  | import datasets | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | _CITATION = "TODO" | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | _DESCRIPTION = "TODO" | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | _HOMEPAGE = "TODO" | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | _LICENSE = "Public Domain Mark 1.0" | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | _IMAGES_URL = "https://nlsfoundry.s3.amazonaws.com/data/nls-data-chapbooks.zip" | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | _ANNOTATIONS_URL = "https://gitlab.com/davanstrien/nls-chapbooks-illustrations/-/raw/master/data/annotations/step5-manual-verification-image-0-47329_train_coco.json" | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | class NationalLibraryScotlandChapBooksConfig(datasets.BuilderConfig): | 
					
						
						|  | """BuilderConfig for National Library of Scotland Chapbooks dataset.""" | 
					
						
						|  |  | 
					
						
						|  | def __init__(self, name, **kwargs): | 
					
						
						|  | super(NationalLibraryScotlandChapBooksConfig, self).__init__( | 
					
						
						|  | version=datasets.Version("1.0.0"), | 
					
						
						|  | name=name, | 
					
						
						|  | description="TODO", | 
					
						
						|  | **kwargs, | 
					
						
						|  | ) | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | class NationalLibraryScotlandChapBooks(datasets.GeneratorBasedBuilder): | 
					
						
						|  | """National Library of Scotland Chapbooks dataset.""" | 
					
						
						|  |  | 
					
						
						|  | BUILDER_CONFIGS = [ | 
					
						
						|  | NationalLibraryScotlandChapBooksConfig("illustration_detection"), | 
					
						
						|  | NationalLibraryScotlandChapBooksConfig("image_classification"), | 
					
						
						|  | ] | 
					
						
						|  |  | 
					
						
						|  | def _info(self): | 
					
						
						|  | if self.config.name == "illustration_detection": | 
					
						
						|  | 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=["early_printed_illustration"] | 
					
						
						|  | ), | 
					
						
						|  | "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"), | 
					
						
						|  | } | 
					
						
						|  | features["objects"] = [object_dict] | 
					
						
						|  | if self.config.name == "image_classification": | 
					
						
						|  | features = datasets.Features( | 
					
						
						|  | { | 
					
						
						|  | "image": datasets.Image(), | 
					
						
						|  | "label": datasets.ClassLabel( | 
					
						
						|  | num_classes=2, names=["not-illustrated", "illustrated"] | 
					
						
						|  | ), | 
					
						
						|  | } | 
					
						
						|  | ) | 
					
						
						|  | return datasets.DatasetInfo( | 
					
						
						|  | description=_DESCRIPTION, | 
					
						
						|  | features=features, | 
					
						
						|  | homepage=_HOMEPAGE, | 
					
						
						|  | license=_LICENSE, | 
					
						
						|  | citation=_CITATION, | 
					
						
						|  | ) | 
					
						
						|  |  | 
					
						
						|  | def _split_generators(self, dl_manager): | 
					
						
						|  | images = dl_manager.download_and_extract(_IMAGES_URL) | 
					
						
						|  | annotations = dl_manager.download(_ANNOTATIONS_URL) | 
					
						
						|  | return [ | 
					
						
						|  | datasets.SplitGenerator( | 
					
						
						|  | name=datasets.Split.TRAIN, | 
					
						
						|  | gen_kwargs={ | 
					
						
						|  | "annotations_file": os.path.join(annotations), | 
					
						
						|  | "image_dir": os.path.join(images, "nls-data-chapbooks"), | 
					
						
						|  | }, | 
					
						
						|  | ) | 
					
						
						|  | ] | 
					
						
						|  |  | 
					
						
						|  | def _get_image_id_to_annotations_mapping( | 
					
						
						|  | self, annotations: List[Dict] | 
					
						
						|  | ) -> Dict[int, List[Dict[Any, Any]]]: | 
					
						
						|  | """ | 
					
						
						|  | A helper function to build a mapping from image ids to annotations. | 
					
						
						|  | """ | 
					
						
						|  | image_id_to_annotations = collections.defaultdict(list) | 
					
						
						|  | for annotation in annotations: | 
					
						
						|  | image_id_to_annotations[annotation["image_id"]].append(annotation) | 
					
						
						|  | return image_id_to_annotations | 
					
						
						|  |  | 
					
						
						|  | def _generate_examples(self, annotations_file, image_dir): | 
					
						
						|  | 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(annotations_file, encoding="utf8") as f: | 
					
						
						|  | annotation_data = json.load(f) | 
					
						
						|  | images = annotation_data["images"] | 
					
						
						|  | annotations = annotation_data["annotations"] | 
					
						
						|  |  | 
					
						
						|  | image_id_to_annotations = self._get_image_id_to_annotations_mapping( | 
					
						
						|  | annotations | 
					
						
						|  | ) | 
					
						
						|  | if self.config.name == "illustration_detection": | 
					
						
						|  | for idx, image_info in enumerate(images): | 
					
						
						|  | example = _image_info_to_example( | 
					
						
						|  | image_info, | 
					
						
						|  | image_dir, | 
					
						
						|  | ) | 
					
						
						|  | annotations = image_id_to_annotations[image_info["id"]] | 
					
						
						|  | objects = [] | 
					
						
						|  | for annot in annotations: | 
					
						
						|  | category_id = annot["category_id"] | 
					
						
						|  | if category_id == 1: | 
					
						
						|  | annot["category_id"] = 0 | 
					
						
						|  | objects.append(annot) | 
					
						
						|  | example["objects"] = objects | 
					
						
						|  | yield idx, example | 
					
						
						|  | if self.config.name == "image_classification": | 
					
						
						|  | for idx, image_info in enumerate(images): | 
					
						
						|  | example = _image_info_to_example(image_info, image_dir) | 
					
						
						|  | annotations = image_id_to_annotations[image_info["id"]] | 
					
						
						|  | if len(annotations) < 1: | 
					
						
						|  | label = 0 | 
					
						
						|  | else: | 
					
						
						|  | label = 1 | 
					
						
						|  | example = { | 
					
						
						|  | "image": os.path.join(image_dir, image_info["file_name"]), | 
					
						
						|  | "label": label, | 
					
						
						|  | } | 
					
						
						|  | yield idx, example | 
					
						
						|  |  |