ShixuanAn commited on
Commit
3f895a0
1 Parent(s): a38cb50

Update RDD_2020.py

Browse files
Files changed (1) hide show
  1. RDD_2020.py +42 -41
RDD_2020.py CHANGED
@@ -81,69 +81,69 @@ class RDD2020_Dataset(datasets.GeneratorBasedBuilder):
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
- # Loop over each country directory in the images_dir
111
- for country_dir in os.listdir(images_dir):
112
- country_images_dir = os.path.join(images_dir, country_dir)
113
- country_annotations_dir = os.path.join(annotations_dir, country_dir, "xmls")
114
-
115
- # Now loop over each image in the country's image directory
116
- for image_file in os.listdir(country_images_dir):
 
 
 
117
  if not image_file.endswith('.jpg'):
118
  continue
119
- image_id = image_file.split('.')[0]
120
- annotation_file = image_id + '.xml'
121
- annotation_path = os.path.join(country_annotations_dir, annotation_file)
122
-
123
- if not os.path.exists(annotation_path):
124
- continue
125
-
126
- tree = ET.parse(annotation_path)
127
- root = tree.getroot()
128
-
129
  image_path = os.path.join(country_images_dir, image_file)
130
- crack_type = []
131
- crack_coordinates = []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
132
 
133
- for obj in root.findall('object'):
134
- crack_type.append(obj.find('name').text)
135
- bndbox = obj.find('bndbox')
136
- coordinates = {
137
- "x_min": int(bndbox.find('xmin').text),
138
- "x_max": int(bndbox.find('xmax').text),
139
- "y_min": int(bndbox.find('ymin').text),
140
- "y_max": int(bndbox.find('ymax').text),
141
- }
142
- crack_coordinates.append(coordinates)
143
-
144
- # Assuming images are of uniform size, you might want to adjust this or extract from image directly
145
- image_resolution = {"width": 600, "height": 600, "depth": 3} if country_dir != "India" else {"width": 720, "height": 720, "depth": 3}
146
-
147
  yield image_id, {
148
  "image_id": image_id,
149
  "country": country_dir,
@@ -153,4 +153,5 @@ class RDD2020_Dataset(datasets.GeneratorBasedBuilder):
153
  "crack_type": crack_type,
154
  "crack_coordinates": crack_coordinates,
155
  }
156
-
 
 
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"),
85
  "split": "train",
86
  },
87
  ),
88
  datasets.SplitGenerator(
89
+ name="test1",
90
  gen_kwargs={
91
  "images_dir": os.path.join(data_dir, "test1"),
92
+ "annotations_dir": None, # No annotations for test1
93
  "split": "test1",
94
  },
95
  ),
96
  datasets.SplitGenerator(
97
+ name="test2",
98
  gen_kwargs={
99
  "images_dir": os.path.join(data_dir, "test2"),
100
+ "annotations_dir": None, # No annotations for test2
101
  "split": "test2",
102
  },
103
  ),
104
  ]
105
 
 
 
106
  def _generate_examples(self, images_dir, annotations_dir, split):
107
  """Yields examples as (key, example) tuples."""
108
+ for country_dir in sorted(os.listdir(images_dir)): # Sort to ensure consistent order
109
+ country_images_dir = os.path.join(images_dir, country_dir, "images")
110
+
111
+ # Check if we are in a test split without annotations
112
+ if annotations_dir is None:
113
+ country_annotations_dir = None
114
+ else:
115
+ country_annotations_dir = os.path.join(annotations_dir, country_dir, "xmls")
116
+
117
+ for image_file in sorted(os.listdir(country_images_dir)): # Sort to ensure consistent order
118
  if not image_file.endswith('.jpg'):
119
  continue
120
+
121
+ image_id = f"{country_dir}_{image_file.split('.')[0]}"
 
 
 
 
 
 
 
 
122
  image_path = os.path.join(country_images_dir, image_file)
123
+ if country_annotations_dir:
124
+ annotation_file = image_id + '.xml'
125
+ annotation_path = os.path.join(country_annotations_dir, annotation_file)
126
+ if not os.path.exists(annotation_path):
127
+ continue
128
+ tree = ET.parse(annotation_path)
129
+ root = tree.getroot()
130
+ crack_type = []
131
+ crack_coordinates = []
132
+ for obj in root.findall('object'):
133
+ crack_type.append(obj.find('name').text)
134
+ bndbox = obj.find('bndbox')
135
+ coordinates = {
136
+ "x_min": int(bndbox.find('xmin').text),
137
+ "x_max": int(bndbox.find('xmax').text),
138
+ "y_min": int(bndbox.find('ymin').text),
139
+ "y_max": int(bndbox.find('ymax').text),
140
+ }
141
+ crack_coordinates.append(coordinates)
142
+ else:
143
+ crack_type = []
144
+ crack_coordinates = []
145
 
146
+ image_resolution = {"width": 600, "height": 600, "depth": 3}
 
 
 
 
 
 
 
 
 
 
 
 
 
147
  yield image_id, {
148
  "image_id": image_id,
149
  "country": country_dir,
 
153
  "crack_type": crack_type,
154
  "crack_coordinates": crack_coordinates,
155
  }
156
+
157
+