# Source: https://github.com/huggingface/datasets/blob/main/templates/new_dataset_script.py # Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # TODO: Address all TODOs and remove all explanatory comments import csv import json import os import datasets # Add BibTeX citation # Find for instance the citation on arxiv or on the dataset repo/website _CITATION = """\ @InProceedings{huggingface:dataset, title = {Boat dataset}, author={Tzu-Chi Chen, Inc. }, year={2024} } """ # Add description of the dataset here # You can copy an official description _DESCRIPTION = """\ This dataset is designed to solve object detection task. """ # Add a link to an official homepage for the dataset here _HOMEPAGE = "https://huggingface.co/datasets/zhuchi76/Boat_dataset" # Add the licence for the dataset here if you can find it _LICENSE = "" # Add link to the official dataset URLs here # The HuggingFace Datasets library doesn't host the datasets but only points to the original files. # This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method) _URLS = { "images": f"{_HOMEPAGE}/data/images.tar.gz", "anno": { "train": f"{_HOMEPAGE}/data/instances_train2023.jsonl", "val": f"{_HOMEPAGE}/data/instances_val2023.jsonl", "test": f"{_HOMEPAGE}/data/instances_val2023r.jsonl" }, } class BoatDataset(datasets.GeneratorBasedBuilder): VERSION = datasets.Version("1.1.0") def _info(self): 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)), # [x, y, width, height] 'category': datasets.Sequence(datasets.Value('int32')) }), }) return datasets.DatasetInfo( description=_DESCRIPTION, features=features, homepage=_HOMEPAGE, license=_LICENSE, citation=_CITATION, ) def _split_generators(self, dl_manager): downloaded_files = dl_manager.download_and_extract(_URLS) image_dir = dl_manager.extract(downloaded_files['images']) return [ datasets.SplitGenerator( name=datasets.Split.TRAIN, gen_kwargs={ 'image_dir': image_dir, 'annotations_file': downloaded_files['anno']['train'], 'split': 'train' } ), datasets.SplitGenerator( name=datasets.Split.VALIDATION, gen_kwargs={ 'image_dir': image_dir, 'annotations_file': downloaded_files['anno']['val'], 'split': 'val' } ), datasets.SplitGenerator( name=datasets.Split.TEST, gen_kwargs={ 'image_dir': image_dir, 'annotations_file': downloaded_files['anno']['test'], 'split': 'val_real' } ) ] def _generate_examples(self, image_dir, annotations_file, 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