roboflownormal / roboflownormal.py
flipwooyoung's picture
dataset uploaded by roboflow2huggingface package
601ae29 verified
import collections
import json
import os
import datasets
_HOMEPAGE = "https://universe.roboflow.com/vlm/air-threats-detector/dataset/2"
_LICENSE = "CC BY 4.0"
_CITATION = """\
@misc{
air-threats-detector_dataset,
title = { Air Threats Detector Dataset },
type = { Open Source Dataset },
author = { VLM },
howpublished = { \\url{ https://universe.roboflow.com/vlm/air-threats-detector } },
url = { https://universe.roboflow.com/vlm/air-threats-detector },
journal = { Roboflow Universe },
publisher = { Roboflow },
year = { 2024 },
month = { may },
note = { visited on 2024-06-11 },
}
"""
_CATEGORIES = ['black and brown camouflage helicopter', 'black and orange drone', 'black and white cargo aircraft', 'black and white commercial aircraft', 'black and white missile', 'black and yellow drone', 'black and yellow missile', 'black camouflage fighter jet', 'black cargo aircraft', 'black drone', 'black fighter jet', 'black fighter plane', 'black helicopter', 'blue and green fighter plane', 'blue and grey fighter jet', 'blue and red commercial aircraft', 'blue and red light aircraft', 'blue and white commercial aircraft', 'blue and white helicopter', 'blue and white light aircraft', 'blue and white missile', 'blue and yellow fighter jet', 'blue and yellow helicopter', 'blue camouflage fighter jet', 'blue commercial aircraft', 'blue helicopter', 'blue missile', 'blue- yellow- and black helicopter', 'blue- yellow- and green fighter plane', 'blue- yellow- and white cargo aircraft', 'green and black camouflage helicopter', 'green and black missile', 'green and brown camouflage fighter jet', 'green and brown camouflage fighter plane', 'green and brown camouflage helicopter', 'green and grey helicopter', 'green and white fighter plane', 'green and yellow fighter plane', 'green camouflage helicopter', 'green fighter plane', 'green helicopter', 'green light aircraft', 'green missile', 'grey and black fighter plane', 'grey and black helicopter', 'grey and green cargo aircraft', 'grey and red commercial aircraft', 'grey and red fighter jet', 'grey and red missile', 'grey and white fighter plane', 'grey and white light aircraft', 'grey and yellow fighter plane', 'grey camouflage fighter jet', 'grey cargo aircraft', 'grey commercial aircraft', 'grey drone', 'grey fighter jet', 'grey fighter plane', 'grey helicopter', 'grey light aircraft', 'grey missile', 'grey- red- and blue commercial aircraft', 'orange and black fighter jet', 'orange light aircraft', 'red and black drone', 'red and grey missile', 'red and white fighter jet', 'red and white fighter plane', 'red and white helicopter', 'red and white light aircraft', 'red and white missile', 'red fighter jet', 'red fighter plane', 'red helicopter', 'red light aircraft', 'red- white- and blue fighter jet', 'red- white- and blue light aircraft', 'silver and blue fighter plane', 'silver fighter plane', 'white and black cargo aircraft', 'white and black drone', 'white and black fighter jet', 'white and black fighter plane', 'white and black helicopter', 'white and black light aircraft', 'white and blue cargo aircraft', 'white and blue commercial aircraft', 'white and blue fighter jet', 'white and blue fighter plane', 'white and blue helicopter', 'white and blue light aircraft', 'white and grey helicopter', 'white and orange commercial aircraft', 'white and orange light aircraft', 'white and red commercial aircraft', 'white and red fighter jet', 'white and red fighter plane', 'white and red helicopter', 'white and red light aircraft', 'white and red missile', 'white and yellow commercial aircraft', 'white cargo aircraft', 'white commercial aircraft', 'white drone', 'white fighter jet', 'white fighter plane', 'white helicopter', 'white light aircraft', 'white missile', 'white- black- and grey missile', 'white- black- and red drone', 'white- blue- and red commercial aircraft', 'white- red- and blue commercial aircraft', 'white- red- and green fighter plane', 'yellow and black fighter plane', 'yellow and green helicopter', 'yellow and red light aircraft', 'yellow commercial aircraft', 'yellow fighter jet', 'yellow fighter plane', 'yellow helicopter', 'yellow light aircraft', 'yellow missile', 'yellow- black- and red helicopter', 'yellow- red- and blue fighter plane', 'yellow- red- and grey helicopter']
_ANNOTATION_FILENAME = "_annotations.coco.json"
class ROBOFLOWNORMALConfig(datasets.BuilderConfig):
"""Builder Config for roboflownormal"""
def __init__(self, data_urls, **kwargs):
"""
BuilderConfig for roboflownormal.
Args:
data_urls: `dict`, name to url to download the zip file from.
**kwargs: keyword arguments forwarded to super.
"""
super(ROBOFLOWNORMALConfig, self).__init__(version=datasets.Version("1.0.0"), **kwargs)
self.data_urls = data_urls
class ROBOFLOWNORMAL(datasets.GeneratorBasedBuilder):
"""roboflownormal object detection dataset"""
VERSION = datasets.Version("1.0.0")
BUILDER_CONFIGS = [
ROBOFLOWNORMALConfig(
name="full",
description="Full version of roboflownormal dataset.",
data_urls={
"train": "https://huggingface.co/datasets/flipwooyoung/roboflownormal/resolve/main/data/train.zip",
"validation": "https://huggingface.co/datasets/flipwooyoung/roboflownormal/resolve/main/data/valid.zip",
"test": "https://huggingface.co/datasets/flipwooyoung/roboflownormal/resolve/main/data/test.zip",
},
),
ROBOFLOWNORMALConfig(
name="mini",
description="Mini version of roboflownormal dataset.",
data_urls={
"train": "https://huggingface.co/datasets/flipwooyoung/roboflownormal/resolve/main/data/valid-mini.zip",
"validation": "https://huggingface.co/datasets/flipwooyoung/roboflownormal/resolve/main/data/valid-mini.zip",
"test": "https://huggingface.co/datasets/flipwooyoung/roboflownormal/resolve/main/data/valid-mini.zip",
},
)
]
def _info(self):
features = datasets.Features(
{
"image_id": datasets.Value("int64"),
"image": datasets.Image(),
"width": datasets.Value("int32"),
"height": datasets.Value("int32"),
"objects": datasets.Sequence(
{
"id": datasets.Value("int64"),
"area": datasets.Value("int64"),
"bbox": datasets.Sequence(datasets.Value("float32"), length=4),
"category": datasets.ClassLabel(names=_CATEGORIES),
}
),
}
)
return datasets.DatasetInfo(
features=features,
homepage=_HOMEPAGE,
citation=_CITATION,
license=_LICENSE,
)
def _split_generators(self, dl_manager):
data_files = dl_manager.download_and_extract(self.config.data_urls)
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={
"folder_dir": data_files["train"],
},
),
datasets.SplitGenerator(
name=datasets.Split.VALIDATION,
gen_kwargs={
"folder_dir": data_files["validation"],
},
),
datasets.SplitGenerator(
name=datasets.Split.TEST,
gen_kwargs={
"folder_dir": data_files["test"],
},
),
]
def _generate_examples(self, folder_dir):
def process_annot(annot, category_id_to_category):
return {
"id": annot["id"],
"area": annot["area"],
"bbox": annot["bbox"],
"category": category_id_to_category[annot["category_id"]],
}
image_id_to_image = {}
idx = 0
annotation_filepath = os.path.join(folder_dir, _ANNOTATION_FILENAME)
with open(annotation_filepath, "r") as f:
annotations = json.load(f)
category_id_to_category = {category["id"]: category["name"] for category in annotations["categories"]}
image_id_to_annotations = collections.defaultdict(list)
for annot in annotations["annotations"]:
image_id_to_annotations[annot["image_id"]].append(annot)
filename_to_image = {image["file_name"]: image for image in annotations["images"]}
for filename in os.listdir(folder_dir):
filepath = os.path.join(folder_dir, filename)
if filename in filename_to_image:
image = filename_to_image[filename]
objects = [
process_annot(annot, category_id_to_category) for annot in image_id_to_annotations[image["id"]]
]
with open(filepath, "rb") as f:
image_bytes = f.read()
yield idx, {
"image_id": image["id"],
"image": {"path": filepath, "bytes": image_bytes},
"width": image["width"],
"height": image["height"],
"objects": objects,
}
idx += 1