ShixuanAn commited on
Commit
c836c23
1 Parent(s): 622ccd5

Update RDD_2020.py

Browse files
Files changed (1) hide show
  1. RDD_2020.py +39 -45
RDD_2020.py CHANGED
@@ -116,56 +116,50 @@ class RDD2020_Dataset(datasets.GeneratorBasedBuilder):
116
 
117
 
118
  def _generate_examples(self, filepath, split):
119
-
120
- # Iterate over each country directory
121
  for country_dir in ['Czech', 'India', 'Japan']:
122
  images_dir = f"{filepath}/{country_dir}/images"
123
-
124
- # print(os.listdir(filepath))
125
- # print(os.listdir(filepath))
126
- # print(os.listdir(f"{filepath}/{country_dir}"))
127
-
128
  annotations_dir = f"{filepath}/{country_dir}/annotations/xmls" if split == "train" else None
129
-
130
- # Iterate over each image in the country's image directory
131
  for image_file in os.listdir(images_dir):
132
  if not image_file.endswith('.jpg'):
133
  continue
134
-
135
  image_id = f"{image_file.split('.')[0]}"
136
-
137
  image_path = os.path.join(images_dir, image_file)
138
- # img = Image.open(image_path)
139
-
140
- if annotations_dir:
141
- annotation_file = image_id + '.xml'
142
- annotation_path = os.path.join(annotations_dir, annotation_file)
143
- if not os.path.exists(annotation_path):
144
- continue
145
- tree = ET.parse(annotation_path)
146
- root = tree.getroot()
147
- crack_type = []
148
- crack_coordinates = []
149
- for obj in root.findall('object'):
150
- crack_type.append(obj.find('name').text)
151
- bndbox = obj.find('bndbox')
152
- coordinates = {
153
- "x_min": int(bndbox.find('xmin').text),
154
- "x_max": int(bndbox.find('xmax').text),
155
- "y_min": int(bndbox.find('ymin').text),
156
- "y_max": int(bndbox.find('ymax').text),
157
- }
158
- crack_coordinates.append(coordinates)
159
- else:
160
- crack_type = []
161
- crack_coordinates = []
162
-
163
- yield image_id, {
164
- "image_id": image_id,
165
- "country": country_dir,
166
- "type": split,
167
- "image_path": image_path,
168
- # "image": img,
169
- "crack_type": crack_type,
170
- "crack_coordinates": crack_coordinates,
171
- }
 
 
 
 
116
 
117
 
118
  def _generate_examples(self, filepath, split):
 
 
119
  for country_dir in ['Czech', 'India', 'Japan']:
120
  images_dir = f"{filepath}/{country_dir}/images"
 
 
 
 
 
121
  annotations_dir = f"{filepath}/{country_dir}/annotations/xmls" if split == "train" else None
122
+
 
123
  for image_file in os.listdir(images_dir):
124
  if not image_file.endswith('.jpg'):
125
  continue
126
+
127
  image_id = f"{image_file.split('.')[0]}"
 
128
  image_path = os.path.join(images_dir, image_file)
129
+
130
+ # Load the image as a PIL Image object
131
+ with Image.open(image_path) as img:
132
+ # If you need to process the image, do it here
133
+
134
+ if annotations_dir:
135
+ annotation_file = image_id + '.xml'
136
+ annotation_path = os.path.join(annotations_dir, annotation_file)
137
+ if not os.path.exists(annotation_path):
138
+ continue
139
+ tree = ET.parse(annotation_path)
140
+ root = tree.getroot()
141
+ crack_type = []
142
+ crack_coordinates = []
143
+ for obj in root.findall('object'):
144
+ crack_type.append(obj.find('name').text)
145
+ bndbox = obj.find('bndbox')
146
+ coordinates = {
147
+ "x_min": int(bndbox.find('xmin').text),
148
+ "x_max": int(bndbox.find('xmax').text),
149
+ "y_min": int(bndbox.find('ymin').text),
150
+ "y_max": int(bndbox.find('ymax').text),
151
+ }
152
+ crack_coordinates.append(coordinates)
153
+ else:
154
+ crack_type = []
155
+ crack_coordinates = []
156
+
157
+ # Now 'img' is a PIL Image object, we keep it open and yield it
158
+ yield image_id, {
159
+ "image_id": image_id,
160
+ "country": country_dir,
161
+ "type": split,
162
+ "image": img.copy(), # We make a copy of the image object
163
+ "crack_type": crack_type,
164
+ "crack_coordinates": crack_coordinates,
165
+ }