vkashko commited on
Commit
ba7e3d3
1 Parent(s): 9c200cf

feat: add load script

Browse files
Files changed (1) hide show
  1. medical-staff-people-tracking.py +168 -0
medical-staff-people-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 = {medical-staff-people-tracking},
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 = "medical-staff-people-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 = ["nurse", "doctor", "other_people"]
31
+
32
+
33
+ class ElectricScootersTracking(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
+ ]
38
+
39
+ DEFAULT_CONFIG_NAME = "video_01"
40
+
41
+ def _info(self):
42
+ return datasets.DatasetInfo(
43
+ description=_DESCRIPTION,
44
+ features=datasets.Features(
45
+ {
46
+ "id": datasets.Value("int32"),
47
+ "name": datasets.Value("string"),
48
+ "image": datasets.Image(),
49
+ "mask": datasets.Image(),
50
+ "shapes": datasets.Sequence(
51
+ {
52
+ "track_id": datasets.Value("uint32"),
53
+ "label": datasets.ClassLabel(
54
+ num_classes=len(_LABELS),
55
+ names=_LABELS,
56
+ ),
57
+ "type": datasets.Value("string"),
58
+ "points": datasets.Sequence(
59
+ datasets.Sequence(
60
+ datasets.Value("float"),
61
+ ),
62
+ ),
63
+ "rotation": datasets.Value("float"),
64
+ "occluded": datasets.Value("uint8"),
65
+ "attributes": datasets.Sequence(
66
+ {
67
+ "name": datasets.Value("string"),
68
+ "text": datasets.Value("string"),
69
+ }
70
+ ),
71
+ }
72
+ ),
73
+ }
74
+ ),
75
+ supervised_keys=None,
76
+ homepage=_HOMEPAGE,
77
+ citation=_CITATION,
78
+ )
79
+
80
+ def _split_generators(self, dl_manager):
81
+ data = dl_manager.download_and_extract(self.config.data_dir)
82
+ return [
83
+ datasets.SplitGenerator(
84
+ name=datasets.Split.TRAIN,
85
+ gen_kwargs={
86
+ "data": data,
87
+ },
88
+ ),
89
+ ]
90
+
91
+ @staticmethod
92
+ def extract_shapes_from_tracks(
93
+ root: ET.Element, file: str, index: int
94
+ ) -> ET.Element:
95
+ img = ET.Element("image")
96
+ img.set("name", file)
97
+ img.set("id", str(index))
98
+ for track in root.iter("track"):
99
+ shape = track.find(f".//*[@frame='{index}']")
100
+ if shape:
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
+ }