Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -18,7 +18,6 @@ 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 |
-
from split_merge import split, merge
|
| 22 |
from Utils.convert import read_pansharpened_rgb
|
| 23 |
|
| 24 |
# Initialize Hugging Face API
|
|
@@ -34,12 +33,15 @@ if not HF_TOKEN:
|
|
| 34 |
REPO_ID = "Pavan2k4/Building_area"
|
| 35 |
REPO_TYPE = "space"
|
| 36 |
|
|
|
|
|
|
|
|
|
|
| 37 |
# Define subdirectories
|
| 38 |
-
UPLOAD_DIR = "uploaded_images"
|
| 39 |
-
MASK_DIR = "generated_masks"
|
| 40 |
-
PATCHES_DIR = "patches"
|
| 41 |
-
PRED_PATCHES_DIR = "pred_patches"
|
| 42 |
-
CSV_LOG_PATH = "image_log.csv"
|
| 43 |
|
| 44 |
def split(image, destination=PATCHES_DIR, patch_size=650):
|
| 45 |
img = cv2.imread(image)
|
|
@@ -111,6 +113,7 @@ def save_to_hf_repo(local_path, repo_path):
|
|
| 111 |
repo_type=REPO_TYPE,
|
| 112 |
token=HF_TOKEN
|
| 113 |
)
|
|
|
|
| 114 |
except Exception as e:
|
| 115 |
st.error(f"Error uploading file: {str(e)}")
|
| 116 |
st.error("Detailed error information:")
|
|
@@ -174,7 +177,7 @@ def upload_page():
|
|
| 174 |
with open(filepath, "wb") as f:
|
| 175 |
f.write(bytes_data)
|
| 176 |
|
| 177 |
-
st.success(
|
| 178 |
|
| 179 |
# Save image to Hugging Face repo
|
| 180 |
save_to_hf_repo(filepath, f'uploaded_images/{filename}')
|
|
@@ -184,11 +187,13 @@ def upload_page():
|
|
| 184 |
st.info('Processing GeoTIFF image...')
|
| 185 |
rgb_image = read_pansharpened_rgb(filepath)
|
| 186 |
cv2.imwrite(converted_filepath, cv2.cvtColor(rgb_image, cv2.COLOR_RGB2BGR))
|
|
|
|
| 187 |
img = Image.open(converted_filepath)
|
| 188 |
else:
|
| 189 |
img = Image.open(filepath)
|
| 190 |
|
| 191 |
st.image(img, caption='Uploaded Image', use_column_width=True)
|
|
|
|
| 192 |
|
| 193 |
# Store the full path of the converted image
|
| 194 |
st.session_state.filename = converted_filename
|
|
@@ -225,10 +230,11 @@ def upload_page():
|
|
| 225 |
|
| 226 |
# Clean up temporary patch files
|
| 227 |
st.info('Cleaning up temporary files...')
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
os.
|
| 231 |
-
|
|
|
|
| 232 |
else:
|
| 233 |
# Predict on whole image
|
| 234 |
st.session_state.tr_img = transforms(img)
|
|
@@ -239,8 +245,6 @@ def upload_page():
|
|
| 239 |
Image.fromarray(mask).save(mask_filepath)
|
| 240 |
st.session_state.mask_filename = mask_filename
|
| 241 |
|
| 242 |
-
st.success('Mask generated and saved')
|
| 243 |
-
|
| 244 |
# Save mask to Hugging Face repo
|
| 245 |
mask_filepath = os.path.join(MASK_DIR, st.session_state.mask_filename)
|
| 246 |
save_to_hf_repo(mask_filepath, f'generated_masks/{st.session_state.mask_filename}')
|
|
@@ -255,15 +259,6 @@ def upload_page():
|
|
| 255 |
st.error("Please check the logs for more details.")
|
| 256 |
print(f"Error in upload_page: {str(e)}") # This will appear in the Streamlit logs
|
| 257 |
|
| 258 |
-
for filename in os.listdir(PATCHES_DIR):
|
| 259 |
-
file_path = os.path.join(PATCHES_DIR, filename)
|
| 260 |
-
if os.path.isfile(file_path):
|
| 261 |
-
os.remove(file_path)
|
| 262 |
-
for filename in os.listdir(PRED_PATCHES_DIR):
|
| 263 |
-
file_path = os.path.join(PRED_PATCHES_DIR, filename)
|
| 264 |
-
if os.path.isfile(file_path):
|
| 265 |
-
os.remove(file_path)
|
| 266 |
-
|
| 267 |
if st.session_state.file_uploaded and st.button('View result'):
|
| 268 |
if st.session_state.filename is None:
|
| 269 |
st.error("Please upload an image before viewing the result.")
|
|
@@ -280,6 +275,8 @@ def result_page():
|
|
| 280 |
if st.button('Back to Upload'):
|
| 281 |
st.session_state.page = 'upload'
|
| 282 |
st.session_state.file_uploaded = False
|
|
|
|
|
|
|
| 283 |
st.rerun()
|
| 284 |
return
|
| 285 |
|
|
@@ -323,8 +320,11 @@ def result_page():
|
|
| 323 |
st.error("Image or mask file not found for overlay.")
|
| 324 |
|
| 325 |
if st.button('Back to Upload'):
|
|
|
|
| 326 |
st.session_state.page = 'upload'
|
| 327 |
st.session_state.file_uploaded = False
|
|
|
|
|
|
|
| 328 |
st.rerun()
|
| 329 |
|
| 330 |
def main():
|
|
@@ -336,7 +336,4 @@ def main():
|
|
| 336 |
if st.session_state.page == 'upload':
|
| 337 |
upload_page()
|
| 338 |
elif st.session_state.page == 'result':
|
| 339 |
-
result_page()
|
| 340 |
-
|
| 341 |
-
if __name__ == '__main__':
|
| 342 |
-
main()
|
|
|
|
| 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 Utils.convert import read_pansharpened_rgb
|
| 22 |
|
| 23 |
# Initialize Hugging Face API
|
|
|
|
| 33 |
REPO_ID = "Pavan2k4/Building_area"
|
| 34 |
REPO_TYPE = "space"
|
| 35 |
|
| 36 |
+
# Define base directory for Hugging Face Spaces
|
| 37 |
+
BASE_DIR = "DATA/"
|
| 38 |
+
|
| 39 |
# Define subdirectories
|
| 40 |
+
UPLOAD_DIR = os.path.join(BASE_DIR, "uploaded_images")
|
| 41 |
+
MASK_DIR = os.path.join(BASE_DIR, "generated_masks")
|
| 42 |
+
PATCHES_DIR = os.path.join(BASE_DIR, "patches")
|
| 43 |
+
PRED_PATCHES_DIR = os.path.join(BASE_DIR, "pred_patches")
|
| 44 |
+
CSV_LOG_PATH = os.path.join(BASE_DIR, "image_log.csv")
|
| 45 |
|
| 46 |
def split(image, destination=PATCHES_DIR, patch_size=650):
|
| 47 |
img = cv2.imread(image)
|
|
|
|
| 113 |
repo_type=REPO_TYPE,
|
| 114 |
token=HF_TOKEN
|
| 115 |
)
|
| 116 |
+
st.success(f"File uploaded successfully to {repo_path}")
|
| 117 |
except Exception as e:
|
| 118 |
st.error(f"Error uploading file: {str(e)}")
|
| 119 |
st.error("Detailed error information:")
|
|
|
|
| 177 |
with open(filepath, "wb") as f:
|
| 178 |
f.write(bytes_data)
|
| 179 |
|
| 180 |
+
st.success(f"Image saved to {filepath}")
|
| 181 |
|
| 182 |
# Save image to Hugging Face repo
|
| 183 |
save_to_hf_repo(filepath, f'uploaded_images/{filename}')
|
|
|
|
| 187 |
st.info('Processing GeoTIFF image...')
|
| 188 |
rgb_image = read_pansharpened_rgb(filepath)
|
| 189 |
cv2.imwrite(converted_filepath, cv2.cvtColor(rgb_image, cv2.COLOR_RGB2BGR))
|
| 190 |
+
st.success(f'GeoTIFF converted to 8-bit image and saved as {converted_filename}')
|
| 191 |
img = Image.open(converted_filepath)
|
| 192 |
else:
|
| 193 |
img = Image.open(filepath)
|
| 194 |
|
| 195 |
st.image(img, caption='Uploaded Image', use_column_width=True)
|
| 196 |
+
st.success(f'Image processed and saved as {converted_filename}')
|
| 197 |
|
| 198 |
# Store the full path of the converted image
|
| 199 |
st.session_state.filename = converted_filename
|
|
|
|
| 230 |
|
| 231 |
# Clean up temporary patch files
|
| 232 |
st.info('Cleaning up temporary files...')
|
| 233 |
+
for file in os.listdir(PATCHES_DIR):
|
| 234 |
+
os.remove(os.path.join(PATCHES_DIR, file))
|
| 235 |
+
for file in os.listdir(PRED_PATCHES_DIR):
|
| 236 |
+
os.remove(os.path.join(PRED_PATCHES_DIR, file))
|
| 237 |
+
st.success('Temporary files cleaned up')
|
| 238 |
else:
|
| 239 |
# Predict on whole image
|
| 240 |
st.session_state.tr_img = transforms(img)
|
|
|
|
| 245 |
Image.fromarray(mask).save(mask_filepath)
|
| 246 |
st.session_state.mask_filename = mask_filename
|
| 247 |
|
|
|
|
|
|
|
| 248 |
# Save mask to Hugging Face repo
|
| 249 |
mask_filepath = os.path.join(MASK_DIR, st.session_state.mask_filename)
|
| 250 |
save_to_hf_repo(mask_filepath, f'generated_masks/{st.session_state.mask_filename}')
|
|
|
|
| 259 |
st.error("Please check the logs for more details.")
|
| 260 |
print(f"Error in upload_page: {str(e)}") # This will appear in the Streamlit logs
|
| 261 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 262 |
if st.session_state.file_uploaded and st.button('View result'):
|
| 263 |
if st.session_state.filename is None:
|
| 264 |
st.error("Please upload an image before viewing the result.")
|
|
|
|
| 275 |
if st.button('Back to Upload'):
|
| 276 |
st.session_state.page = 'upload'
|
| 277 |
st.session_state.file_uploaded = False
|
| 278 |
+
st.session_state.filename = None
|
| 279 |
+
st.session_state.mask_filename = None
|
| 280 |
st.rerun()
|
| 281 |
return
|
| 282 |
|
|
|
|
| 320 |
st.error("Image or mask file not found for overlay.")
|
| 321 |
|
| 322 |
if st.button('Back to Upload'):
|
| 323 |
+
|
| 324 |
st.session_state.page = 'upload'
|
| 325 |
st.session_state.file_uploaded = False
|
| 326 |
+
st.session_state.filename = None
|
| 327 |
+
st.session_state.mask_filename = None
|
| 328 |
st.rerun()
|
| 329 |
|
| 330 |
def main():
|
|
|
|
| 336 |
if st.session_state.page == 'upload':
|
| 337 |
upload_page()
|
| 338 |
elif st.session_state.page == 'result':
|
| 339 |
+
result_page()
|
|
|
|
|
|
|
|
|