import collections import json import os import datasets _DESCRIPTION = """ """ _HOMEPAGE = "" _LICENSE = "" _URL = "https://huggingface.co/datasets/alexrods/mini_car_bikes_detection/resolve/main" _URLS = { "train_images": f"{_URL}/data/train.zip", "test_images": f"{_URL}/data/test.zip", } _ANNOTATIONS = { "train_annotations": f"{_URL}/annotations/train_annotations.json", "test_annotations": f"{_URL}/annotations/test_annotations.json" } _CATEGORIES = ['Car', 'bike'] class MiniCarBikesDetection(datasets.GeneratorBasedBuilder): VERSION = datasets.Version("1.0.0") def _info(self): features = datasets.Features( { "image": datasets.Image(), "image_name": datasets.Value("string"), "width": datasets.Value("int32"), "height": datasets.Value("int32"), "objects": datasets.Sequence( { # "id": datasets.Sequence(datasets.Value("int32")), "category": datasets.ClassLabel(names=_CATEGORIES), "bbox": datasets.Sequence(datasets.Value("float32"), length=4), } ), } ) return datasets.DatasetInfo( description=_DESCRIPTION, features=features, homepage=_HOMEPAGE, license=_LICENSE, ) def _split_generators(self, dl_manager): data_files = dl_manager.download(_URLS) annotations_files = _ANNOTATIONS return [ datasets.SplitGenerator( name=datasets.Split.TRAIN, gen_kwargs={ "image_files": dl_manager.iter_archive(data_files["train_images"]), "annotations_file": annotations_files["train_annotations"] }, ), datasets.SplitGenerator( name=datasets.Split.TEST, gen_kwargs={ "image_files": dl_manager.iter_archive(data_files["test_images"]), "annotations_file": annotations_files["test_annotations"] }, ), ] def _generate_examples(self, image_files, annotations_file): with open(annotations_file) as jf: annotations = json.load(jf) for image_file in image_files: image_name = image_file[0].split("/")[1] for annotation in annotations: if image_name == annotation["image"]: yield image_file[0], { "image": {"path": image_file[0], "bytes": image_file[1].read()}, "image_name": image_name, "width": annotation["width"], "height": annotation["height"], "objects": { "category": annotation["name"], "bbox": annotation["bbox"] } }