Spaces:
Sleeping
Sleeping
Update utils.py
Browse files
utils.py
CHANGED
|
@@ -1,41 +1,28 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
mask = Image.open(mask_path).convert("L")
|
| 30 |
-
mask_np = np.array(mask)
|
| 31 |
-
|
| 32 |
-
if np.any(mask_np > 0): # keep only if mask has buildings
|
| 33 |
-
valid_images.append({
|
| 34 |
-
"image_a": os.path.join(test_a, img_name),
|
| 35 |
-
"image_b": os.path.join(test_b, img_name),
|
| 36 |
-
"mask": mask_path
|
| 37 |
-
})
|
| 38 |
-
return valid_images
|
| 39 |
-
|
| 40 |
-
def get_random_valid_image(valid_images):
|
| 41 |
-
return random.choice(valid_images)
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import zipfile
|
| 3 |
+
import shutil
|
| 4 |
+
|
| 5 |
+
def setup_dataset(zip_path="test.zip", extract_path="./dataset"):
|
| 6 |
+
"""Extract dataset if not already extracted"""
|
| 7 |
+
dataset_path = os.path.join(extract_path, "test")
|
| 8 |
+
|
| 9 |
+
if not os.path.exists(dataset_path):
|
| 10 |
+
print("Extracting dataset...")
|
| 11 |
+
os.makedirs(extract_path, exist_ok=True)
|
| 12 |
+
|
| 13 |
+
try:
|
| 14 |
+
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
|
| 15 |
+
zip_ref.extractall(extract_path)
|
| 16 |
+
print("Dataset extracted successfully!")
|
| 17 |
+
except Exception as e:
|
| 18 |
+
print(f"Error extracting dataset: {e}")
|
| 19 |
+
return None
|
| 20 |
+
|
| 21 |
+
return dataset_path
|
| 22 |
+
|
| 23 |
+
def cleanup_cache():
|
| 24 |
+
"""Clean up any temporary files"""
|
| 25 |
+
cache_dirs = ['__pycache__', '.ipynb_checkpoints']
|
| 26 |
+
for cache_dir in cache_dirs:
|
| 27 |
+
if os.path.exists(cache_dir):
|
| 28 |
+
shutil.rmtree(cache_dir)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|