Update RDD_2020.py
Browse files- RDD_2020.py +47 -41
RDD_2020.py
CHANGED
@@ -57,8 +57,8 @@ class RDD2020_Dataset(datasets.GeneratorBasedBuilder):
|
|
57 |
"height": datasets.Value("int32"),
|
58 |
"depth": datasets.Value("int32"),
|
59 |
}),
|
60 |
-
|
61 |
-
|
62 |
#"pics_array": datasets.Array3D(shape=(None, None, 3), dtype="uint8"),
|
63 |
"crack_type": datasets.Sequence(datasets.Value("string")),
|
64 |
"crack_coordinates": datasets.Sequence(datasets.Features({
|
@@ -116,50 +116,56 @@ class RDD2020_Dataset(datasets.GeneratorBasedBuilder):
|
|
116 |
|
117 |
|
118 |
def _generate_examples(self, filepath, split):
|
|
|
|
|
119 |
for country_dir in ['Czech', 'India', 'Japan']:
|
120 |
images_dir = f"{filepath}/{country_dir}/images"
|
|
|
|
|
|
|
|
|
|
|
121 |
annotations_dir = f"{filepath}/{country_dir}/annotations/xmls" if split == "train" else None
|
122 |
-
|
|
|
123 |
for image_file in os.listdir(images_dir):
|
124 |
if not image_file.endswith('.jpg'):
|
125 |
continue
|
126 |
-
|
127 |
image_id = f"{image_file.split('.')[0]}"
|
|
|
128 |
image_path = os.path.join(images_dir, image_file)
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
if
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
crack_type
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
bndbox
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
"crack_type": crack_type,
|
164 |
-
"crack_coordinates": crack_coordinates,
|
165 |
-
}
|
|
|
57 |
"height": datasets.Value("int32"),
|
58 |
"depth": datasets.Value("int32"),
|
59 |
}),
|
60 |
+
# "image": datasets.Value("string"),
|
61 |
+
"image_path": datasets.Value("string"),
|
62 |
#"pics_array": datasets.Array3D(shape=(None, None, 3), dtype="uint8"),
|
63 |
"crack_type": datasets.Sequence(datasets.Value("string")),
|
64 |
"crack_coordinates": datasets.Sequence(datasets.Features({
|
|
|
116 |
|
117 |
|
118 |
def _generate_examples(self, filepath, split):
|
119 |
+
|
120 |
+
# Iterate over each country directory
|
121 |
for country_dir in ['Czech', 'India', 'Japan']:
|
122 |
images_dir = f"{filepath}/{country_dir}/images"
|
123 |
+
|
124 |
+
# print(os.listdir(filepath))
|
125 |
+
# print(os.listdir(filepath))
|
126 |
+
# print(os.listdir(f"{filepath}/{country_dir}"))
|
127 |
+
|
128 |
annotations_dir = f"{filepath}/{country_dir}/annotations/xmls" if split == "train" else None
|
129 |
+
|
130 |
+
# Iterate over each image in the country's image directory
|
131 |
for image_file in os.listdir(images_dir):
|
132 |
if not image_file.endswith('.jpg'):
|
133 |
continue
|
134 |
+
|
135 |
image_id = f"{image_file.split('.')[0]}"
|
136 |
+
|
137 |
image_path = os.path.join(images_dir, image_file)
|
138 |
+
# img = Image.open(image_path)
|
139 |
+
|
140 |
+
if annotations_dir:
|
141 |
+
annotation_file = image_id + '.xml'
|
142 |
+
annotation_path = os.path.join(annotations_dir, annotation_file)
|
143 |
+
if not os.path.exists(annotation_path):
|
144 |
+
continue
|
145 |
+
tree = ET.parse(annotation_path)
|
146 |
+
root = tree.getroot()
|
147 |
+
crack_type = []
|
148 |
+
crack_coordinates = []
|
149 |
+
for obj in root.findall('object'):
|
150 |
+
crack_type.append(obj.find('name').text)
|
151 |
+
bndbox = obj.find('bndbox')
|
152 |
+
coordinates = {
|
153 |
+
"x_min": int(bndbox.find('xmin').text),
|
154 |
+
"x_max": int(bndbox.find('xmax').text),
|
155 |
+
"y_min": int(bndbox.find('ymin').text),
|
156 |
+
"y_max": int(bndbox.find('ymax').text),
|
157 |
+
}
|
158 |
+
crack_coordinates.append(coordinates)
|
159 |
+
else:
|
160 |
+
crack_type = []
|
161 |
+
crack_coordinates = []
|
162 |
+
|
163 |
+
yield image_id, {
|
164 |
+
"image_id": image_id,
|
165 |
+
"country": country_dir,
|
166 |
+
"type": split,
|
167 |
+
"image_path": image_path,
|
168 |
+
# "image": img,
|
169 |
+
"crack_type": crack_type,
|
170 |
+
"crack_coordinates": crack_coordinates,
|
171 |
+
}
|
|
|
|
|
|