vkashko commited on
Commit
eb7f9ca
·
1 Parent(s): a04369d

feat: add load script

Browse files
Files changed (1) hide show
  1. miners-detection.py +159 -0
miners-detection.py ADDED
@@ -0,0 +1,159 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from xml.etree import ElementTree as ET
2
+
3
+ import datasets
4
+
5
+ _CITATION = """\
6
+ @InProceedings{huggingface:dataset,
7
+ title = {miners-detection},
8
+ author = {TrainingDataPro},
9
+ year = {2023}
10
+ }
11
+ """
12
+
13
+ _DESCRIPTION = """\
14
+ The dataset consists of of photos captured within various mines, focusing on **miners**
15
+ engaged in their work. Each photo is annotated with bounding box detection of the
16
+ miners, an attribute highlights whether each miner is sitting or standing in the photo.
17
+
18
+ The dataset's diverse applications such as computer vision, safety assessment and others
19
+ make it a valuable resource for *researchers, employers, and policymakers in the mining
20
+ industry*.
21
+ """
22
+
23
+ _NAME = "miners-detection"
24
+
25
+ _HOMEPAGE = f"https://huggingface.co/datasets/TrainingDataPro/{_NAME}"
26
+
27
+ _LICENSE = ""
28
+
29
+ _DATA = f"https://huggingface.co/datasets/TrainingDataPro/{_NAME}/resolve/main/data/"
30
+
31
+ _LABELS = ["receipt", "shop", "item", "date_time", "total"]
32
+
33
+
34
+ class MinersDetection(datasets.GeneratorBasedBuilder):
35
+ def _info(self):
36
+ return datasets.DatasetInfo(
37
+ description=_DESCRIPTION,
38
+ features=datasets.Features(
39
+ {
40
+ "id": datasets.Value("int32"),
41
+ "name": datasets.Value("string"),
42
+ "image": datasets.Image(),
43
+ "mask": datasets.Image(),
44
+ "width": datasets.Value("uint16"),
45
+ "height": datasets.Value("uint16"),
46
+ "shapes": datasets.Sequence(
47
+ {
48
+ "label": datasets.ClassLabel(
49
+ num_classes=len(_LABELS),
50
+ names=_LABELS,
51
+ ),
52
+ "type": datasets.Value("string"),
53
+ "points": datasets.Sequence(
54
+ datasets.Sequence(
55
+ datasets.Value("float"),
56
+ ),
57
+ ),
58
+ "rotation": datasets.Value("float"),
59
+ "occluded": datasets.Value("uint8"),
60
+ "attributes": datasets.Sequence(
61
+ {
62
+ "name": datasets.Value("string"),
63
+ "text": datasets.Value("string"),
64
+ }
65
+ ),
66
+ }
67
+ ),
68
+ }
69
+ ),
70
+ supervised_keys=None,
71
+ homepage=_HOMEPAGE,
72
+ citation=_CITATION,
73
+ )
74
+
75
+ def _split_generators(self, dl_manager):
76
+ images = dl_manager.download(f"{_DATA}images.tar.gz")
77
+ masks = dl_manager.download(f"{_DATA}boxes.tar.gz")
78
+ annotations = dl_manager.download(f"{_DATA}annotations.xml")
79
+ images = dl_manager.iter_archive(images)
80
+ masks = dl_manager.iter_archive(masks)
81
+ return [
82
+ datasets.SplitGenerator(
83
+ name=datasets.Split.TRAIN,
84
+ gen_kwargs={
85
+ "images": images,
86
+ "masks": masks,
87
+ "annotations": annotations,
88
+ },
89
+ ),
90
+ ]
91
+
92
+ @staticmethod
93
+ def parse_shape(shape: ET.Element) -> dict:
94
+ label = shape.get("label")
95
+ shape_type = shape.tag
96
+ rotation = shape.get("rotation", 0.0)
97
+ occluded = shape.get("occluded", 0)
98
+
99
+ points = None
100
+
101
+ if shape_type == "points":
102
+ points = tuple(map(float, shape.get("points").split(",")))
103
+
104
+ elif shape_type == "box":
105
+ points = [
106
+ (float(shape.get("xtl")), float(shape.get("ytl"))),
107
+ (float(shape.get("xbr")), float(shape.get("ybr"))),
108
+ ]
109
+
110
+ elif shape_type == "polygon":
111
+ points = [
112
+ tuple(map(float, point.split(",")))
113
+ for point in shape.get("points").split(";")
114
+ ]
115
+
116
+ attributes = []
117
+
118
+ for attr in shape:
119
+ attr_name = attr.get("name")
120
+ attr_text = attr.text
121
+ attributes.append({"name": attr_name, "text": attr_text})
122
+
123
+ shape_data = {
124
+ "label": label,
125
+ "type": shape_type,
126
+ "points": points,
127
+ "rotation": rotation,
128
+ "occluded": occluded,
129
+ "attributes": attributes,
130
+ }
131
+
132
+ return shape_data
133
+
134
+ def _generate_examples(self, images, masks, annotations):
135
+ tree = ET.parse(annotations)
136
+ root = tree.getroot()
137
+
138
+ for idx, (
139
+ (image_path, image),
140
+ (mask_path, mask),
141
+ ) in enumerate(zip(images, masks)):
142
+ image_name = image_path.split("/")[-1]
143
+ img = root.find(f"./image[@name='images/{image_name}']")
144
+
145
+ image_id = img.get("id")
146
+ name = img.get("name")
147
+ width = img.get("width")
148
+ height = img.get("height")
149
+ shapes = [self.parse_shape(shape) for shape in img]
150
+
151
+ yield idx, {
152
+ "id": image_id,
153
+ "name": name,
154
+ "image": {"path": image_path, "bytes": image.read()},
155
+ "mask": {"path": mask_path, "bytes": mask.read()},
156
+ "width": width,
157
+ "height": height,
158
+ "shapes": shapes,
159
+ }