ShixuanAn commited on
Commit
5482b91
1 Parent(s): 8d102e2

Update RDD_2020.py

Browse files
Files changed (1) hide show
  1. 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
- """This method downloads/extracts the data and defines the splits."""
79
- data_dir = dl_manager.download_and_extract(_URLS["dataset"])
80
-
81
- return [
82
- datasets.SplitGenerator(
83
- name=datasets.Split.TRAIN,
84
- gen_kwargs={
85
- "images_dir": os.path.join(data_dir, "train"),
86
- "annotations_dir": os.path.join(data_dir, "train", "annotations"),
87
- "split": "train",
88
- },
89
- ),
90
- datasets.SplitGenerator(
91
- name="test1",
92
- gen_kwargs={
93
- "images_dir": os.path.join(data_dir, "test1"),
94
- "annotations_dir": os.path.join(data_dir, "test1", "annotations"),
95
- "split": "test1",
96
- },
97
- ),
98
- datasets.SplitGenerator(
99
- name="test2",
100
- gen_kwargs={
101
- "images_dir": os.path.join(data_dir, "test2"),
102
- "annotations_dir": os.path.join(data_dir, "test2", "annotations"),
103
- "split": "test2",
104
- },
105
- ),
106
- ]
107
 
108
 
109
 
110
  def _generate_examples(self, images_dir, annotations_dir, split):
111
- """Yields examples as (key, example) tuples."""
112
- for image_file in os.listdir(images_dir):
113
- if not image_file.endswith('.jpg'):
114
- continue
115
- image_id = image_file.split('.')[0]
116
- annotation_file = image_id + '.xml'
117
- annotation_path = os.path.join(annotations_dir, annotation_file)
118
-
119
- if not os.path.exists(annotation_path):
120
- continue
121
-
122
- tree = ET.parse(annotation_path)
123
- root = tree.getroot()
124
-
125
- country = split.capitalize()
126
- image_path = os.path.join(images_dir, image_file)
127
- crack_type = []
128
- crack_coordinates = []
129
-
130
- for obj in root.findall('object'):
131
- crack_type.append(obj.find('name').text)
132
- bndbox = obj.find('bndbox')
133
- coordinates = {
134
- "x_min": int(bndbox.find('xmin').text),
135
- "x_max": int(bndbox.find('xmax').text),
136
- "y_min": int(bndbox.find('ymin').text),
137
- "y_max": int(bndbox.find('ymax').text),
138
- }
139
- crack_coordinates.append(coordinates)
 
 
 
 
 
 
140
 
141
  # Assuming images are of uniform size, you might want to adjust this or extract from image directly
142
- image_resolution = {"width": 600, "height": 600, "depth": 3} if country != "India" else {"width": 720,
143
- "height": 720,
144
- "depth": 3}
145
- yield image_id, {
146
- "image_id": image_id,
147
- "country": country,
148
- "type": split,
149
- "image_resolution": image_resolution,
150
- "image_path": image_path,
151
- "crack_type": crack_type,
152
- "crack_coordinates": crack_coordinates,
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