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 |
-
from split_merge import
|
| 22 |
from Utils.convert import read_pansharpened_rgb
|
| 23 |
|
| 24 |
# Initialize Hugging Face API
|
|
@@ -34,15 +34,12 @@ if not HF_TOKEN:
|
|
| 34 |
REPO_ID = "Pavan2k4/Building_area"
|
| 35 |
REPO_TYPE = "space"
|
| 36 |
|
| 37 |
-
# Define
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
PATCHES_DIR = os.path.join(BASE_DIR, "patches")
|
| 44 |
-
PRED_PATCHES_DIR = os.path.join(BASE_DIR, "pred_patches")
|
| 45 |
-
CSV_LOG_PATH = os.path.join(BASE_DIR, "image_log.csv")
|
| 46 |
|
| 47 |
# Create directories
|
| 48 |
for directory in [UPLOAD_DIR, MASK_DIR, PATCHES_DIR, PRED_PATCHES_DIR]:
|
|
@@ -103,6 +100,19 @@ def log_image_details(image_id, image_filename, mask_filename):
|
|
| 103 |
# Save CSV to Hugging Face repo
|
| 104 |
save_to_hf_repo(CSV_LOG_PATH, 'image_log.csv')
|
| 105 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
def upload_page():
|
| 107 |
if 'file_uploaded' not in st.session_state:
|
| 108 |
st.session_state.file_uploaded = False
|
|
@@ -160,17 +170,25 @@ def upload_page():
|
|
| 160 |
# Convert image to numpy array
|
| 161 |
img_array = np.array(img)
|
| 162 |
|
|
|
|
|
|
|
| 163 |
# Check if image shape is more than 650x650
|
| 164 |
if img_array.shape[0] > 650 or img_array.shape[1] > 650:
|
|
|
|
| 165 |
# Split image into patches
|
| 166 |
split(converted_filepath, patch_size=512)
|
| 167 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
# Display buffer while analyzing
|
| 169 |
with st.spinner('Analyzing...'):
|
| 170 |
# Predict on each patch
|
| 171 |
for patch_filename in os.listdir(PATCHES_DIR):
|
| 172 |
if patch_filename.endswith(".png"):
|
| 173 |
patch_path = os.path.join(PATCHES_DIR, patch_filename)
|
|
|
|
| 174 |
patch_img = Image.open(patch_path)
|
| 175 |
patch_tr_img = transforms(patch_img)
|
| 176 |
prediction = predict(patch_tr_img)
|
|
@@ -178,24 +196,27 @@ def upload_page():
|
|
| 178 |
mask_filename = f"mask_{patch_filename}"
|
| 179 |
mask_filepath = os.path.join(PRED_PATCHES_DIR, mask_filename)
|
| 180 |
Image.fromarray(mask).save(mask_filepath)
|
|
|
|
| 181 |
|
| 182 |
# Merge predicted patches
|
| 183 |
merged_mask_filename = f"mask_{timestamp}.png"
|
| 184 |
merged_mask_path = os.path.join(MASK_DIR, merged_mask_filename)
|
| 185 |
merge(PRED_PATCHES_DIR, merged_mask_path, img_array.shape)
|
|
|
|
| 186 |
|
| 187 |
# Save merged mask
|
| 188 |
st.session_state.mask_filename = merged_mask_filename
|
| 189 |
|
| 190 |
-
# Clean up temporary patch files
|
| 191 |
st.info('Cleaning up temporary files...')
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
os.
|
| 195 |
-
|
| 196 |
st.success('Temporary files cleaned up')
|
| 197 |
else:
|
| 198 |
# Predict on whole image
|
|
|
|
| 199 |
st.session_state.tr_img = transforms(img)
|
| 200 |
prediction = predict(st.session_state.tr_img)
|
| 201 |
mask = (prediction > 0.5).astype(np.uint8) * 255
|
|
@@ -203,6 +224,7 @@ def upload_page():
|
|
| 203 |
mask_filepath = os.path.join(MASK_DIR, mask_filename)
|
| 204 |
Image.fromarray(mask).save(mask_filepath)
|
| 205 |
st.session_state.mask_filename = mask_filename
|
|
|
|
| 206 |
|
| 207 |
# Save mask to Hugging Face repo
|
| 208 |
mask_filepath = os.path.join(MASK_DIR, st.session_state.mask_filename)
|
|
@@ -216,7 +238,7 @@ def upload_page():
|
|
| 216 |
except Exception as e:
|
| 217 |
st.error(f"An error occurred: {str(e)}")
|
| 218 |
st.error("Please check the logs for more details.")
|
| 219 |
-
|
| 220 |
|
| 221 |
if st.session_state.file_uploaded and st.button('View result'):
|
| 222 |
if st.session_state.filename is None:
|
|
@@ -279,8 +301,11 @@ def result_page():
|
|
| 279 |
st.error("Image or mask file not found for overlay.")
|
| 280 |
|
| 281 |
if st.button('Back to Upload'):
|
| 282 |
-
|
| 283 |
-
|
|
|
|
|
|
|
|
|
|
| 284 |
st.session_state.page = 'upload'
|
| 285 |
st.session_state.file_uploaded = False
|
| 286 |
st.session_state.filename = None
|
|
|
|
| 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 merge
|
| 22 |
from Utils.convert import read_pansharpened_rgb
|
| 23 |
|
| 24 |
# Initialize Hugging Face API
|
|
|
|
| 34 |
REPO_ID = "Pavan2k4/Building_area"
|
| 35 |
REPO_TYPE = "space"
|
| 36 |
|
| 37 |
+
# Define subdirectories using relative paths
|
| 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 |
# Create directories
|
| 45 |
for directory in [UPLOAD_DIR, MASK_DIR, PATCHES_DIR, PRED_PATCHES_DIR]:
|
|
|
|
| 100 |
# Save CSV to Hugging Face repo
|
| 101 |
save_to_hf_repo(CSV_LOG_PATH, 'image_log.csv')
|
| 102 |
|
| 103 |
+
def split(image_path, patch_size=512):
|
| 104 |
+
img = Image.open(image_path)
|
| 105 |
+
width, height = img.size
|
| 106 |
+
|
| 107 |
+
for i in range(0, height, patch_size):
|
| 108 |
+
for j in range(0, width, patch_size):
|
| 109 |
+
box = (j, i, j+patch_size, i+patch_size)
|
| 110 |
+
patch = img.crop(box)
|
| 111 |
+
patch_filename = f"patch_{i}_{j}.png"
|
| 112 |
+
patch_path = os.path.join(PATCHES_DIR, patch_filename)
|
| 113 |
+
patch.save(patch_path)
|
| 114 |
+
st.write(f"Saved patch: {patch_path}") # Debug output
|
| 115 |
+
|
| 116 |
def upload_page():
|
| 117 |
if 'file_uploaded' not in st.session_state:
|
| 118 |
st.session_state.file_uploaded = False
|
|
|
|
| 170 |
# Convert image to numpy array
|
| 171 |
img_array = np.array(img)
|
| 172 |
|
| 173 |
+
st.write(f"Image shape: {img_array.shape}") # Debug output
|
| 174 |
+
|
| 175 |
# Check if image shape is more than 650x650
|
| 176 |
if img_array.shape[0] > 650 or img_array.shape[1] > 650:
|
| 177 |
+
st.write("Splitting image into patches...") # Debug output
|
| 178 |
# Split image into patches
|
| 179 |
split(converted_filepath, patch_size=512)
|
| 180 |
|
| 181 |
+
# Count and display the number of patches
|
| 182 |
+
num_patches = len([f for f in os.listdir(PATCHES_DIR) if f.endswith('.png')])
|
| 183 |
+
st.write(f"Number of patches created: {num_patches}") # Debug output
|
| 184 |
+
|
| 185 |
# Display buffer while analyzing
|
| 186 |
with st.spinner('Analyzing...'):
|
| 187 |
# Predict on each patch
|
| 188 |
for patch_filename in os.listdir(PATCHES_DIR):
|
| 189 |
if patch_filename.endswith(".png"):
|
| 190 |
patch_path = os.path.join(PATCHES_DIR, patch_filename)
|
| 191 |
+
st.write(f"Processing patch: {patch_path}") # Debug output
|
| 192 |
patch_img = Image.open(patch_path)
|
| 193 |
patch_tr_img = transforms(patch_img)
|
| 194 |
prediction = predict(patch_tr_img)
|
|
|
|
| 196 |
mask_filename = f"mask_{patch_filename}"
|
| 197 |
mask_filepath = os.path.join(PRED_PATCHES_DIR, mask_filename)
|
| 198 |
Image.fromarray(mask).save(mask_filepath)
|
| 199 |
+
st.write(f"Saved mask: {mask_filepath}") # Debug output
|
| 200 |
|
| 201 |
# Merge predicted patches
|
| 202 |
merged_mask_filename = f"mask_{timestamp}.png"
|
| 203 |
merged_mask_path = os.path.join(MASK_DIR, merged_mask_filename)
|
| 204 |
merge(PRED_PATCHES_DIR, merged_mask_path, img_array.shape)
|
| 205 |
+
st.write(f"Merged mask saved: {merged_mask_path}") # Debug output
|
| 206 |
|
| 207 |
# Save merged mask
|
| 208 |
st.session_state.mask_filename = merged_mask_filename
|
| 209 |
|
| 210 |
+
# Clean up temporary patch files but keep the folders
|
| 211 |
st.info('Cleaning up temporary files...')
|
| 212 |
+
for file in os.listdir(PATCHES_DIR):
|
| 213 |
+
os.remove(os.path.join(PATCHES_DIR, file))
|
| 214 |
+
for file in os.listdir(PRED_PATCHES_DIR):
|
| 215 |
+
os.remove(os.path.join(PRED_PATCHES_DIR, file))
|
| 216 |
st.success('Temporary files cleaned up')
|
| 217 |
else:
|
| 218 |
# Predict on whole image
|
| 219 |
+
st.write("Processing whole image without splitting") # Debug output
|
| 220 |
st.session_state.tr_img = transforms(img)
|
| 221 |
prediction = predict(st.session_state.tr_img)
|
| 222 |
mask = (prediction > 0.5).astype(np.uint8) * 255
|
|
|
|
| 224 |
mask_filepath = os.path.join(MASK_DIR, mask_filename)
|
| 225 |
Image.fromarray(mask).save(mask_filepath)
|
| 226 |
st.session_state.mask_filename = mask_filename
|
| 227 |
+
st.write(f"Mask saved: {mask_filepath}") # Debug output
|
| 228 |
|
| 229 |
# Save mask to Hugging Face repo
|
| 230 |
mask_filepath = os.path.join(MASK_DIR, st.session_state.mask_filename)
|
|
|
|
| 238 |
except Exception as e:
|
| 239 |
st.error(f"An error occurred: {str(e)}")
|
| 240 |
st.error("Please check the logs for more details.")
|
| 241 |
+
st.exception(e) # This will display the full traceback in the Streamlit app
|
| 242 |
|
| 243 |
if st.session_state.file_uploaded and st.button('View result'):
|
| 244 |
if st.session_state.filename is None:
|
|
|
|
| 301 |
st.error("Image or mask file not found for overlay.")
|
| 302 |
|
| 303 |
if st.button('Back to Upload'):
|
| 304 |
+
# Remove files from PATCHES_DIR and PRED_PATCHES_DIR without deleting the folders
|
| 305 |
+
for file in os.listdir(PATCHES_DIR):
|
| 306 |
+
os.remove(os.path.join(PATCHES_DIR, file))
|
| 307 |
+
for file in os.listdir(PRED_PATCHES_DIR):
|
| 308 |
+
os.remove(os.path.join(PRED_PATCHES_DIR, file))
|
| 309 |
st.session_state.page = 'upload'
|
| 310 |
st.session_state.file_uploaded = False
|
| 311 |
st.session_state.filename = None
|