Pavan2k4 commited on
Commit
3529767
·
verified ·
1 Parent(s): c11e6fb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -16
app.py CHANGED
@@ -1,3 +1,5 @@
 
 
1
  import streamlit as st
2
  import sys
3
  import os
@@ -16,7 +18,7 @@ sys.path.append('Utils')
16
  sys.path.append('model')
17
  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 merge
22
  from Utils.convert import read_pansharpened_rgb
@@ -34,19 +36,19 @@ if not HF_TOKEN:
34
  REPO_ID = "Pavan2k4/Building_area"
35
  REPO_TYPE = "space"
36
 
37
- # Define temporary directories outside of tracked directories
38
- TMP_DIR = "/tmp/space_files"
39
- PATCHES_DIR = os.path.join(TMP_DIR, "patches")
40
- PRED_PATCHES_DIR = os.path.join(TMP_DIR, "pred_patches")
41
- MASK_DIR = os.path.join(TMP_DIR, "generated_masks")
 
42
 
43
- # Create temporary directories
44
- os.makedirs(TMP_DIR, exist_ok=True)
45
- for directory in [PATCHES_DIR, PRED_PATCHES_DIR, MASK_DIR]:
46
  os.makedirs(directory, exist_ok=True)
47
 
48
- # Load model and cache it to avoid unnecessary reloading
49
- @st.cache_resource(show_spinner=False)
50
  def load_model():
51
  model = reunet_cbam()
52
  model.load_state_dict(torch.load('latest.pth', map_location='cpu')['model_state_dict'])
@@ -111,8 +113,7 @@ def split(image_path, patch_size=512):
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}")
115
-
116
 
117
  def upload_page():
118
  if 'file_uploaded' not in st.session_state:
@@ -181,7 +182,7 @@ def upload_page():
181
 
182
  # Count and display the number of patches
183
  num_patches = len([f for f in os.listdir(PATCHES_DIR) if f.endswith('.png')])
184
- #st.write(f"Number of patches created: {num_patches}") # Debug output
185
 
186
  # Display buffer while analyzing
187
  with st.spinner('Analyzing...'):
@@ -189,7 +190,7 @@ def upload_page():
189
  for patch_filename in os.listdir(PATCHES_DIR):
190
  if patch_filename.endswith(".png"):
191
  patch_path = os.path.join(PATCHES_DIR, patch_filename)
192
- #st.write(f"Processing patch: {patch_path}") # Debug output
193
  patch_img = Image.open(patch_path)
194
  patch_tr_img = transforms(patch_img)
195
  prediction = predict(patch_tr_img)
@@ -197,7 +198,7 @@ def upload_page():
197
  mask_filename = f"mask_{patch_filename}"
198
  mask_filepath = os.path.join(PRED_PATCHES_DIR, mask_filename)
199
  Image.fromarray(mask).save(mask_filepath)
200
- #st.write(f"Saved mask: {mask_filepath}") # Debug output
201
 
202
  # Merge predicted patches
203
  merged_mask_filename = f"mask_{timestamp}.png"
 
1
+
2
+
3
  import streamlit as st
4
  import sys
5
  import os
 
18
  sys.path.append('model')
19
  from model.CBAM.reunet_cbam import reunet_cbam
20
  from model.transform import transforms
21
+
22
  from Utils.area import pixel_to_sqft, process_and_overlay_image
23
  from split_merge import merge
24
  from Utils.convert import read_pansharpened_rgb
 
36
  REPO_ID = "Pavan2k4/Building_area"
37
  REPO_TYPE = "space"
38
 
39
+ # Define subdirectories using relative paths
40
+ UPLOAD_DIR = "uploaded_images"
41
+ MASK_DIR = "generated_masks"
42
+ PATCHES_DIR = "patches"
43
+ PRED_PATCHES_DIR = "pred_patches"
44
+ CSV_LOG_PATH = "image_log.csv"
45
 
46
+ # Create directories
47
+ for directory in [UPLOAD_DIR, MASK_DIR, PATCHES_DIR, PRED_PATCHES_DIR]:
 
48
  os.makedirs(directory, exist_ok=True)
49
 
50
+ # Load model
51
+ @st.cache_resource
52
  def load_model():
53
  model = reunet_cbam()
54
  model.load_state_dict(torch.load('latest.pth', map_location='cpu')['model_state_dict'])
 
113
  patch_filename = f"patch_{i}_{j}.png"
114
  patch_path = os.path.join(PATCHES_DIR, patch_filename)
115
  patch.save(patch_path)
116
+ st.write(f"Saved patch: {patch_path}") # Debug output
 
117
 
118
  def upload_page():
119
  if 'file_uploaded' not in st.session_state:
 
182
 
183
  # Count and display the number of patches
184
  num_patches = len([f for f in os.listdir(PATCHES_DIR) if f.endswith('.png')])
185
+ st.write(f"Number of patches created: {num_patches}") # Debug output
186
 
187
  # Display buffer while analyzing
188
  with st.spinner('Analyzing...'):
 
190
  for patch_filename in os.listdir(PATCHES_DIR):
191
  if patch_filename.endswith(".png"):
192
  patch_path = os.path.join(PATCHES_DIR, patch_filename)
193
+ st.write(f"Processing patch: {patch_path}") # Debug output
194
  patch_img = Image.open(patch_path)
195
  patch_tr_img = transforms(patch_img)
196
  prediction = predict(patch_tr_img)
 
198
  mask_filename = f"mask_{patch_filename}"
199
  mask_filepath = os.path.join(PRED_PATCHES_DIR, mask_filename)
200
  Image.fromarray(mask).save(mask_filepath)
201
+ st.write(f"Saved mask: {mask_filepath}") # Debug output
202
 
203
  # Merge predicted patches
204
  merged_mask_filename = f"mask_{timestamp}.png"