lisawen commited on
Commit
06eb2ae
1 Parent(s): 0592f99

Update soybean_dataset.py

Browse files
Files changed (1) hide show
  1. soybean_dataset.py +21 -27
soybean_dataset.py CHANGED
@@ -122,40 +122,33 @@ class SoybeanDataset(datasets.GeneratorBasedBuilder):
122
  name=datasets.Split.VALIDATION, gen_kwargs={"filepath": downloaded_files["valid"]}),
123
  ]
124
 
125
- def background(f):
126
- @wraps(f)
127
- def wrapped(*args, **kwargs):
128
- loop = asyncio.get_event_loop()
129
- if loop.is_running():
130
- # If the loop is already running, we can't use run_in_executor directly.
131
- # You would need to ensure this is handled correctly depending on your application's architecture.
132
- raise RuntimeError("Asyncio loop already running")
133
- return loop.run_in_executor(None, f, *args, **kwargs)
134
- return wrapped
135
-
136
- @background
137
- # Define the download function that returns the response object
138
- def download(self,url):
139
- r = requests.get(url)
140
- r.raise_for_status() # This will raise an exception if there is a download error
141
- return r
142
-
143
- # Define the process_image function that uses the response from download
144
- def process_image(self, image_url):
145
- response = self.download(image_url)
146
- img = Image.open(BytesIO(response.content))
147
- return img
148
 
 
 
 
149
 
150
-
151
-
152
  def _generate_examples(self, filepath):
153
  #"""Yields examples as (key, example) tuples."""
154
  logging.info("generating examples from = %s", filepath)
155
-
 
 
 
 
 
 
 
 
 
 
 
 
156
  with open(filepath, encoding="utf-8") as f:
157
  data = csv.DictReader(f)
158
-
159
 
160
  for row in data:
161
  # Assuming the 'original_image' column has the full path to the image file
@@ -178,6 +171,7 @@ class SoybeanDataset(datasets.GeneratorBasedBuilder):
178
  # ... add other features if necessary
179
  }
180
 
 
181
 
182
 
183
 
 
122
  name=datasets.Split.VALIDATION, gen_kwargs={"filepath": downloaded_files["valid"]}),
123
  ]
124
 
125
+ def process_image(self,image_url):
126
+ response = requests.get(image_url)
127
+ response.raise_for_status() # This will raise an exception if there is a download error
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
128
 
129
+ # Open the image from the downloaded bytes and return the PIL Image
130
+ img = Image.open(BytesIO(response.content))
131
+ return img
132
 
 
 
133
  def _generate_examples(self, filepath):
134
  #"""Yields examples as (key, example) tuples."""
135
  logging.info("generating examples from = %s", filepath)
136
+ with ThreadPoolExecutor(max_workers=5) as executor:
137
+ # Create a future to image mapping
138
+ future_to_image = {executor.submit(self.process_image, row['original_image']): row for row in data}
139
+
140
+ for future in as_completed(future_to_image):
141
+ row = future_to_image[future]
142
+ try:
143
+ original_image = future.result() # This will block until the image is ready
144
+ # ... do something with the original_image ...
145
+ except Exception as e:
146
+ logging.error(f"Error processing image: {e}")
147
+ continue
148
+
149
  with open(filepath, encoding="utf-8") as f:
150
  data = csv.DictReader(f)
151
+
152
 
153
  for row in data:
154
  # Assuming the 'original_image' column has the full path to the image file
 
171
  # ... add other features if necessary
172
  }
173
 
174
+
175
 
176
 
177