Update script to hub
Browse files- Boat_dataset.py +13 -21
Boat_dataset.py
CHANGED
|
@@ -6,22 +6,19 @@ import os
|
|
| 6 |
|
| 7 |
import datasets
|
| 8 |
|
| 9 |
-
|
| 10 |
_CITATION = """\
|
| 11 |
@InProceedings{huggingface:dataset,
|
| 12 |
title = {Boat dataset},
|
| 13 |
-
author={Tzu-Chi Chen, Inc.
|
| 14 |
-
},
|
| 15 |
year={2024}
|
| 16 |
}
|
| 17 |
"""
|
| 18 |
|
| 19 |
-
|
| 20 |
_DESCRIPTION = """\
|
| 21 |
-
This dataset is designed to solve object detection task.
|
| 22 |
"""
|
| 23 |
|
| 24 |
-
_HOMEPAGE = "https://huggingface.co/datasets/zhuchi76/Boat_dataset
|
| 25 |
|
| 26 |
_LICENSE = ""
|
| 27 |
|
|
@@ -69,8 +66,8 @@ class BoatDataset(datasets.GeneratorBasedBuilder):
|
|
| 69 |
# Download all files and extract them
|
| 70 |
downloaded_files = dl_manager.download_and_extract(_URLS)
|
| 71 |
|
| 72 |
-
#
|
| 73 |
-
|
| 74 |
classes_file_path = downloaded_files["classes"]
|
| 75 |
annotations_paths = downloaded_files["anno"]
|
| 76 |
|
|
@@ -82,7 +79,7 @@ class BoatDataset(datasets.GeneratorBasedBuilder):
|
|
| 82 |
datasets.SplitGenerator(
|
| 83 |
name=datasets.Split.TRAIN,
|
| 84 |
gen_kwargs={
|
| 85 |
-
"
|
| 86 |
"annotations_file": annotations_paths["train"],
|
| 87 |
"classes": classes,
|
| 88 |
"split": "train",
|
|
@@ -91,7 +88,7 @@ class BoatDataset(datasets.GeneratorBasedBuilder):
|
|
| 91 |
datasets.SplitGenerator(
|
| 92 |
name=datasets.Split.VALIDATION,
|
| 93 |
gen_kwargs={
|
| 94 |
-
"
|
| 95 |
"annotations_file": annotations_paths["val"],
|
| 96 |
"classes": classes,
|
| 97 |
"split": "val",
|
|
@@ -100,7 +97,7 @@ class BoatDataset(datasets.GeneratorBasedBuilder):
|
|
| 100 |
datasets.SplitGenerator(
|
| 101 |
name=datasets.Split.TEST,
|
| 102 |
gen_kwargs={
|
| 103 |
-
"
|
| 104 |
"annotations_file": annotations_paths["test"],
|
| 105 |
"classes": classes,
|
| 106 |
"split": "val_real",
|
|
@@ -108,23 +105,18 @@ class BoatDataset(datasets.GeneratorBasedBuilder):
|
|
| 108 |
),
|
| 109 |
]
|
| 110 |
|
| 111 |
-
def _generate_examples(self,
|
| 112 |
-
# Open the archive using iter_archive to access files without extracting them
|
| 113 |
-
with dl_manager.iter_archive(image_archive_path) as archive:
|
| 114 |
-
# Convert path: file-like object pairs to a dict for quick access
|
| 115 |
-
images_dict = {os.path.basename(path): file for path, file in archive}
|
| 116 |
-
|
| 117 |
# Process annotations
|
| 118 |
with open(annotations_file, encoding="utf-8") as f:
|
| 119 |
for key, row in enumerate(f):
|
| 120 |
try:
|
| 121 |
data = json.loads(row.strip())
|
| 122 |
-
|
| 123 |
-
if not
|
| 124 |
-
continue # Skip if file is not found in the
|
| 125 |
yield key, {
|
| 126 |
"image_id": data["image_id"],
|
| 127 |
-
"image_path":
|
| 128 |
"width": data["width"],
|
| 129 |
"height": data["height"],
|
| 130 |
"objects": {
|
|
|
|
| 6 |
|
| 7 |
import datasets
|
| 8 |
|
|
|
|
| 9 |
_CITATION = """\
|
| 10 |
@InProceedings{huggingface:dataset,
|
| 11 |
title = {Boat dataset},
|
| 12 |
+
author={Tzu-Chi Chen, Inc.},
|
|
|
|
| 13 |
year={2024}
|
| 14 |
}
|
| 15 |
"""
|
| 16 |
|
|
|
|
| 17 |
_DESCRIPTION = """\
|
| 18 |
+
This dataset is designed to solve an object detection task with images of boats.
|
| 19 |
"""
|
| 20 |
|
| 21 |
+
_HOMEPAGE = "https://huggingface.co/datasets/zhuchi76/Boat_dataset"
|
| 22 |
|
| 23 |
_LICENSE = ""
|
| 24 |
|
|
|
|
| 66 |
# Download all files and extract them
|
| 67 |
downloaded_files = dl_manager.download_and_extract(_URLS)
|
| 68 |
|
| 69 |
+
# Extract the image archive
|
| 70 |
+
image_dir = dl_manager.extract(downloaded_files["images"])
|
| 71 |
classes_file_path = downloaded_files["classes"]
|
| 72 |
annotations_paths = downloaded_files["anno"]
|
| 73 |
|
|
|
|
| 79 |
datasets.SplitGenerator(
|
| 80 |
name=datasets.Split.TRAIN,
|
| 81 |
gen_kwargs={
|
| 82 |
+
"image_dir": image_dir,
|
| 83 |
"annotations_file": annotations_paths["train"],
|
| 84 |
"classes": classes,
|
| 85 |
"split": "train",
|
|
|
|
| 88 |
datasets.SplitGenerator(
|
| 89 |
name=datasets.Split.VALIDATION,
|
| 90 |
gen_kwargs={
|
| 91 |
+
"image_dir": image_dir,
|
| 92 |
"annotations_file": annotations_paths["val"],
|
| 93 |
"classes": classes,
|
| 94 |
"split": "val",
|
|
|
|
| 97 |
datasets.SplitGenerator(
|
| 98 |
name=datasets.Split.TEST,
|
| 99 |
gen_kwargs={
|
| 100 |
+
"image_dir": image_dir,
|
| 101 |
"annotations_file": annotations_paths["test"],
|
| 102 |
"classes": classes,
|
| 103 |
"split": "val_real",
|
|
|
|
| 105 |
),
|
| 106 |
]
|
| 107 |
|
| 108 |
+
def _generate_examples(self, image_dir, annotations_file, classes, split):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
# Process annotations
|
| 110 |
with open(annotations_file, encoding="utf-8") as f:
|
| 111 |
for key, row in enumerate(f):
|
| 112 |
try:
|
| 113 |
data = json.loads(row.strip())
|
| 114 |
+
file_path = os.path.join(image_dir, data["file_name"])
|
| 115 |
+
if not os.path.isfile(file_path):
|
| 116 |
+
continue # Skip if file is not found in the directory
|
| 117 |
yield key, {
|
| 118 |
"image_id": data["image_id"],
|
| 119 |
+
"image_path": file_path, # Provide the full path to the image
|
| 120 |
"width": data["width"],
|
| 121 |
"height": data["height"],
|
| 122 |
"objects": {
|