Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,6 +10,8 @@ import numpy as np
|
|
| 10 |
from PIL import Image
|
| 11 |
import torch
|
| 12 |
from huggingface_hub import HfApi
|
|
|
|
|
|
|
| 13 |
sys.path.append('Utils')
|
| 14 |
sys.path.append('model')
|
| 15 |
from model.CBAM.reunet_cbam import reunet_cbam
|
|
@@ -19,8 +21,21 @@ from Utils.area import pixel_to_sqft, process_and_overlay_image
|
|
| 19 |
from split_merge import split, merge
|
| 20 |
from Utils.convert import read_pansharpened_rgb
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
# Define base directory for Hugging Face Spaces
|
| 23 |
-
BASE_DIR = "data"
|
| 24 |
|
| 25 |
# Define subdirectories
|
| 26 |
UPLOAD_DIR = os.path.join(BASE_DIR, "uploaded_images")
|
|
@@ -33,18 +48,6 @@ CSV_LOG_PATH = os.path.join(BASE_DIR, "image_log.csv")
|
|
| 33 |
for directory in [UPLOAD_DIR, MASK_DIR, PATCHES_DIR, PRED_PATCHES_DIR]:
|
| 34 |
os.makedirs(directory, exist_ok=True)
|
| 35 |
|
| 36 |
-
# Initialize Hugging Face API
|
| 37 |
-
hf_api = HfApi()
|
| 38 |
-
|
| 39 |
-
# Get the token from environment variable
|
| 40 |
-
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 41 |
-
if not HF_TOKEN:
|
| 42 |
-
st.error("HF_TOKEN not found in environment variables. Please set it in your Space settings.")
|
| 43 |
-
st.stop()
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
REPO_ID = "Pavan2k4/Building_area"
|
| 47 |
-
|
| 48 |
# Load model
|
| 49 |
@st.cache_resource
|
| 50 |
def load_model():
|
|
@@ -66,11 +69,14 @@ def save_to_hf_repo(local_path, repo_path):
|
|
| 66 |
path_or_fileobj=local_path,
|
| 67 |
path_in_repo=repo_path,
|
| 68 |
repo_id=REPO_ID,
|
|
|
|
| 69 |
token=HF_TOKEN
|
| 70 |
)
|
| 71 |
st.success(f"File uploaded successfully to {repo_path}")
|
| 72 |
except Exception as e:
|
| 73 |
st.error(f"Error uploading file: {str(e)}")
|
|
|
|
|
|
|
| 74 |
|
| 75 |
def log_image_details(image_id, image_filename, mask_filename):
|
| 76 |
file_exists = os.path.exists(CSV_LOG_PATH)
|
|
@@ -258,7 +264,7 @@ def result_page():
|
|
| 258 |
original_np = cv2.imread(original_img_path)
|
| 259 |
mask_np = cv2.imread(mask_path, cv2.IMREAD_GRAYSCALE)
|
| 260 |
|
| 261 |
-
#
|
| 262 |
_, mask_np = cv2.threshold(mask_np, 127, 255, cv2.THRESH_BINARY)
|
| 263 |
|
| 264 |
# Resize mask to match original image size if necessary
|
|
|
|
| 10 |
from PIL import Image
|
| 11 |
import torch
|
| 12 |
from huggingface_hub import HfApi
|
| 13 |
+
|
| 14 |
+
# Adjust import paths as needed
|
| 15 |
sys.path.append('Utils')
|
| 16 |
sys.path.append('model')
|
| 17 |
from model.CBAM.reunet_cbam import reunet_cbam
|
|
|
|
| 21 |
from split_merge import split, merge
|
| 22 |
from Utils.convert import read_pansharpened_rgb
|
| 23 |
|
| 24 |
+
# Initialize Hugging Face API
|
| 25 |
+
hf_api = HfApi()
|
| 26 |
+
|
| 27 |
+
# Get the token from secrets
|
| 28 |
+
HF_TOKEN = st.secrets.get("HF_TOKEN")
|
| 29 |
+
if not HF_TOKEN:
|
| 30 |
+
st.error("HF_TOKEN not found in secrets. Please set it in your Space's Configuration > Secrets.")
|
| 31 |
+
st.stop()
|
| 32 |
+
|
| 33 |
+
# Your Space ID (this should match exactly with your Hugging Face Space URL)
|
| 34 |
+
REPO_ID = "Pavan2k4/Building_area"
|
| 35 |
+
REPO_TYPE = "space"
|
| 36 |
+
|
| 37 |
# Define base directory for Hugging Face Spaces
|
| 38 |
+
BASE_DIR = "data/"
|
| 39 |
|
| 40 |
# Define subdirectories
|
| 41 |
UPLOAD_DIR = os.path.join(BASE_DIR, "uploaded_images")
|
|
|
|
| 48 |
for directory in [UPLOAD_DIR, MASK_DIR, PATCHES_DIR, PRED_PATCHES_DIR]:
|
| 49 |
os.makedirs(directory, exist_ok=True)
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
# Load model
|
| 52 |
@st.cache_resource
|
| 53 |
def load_model():
|
|
|
|
| 69 |
path_or_fileobj=local_path,
|
| 70 |
path_in_repo=repo_path,
|
| 71 |
repo_id=REPO_ID,
|
| 72 |
+
repo_type=REPO_TYPE,
|
| 73 |
token=HF_TOKEN
|
| 74 |
)
|
| 75 |
st.success(f"File uploaded successfully to {repo_path}")
|
| 76 |
except Exception as e:
|
| 77 |
st.error(f"Error uploading file: {str(e)}")
|
| 78 |
+
st.error("Detailed error information:")
|
| 79 |
+
st.exception(e)
|
| 80 |
|
| 81 |
def log_image_details(image_id, image_filename, mask_filename):
|
| 82 |
file_exists = os.path.exists(CSV_LOG_PATH)
|
|
|
|
| 264 |
original_np = cv2.imread(original_img_path)
|
| 265 |
mask_np = cv2.imread(mask_path, cv2.IMREAD_GRAYSCALE)
|
| 266 |
|
| 267 |
+
# Ensure mask is binary
|
| 268 |
_, mask_np = cv2.threshold(mask_np, 127, 255, cv2.THRESH_BINARY)
|
| 269 |
|
| 270 |
# Resize mask to match original image size if necessary
|