Update RDD_2020.py
Browse files- RDD_2020.py +42 -31
RDD_2020.py
CHANGED
@@ -73,59 +73,70 @@ class RDD2020_Dataset(datasets.GeneratorBasedBuilder):
|
|
73 |
homepage='https://data.mendeley.com/datasets/5ty2wb6gvg/1',
|
74 |
citation=_CITATION,
|
75 |
)
|
76 |
-
|
77 |
def _split_generators(self, dl_manager):
|
78 |
-
#
|
79 |
-
|
80 |
-
|
81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
|
83 |
return [
|
84 |
datasets.SplitGenerator(
|
85 |
name=datasets.Split.TRAIN,
|
86 |
gen_kwargs={
|
87 |
-
"
|
|
|
88 |
"split": "train",
|
89 |
},
|
90 |
),
|
91 |
datasets.SplitGenerator(
|
92 |
-
name=
|
93 |
gen_kwargs={
|
94 |
-
"
|
|
|
95 |
"split": "test1",
|
96 |
},
|
97 |
),
|
98 |
datasets.SplitGenerator(
|
99 |
-
name=
|
100 |
gen_kwargs={
|
101 |
-
"
|
|
|
102 |
"split": "test2",
|
103 |
},
|
104 |
),
|
105 |
]
|
106 |
|
107 |
-
def _generate_examples(self,
|
108 |
-
#
|
109 |
-
for country_dir in
|
110 |
-
|
111 |
-
|
112 |
|
113 |
-
#
|
114 |
-
for image_file in os.listdir(
|
115 |
if not image_file.endswith('.jpg'):
|
116 |
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
117 |
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
annotation_path = os.path.join(annotations_dir, annotation_file)
|
123 |
-
if not os.path.exists(annotation_path):
|
124 |
-
continue
|
125 |
tree = ET.parse(annotation_path)
|
126 |
root = tree.getroot()
|
127 |
-
crack_type = []
|
128 |
-
crack_coordinates = []
|
129 |
for obj in root.findall('object'):
|
130 |
crack_type.append(obj.find('name').text)
|
131 |
bndbox = obj.find('bndbox')
|
@@ -136,17 +147,17 @@ class RDD2020_Dataset(datasets.GeneratorBasedBuilder):
|
|
136 |
"y_max": int(bndbox.find('ymax').text),
|
137 |
}
|
138 |
crack_coordinates.append(coordinates)
|
139 |
-
else:
|
140 |
-
crack_type = []
|
141 |
-
crack_coordinates = []
|
142 |
|
143 |
-
|
|
|
|
|
|
|
144 |
yield image_id, {
|
145 |
"image_id": image_id,
|
146 |
"country": country_dir,
|
147 |
"type": split,
|
148 |
"image_resolution": image_resolution,
|
149 |
-
"image_path":
|
150 |
"crack_type": crack_type,
|
151 |
"crack_coordinates": crack_coordinates,
|
152 |
}
|
|
|
73 |
homepage='https://data.mendeley.com/datasets/5ty2wb6gvg/1',
|
74 |
citation=_CITATION,
|
75 |
)
|
76 |
+
|
77 |
def _split_generators(self, dl_manager):
|
78 |
+
# The direct links to the zipped files on Hugging Face
|
79 |
+
urls_to_download = {
|
80 |
+
"train": "https://huggingface.co/datasets/ShixuanAn/RDD2020/blob/main/train.zip",
|
81 |
+
"test1": "https://huggingface.co/datasets/ShixuanAn/RDD2020/blob/main/test1.zip",
|
82 |
+
"test2": "https://huggingface.co/datasets/ShixuanAn/RDD2020/blob/main/test2.zip",
|
83 |
+
}
|
84 |
+
|
85 |
+
# Download and extract the dataset using the dl_manager
|
86 |
+
downloaded_files = {
|
87 |
+
key: dl_manager.download_and_extract(url) for key, url in urls_to_download.items()
|
88 |
+
}
|
89 |
|
90 |
return [
|
91 |
datasets.SplitGenerator(
|
92 |
name=datasets.Split.TRAIN,
|
93 |
gen_kwargs={
|
94 |
+
"images_dir": os.path.join(downloaded_files["train"], "images"),
|
95 |
+
"annotations_dir": os.path.join(downloaded_files["train"], "annotations", "xmls"),
|
96 |
"split": "train",
|
97 |
},
|
98 |
),
|
99 |
datasets.SplitGenerator(
|
100 |
+
name=datasets.Split.TEST,
|
101 |
gen_kwargs={
|
102 |
+
"images_dir": os.path.join(downloaded_files["test1"], "images"),
|
103 |
+
"annotations_dir": None, # No annotations for test1
|
104 |
"split": "test1",
|
105 |
},
|
106 |
),
|
107 |
datasets.SplitGenerator(
|
108 |
+
name=datasets.Split.VALIDATION,
|
109 |
gen_kwargs={
|
110 |
+
"images_dir": os.path.join(downloaded_files["test2"], "images"),
|
111 |
+
"annotations_dir": None, # No annotations for test2
|
112 |
"split": "test2",
|
113 |
},
|
114 |
),
|
115 |
]
|
116 |
|
117 |
+
def _generate_examples(self, images_dir, annotations_dir, split):
|
118 |
+
# Loop over each country directory in the images_dir
|
119 |
+
for country_dir in os.listdir(images_dir):
|
120 |
+
country_images_dir = os.path.join(images_dir, country_dir)
|
121 |
+
country_annotations_dir = os.path.join(annotations_dir, country_dir, "xmls") if annotations_dir else None
|
122 |
|
123 |
+
# Now loop over each image in the country's image directory
|
124 |
+
for image_file in os.listdir(country_images_dir):
|
125 |
if not image_file.endswith('.jpg'):
|
126 |
continue
|
127 |
+
image_id = image_file.split('.')[0]
|
128 |
+
annotation_file = image_id + '.xml'
|
129 |
+
annotation_path = os.path.join(country_annotations_dir, annotation_file) if country_annotations_dir else None
|
130 |
+
|
131 |
+
if annotation_path and not os.path.exists(annotation_path):
|
132 |
+
continue
|
133 |
|
134 |
+
# Parse the XML file for annotations if it exists
|
135 |
+
crack_type = []
|
136 |
+
crack_coordinates = []
|
137 |
+
if annotation_path:
|
|
|
|
|
|
|
138 |
tree = ET.parse(annotation_path)
|
139 |
root = tree.getroot()
|
|
|
|
|
140 |
for obj in root.findall('object'):
|
141 |
crack_type.append(obj.find('name').text)
|
142 |
bndbox = obj.find('bndbox')
|
|
|
147 |
"y_max": int(bndbox.find('ymax').text),
|
148 |
}
|
149 |
crack_coordinates.append(coordinates)
|
|
|
|
|
|
|
150 |
|
151 |
+
# Assuming images are of uniform size, you might want to adjust this or extract from image directly
|
152 |
+
image_resolution = {"width": 600, "height": 600, "depth": 3} if country_dir != "India" else {"width": 720, "height": 720, "depth": 3}
|
153 |
+
|
154 |
+
# Yield the example as a key, value pair
|
155 |
yield image_id, {
|
156 |
"image_id": image_id,
|
157 |
"country": country_dir,
|
158 |
"type": split,
|
159 |
"image_resolution": image_resolution,
|
160 |
+
"image_path": os.path.join(country_images_dir, image_file),
|
161 |
"crack_type": crack_type,
|
162 |
"crack_coordinates": crack_coordinates,
|
163 |
}
|