Update app.py
Browse files
app.py
CHANGED
|
@@ -141,6 +141,40 @@ def save_uploaded_file(uploaded_file, suffix=".tif"):
|
|
| 141 |
tmp.write(uploaded_file.read())
|
| 142 |
return tmp.name
|
| 143 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
|
| 145 |
# Streamlit App Title
|
| 146 |
st.title("Satellite Mining Segmentation: SAR + Optic Image Inference")
|
|
@@ -289,27 +323,32 @@ if st.button("Run Inference"):
|
|
| 289 |
plt.tight_layout()
|
| 290 |
st.pyplot(fig)
|
| 291 |
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
)
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 307 |
|
| 308 |
-
fig2, ax2 = plt.subplots(figsize=(10, 6))
|
| 309 |
-
ax2.imshow(outside_mask, cmap='Reds')
|
| 310 |
-
ax2.set_title("Illegal Mining Outside WIUP Boundary")
|
| 311 |
-
ax2.axis('off')
|
| 312 |
-
st.pyplot(fig2)
|
| 313 |
|
| 314 |
else:
|
| 315 |
st.warning("Please upload all three .tiff files to proceed.")
|
|
|
|
| 141 |
tmp.write(uploaded_file.read())
|
| 142 |
return tmp.name
|
| 143 |
|
| 144 |
+
def get_transform_from_tif(tif_path):
|
| 145 |
+
"""
|
| 146 |
+
Extracts the affine transform and CRS from a GeoTIFF image.
|
| 147 |
+
|
| 148 |
+
Args:
|
| 149 |
+
tif_path (str): Path to the GeoTIFF file.
|
| 150 |
+
|
| 151 |
+
Returns:
|
| 152 |
+
transform (Affine): Affine transformation for pixel to coordinate mapping.
|
| 153 |
+
crs (CRS): Coordinate Reference System of the image.
|
| 154 |
+
"""
|
| 155 |
+
with rasterio.open(tif_path) as src:
|
| 156 |
+
transform = src.transform
|
| 157 |
+
crs = src.crs
|
| 158 |
+
return transform, crs
|
| 159 |
+
|
| 160 |
+
def mask_to_polygons(mask, transform, mining_class_id=1):
|
| 161 |
+
# Keep only mining class
|
| 162 |
+
mining_mask = (mask == mining_class_id).astype(np.uint8)
|
| 163 |
+
|
| 164 |
+
# Extract shapes for mining
|
| 165 |
+
results = (
|
| 166 |
+
{'properties': {'class': 'mining_land'}, 'geometry': s}
|
| 167 |
+
for s, v in shapes(mining_mask, mask=None, transform=transform)
|
| 168 |
+
if v == 1
|
| 169 |
+
)
|
| 170 |
+
|
| 171 |
+
geoms = list(results)
|
| 172 |
+
if not geoms:
|
| 173 |
+
return gpd.GeoDataFrame(columns=['geometry'], geometry='geometry', crs='EPSG:4326')
|
| 174 |
+
|
| 175 |
+
gdf = gpd.GeoDataFrame.from_features(geoms)
|
| 176 |
+
gdf.set_crs(epsg=4326, inplace=True)
|
| 177 |
+
return gdf
|
| 178 |
|
| 179 |
# Streamlit App Title
|
| 180 |
st.title("Satellite Mining Segmentation: SAR + Optic Image Inference")
|
|
|
|
| 323 |
plt.tight_layout()
|
| 324 |
st.pyplot(fig)
|
| 325 |
|
| 326 |
+
transform, crs = get_transform_from_tif(optic_path)
|
| 327 |
+
|
| 328 |
+
# After getting prediction from model
|
| 329 |
+
pred_label_mask = np.argmax(pred_masks[0], axis=-1) # shape: (H, W)
|
| 330 |
+
|
| 331 |
+
# Get georeference transform from TIF image
|
| 332 |
+
transform, crs = get_transform_from_tif(optic_path)
|
| 333 |
+
|
| 334 |
+
# Convert mask to mining polygons
|
| 335 |
+
mining_gdf = mask_to_polygons(pred_label_mask, transform, mining_class_id=1)
|
| 336 |
+
|
| 337 |
+
# Make sure WIUP and prediction are in same CRS
|
| 338 |
+
wiup_gdf = wiup_gdf.to_crs(mining_gdf.crs)
|
| 339 |
+
|
| 340 |
+
# Find area outside WIUP
|
| 341 |
+
illegal_area, illegal_area_gdf = calculate_illegal_area(mining_gdf, wiup_gdf)
|
| 342 |
+
|
| 343 |
+
# Display in Streamlit
|
| 344 |
+
st.success(f"🛑 Illegal Mining Area Outside WIUP: {illegal_area/1e6:.2f} sq. km")
|
| 345 |
+
|
| 346 |
+
# Optional: Visualize and allow download
|
| 347 |
+
st.map(illegal_area_gdf.to_crs(epsg=4326))
|
| 348 |
+
|
| 349 |
+
geojson = illegal_area_gdf.to_crs(epsg=4326).to_json()
|
| 350 |
+
st.download_button("Download GeoJSON", geojson, file_name="illegal_area.geojson", mime="application/json")
|
| 351 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 352 |
|
| 353 |
else:
|
| 354 |
st.warning("Please upload all three .tiff files to proceed.")
|