Datasets:
Tasks:
Object Detection
Size Categories:
1K<n<10K
Source Datasets:
original
Tags:
other-object-detection
PierreLeveau
commited on
Commit
•
3b5884c
1
Parent(s):
b7bf380
added ds download script
Browse files- plastic_in_river.py +93 -0
plastic_in_river.py
ADDED
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# from https://github.com/huggingface/datasets/blob/master/templates/new_dataset_script.py
|
2 |
+
|
3 |
+
import numpy as np
|
4 |
+
|
5 |
+
import datasets
|
6 |
+
from PIL import Image
|
7 |
+
|
8 |
+
|
9 |
+
_DESCRIPTION = """
|
10 |
+
This dataset contains photos of rivers on which there may be waste. The waste items are annotated
|
11 |
+
through bounding boxes, and are assigned to one of the 4 following categories: plastic bottle, plastic bag,
|
12 |
+
another plastic waste, or non-plastic waste. Note that some photos may not contain any waste.
|
13 |
+
"""
|
14 |
+
|
15 |
+
_HOMEPAGE = ""
|
16 |
+
|
17 |
+
_LICENSE = ""
|
18 |
+
|
19 |
+
# # TODO: Add link to the official dataset URLs here
|
20 |
+
# # The HuggingFace Datasets library doesn't host the datasets but only points to the original files.
|
21 |
+
# # This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
|
22 |
+
_URL = "https://storage.googleapis.com/kili-datasets-public/plastic-in-river/"
|
23 |
+
|
24 |
+
_URLS = {
|
25 |
+
"train_images": f"{_URL}train/images.tar.gz",
|
26 |
+
"train_annotations": f"{_URL}train/annotations.tar.gz",
|
27 |
+
"validation_images": f"{_URL}validation/images.tar.gz",
|
28 |
+
"validation_annotations": f"{_URL}validation/annotations.tar.gz",
|
29 |
+
"test_images": f"{_URL}test/images.tar.gz",
|
30 |
+
"test_annotations": f"{_URL}test/annotations.tar.gz"
|
31 |
+
}
|
32 |
+
|
33 |
+
class PlasticInRiver(datasets.GeneratorBasedBuilder):
|
34 |
+
"""Download script for the Plastic In River dataset"""
|
35 |
+
|
36 |
+
VERSION = datasets.Version("1.0.0")
|
37 |
+
|
38 |
+
def _info(self):
|
39 |
+
features = datasets.Features(
|
40 |
+
{
|
41 |
+
"image": datasets.Image(),
|
42 |
+
"litter": datasets.Sequence(
|
43 |
+
{
|
44 |
+
"label": datasets.ClassLabel(num_classes=3, names=["PLASTIC_BAG", "PLASTIC_BOTTLE", "OTHER_PLASTIC_WASTE"]),
|
45 |
+
"bbox": datasets.Sequence(datasets.Value("float32"), length=4),
|
46 |
+
}
|
47 |
+
)
|
48 |
+
}
|
49 |
+
)
|
50 |
+
|
51 |
+
return datasets.DatasetInfo(
|
52 |
+
description=_DESCRIPTION,
|
53 |
+
features=features,
|
54 |
+
license=_LICENSE,
|
55 |
+
)
|
56 |
+
|
57 |
+
def _split_generators(self, dl_manager):
|
58 |
+
downloaded_files = dl_manager.download(_URLS)
|
59 |
+
|
60 |
+
return [
|
61 |
+
datasets.SplitGenerator(
|
62 |
+
name=split,
|
63 |
+
gen_kwargs={
|
64 |
+
"image_files": dl_manager.iter_archive(downloaded_files[f"{split_name}_images"]),
|
65 |
+
"annotations_files": dl_manager.iter_archive(downloaded_files[f"{split_name}_annotations"]),
|
66 |
+
"split": split_name,
|
67 |
+
},
|
68 |
+
) for split, split_name in [
|
69 |
+
(datasets.Split.TRAIN, "train"),
|
70 |
+
(datasets.Split.TEST, "test"),
|
71 |
+
(datasets.Split.VALIDATION, "validation"),
|
72 |
+
]
|
73 |
+
]
|
74 |
+
|
75 |
+
def _generate_examples(self, image_files, annotations_files, split):
|
76 |
+
|
77 |
+
for idx, (image_file, annotations_file) in enumerate(zip(image_files, annotations_files)):
|
78 |
+
image_array = np.array(Image.open(image_file[1]))
|
79 |
+
data = {
|
80 |
+
"image": image_array,
|
81 |
+
"litter": []
|
82 |
+
}
|
83 |
+
|
84 |
+
for l in annotations_file[1].readlines():
|
85 |
+
numbers = l.decode("utf-8").split(" ")
|
86 |
+
|
87 |
+
data["litter"].append({
|
88 |
+
"label": int(numbers[0]),
|
89 |
+
"bbox": [float(n) for n in numbers[1:]]
|
90 |
+
})
|
91 |
+
|
92 |
+
yield idx, data
|
93 |
+
|