vkashko commited on
Commit
e6a5769
1 Parent(s): c2e20fb

feat: add load script

Browse files
Files changed (1) hide show
  1. dogs-video-object-tracking-dataset.py +169 -0
dogs-video-object-tracking-dataset.py ADDED
@@ -0,0 +1,169 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from xml.etree import ElementTree as ET
3
+
4
+ import datasets
5
+
6
+ _CITATION = """\
7
+ @InProceedings{huggingface:dataset,
8
+ title = {dogs-video-object-tracking-dataset},
9
+ author = {TrainingDataPro},
10
+ year = {2023}
11
+ }
12
+ """
13
+
14
+ _DESCRIPTION = """\
15
+ The dataset contains frames extracted from videos with dogs on the streets.
16
+ Each frame is accompanied by **bounding box** that specifically **tracks the dog**
17
+ in the image.
18
+ The dataset provides a valuable resource for advancing computer vision tasks,
19
+ enabling the development of more accurate and effective solutions for monitoring and
20
+ understanding dog behavior in urban settings.
21
+ """
22
+ _NAME = "dogs-video-object-tracking-dataset"
23
+
24
+ _HOMEPAGE = f"https://huggingface.co/datasets/TrainingDataPro/{_NAME}"
25
+
26
+ _LICENSE = ""
27
+
28
+ _DATA = f"https://huggingface.co/datasets/TrainingDataPro/{_NAME}/resolve/main/data/"
29
+
30
+ _LABELS = ["dog"]
31
+
32
+
33
+ class DogsVideoObjectTrackingDataset(datasets.GeneratorBasedBuilder):
34
+ BUILDER_CONFIGS = [
35
+ datasets.BuilderConfig(name="video_01", data_dir=f"{_DATA}video_01.zip"),
36
+ datasets.BuilderConfig(name="video_02", data_dir=f"{_DATA}video_02.zip"),
37
+ datasets.BuilderConfig(name="video_03", data_dir=f"{_DATA}video_03.zip"),
38
+ ]
39
+
40
+ DEFAULT_CONFIG_NAME = "video_01"
41
+
42
+ def _info(self):
43
+ return datasets.DatasetInfo(
44
+ description=_DESCRIPTION,
45
+ features=datasets.Features(
46
+ {
47
+ "id": datasets.Value("int32"),
48
+ "name": datasets.Value("string"),
49
+ "image": datasets.Image(),
50
+ "mask": datasets.Image(),
51
+ "shapes": datasets.Sequence(
52
+ {
53
+ "track_id": datasets.Value("uint32"),
54
+ "label": datasets.ClassLabel(
55
+ num_classes=len(_LABELS),
56
+ names=_LABELS,
57
+ ),
58
+ "type": datasets.Value("string"),
59
+ "points": datasets.Sequence(
60
+ datasets.Sequence(
61
+ datasets.Value("float"),
62
+ ),
63
+ ),
64
+ "rotation": datasets.Value("float"),
65
+ "occluded": datasets.Value("uint8"),
66
+ "attributes": datasets.Sequence(
67
+ {
68
+ "name": datasets.Value("string"),
69
+ "text": datasets.Value("string"),
70
+ }
71
+ ),
72
+ }
73
+ ),
74
+ }
75
+ ),
76
+ supervised_keys=None,
77
+ homepage=_HOMEPAGE,
78
+ citation=_CITATION,
79
+ )
80
+
81
+ def _split_generators(self, dl_manager):
82
+ data = dl_manager.download_and_extract(self.config.data_dir)
83
+ return [
84
+ datasets.SplitGenerator(
85
+ name=datasets.Split.TRAIN,
86
+ gen_kwargs={
87
+ "data": data,
88
+ },
89
+ ),
90
+ ]
91
+
92
+ @staticmethod
93
+ def extract_shapes_from_tracks(
94
+ root: ET.Element, file: str, index: int
95
+ ) -> ET.Element:
96
+ img = ET.Element("image")
97
+ img.set("name", file)
98
+ img.set("id", str(index))
99
+ for track in root.iter("track"):
100
+ shape = track.find(f".//*[@frame='{index}']")
101
+ if shape:
102
+ shape.set("label", track.get("label"))
103
+ shape.set("track_id", track.get("id"))
104
+ img.append(shape)
105
+
106
+ return img
107
+
108
+ @staticmethod
109
+ def parse_shape(shape: ET.Element) -> dict:
110
+ label = shape.get("label")
111
+ track_id = shape.get("track_id")
112
+ shape_type = shape.tag
113
+ rotation = shape.get("rotation", 0.0)
114
+ occluded = shape.get("occluded", 0)
115
+
116
+ points = None
117
+
118
+ if shape_type == "points":
119
+ points = tuple(map(float, shape.get("points").split(",")))
120
+
121
+ elif shape_type == "box":
122
+ points = [
123
+ (float(shape.get("xtl")), float(shape.get("ytl"))),
124
+ (float(shape.get("xbr")), float(shape.get("ybr"))),
125
+ ]
126
+
127
+ elif shape_type == "polygon":
128
+ points = [
129
+ tuple(map(float, point.split(",")))
130
+ for point in shape.get("points").split(";")
131
+ ]
132
+
133
+ attributes = []
134
+
135
+ for attr in shape:
136
+ attr_name = attr.get("name")
137
+ attr_text = attr.text
138
+ attributes.append({"name": attr_name, "text": attr_text})
139
+
140
+ shape_data = {
141
+ "label": label,
142
+ "track_id": track_id,
143
+ "type": shape_type,
144
+ "points": points,
145
+ "rotation": rotation,
146
+ "occluded": occluded,
147
+ "attributes": attributes,
148
+ }
149
+
150
+ return shape_data
151
+
152
+ def _generate_examples(self, data):
153
+ tree = ET.parse(f"{data}/annotations.xml")
154
+ root = tree.getroot()
155
+
156
+ for idx, file in enumerate(sorted(os.listdir(f"{data}/images"))):
157
+ img = self.extract_shapes_from_tracks(root, file, idx)
158
+
159
+ image_id = img.get("id")
160
+ name = img.get("name")
161
+ shapes = [self.parse_shape(shape) for shape in img]
162
+
163
+ yield idx, {
164
+ "id": image_id,
165
+ "name": name,
166
+ "image": f"{data}/images/{file}",
167
+ "mask": f"{data}/boxes/{file}",
168
+ "shapes": shapes,
169
+ }