Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -18,6 +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 Utils.convert import read_pansharpened_rgb
|
| 22 |
|
| 23 |
# Initialize Hugging Face API
|
|
@@ -34,7 +35,7 @@ REPO_ID = "Pavan2k4/Building_area"
|
|
| 34 |
REPO_TYPE = "space"
|
| 35 |
|
| 36 |
# Define base directory for Hugging Face Spaces
|
| 37 |
-
BASE_DIR = "
|
| 38 |
|
| 39 |
# Define subdirectories
|
| 40 |
UPLOAD_DIR = os.path.join(BASE_DIR, "uploaded_images")
|
|
@@ -43,48 +44,6 @@ 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)
|
| 48 |
-
h, w, _ = img.shape
|
| 49 |
-
for y in range(0, h, patch_size):
|
| 50 |
-
for x in range(0, w, patch_size):
|
| 51 |
-
patch = img[y:y+patch_size, x:x+patch_size]
|
| 52 |
-
patch_filename = f"patch_{y}_{x}.png"
|
| 53 |
-
patch_path = os.path.join(destination, patch_filename)
|
| 54 |
-
cv2.imwrite(patch_path, patch)
|
| 55 |
-
|
| 56 |
-
def merge(patch_folder, dest_image='out.png', image_shape=None):
|
| 57 |
-
merged = np.zeros(image_shape[:-1] + (3,), dtype=np.uint8)
|
| 58 |
-
for filename in os.listdir(patch_folder):
|
| 59 |
-
if filename.endswith(".png"):
|
| 60 |
-
patch_path = os.path.join(patch_folder, filename)
|
| 61 |
-
patch = cv2.imread(patch_path)
|
| 62 |
-
patch_height, patch_width, _ = patch.shape
|
| 63 |
-
|
| 64 |
-
# Extract patch coordinates from filename
|
| 65 |
-
parts = filename.split("_")
|
| 66 |
-
x, y = None, None
|
| 67 |
-
for part in parts:
|
| 68 |
-
if part.endswith(".png"):
|
| 69 |
-
x = int(part.split(".")[0])
|
| 70 |
-
elif part.isdigit():
|
| 71 |
-
y = int(part)
|
| 72 |
-
if x is None or y is None:
|
| 73 |
-
raise ValueError(f"Invalid filename: {filename}")
|
| 74 |
-
|
| 75 |
-
# Check if patch fits within image boundaries
|
| 76 |
-
if x + patch_width > image_shape[1] or y + patch_height > image_shape[0]:
|
| 77 |
-
# Adjust patch position to fit within image boundaries
|
| 78 |
-
if x + patch_width > image_shape[1]:
|
| 79 |
-
x = image_shape[1] - patch_width
|
| 80 |
-
if y + patch_height > image_shape[0]:
|
| 81 |
-
y = image_shape[0] - patch_height
|
| 82 |
-
|
| 83 |
-
# Merge patch into the main image
|
| 84 |
-
merged[y:y+patch_height, x:x+patch_width, :] = patch
|
| 85 |
-
|
| 86 |
-
cv2.imwrite(dest_image, merged)
|
| 87 |
-
|
| 88 |
# Create directories
|
| 89 |
for directory in [UPLOAD_DIR, MASK_DIR, PATCHES_DIR, PRED_PATCHES_DIR]:
|
| 90 |
os.makedirs(directory, exist_ok=True)
|
|
@@ -230,10 +189,10 @@ def upload_page():
|
|
| 230 |
|
| 231 |
# Clean up temporary patch files
|
| 232 |
st.info('Cleaning up temporary files...')
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
st.success('Temporary files cleaned up')
|
| 238 |
else:
|
| 239 |
# Predict on whole image
|
|
@@ -320,10 +279,8 @@ def result_page():
|
|
| 320 |
st.error("Image or mask file not found for overlay.")
|
| 321 |
|
| 322 |
if st.button('Back to Upload'):
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
for file in os.listdir(PRED_PATCHES_DIR):
|
| 326 |
-
os.remove(os.path.join(PRED_PATCHES_DIR, file))
|
| 327 |
st.session_state.page = 'upload'
|
| 328 |
st.session_state.file_uploaded = False
|
| 329 |
st.session_state.filename = None
|
|
@@ -339,4 +296,7 @@ def main():
|
|
| 339 |
if st.session_state.page == 'upload':
|
| 340 |
upload_page()
|
| 341 |
elif st.session_state.page == 'result':
|
| 342 |
-
result_page()
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
|
|
|
| 35 |
REPO_TYPE = "space"
|
| 36 |
|
| 37 |
# Define base directory for Hugging Face Spaces
|
| 38 |
+
BASE_DIR = "/home/user"
|
| 39 |
|
| 40 |
# Define subdirectories
|
| 41 |
UPLOAD_DIR = os.path.join(BASE_DIR, "uploaded_images")
|
|
|
|
| 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]:
|
| 49 |
os.makedirs(directory, exist_ok=True)
|
|
|
|
| 189 |
|
| 190 |
# Clean up temporary patch files
|
| 191 |
st.info('Cleaning up temporary files...')
|
| 192 |
+
shutil.rmtree(PATCHES_DIR)
|
| 193 |
+
shutil.rmtree(PRED_PATCHES_DIR)
|
| 194 |
+
os.makedirs(PATCHES_DIR) # Recreate empty folders
|
| 195 |
+
os.makedirs(PRED_PATCHES_DIR)
|
| 196 |
st.success('Temporary files cleaned up')
|
| 197 |
else:
|
| 198 |
# Predict on whole image
|
|
|
|
| 279 |
st.error("Image or mask file not found for overlay.")
|
| 280 |
|
| 281 |
if st.button('Back to Upload'):
|
| 282 |
+
shutil.rmtree(PATCHES_DIR)
|
| 283 |
+
shutil.rmtree(PRED_PATCHES_DIR)
|
|
|
|
|
|
|
| 284 |
st.session_state.page = 'upload'
|
| 285 |
st.session_state.file_uploaded = False
|
| 286 |
st.session_state.filename = None
|
|
|
|
| 296 |
if st.session_state.page == 'upload':
|
| 297 |
upload_page()
|
| 298 |
elif st.session_state.page == 'result':
|
| 299 |
+
result_page()
|
| 300 |
+
|
| 301 |
+
if __name__ == '__main__':
|
| 302 |
+
main()
|