ShixuanAn commited on
Commit
de8e778
1 Parent(s): cd0832f

Update RDD_2020.py

Browse files
Files changed (1) hide show
  1. RDD_2020.py +36 -34
RDD_2020.py CHANGED
@@ -140,37 +140,39 @@ class RDD2020_Dataset(datasets.GeneratorBasedBuilder):
140
  image_id = f"{image_file.split('.')[0]}"
141
 
142
  image_path = os.path.join(images_dir, image_file)
143
- img = Image.open(image_path)
144
-
145
- if annotations_dir:
146
- annotation_file = image_id + '.xml'
147
- annotation_path = os.path.join(annotations_dir, annotation_file)
148
- if not os.path.exists(annotation_path):
149
- continue
150
- tree = ET.parse(annotation_path)
151
- root = tree.getroot()
152
- crack_type = []
153
- crack_coordinates = []
154
- for obj in root.findall('object'):
155
- crack_type.append(obj.find('name').text)
156
- bndbox = obj.find('bndbox')
157
- coordinates = {
158
- "x_min": int(bndbox.find('xmin').text),
159
- "x_max": int(bndbox.find('xmax').text),
160
- "y_min": int(bndbox.find('ymin').text),
161
- "y_max": int(bndbox.find('ymax').text),
162
- }
163
- crack_coordinates.append(coordinates)
164
- else:
165
- crack_type = []
166
- crack_coordinates = []
167
-
168
- yield image_id, {
169
- "image_id": image_id,
170
- "country": country_dir,
171
- "type": split,
172
- "image": img,
173
- "image_path": image_path,
174
- "crack_type": crack_type,
175
- "crack_coordinates": crack_coordinates,
176
- }
 
 
 
140
  image_id = f"{image_file.split('.')[0]}"
141
 
142
  image_path = os.path.join(images_dir, image_file)
143
+ # Load the image as a PIL Image object
144
+ with Image.open(image_path) as img:
145
+ img.load() # This is important to load the image data
146
+
147
+ if annotations_dir:
148
+ annotation_file = image_id + '.xml'
149
+ annotation_path = os.path.join(annotations_dir, annotation_file)
150
+ if not os.path.exists(annotation_path):
151
+ continue
152
+ tree = ET.parse(annotation_path)
153
+ root = tree.getroot()
154
+ crack_type = []
155
+ crack_coordinates = []
156
+ for obj in root.findall('object'):
157
+ crack_type.append(obj.find('name').text)
158
+ bndbox = obj.find('bndbox')
159
+ coordinates = {
160
+ "x_min": int(bndbox.find('xmin').text),
161
+ "x_max": int(bndbox.find('xmax').text),
162
+ "y_min": int(bndbox.find('ymin').text),
163
+ "y_max": int(bndbox.find('ymax').text),
164
+ }
165
+ crack_coordinates.append(coordinates)
166
+ else:
167
+ crack_type = []
168
+ crack_coordinates = []
169
+
170
+ # 将 PIL 图像对象添加到生成的字典中
171
+ yield image_id, {
172
+ "image_id": image_id,
173
+ "country": country_dir,
174
+ "type": split,
175
+ "image": img, # PIL 图像对象
176
+ "crack_type": crack_type,
177
+ "crack_coordinates": crack_coordinates,
178
+ }