Datasets:
Create waste_dumpsites.py
Browse files- waste_dumpsites.py +109 -0
waste_dumpsites.py
ADDED
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import os
|
3 |
+
from pathlib import Path
|
4 |
+
import datasets
|
5 |
+
|
6 |
+
class WasteDumpsitesConfig(datasets.BuilderConfig):
|
7 |
+
def __init__(self, **kwargs):
|
8 |
+
super().__init__(version=datasets.Version("1.0.0"), **kwargs)
|
9 |
+
|
10 |
+
class WasteDumpsites(datasets.GeneratorBasedBuilder):
|
11 |
+
"""Waste Dumpsites Drone Imagery dataset in COCO format."""
|
12 |
+
|
13 |
+
BUILDER_CONFIGS = [
|
14 |
+
WasteDumpsitesConfig(name="default", description="Default config for Waste Dumpsites dataset")
|
15 |
+
]
|
16 |
+
|
17 |
+
def _info(self):
|
18 |
+
print("datasets.DatasetInfo")
|
19 |
+
return datasets.DatasetInfo(
|
20 |
+
features=datasets.Features({
|
21 |
+
"image": datasets.Image(),
|
22 |
+
"image_id": datasets.Value("int64"),
|
23 |
+
"width": datasets.Value("int64"),
|
24 |
+
"height": datasets.Value("int64"),
|
25 |
+
"annotations": datasets.Sequence({
|
26 |
+
"id": datasets.Value("int64"),
|
27 |
+
"bbox": datasets.Sequence(datasets.Value("float32"), length=4),
|
28 |
+
"area": datasets.Value("float32"),
|
29 |
+
"category_id": datasets.Value("int64"),
|
30 |
+
"category_name": datasets.Value("string"),
|
31 |
+
"iscrowd": datasets.Value("int64"),
|
32 |
+
})
|
33 |
+
}),
|
34 |
+
supervised_keys=None,
|
35 |
+
)
|
36 |
+
|
37 |
+
def _split_generators(self, dl_manager):
|
38 |
+
"""Returns SplitGenerators."""
|
39 |
+
print("Returns SplitGenerators.")
|
40 |
+
return [
|
41 |
+
datasets.SplitGenerator(
|
42 |
+
name=datasets.Split.TRAIN,
|
43 |
+
gen_kwargs={"split": "train"}
|
44 |
+
),
|
45 |
+
datasets.SplitGenerator(
|
46 |
+
name=datasets.Split.VALIDATION,
|
47 |
+
gen_kwargs={"split": "valid"}
|
48 |
+
),
|
49 |
+
datasets.SplitGenerator(
|
50 |
+
name=datasets.Split.TEST,
|
51 |
+
gen_kwargs={"split": "test"}
|
52 |
+
),
|
53 |
+
]
|
54 |
+
|
55 |
+
def _generate_examples(self, split):
|
56 |
+
"""Yields examples for COCO format."""
|
57 |
+
print("Yields examples for COCO format.")
|
58 |
+
# Using your exact directory and file structure
|
59 |
+
split_dir = os.path.join(self.config.data_dir, split)
|
60 |
+
annotations_file = os.path.join(split_dir, "annotations_coco.json")
|
61 |
+
|
62 |
+
# Read COCO format annotations
|
63 |
+
with open(annotations_file, 'r') as f:
|
64 |
+
coco_data = json.load(f)
|
65 |
+
|
66 |
+
# Create category id to name mapping
|
67 |
+
categories = {cat['id']: cat['name'] for cat in coco_data['categories']}
|
68 |
+
|
69 |
+
# Group annotations by image
|
70 |
+
image_annotations = {}
|
71 |
+
for ann in coco_data['annotations']:
|
72 |
+
img_id = ann['image_id']
|
73 |
+
if img_id not in image_annotations:
|
74 |
+
image_annotations[img_id] = []
|
75 |
+
image_annotations[img_id].append(ann)
|
76 |
+
|
77 |
+
# Yield examples
|
78 |
+
for image in coco_data['images']:
|
79 |
+
image_id = image['id']
|
80 |
+
print(image_id)
|
81 |
+
|
82 |
+
# Get all annotations for this image
|
83 |
+
annotations = image_annotations.get(image_id, [])
|
84 |
+
|
85 |
+
# Prepare annotations in the required format
|
86 |
+
formatted_annotations = {
|
87 |
+
"id": [],
|
88 |
+
"bbox": [],
|
89 |
+
"area": [],
|
90 |
+
"category_id": [],
|
91 |
+
"category_name": [],
|
92 |
+
"iscrowd": []
|
93 |
+
}
|
94 |
+
|
95 |
+
for ann in annotations:
|
96 |
+
formatted_annotations["id"].append(ann["id"])
|
97 |
+
formatted_annotations["bbox"].append(ann["bbox"])
|
98 |
+
formatted_annotations["area"].append(ann["area"])
|
99 |
+
formatted_annotations["category_id"].append(ann["category_id"])
|
100 |
+
formatted_annotations["category_name"].append(categories[ann["category_id"]])
|
101 |
+
formatted_annotations["iscrowd"].append(ann.get("iscrowd", 0))
|
102 |
+
|
103 |
+
yield str(image_id), {
|
104 |
+
"image": os.path.join(split_dir, image["file_name"]),
|
105 |
+
"image_id": image_id,
|
106 |
+
"width": image["width"],
|
107 |
+
"height": image["height"],
|
108 |
+
"annotations": formatted_annotations
|
109 |
+
}
|