vkashko commited on
Commit
6303c85
·
1 Parent(s): c1fb268

feat: add load script

Browse files
Files changed (1) hide show
  1. electric-scooters-tracking.py +168 -0
electric-scooters-tracking.py ADDED
@@ -0,0 +1,168 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from xml.etree import ElementTree as ET
3
+
4
+ import datasets
5
+
6
+ _CITATION = """\
7
+ @InProceedings{huggingface:dataset,
8
+ title = {electric-scooters-tracking},
9
+ author = {TrainingDataPro},
10
+ year = {2023}
11
+ }
12
+ """
13
+
14
+ _DESCRIPTION = """\
15
+ The dataset contains frames extracted from self-checkout videos, specifically focusing
16
+ on **tracking products**. The tracking data provides the **trajectory of each product**,
17
+ allowing for analysis of customer movement and behavior throughout the transaction.
18
+ The dataset assists in detecting shoplifting and fraud, enhancing efficiency, accuracy,
19
+ and customer experience. It facilitates the development of computer vision models for
20
+ *object detection, tracking, and recognition* within a self-checkout environment.
21
+ """
22
+ _NAME = "electric-scooters-tracking"
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 = ["electric_scooter"]
31
+
32
+
33
+ class SelfCheckoutVideosObjectTracking(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
+ shape.set("label", track.get("label"))
102
+ shape.set("track_id", track.get("id"))
103
+ img.append(shape)
104
+
105
+ return img
106
+
107
+ @staticmethod
108
+ def parse_shape(shape: ET.Element) -> dict:
109
+ label = shape.get("label")
110
+ track_id = shape.get("track_id")
111
+ shape_type = shape.tag
112
+ rotation = shape.get("rotation", 0.0)
113
+ occluded = shape.get("occluded", 0)
114
+
115
+ points = None
116
+
117
+ if shape_type == "points":
118
+ points = tuple(map(float, shape.get("points").split(",")))
119
+
120
+ elif shape_type == "box":
121
+ points = [
122
+ (float(shape.get("xtl")), float(shape.get("ytl"))),
123
+ (float(shape.get("xbr")), float(shape.get("ybr"))),
124
+ ]
125
+
126
+ elif shape_type == "polygon":
127
+ points = [
128
+ tuple(map(float, point.split(",")))
129
+ for point in shape.get("points").split(";")
130
+ ]
131
+
132
+ attributes = []
133
+
134
+ for attr in shape:
135
+ attr_name = attr.get("name")
136
+ attr_text = attr.text
137
+ attributes.append({"name": attr_name, "text": attr_text})
138
+
139
+ shape_data = {
140
+ "label": label,
141
+ "track_id": track_id,
142
+ "type": shape_type,
143
+ "points": points,
144
+ "rotation": rotation,
145
+ "occluded": occluded,
146
+ "attributes": attributes,
147
+ }
148
+
149
+ return shape_data
150
+
151
+ def _generate_examples(self, data):
152
+ tree = ET.parse(f"{data}/annotations.xml")
153
+ root = tree.getroot()
154
+
155
+ for idx, file in enumerate(sorted(os.listdir(f"{data}/images"))):
156
+ img = self.extract_shapes_from_tracks(root, file, idx)
157
+
158
+ image_id = img.get("id")
159
+ name = img.get("name")
160
+ shapes = [self.parse_shape(shape) for shape in img]
161
+
162
+ yield idx, {
163
+ "id": image_id,
164
+ "name": name,
165
+ "image": f"{data}/images/{file}",
166
+ "mask": f"{data}/boxes/{file}",
167
+ "shapes": shapes,
168
+ }