lisawen commited on
Commit
57efcc7
1 Parent(s): 23506a6

Update soybean_dataset.py

Browse files
Files changed (1) hide show
  1. soybean_dataset.py +30 -26
soybean_dataset.py CHANGED
@@ -21,7 +21,7 @@ import os
21
  from typing import List
22
  import datasets
23
  import logging
24
- import zipfile
25
  import numpy as np
26
  from PIL import Image
27
  import os
@@ -127,33 +127,37 @@ class SoybeanDataset(datasets.GeneratorBasedBuilder):
127
 
128
 
129
  def _generate_examples(self, filepath):
130
- logging.info("generating examples from = %s", filepath)
131
- # Open the zip file
132
- with zipfile.ZipFile(filepath, 'r') as zip_ref:
133
- # List all the contents of the zip file
134
- zip_list = zip_ref.namelist()
 
 
 
 
 
 
 
 
 
 
 
135
 
136
- # Create pairs of original and segmentation images
137
- images_pairs = [(f, f.replace('_original.jpg', '_segmentation.png')) for f in zip_list if '_original.jpg' in f]
138
 
139
- # Iterate over the pairs and yield examples
140
- for original_image_name, segmentation_image_name in images_pairs:
141
- # The unique_id is derived from the original image name by stripping away the file extension and the suffix
142
- unique_id = original_image_name.split('_')[0]
143
-
144
- # Extract and process the original image
145
- with zip_ref.open(original_image_name) as original_file:
146
- original_image = Image.open(original_file)
147
-
148
- # Extract and process the segmentation image
149
- with zip_ref.open(segmentation_image_name) as segmentation_file:
150
- segmentation_image = Image.open(segmentation_file)
151
-
152
- yield unique_id, {
153
- "original_image": original_image,
154
- "segmentation_image": segmentation_image,
155
- }
156
-
157
 
158
 
159
 
 
21
  from typing import List
22
  import datasets
23
  import logging
24
+
25
  import numpy as np
26
  from PIL import Image
27
  import os
 
127
 
128
 
129
  def _generate_examples(self, filepath):
130
+ logging.info("Generating examples from = %s", filepath)
131
+
132
+ # Initialize a counter for unique ID
133
+ unique_id = 0
134
+
135
+ # Get the list of files in the directory
136
+ file_list = os.listdir(filepath)
137
+
138
+ # Create pairs of original and segmentation images
139
+ images_pairs = [(f, f.replace('_original.jpg', '_segmentation.png')) for f in file_list if '_original.jpg' in f]
140
+
141
+ # Iterate over the pairs and yield examples
142
+ for original_image_name, segmentation_image_name in images_pairs:
143
+ # Construct the full path to the original and segmentation images
144
+ original_image_path = os.path.join(filepath, original_image_name)
145
+ segmentation_image_path = os.path.join(filepath, segmentation_image_name)
146
 
147
+ # Open the original image
148
+ original_image = Image.open(original_image_path)
149
 
150
+ # Open the segmentation image
151
+ segmentation_image = Image.open(segmentation_image_path)
152
+
153
+ # Yield the current example
154
+ yield unique_id, {
155
+ "original_image": original_image,
156
+ "segmentation_image": segmentation_image,
157
+ }
158
+
159
+ # Increment the unique ID for the next example
160
+ unique_id += 1
 
 
 
 
 
 
 
161
 
162
 
163