File size: 5,634 Bytes
e6a5769
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe345b4
e4a93d7
 
e6a5769
 
fe345b4
e6a5769
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e4a93d7
e6a5769
 
 
 
e4a93d7
e6a5769
 
 
 
 
 
 
 
 
 
 
 
 
e4a93d7
 
e6a5769
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e4a93d7
 
e6a5769
 
e4a93d7
04c3a0b
 
 
 
 
e4a93d7
04c3a0b
 
 
 
e4a93d7
 
04c3a0b
 
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import os
from xml.etree import ElementTree as ET

import datasets

_CITATION = """\
@InProceedings{huggingface:dataset,
title = {dogs-video-object-tracking-dataset},
author = {TrainingDataPro},
year = {2023}
}
"""

_DESCRIPTION = """\
The dataset contains frames extracted from videos with dogs on the streets.
Each frame is accompanied by **bounding box** that specifically **tracks the dog**
in the image.
The dataset provides a valuable resource for advancing computer vision tasks,
enabling the development of more accurate and effective solutions for monitoring and
understanding dog behavior in urban settings.
"""
_NAME = "dogs-video-object-tracking-dataset"

_HOMEPAGE = f"https://huggingface.co/datasets/TrainingDataPro/{_NAME}"

_LICENSE = ""

_DATA = f"https://huggingface.co/datasets/TrainingDataPro/{_NAME}/resolve/main/data/"

_LABELS = ["dog"]


class DogsVideoObjectTrackingDataset(datasets.GeneratorBasedBuilder):
    BUILDER_CONFIGS = [
        # datasets.BuilderConfig(name="video_01", data_dir=f"{_DATA}video_01.zip"),
        datasets.BuilderConfig(name="video_02", data_dir=f"{_DATA}video_02.zip"),
        datasets.BuilderConfig(name="video_03", data_dir=f"{_DATA}video_03.zip"),
    ]

    DEFAULT_CONFIG_NAME = "video_02"

    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features(
                {
                    "id": datasets.Value("int32"),
                    "name": datasets.Value("string"),
                    "image": datasets.Image(),
                    "mask": datasets.Image(),
                    "shapes": datasets.Sequence(
                        {
                            "track_id": datasets.Value("uint32"),
                            "label": datasets.ClassLabel(
                                num_classes=len(_LABELS),
                                names=_LABELS,
                            ),
                            "type": datasets.Value("string"),
                            "points": datasets.Sequence(
                                datasets.Sequence(
                                    datasets.Value("float"),
                                ),
                            ),
                            "rotation": datasets.Value("float"),
                            "occluded": datasets.Value("uint8"),
                            "attributes": datasets.Sequence(
                                {
                                    "name": datasets.Value("string"),
                                    "text": datasets.Value("string"),
                                }
                            ),
                        }
                    ),
                }
            ),
            supervised_keys=None,
            homepage=_HOMEPAGE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        data = dl_manager.download_and_extract(self.config.data_dir)
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "data": data,
                },
            ),
        ]

    @staticmethod
    def extract_shapes_from_tracks(
        root: ET.Element, file: str, index: int
    ) -> ET.Element:
        img = ET.Element("image")
        img.set("name", file)
        img.set("id", str(index))
        for track in root.iter("track"):
            shape = track.find(f".//*[@frame='{index}']")
            if not (shape is None):

                shape.set("label", track.get("label"))
                shape.set("track_id", track.get("id"))
                img.append(shape)

        return img

    @staticmethod
    def parse_shape(shape: ET.Element) -> dict:
        label = shape.get("label")
        track_id = shape.get("track_id")
        shape_type = shape.tag
        rotation = shape.get("rotation", 0.0)
        occluded = shape.get("occluded", 0)

        points = None

        if shape_type == "points":
            points = tuple(map(float, shape.get("points").split(",")))

        elif shape_type == "box":
            points = [
                (float(shape.get("xtl")), float(shape.get("ytl"))),
                (float(shape.get("xbr")), float(shape.get("ybr"))),
            ]

        elif shape_type == "polygon":
            points = [
                tuple(map(float, point.split(",")))
                for point in shape.get("points").split(";")
            ]

        attributes = []

        for attr in shape:
            attr_name = attr.get("name")
            attr_text = attr.text
            attributes.append({"name": attr_name, "text": attr_text})

        shape_data = {
            "label": label,
            "track_id": track_id,
            "type": shape_type,
            "points": points,
            "rotation": rotation,
            "occluded": occluded,
            "attributes": attributes,
        }

        return shape_data

    def _generate_examples(self, data):
        tree = ET.parse(f"{data}/annotations.xml")
        root = tree.getroot()

        for idx, file in enumerate(sorted(os.listdir(f"{data}/images"))):
            img = self.extract_shapes_from_tracks(root, file, idx)

            image_id = img.get("id")
            name = img.get("name")
            shapes = [self.parse_shape(shape) for shape in img]
            print(shapes)

            yield idx, {
                "id": image_id,
                "name": name,
                "image": f"{data}/images/{file}",
                "mask": f"{data}/masks/{file}",
                "shapes": shapes,
            }