Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -18,7 +18,7 @@ from model.CBAM.reunet_cbam import reunet_cbam
|
|
| 18 |
from model.transform import transforms
|
| 19 |
from model.unet import UNET
|
| 20 |
from Utils.area import pixel_to_sqft, process_and_overlay_image
|
| 21 |
-
|
| 22 |
from Utils.convert import read_pansharpened_rgb
|
| 23 |
|
| 24 |
# Initialize Hugging Face API
|
|
@@ -40,9 +40,7 @@ MASK_DIR = "generated_masks"
|
|
| 40 |
PATCHES_DIR = "patches"
|
| 41 |
PRED_PATCHES_DIR = "pred_patches"
|
| 42 |
CSV_LOG_PATH = "image_log.csv"
|
| 43 |
-
|
| 44 |
-
for directory in [UPLOAD_DIR, MASK_DIR, PATCHES_DIR, PRED_PATCHES_DIR]:
|
| 45 |
-
os.makedirs(directory, exist_ok=True)
|
| 46 |
|
| 47 |
def split(image, destination = PATCHES_DIR, patch_size = 650):
|
| 48 |
img = cv2.imread(image)
|
|
@@ -88,7 +86,9 @@ def merge(patch_folder , dest_image = 'out.png', image_shape = None):
|
|
| 88 |
|
| 89 |
cv2.imwrite(dest_image, merged)
|
| 90 |
|
| 91 |
-
|
|
|
|
|
|
|
| 92 |
|
| 93 |
# Load model
|
| 94 |
@st.cache_resource
|
|
@@ -156,12 +156,6 @@ def upload_page():
|
|
| 156 |
|
| 157 |
image = st.file_uploader('Choose a satellite image', type=['jpg', 'png', 'jpeg', 'tiff', 'tif'])
|
| 158 |
|
| 159 |
-
if st.button('Back to Upload'):
|
| 160 |
-
st.session_state.page = 'upload'
|
| 161 |
-
st.session_state.file_uploaded = False
|
| 162 |
-
st.session_state.filename = None
|
| 163 |
-
st.session_state.mask_filename = None
|
| 164 |
-
st.rerun()
|
| 165 |
if image is not None and not st.session_state.file_uploaded:
|
| 166 |
try:
|
| 167 |
bytes_data = image.getvalue()
|
|
@@ -232,17 +226,12 @@ def upload_page():
|
|
| 232 |
# Save merged mask
|
| 233 |
st.session_state.mask_filename = merged_mask_filename
|
| 234 |
|
| 235 |
-
# Clean up temporary patch files
|
| 236 |
# Clean up temporary patch files
|
| 237 |
st.info('Cleaning up temporary files...')
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
for filename in os.listdir(PRED_PATCHES_DIR):
|
| 243 |
-
file_path = os.path.join(PRED_PATCHES_DIR, filename)
|
| 244 |
-
if os.path.isfile(file_path):
|
| 245 |
-
os.remove(file_path)
|
| 246 |
else:
|
| 247 |
# Predict on whole image
|
| 248 |
st.session_state.tr_img = transforms(img)
|
|
@@ -269,6 +258,15 @@ def upload_page():
|
|
| 269 |
st.error("Please check the logs for more details.")
|
| 270 |
print(f"Error in upload_page: {str(e)}") # This will appear in the Streamlit logs
|
| 271 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 272 |
if st.session_state.file_uploaded and st.button('View result'):
|
| 273 |
if st.session_state.filename is None:
|
| 274 |
st.error("Please upload an image before viewing the result.")
|
|
@@ -287,7 +285,7 @@ def result_page():
|
|
| 287 |
st.session_state.file_uploaded = False
|
| 288 |
st.session_state.filename = None
|
| 289 |
st.session_state.mask_filename = None
|
| 290 |
-
st.
|
| 291 |
return
|
| 292 |
|
| 293 |
col1, col2 = st.columns(2)
|
|
@@ -329,13 +327,13 @@ def result_page():
|
|
| 329 |
else:
|
| 330 |
st.error("Image or mask file not found for overlay.")
|
| 331 |
|
| 332 |
-
|
| 333 |
if st.button('Back to Upload'):
|
|
|
|
| 334 |
st.session_state.page = 'upload'
|
| 335 |
st.session_state.file_uploaded = False
|
| 336 |
st.session_state.filename = None
|
| 337 |
st.session_state.mask_filename = None
|
| 338 |
-
st.
|
| 339 |
|
| 340 |
def main():
|
| 341 |
st.title('Building area estimation')
|
|
|
|
| 18 |
from model.transform import transforms
|
| 19 |
from model.unet import UNET
|
| 20 |
from Utils.area import pixel_to_sqft, process_and_overlay_image
|
| 21 |
+
from split_merge import split, merge
|
| 22 |
from Utils.convert import read_pansharpened_rgb
|
| 23 |
|
| 24 |
# Initialize Hugging Face API
|
|
|
|
| 40 |
PATCHES_DIR = "patches"
|
| 41 |
PRED_PATCHES_DIR = "pred_patches"
|
| 42 |
CSV_LOG_PATH = "image_log.csv"
|
| 43 |
+
|
|
|
|
|
|
|
| 44 |
|
| 45 |
def split(image, destination = PATCHES_DIR, patch_size = 650):
|
| 46 |
img = cv2.imread(image)
|
|
|
|
| 86 |
|
| 87 |
cv2.imwrite(dest_image, merged)
|
| 88 |
|
| 89 |
+
# Create directories
|
| 90 |
+
for directory in [UPLOAD_DIR, MASK_DIR, PATCHES_DIR, PRED_PATCHES_DIR]:
|
| 91 |
+
os.makedirs(directory, exist_ok=True)
|
| 92 |
|
| 93 |
# Load model
|
| 94 |
@st.cache_resource
|
|
|
|
| 156 |
|
| 157 |
image = st.file_uploader('Choose a satellite image', type=['jpg', 'png', 'jpeg', 'tiff', 'tif'])
|
| 158 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
if image is not None and not st.session_state.file_uploaded:
|
| 160 |
try:
|
| 161 |
bytes_data = image.getvalue()
|
|
|
|
| 226 |
# Save merged mask
|
| 227 |
st.session_state.mask_filename = merged_mask_filename
|
| 228 |
|
|
|
|
| 229 |
# Clean up temporary patch files
|
| 230 |
st.info('Cleaning up temporary files...')
|
| 231 |
+
shutil.rmtree(PATCHES_DIR)
|
| 232 |
+
shutil.rmtree(PRED_PATCHES_DIR)
|
| 233 |
+
os.makedirs(PATCHES_DIR) # Recreate empty folders
|
| 234 |
+
os.makedirs(PRED_PATCHES_DIR)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
else:
|
| 236 |
# Predict on whole image
|
| 237 |
st.session_state.tr_img = transforms(img)
|
|
|
|
| 258 |
st.error("Please check the logs for more details.")
|
| 259 |
print(f"Error in upload_page: {str(e)}") # This will appear in the Streamlit logs
|
| 260 |
|
| 261 |
+
for filename in os.listdir(PATCHES_DIR):
|
| 262 |
+
file_path = os.path.join(PATCHES_DIR, filename)
|
| 263 |
+
if os.path.isfile(file_path):
|
| 264 |
+
os.remove(file_path)
|
| 265 |
+
for filename in os.listdir(PRED_PATCHES_DIR):
|
| 266 |
+
file_path = os.path.join(PRED_PATCHES_DIR, filename)
|
| 267 |
+
if os.path.isfile(file_path):
|
| 268 |
+
os.remove(file_path)
|
| 269 |
+
|
| 270 |
if st.session_state.file_uploaded and st.button('View result'):
|
| 271 |
if st.session_state.filename is None:
|
| 272 |
st.error("Please upload an image before viewing the result.")
|
|
|
|
| 285 |
st.session_state.file_uploaded = False
|
| 286 |
st.session_state.filename = None
|
| 287 |
st.session_state.mask_filename = None
|
| 288 |
+
st.rerun()
|
| 289 |
return
|
| 290 |
|
| 291 |
col1, col2 = st.columns(2)
|
|
|
|
| 327 |
else:
|
| 328 |
st.error("Image or mask file not found for overlay.")
|
| 329 |
|
|
|
|
| 330 |
if st.button('Back to Upload'):
|
| 331 |
+
|
| 332 |
st.session_state.page = 'upload'
|
| 333 |
st.session_state.file_uploaded = False
|
| 334 |
st.session_state.filename = None
|
| 335 |
st.session_state.mask_filename = None
|
| 336 |
+
st.rerun()
|
| 337 |
|
| 338 |
def main():
|
| 339 |
st.title('Building area estimation')
|