Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import torch
|
| 3 |
+
from torchvision import transforms
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import numpy as np
|
| 6 |
+
from unet_model import UNet # Make sure unet_model.py is also uploaded
|
| 7 |
+
|
| 8 |
+
# Load the model
|
| 9 |
+
model = UNet()
|
| 10 |
+
model.load_state_dict(torch.load("model.pth", map_location="cpu"))
|
| 11 |
+
model.eval()
|
| 12 |
+
|
| 13 |
+
st.title("🌊 Flood Prediction App")
|
| 14 |
+
st.write("Upload a satellite image, and the model will predict flood-affected areas.")
|
| 15 |
+
|
| 16 |
+
# Image uploader
|
| 17 |
+
uploaded_file = st.file_uploader("Choose a satellite image", type=["jpg", "png", "jpeg"])
|
| 18 |
+
|
| 19 |
+
if uploaded_file:
|
| 20 |
+
# Load and display input image
|
| 21 |
+
image = Image.open(uploaded_file).convert("RGB")
|
| 22 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 23 |
+
|
| 24 |
+
# Preprocess image
|
| 25 |
+
transform = transforms.Compose([
|
| 26 |
+
transforms.Resize((256, 256)),
|
| 27 |
+
transforms.ToTensor()
|
| 28 |
+
])
|
| 29 |
+
input_tensor = transform(image).unsqueeze(0)
|
| 30 |
+
|
| 31 |
+
# Predict flood mask
|
| 32 |
+
with torch.no_grad():
|
| 33 |
+
output = model(input_tensor)[0, 0].numpy()
|
| 34 |
+
|
| 35 |
+
# Binarize and display mask
|
| 36 |
+
mask = (output > 0.5).astype(np.uint8) * 255
|
| 37 |
+
mask_image = Image.fromarray(mask).resize(image.size)
|
| 38 |
+
st.image(mask_image, caption="Predicted Flood Mask", use_column_width=True)
|