moukaii commited on
Commit
17d7c85
·
verified ·
1 Parent(s): 9c90d13

Update Tuberculosis_Dataset.py

Browse files
Files changed (1) hide show
  1. Tuberculosis_Dataset.py +20 -10
Tuberculosis_Dataset.py CHANGED
@@ -93,19 +93,29 @@ class TuberculosisDataset(GeneratorBasedBuilder):
93
 
94
  def _prepare_image_file_paths(self, images_zip_path):
95
  image_file_paths = {}
96
- # Extract the archive to a temporary directory
97
- with zipfile.ZipFile(images_zip_path, 'r') as zip_ref:
98
- zip_ref.extractall("temp_images")
99
- # Walk through the extracted files
100
- for root, dirs, files in os.walk("temp_images"):
 
 
 
 
 
 
 
 
 
 
101
  for file in files:
102
  if file.endswith('.jpg'):
103
  key = file.split('_')[0]
104
- # If the key is not already in the dictionary, add it with the current file's path
105
  if key not in image_file_paths:
106
  image_file_paths[key] = os.path.join(root, file)
107
- # No need for break here since we're checking if key is not in dictionary before adding
108
- # Cleanup extracted files after processing
109
- shutil.rmtree("temp_images")
110
- return image_file_paths
111
 
 
 
93
 
94
  def _prepare_image_file_paths(self, images_zip_path):
95
  image_file_paths = {}
96
+ temp_dir = "temp_images"
97
+
98
+ # Check if images_zip_path is a directory or a zip file
99
+ if os.path.isdir(images_zip_path):
100
+ base_path = images_zip_path
101
+ elif os.path.isfile(images_zip_path) and zipfile.is_zipfile(images_zip_path):
102
+ # Extract the zip file to a temporary directory
103
+ with zipfile.ZipFile(images_zip_path, 'r') as zip_ref:
104
+ zip_ref.extractall(temp_dir)
105
+ base_path = temp_dir
106
+ else:
107
+ raise ValueError("images_zip_path must be a directory or a zip file")
108
+
109
+ # Walk through the base_path
110
+ for root, _, files in os.walk(base_path):
111
  for file in files:
112
  if file.endswith('.jpg'):
113
  key = file.split('_')[0]
 
114
  if key not in image_file_paths:
115
  image_file_paths[key] = os.path.join(root, file)
116
+
117
+ # Cleanup if we extracted files
118
+ if base_path == temp_dir:
119
+ shutil.rmtree(temp_dir)
120
 
121
+ return image_file_paths