Alexvatti commited on
Commit
6695dcb
·
verified ·
1 Parent(s): 6bc373a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -1
app.py CHANGED
@@ -11,6 +11,8 @@ import rasterio
11
  import cv2
12
  import tensorflow as tf
13
  import tempfile
 
 
14
 
15
  # Configuration
16
  HEIGHT = WIDTH = 256
@@ -145,11 +147,12 @@ st.title("Satellite Mining Segmentation: SAR + Optic Image Inference")
145
  sar_file = st.file_uploader("Upload SAR Image", type=["tiff"])
146
  optic_file = st.file_uploader("Upload Optical Image", type=["tiff"])
147
  mask_file = st.file_uploader("Upload Mask Image", type=["tiff"])
 
148
 
149
  num_samples = 1
150
  if st.button("Run Inference"):
151
  with st.spinner("Loading data and model..."):
152
- if sar_file is not None and optic_file is not None and mask_file is not None:
153
  st.success("All files uploaded successfully!")
154
 
155
  # Save uploaded files
@@ -157,6 +160,14 @@ if st.button("Run Inference"):
157
  optic_path = save_uploaded_file(optic_file, suffix=".tif")
158
  mask_path = save_uploaded_file(mask_file, suffix=".tif")
159
 
 
 
 
 
 
 
 
 
160
  # Create image lists
161
  sarImages = [sar_path]
162
  opticImages = [optic_path]
@@ -274,6 +285,29 @@ if st.button("Run Inference"):
274
 
275
  plt.tight_layout()
276
  st.pyplot(fig)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
277
  else:
278
  st.warning("Please upload all three .tiff files to proceed.")
279
 
 
11
  import cv2
12
  import tensorflow as tf
13
  import tempfile
14
+ from rasterio import features
15
+ from shapely.geometry import shape
16
 
17
  # Configuration
18
  HEIGHT = WIDTH = 256
 
147
  sar_file = st.file_uploader("Upload SAR Image", type=["tiff"])
148
  optic_file = st.file_uploader("Upload Optical Image", type=["tiff"])
149
  mask_file = st.file_uploader("Upload Mask Image", type=["tiff"])
150
+ wiup_file = st.file_uploader("Upload WIUP Boundary (Shapefile ZIP)", type=["zip"])
151
 
152
  num_samples = 1
153
  if st.button("Run Inference"):
154
  with st.spinner("Loading data and model..."):
155
+ if sar_file is not None and optic_file is not None and mask_file is not None and wiup_file is not None:
156
  st.success("All files uploaded successfully!")
157
 
158
  # Save uploaded files
 
160
  optic_path = save_uploaded_file(optic_file, suffix=".tif")
161
  mask_path = save_uploaded_file(mask_file, suffix=".tif")
162
 
163
+ wiup_zip_path = save_uploaded_file(wiup_file, suffix=".zip")
164
+ extract_folder = wiup_zip_path.replace(".zip", "")
165
+ with zipfile.ZipFile(wiup_zip_path, "r") as zip_ref:
166
+ zip_ref.extractall(extract_folder)
167
+
168
+ # Load WIUP shapefile
169
+ wiup_gdf = gpd.read_file(extract_folder)
170
+
171
  # Create image lists
172
  sarImages = [sar_path]
173
  opticImages = [optic_path]
 
285
 
286
  plt.tight_layout()
287
  st.pyplot(fig)
288
+
289
+ wiup_mask = features.rasterize(
290
+ [(geom, 1) for geom in wiup_gdf.geometry],
291
+ out_shape=(HEIGHT, WIDTH),
292
+ transform=transform,
293
+ fill=0,
294
+ dtype=np.uint8
295
+ )
296
+ # Binary mask of predicted illegal mining
297
+ pred_illegal_mask = illegal_mask.astype(np.uint8)
298
+
299
+ # Mining outside WIUP
300
+ outside_mask = (pred_illegal_mask == 1) & (wiup_mask == 0)
301
+ outside_percentage = 100 * np.sum(outside_mask) / np.sum(pred_illegal_mask)
302
+
303
+ st.markdown(f"### 🚨 Illegal Mining Outside WIUP: `{outside_percentage:.2f}%`")
304
+
305
+ fig2, ax2 = plt.subplots(figsize=(10, 6))
306
+ ax2.imshow(outside_mask, cmap='Reds')
307
+ ax2.set_title("Illegal Mining Outside WIUP Boundary")
308
+ ax2.axis('off')
309
+ st.pyplot(fig2)
310
+
311
  else:
312
  st.warning("Please upload all three .tiff files to proceed.")
313