vkapoor commited on
Commit
9c99040
·
1 Parent(s): d27aea0

Update ecoset.py

Browse files
Files changed (1) hide show
  1. ecoset.py +30 -22
ecoset.py CHANGED
@@ -118,7 +118,7 @@ class Ecoset(datasets.GeneratorBasedBuilder):
118
 
119
  def abslist(path):
120
  """Helper function to give abspaths of os.listdir"""
121
- return [op.join(path, p) for p in os.listdir(path)]
122
 
123
  def subprocess_call_print(command_list):
124
  """Execute a subprocess while printing the command line output"""
@@ -151,18 +151,18 @@ class Ecoset(datasets.GeneratorBasedBuilder):
151
  zip_file_path = os.path.join(target_dir, filename)
152
 
153
  # Use subprocess to execute the modified wget command.
154
- wget_command = f"wget --no-check-certificate {source_url} -O {zip_file_path}"
155
- subprocess.run(wget_command, shell=True)
156
 
157
 
158
  print(f"Downloaded {filename}")
159
  # unzip using platform-based subprocess
160
- if platform.system() in ("Linux", "Darwin"):
161
- #subprocess.call(["unzip", "-n", "-P", password.encode("ascii"), "-o", zip_path, "-d", target_dir], shell=False)
162
- subprocess_call_print(["unzip", "-n", "-P", password.encode("ascii"), "-o", zip_file_path, "-d", target_dir])
163
- else:
164
- #subprocess.call(["tar.exe", "-xf", zip_path, "-C", target_dir, "--passphrase", password], shell=False)
165
- subprocess_call_print(["tar.exe", "-xf", zip_file_path, "-C", target_dir, "--passphrase", password])
166
 
167
 
168
 
@@ -174,10 +174,12 @@ class Ecoset(datasets.GeneratorBasedBuilder):
174
  # create a dict containing all files
175
  split_dict = {split:[] for split in ("train", "val", "test")}
176
  for split in split_dict.keys():
177
- fnames = abslist(op.join(archives, split))
178
- for f in fnames:
179
- split_dict[split].extend(abslist(f))
180
-
 
 
181
  # return data splits
182
  return [datasets.SplitGenerator(
183
  name=datasets.Split.TRAIN,
@@ -206,13 +208,19 @@ class Ecoset(datasets.GeneratorBasedBuilder):
206
  def _generate_examples(self, archives, split):
207
  """Yields examples."""
208
  idx = 0
209
- for archive in archives:
210
- if any(archive.endswith(i) for i in (".JPEG", ".JPG", ".jpeg", ".jpg")):
211
-
 
 
 
 
212
  # extract file, label, etc
213
- file = open(archive, 'rb')
214
- synset_id, label = archive.split("/")[-2].split("_")
215
- ex = {"image": {"path": archive, "bytes": file.read()}, "label": label}
216
-
217
- yield idx, ex
218
- idx += 1
 
 
 
118
 
119
  def abslist(path):
120
  """Helper function to give abspaths of os.listdir"""
121
+ return [os.path.join(path, p) for p in os.listdir(path)if os.path.isdir(os.path.join(path, p))]
122
 
123
  def subprocess_call_print(command_list):
124
  """Execute a subprocess while printing the command line output"""
 
151
  zip_file_path = os.path.join(target_dir, filename)
152
 
153
  # Use subprocess to execute the modified wget command.
154
+ #wget_command = f"wget --no-check-certificate {source_url} -O {zip_file_path}"
155
+ #subprocess.run(wget_command, shell=True)
156
 
157
 
158
  print(f"Downloaded {filename}")
159
  # unzip using platform-based subprocess
160
+ #if platform.system() in ("Linux", "Darwin"):
161
+
162
+ #subprocess_call_print(["unzip", "-n", "-P", password.encode("ascii"), "-o", zip_file_path, "-d", target_dir])
163
+ #else:
164
+
165
+ #subprocess_call_print(["tar.exe", "-xf", zip_file_path, "-C", target_dir, "--passphrase", password])
166
 
167
 
168
 
 
174
  # create a dict containing all files
175
  split_dict = {split:[] for split in ("train", "val", "test")}
176
  for split in split_dict.keys():
177
+ # Get a list of file names in the current split's directory
178
+ fnames = abslist(os.path.join(archives, split))
179
+
180
+ # Extend the corresponding split's list in split_dict with the file names
181
+ split_dict[split].extend(fnames)
182
+
183
  # return data splits
184
  return [datasets.SplitGenerator(
185
  name=datasets.Split.TRAIN,
 
208
  def _generate_examples(self, archives, split):
209
  """Yields examples."""
210
  idx = 0
211
+ split_archives = os.path.join(archives, split)
212
+ print(split_archives)
213
+ acceptable_formats = [".JPEG", ".JPG", ".jpeg", ".jpg"]
214
+ for archive in os.listdir(split_archives):
215
+ print(archive)
216
+ if any(archive.endswith(f) for f in acceptable_formats):
217
+ print('in')
218
  # extract file, label, etc
219
+ file_path = os.path.join(split_archives, archive)
220
+ with open(file_path, 'rb') as file:
221
+ synset_id, label = archive.rsplit(os.path.sep, 2)[-2].split("_")
222
+
223
+ ex = {"image": {"path": archive, "bytes": file.read()}, "label": label}
224
+ print(ex)
225
+ yield idx, ex
226
+ idx += 1