ShixuanAn commited on
Commit
8d3515f
1 Parent(s): f0da6d0

Update RDD_2020.py

Browse files
Files changed (1) hide show
  1. 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
- # Assuming the URLs are the GitHub raw URLs to the country directories within each split
79
- train_dir = "https://github.com/ShixuanAn/RDD_2020/tree/main/train"
80
- test1_dir = "https://github.com/ShixuanAn/RDD_2020/tree/main/test/test/test1"
81
- test2_dir = "https://github.com/ShixuanAn/RDD_2020/tree/main/test/test/test2"
 
 
 
 
 
 
 
82
 
83
  return [
84
  datasets.SplitGenerator(
85
  name=datasets.Split.TRAIN,
86
  gen_kwargs={
87
- "base_path": train_dir,
 
88
  "split": "train",
89
  },
90
  ),
91
  datasets.SplitGenerator(
92
- name="test1",
93
  gen_kwargs={
94
- "base_path": test1_dir,
 
95
  "split": "test1",
96
  },
97
  ),
98
  datasets.SplitGenerator(
99
- name="test2",
100
  gen_kwargs={
101
- "base_path": test2_dir,
 
102
  "split": "test2",
103
  },
104
  ),
105
  ]
106
 
107
- def _generate_examples(self, base_path, split):
108
- # Iterate over each country directory
109
- for country_dir in ['Czech', 'India', 'Japan']:
110
- images_dir = f"{base_path}/{country_dir}/images"
111
- annotations_dir = f"{base_path}/{country_dir}/annotations/xmls" if split == "train" else None
112
 
113
- # Iterate over each image in the country's image directory
114
- for image_file in os.listdir(images_dir):
115
  if not image_file.endswith('.jpg'):
116
  continue
 
 
 
 
 
 
117
 
118
- image_id = f"{country_dir}_{image_file.split('.')[0]}"
119
- image_path = os.path.join(images_dir, image_file)
120
- if annotations_dir:
121
- annotation_file = image_id + '.xml'
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
- image_resolution = {"width": 600, "height": 600, "depth": 3}
 
 
 
144
  yield image_id, {
145
  "image_id": image_id,
146
  "country": country_dir,
147
  "type": split,
148
  "image_resolution": image_resolution,
149
- "image_path": 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
  }