Update RDD_2020.py
Browse files- RDD_2020.py +73 -71
RDD_2020.py
CHANGED
@@ -75,81 +75,83 @@ class RDD2020_Dataset(datasets.GeneratorBasedBuilder):
|
|
75 |
)
|
76 |
|
77 |
def _split_generators(self, dl_manager):
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
),
|
106 |
-
]
|
107 |
|
108 |
|
109 |
|
110 |
def _generate_examples(self, images_dir, annotations_dir, split):
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
140 |
|
141 |
# Assuming images are of uniform size, you might want to adjust this or extract from image directly
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
}
|
154 |
-
|
155 |
|
|
|
75 |
)
|
76 |
|
77 |
def _split_generators(self, dl_manager):
|
78 |
+
data_dir = dl_manager.download_and_extract(_URLS["dataset"])
|
79 |
+
return [
|
80 |
+
datasets.SplitGenerator(
|
81 |
+
name=datasets.Split.TRAIN,
|
82 |
+
gen_kwargs={
|
83 |
+
"images_dir": os.path.join(data_dir, "train"),
|
84 |
+
"annotations_dir": os.path.join(data_dir, "train", "annotations", "xmls"),
|
85 |
+
"split": "train",
|
86 |
+
},
|
87 |
+
),
|
88 |
+
datasets.SplitGenerator(
|
89 |
+
name=datasets.Split.TEST,
|
90 |
+
gen_kwargs={
|
91 |
+
"images_dir": os.path.join(data_dir, "test1"),
|
92 |
+
"annotations_dir": os.path.join(data_dir, "test1", "annotations", "xmls"),
|
93 |
+
"split": "test1",
|
94 |
+
},
|
95 |
+
),
|
96 |
+
datasets.SplitGenerator(
|
97 |
+
name=datasets.Split.VALIDATION,
|
98 |
+
gen_kwargs={
|
99 |
+
"images_dir": os.path.join(data_dir, "test2"),
|
100 |
+
"annotations_dir": os.path.join(data_dir, "test2", "annotations", "xmls"),
|
101 |
+
"split": "test2",
|
102 |
+
},
|
103 |
+
),
|
104 |
+
]
|
|
|
|
|
105 |
|
106 |
|
107 |
|
108 |
def _generate_examples(self, images_dir, annotations_dir, split):
|
109 |
+
"""Yields examples as (key, example) tuples."""
|
110 |
+
|
111 |
+
# Loop over each country directory in the images_dir
|
112 |
+
for country_dir in os.listdir(images_dir):
|
113 |
+
country_images_dir = os.path.join(images_dir, country_dir)
|
114 |
+
country_annotations_dir = os.path.join(annotations_dir, country_dir, "xmls")
|
115 |
+
|
116 |
+
# Now loop over each image in the country's image directory
|
117 |
+
for image_file in os.listdir(country_images_dir):
|
118 |
+
if not image_file.endswith('.jpg'):
|
119 |
+
continue
|
120 |
+
image_id = image_file.split('.')[0]
|
121 |
+
annotation_file = image_id + '.xml'
|
122 |
+
annotation_path = os.path.join(country_annotations_dir, annotation_file)
|
123 |
+
|
124 |
+
if not os.path.exists(annotation_path):
|
125 |
+
continue
|
126 |
+
|
127 |
+
tree = ET.parse(annotation_path)
|
128 |
+
root = tree.getroot()
|
129 |
+
|
130 |
+
image_path = os.path.join(country_images_dir, image_file)
|
131 |
+
crack_type = []
|
132 |
+
crack_coordinates = []
|
133 |
+
|
134 |
+
for obj in root.findall('object'):
|
135 |
+
crack_type.append(obj.find('name').text)
|
136 |
+
bndbox = obj.find('bndbox')
|
137 |
+
coordinates = {
|
138 |
+
"x_min": int(bndbox.find('xmin').text),
|
139 |
+
"x_max": int(bndbox.find('xmax').text),
|
140 |
+
"y_min": int(bndbox.find('ymin').text),
|
141 |
+
"y_max": int(bndbox.find('ymax').text),
|
142 |
+
}
|
143 |
+
crack_coordinates.append(coordinates)
|
144 |
|
145 |
# Assuming images are of uniform size, you might want to adjust this or extract from image directly
|
146 |
+
image_resolution = {"width": 600, "height": 600, "depth": 3} if country_dir != "India" else {"width": 720, "height": 720, "depth": 3}
|
147 |
+
|
148 |
+
yield image_id, {
|
149 |
+
"image_id": image_id,
|
150 |
+
"country": country_dir,
|
151 |
+
"type": split,
|
152 |
+
"image_resolution": image_resolution,
|
153 |
+
"image_path": image_path,
|
154 |
+
"crack_type": crack_type,
|
155 |
+
"crack_coordinates": crack_coordinates,
|
156 |
+
}
|
|
|
|
|
157 |
|