Update RDD_2020.py
Browse files- RDD_2020.py +39 -45
RDD_2020.py
CHANGED
@@ -116,56 +116,50 @@ class RDD2020_Dataset(datasets.GeneratorBasedBuilder):
|
|
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 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
if
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
crack_type
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
|
|
|
|
|
|
|
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 |
+
# Load the image as a PIL Image object
|
131 |
+
with Image.open(image_path) as img:
|
132 |
+
# If you need to process the image, do it here
|
133 |
+
|
134 |
+
if annotations_dir:
|
135 |
+
annotation_file = image_id + '.xml'
|
136 |
+
annotation_path = os.path.join(annotations_dir, annotation_file)
|
137 |
+
if not os.path.exists(annotation_path):
|
138 |
+
continue
|
139 |
+
tree = ET.parse(annotation_path)
|
140 |
+
root = tree.getroot()
|
141 |
+
crack_type = []
|
142 |
+
crack_coordinates = []
|
143 |
+
for obj in root.findall('object'):
|
144 |
+
crack_type.append(obj.find('name').text)
|
145 |
+
bndbox = obj.find('bndbox')
|
146 |
+
coordinates = {
|
147 |
+
"x_min": int(bndbox.find('xmin').text),
|
148 |
+
"x_max": int(bndbox.find('xmax').text),
|
149 |
+
"y_min": int(bndbox.find('ymin').text),
|
150 |
+
"y_max": int(bndbox.find('ymax').text),
|
151 |
+
}
|
152 |
+
crack_coordinates.append(coordinates)
|
153 |
+
else:
|
154 |
+
crack_type = []
|
155 |
+
crack_coordinates = []
|
156 |
+
|
157 |
+
# Now 'img' is a PIL Image object, we keep it open and yield it
|
158 |
+
yield image_id, {
|
159 |
+
"image_id": image_id,
|
160 |
+
"country": country_dir,
|
161 |
+
"type": split,
|
162 |
+
"image": img.copy(), # We make a copy of the image object
|
163 |
+
"crack_type": crack_type,
|
164 |
+
"crack_coordinates": crack_coordinates,
|
165 |
+
}
|