|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"""Card Display Detection""" |
|
|
|
import collections |
|
import json |
|
import os |
|
from typing import Any, Dict, List |
|
import pandas as pd |
|
import datasets |
|
|
|
_CITATION = """Connor Hoehn""" |
|
|
|
_DESCRIPTION = "This dataset comprises of card display images from the public domain" |
|
|
|
_HOMEPAGE = "https://www.connorhoehn.com" |
|
|
|
_LICENSE = "Public Domain Mark 1.0" |
|
|
|
_DATASET_URL = "https://www.connorhoehn.com/object_detection_dataset_v2.zip" |
|
|
|
_CATEGORIES = ["boxed","grid","spread","stack"] |
|
|
|
class CardDisplayDetectorConfig(datasets.BuilderConfig): |
|
"""BuilderConfig for card display dataset.""" |
|
|
|
def __init__(self, name, **kwargs): |
|
|
|
super(CardDisplayDetectorConfig, self).__init__( |
|
version=datasets.Version("1.0.0"), |
|
name=name, |
|
description="Card Display Detector", |
|
**kwargs, |
|
) |
|
|
|
|
|
class CardDisplayDetector(datasets.GeneratorBasedBuilder): |
|
"""Card Display dataset.""" |
|
|
|
BUILDER_CONFIGS = [ |
|
CardDisplayDetectorConfig("display-detection"), |
|
] |
|
|
|
def _info(self): |
|
|
|
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=_CATEGORIES), |
|
"image_id": datasets.Value("string"), |
|
"id": datasets.Value("int64"), |
|
"area": datasets.Value("int64"), |
|
"bbox": datasets.Sequence(datasets.Value("float32"), length=4), |
|
"iscrowd": datasets.Value("bool"), |
|
} |
|
features["objects"] = [object_dict] |
|
|
|
return datasets.DatasetInfo( |
|
description=_DESCRIPTION, |
|
features=features, |
|
homepage=_HOMEPAGE, |
|
license=_LICENSE, |
|
citation=_CITATION, |
|
) |
|
|
|
|
|
|
|
def _split_generators(self, dl_manager): |
|
|
|
dataset_zip = dl_manager.download_and_extract(_DATASET_URL) |
|
|
|
return [ |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TRAIN, |
|
|
|
gen_kwargs={ |
|
"annotations_file": os.path.join(dataset_zip, "result.json"), |
|
|
|
"image_dir": os.path.join(dataset_zip), |
|
}, |
|
) |
|
] |
|
|
|
|
|
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 annotation_json: |
|
|
|
annotation_data = json.load(annotation_json) |
|
|
|
images = annotation_data["images"] |
|
|
|
annotations = annotation_data["annotations"] |
|
|
|
|
|
image_id_to_annotations = self._get_image_id_to_annotations_mapping( |
|
annotations |
|
) |
|
|
|
if self.config.name == "display-detection": |
|
|
|
|
|
for image_id, image_info in enumerate(images): |
|
|
|
|
|
image_details = _image_info_to_example(image_info, image_dir) |
|
|
|
|
|
annotations = image_id_to_annotations[image_info["id"]] |
|
|
|
objects = [] |
|
|
|
|
|
for annotation in annotations: |
|
|
|
del annotation['segmentation'] |
|
del annotation['ignore'] |
|
|
|
objects.append(annotation) |
|
|
|
|
|
image_details["objects"] = objects |
|
|
|
yield (image_id, image_details) |