feat: load script
Browse files
people-with-guns-segmentation-and-detection.py
ADDED
@@ -0,0 +1,160 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from xml.etree import ElementTree as ET
|
3 |
+
|
4 |
+
import datasets
|
5 |
+
|
6 |
+
_CITATION = """\
|
7 |
+
@InProceedings{huggingface:dataset,
|
8 |
+
title = {people-with-guns-segmentation-and-detection},
|
9 |
+
author = {TrainingDataPro},
|
10 |
+
year = {2023}
|
11 |
+
}
|
12 |
+
"""
|
13 |
+
|
14 |
+
_DESCRIPTION = """\
|
15 |
+
The dataset consists of photos depicting **individuals holding guns**. It specifically
|
16 |
+
focuses on the **segmentation** of guns within these images and the **detection** of
|
17 |
+
people holding guns.
|
18 |
+
Each image in the dataset presents a different scenario, capturing individuals from
|
19 |
+
various *backgrounds, genders, and age groups in different poses* while holding guns.
|
20 |
+
The dataset is an essential resource for the development and evaluation of computer
|
21 |
+
vision models and algorithms in fields related to *firearms recognition, security
|
22 |
+
systems, law enforcement, and safety analysis*.
|
23 |
+
"""
|
24 |
+
_NAME = "people-with-guns-segmentation-and-detection"
|
25 |
+
|
26 |
+
_HOMEPAGE = f"https://huggingface.co/datasets/TrainingDataPro/{_NAME}"
|
27 |
+
|
28 |
+
_LICENSE = ""
|
29 |
+
|
30 |
+
_DATA = f"https://huggingface.co/datasets/TrainingDataPro/{_NAME}/resolve/main/data/"
|
31 |
+
|
32 |
+
_LABELS = ["person", "gun"]
|
33 |
+
|
34 |
+
|
35 |
+
class PeopleWithGunsSegmentationAndDetection(datasets.GeneratorBasedBuilder):
|
36 |
+
BUILDER_CONFIGS = [
|
37 |
+
datasets.BuilderConfig(name=f"{_NAME}", data_dir=f"{_DATA}{_NAME}"),
|
38 |
+
]
|
39 |
+
|
40 |
+
DEFAULT_CONFIG_NAME = f"{_NAME}"
|
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 |
+
"width": datasets.Value("uint16"),
|
52 |
+
"height": datasets.Value("uint16"),
|
53 |
+
"shapes": datasets.Sequence(
|
54 |
+
{
|
55 |
+
"label": datasets.ClassLabel(
|
56 |
+
num_classes=len(_LABELS),
|
57 |
+
names=_LABELS,
|
58 |
+
),
|
59 |
+
"type": datasets.Value("string"),
|
60 |
+
"points": datasets.Sequence(
|
61 |
+
datasets.Sequence(
|
62 |
+
datasets.Value("float"),
|
63 |
+
),
|
64 |
+
),
|
65 |
+
"rotation": datasets.Value("float"),
|
66 |
+
"occluded": datasets.Value("uint8"),
|
67 |
+
"z_order": datasets.Value("int16"),
|
68 |
+
"attributes": datasets.Sequence(
|
69 |
+
{
|
70 |
+
"name": datasets.Value("string"),
|
71 |
+
"text": datasets.Value("string"),
|
72 |
+
}
|
73 |
+
),
|
74 |
+
}
|
75 |
+
),
|
76 |
+
}
|
77 |
+
),
|
78 |
+
supervised_keys=None,
|
79 |
+
homepage=_HOMEPAGE,
|
80 |
+
citation=_CITATION,
|
81 |
+
)
|
82 |
+
|
83 |
+
def _split_generators(self, dl_manager):
|
84 |
+
data = dl_manager.download_and_extract(self.config.data_dir)
|
85 |
+
return [
|
86 |
+
datasets.SplitGenerator(
|
87 |
+
name=datasets.Split.TRAIN,
|
88 |
+
gen_kwargs={
|
89 |
+
"data": data,
|
90 |
+
},
|
91 |
+
),
|
92 |
+
]
|
93 |
+
|
94 |
+
@staticmethod
|
95 |
+
def parse_shape(shape: ET.Element) -> dict:
|
96 |
+
label = shape.get("label")
|
97 |
+
shape_type = shape.tag
|
98 |
+
rotation = shape.get("rotation", 0.0)
|
99 |
+
occluded = shape.get("occluded", 0)
|
100 |
+
z_order = shape.get("z_order", 0)
|
101 |
+
|
102 |
+
points = None
|
103 |
+
|
104 |
+
if shape_type == "points":
|
105 |
+
points = tuple(map(float, shape.get("points").split(",")))
|
106 |
+
|
107 |
+
elif shape_type == "box":
|
108 |
+
points = [
|
109 |
+
(float(shape.get("xtl")), float(shape.get("ytl"))),
|
110 |
+
(float(shape.get("xbr")), float(shape.get("ybr"))),
|
111 |
+
]
|
112 |
+
|
113 |
+
elif shape_type == "polygon":
|
114 |
+
points = [
|
115 |
+
tuple(map(float, point.split(",")))
|
116 |
+
for point in shape.get("points").split(";")
|
117 |
+
]
|
118 |
+
|
119 |
+
attributes = []
|
120 |
+
|
121 |
+
for attr in shape:
|
122 |
+
attr_name = attr.get("name")
|
123 |
+
attr_text = attr.text
|
124 |
+
attributes.append({"name": attr_name, "text": attr_text})
|
125 |
+
|
126 |
+
shape_data = {
|
127 |
+
"label": label,
|
128 |
+
"type": shape_type,
|
129 |
+
"points": points,
|
130 |
+
"rotation": rotation,
|
131 |
+
"occluded": occluded,
|
132 |
+
"z_order": z_order,
|
133 |
+
"attributes": attributes,
|
134 |
+
}
|
135 |
+
|
136 |
+
return shape_data
|
137 |
+
|
138 |
+
def _generate_examples(self, data):
|
139 |
+
tree = ET.parse(os.path.join(data, "annotations.xml"))
|
140 |
+
root = tree.getroot()
|
141 |
+
|
142 |
+
for idx, file in enumerate(sorted(os.listdir(os.path.join(data, "images")))):
|
143 |
+
image_name = file.split("/")[-1]
|
144 |
+
img = root.find(f"./image[@name='images/{image_name}']")
|
145 |
+
|
146 |
+
image_id = img.get("id")
|
147 |
+
name = img.get("name")
|
148 |
+
width = img.get("width")
|
149 |
+
height = img.get("height")
|
150 |
+
shapes = [self.parse_shape(shape) for shape in img]
|
151 |
+
|
152 |
+
yield idx, {
|
153 |
+
"id": image_id,
|
154 |
+
"name": name,
|
155 |
+
"image": os.path.join(data, "images", file),
|
156 |
+
"mask": os.path.join(data, "masks", file),
|
157 |
+
"width": width,
|
158 |
+
"height": height,
|
159 |
+
"shapes": shapes,
|
160 |
+
}
|