Update Tuberculosis_Dataset.py
Browse files- 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 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 108 |
-
# Cleanup extracted files
|
| 109 |
-
|
| 110 |
-
|
| 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
|