File size: 3,182 Bytes
5953923
0de48b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e01007c
470ea74
e01007c
 
 
0de48b4
 
 
 
 
 
 
 
 
 
1ade40b
ee36e57
a667561
 
b2baa7a
 
dae2c13
0edb31a
bd9d2d9
b2baa7a
 
0de48b4
 
 
 
 
 
 
 
 
 
 
 
bd3f742
e01007c
0de48b4
 
 
 
 
e01007c
0de48b4
 
 
 
 
 
e01007c
0de48b4
 
 
 
e01007c
 
 
77a6074
0cc6c27
058ebea
 
ecefa7c
058ebea
 
654b376
bd39efd
a15a92f
 
 
 
058ebea
7ea3afe
0de48b4
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import collections
import json
import os

import datasets


_DESCRIPTION = """
"""

_HOMEPAGE = ""

_LICENSE = ""

_URL = "https://huggingface.co/datasets/alexrods/mini_car_bikes_detection/resolve/main"

_URLS = {
    "train_images": f"{_URL}/data/train.zip",
    "test_images": f"{_URL}/data/test.zip",
}

_ANNOTATIONS = {
    "train_annotations": f"{_URL}/annotations/train_annotations.json",
    "test_annotations": f"{_URL}/annotations/test_annotations.json"
}

_CATEGORIES = ['Car', 'bike']


class MiniCarBikesDetection(datasets.GeneratorBasedBuilder):

    VERSION = datasets.Version("1.0.0")

    def _info(self):
        features = datasets.Features(
            {
                "image": datasets.Image(),
                "image_name": datasets.Value("string"),
                "width": datasets.Value("int32"),
                "height": datasets.Value("int32"),
                "objects": datasets.Sequence(
                    {
                        # "id": datasets.Sequence(datasets.Value("int32")),
                        "category": datasets.ClassLabel(names=_CATEGORIES),
                        "bbox": datasets.Sequence(datasets.Value("float32"), length=4),                        
                    }
                ),                  
            }
        )
       
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,
            homepage=_HOMEPAGE,
            license=_LICENSE,
        )


    def _split_generators(self, dl_manager):
            data_files = dl_manager.download(_URLS)
            annotations_files = _ANNOTATIONS
            return [
                datasets.SplitGenerator(
                    name=datasets.Split.TRAIN,
                    gen_kwargs={
                        "image_files": dl_manager.iter_archive(data_files["train_images"]),
                        "annotations_file": annotations_files["train_annotations"]
                    },
                ),
                datasets.SplitGenerator(
                    name=datasets.Split.TEST,
                    gen_kwargs={
                        "image_files": dl_manager.iter_archive(data_files["test_images"]),
                        "annotations_file": annotations_files["test_annotations"]
                    },
                ),
            ]
        
    def _generate_examples(self, image_files, annotations_file): 
        with open(annotations_file) as jf:
            annotations = json.load(jf)
        for image_file in image_files:
            image_name = image_file[0].split("/")[1]
            for annotation in annotations:
                if image_name == annotation["image"]:
                    yield  image_file[0], {
                        "image": {"path": image_file[0], "bytes": image_file[1].read()},
                        "image_name": image_name,
                        "width": annotation["width"],
                        "height": annotation["height"],
                        "objects": {
                            "category": annotation["name"],
                            "bbox": annotation["bbox"]
                        }
                    }