| |
|
|
| import csv |
| import json |
| import os |
|
|
| import datasets |
|
|
| _CITATION = """\ |
| @InProceedings{huggingface:dataset, |
| title = {Boat dataset}, |
| author={XXX, Inc.}, |
| year={2024} |
| } |
| """ |
|
|
| _DESCRIPTION = """\ |
| This dataset is designed to solve an object detection task with images of boats. |
| """ |
|
|
| _HOMEPAGE = "https://huggingface.co/datasets/uwwee/Boat_dataset/resolve/main" |
|
|
| _LICENSE = "" |
|
|
| _URLS = { |
| |
| "train": f"{_HOMEPAGE}/data/instances_train2023r.jsonl", |
| "val": f"{_HOMEPAGE}/data/instances_val2023r.jsonl", |
| } |
|
|
| class BoatDataset(datasets.GeneratorBasedBuilder): |
|
|
| VERSION = datasets.Version("1.1.0") |
| |
| BUILDER_CONFIGS = [ |
| datasets.BuilderConfig(name="Boat_dataset", version=VERSION, description="Dataset for detecting boats in aerial images."), |
| ] |
|
|
| DEFAULT_CONFIG_NAME = "Boat_dataset" |
|
|
| def _info(self): |
| return datasets.DatasetInfo( |
| description=_DESCRIPTION, |
| features=datasets.Features({ |
| 'image_id': datasets.Value('int32'), |
| 'image_path': datasets.Value('string'), |
| 'width': datasets.Value('int32'), |
| 'height': datasets.Value('int32'), |
| 'objects': datasets.Features({ |
| 'id': datasets.Sequence(datasets.Value('int32')), |
| 'area': datasets.Sequence(datasets.Value('float32')), |
| 'bbox': datasets.Sequence(datasets.Sequence(datasets.Value('float32'), length=4)), |
| 'category': datasets.Sequence(datasets.Value('int32')) |
| }), |
| }), |
| homepage=_HOMEPAGE, |
| license=_LICENSE, |
| citation=_CITATION, |
| ) |
|
|
| def _split_generators(self, dl_manager): |
| |
| downloaded_files = dl_manager.download_and_extract(_URLS) |
|
|
| |
| with open('classes.txt', 'r') as file: |
| classes = [line.strip() for line in file.readlines()] |
| |
| return [ |
| datasets.SplitGenerator( |
| name=datasets.Split.TRAIN, |
| gen_kwargs={ |
| "annotations_file": downloaded_files["train"], |
| "classes": classes, |
| "split": "train", |
| } |
| ), |
| datasets.SplitGenerator( |
| name=datasets.Split.VALIDATION, |
| gen_kwargs={ |
| "annotations_file": downloaded_files["val"], |
| "classes": classes, |
| "split": "val", |
| } |
| ), |
| ] |
|
|
| def _generate_examples(self, annotations_file, classes, split): |
| |
| with open(annotations_file, encoding="utf-8") as f: |
| for key, row in enumerate(f): |
| try: |
| data = json.loads(row.strip()) |
| yield key, { |
| "image_id": data["image_id"], |
| "image_path": data["image_path"], |
| "width": data["width"], |
| "height": data["height"], |
| "objects": data["objects"], |
| } |
| except json.JSONDecodeError: |
| print(f"Skipping invalid JSON at line {key + 1}: {row}") |
| continue |
|
|