Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -45,30 +45,38 @@ model = load_model()
|
|
45 |
# === Title ===
|
46 |
st.title("🕳️ Sinkhole Segmentation with EffV2-UNet")
|
47 |
|
48 |
-
# ===
|
49 |
-
|
|
|
50 |
|
51 |
-
# ===
|
|
|
|
|
|
|
52 |
example_dir = "examples"
|
53 |
example_files = sorted([
|
54 |
f for f in os.listdir(example_dir)
|
55 |
if f.lower().endswith((".jpg", ".jpeg", ".png", ".tif", ".tiff"))
|
56 |
])
|
57 |
|
|
|
|
|
58 |
if example_files:
|
59 |
st.subheader("🖼️ Try with an Example Image")
|
60 |
-
cols = st.columns(min(len(example_files), 4))
|
61 |
-
|
62 |
for i, file in enumerate(example_files):
|
63 |
img_path = os.path.join(example_dir, file)
|
64 |
-
|
65 |
with cols[i % len(cols)]:
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
st.image(image, caption=f"Example: {file}", use_column_width=True)
|
70 |
|
71 |
-
# ===
|
|
|
|
|
|
|
|
|
72 |
if uploaded_image:
|
73 |
if isinstance(uploaded_image, str):
|
74 |
image = Image.open(uploaded_image).convert("RGB")
|
@@ -77,18 +85,14 @@ if uploaded_image:
|
|
77 |
|
78 |
st.image(image, caption="Input Image", use_column_width=True)
|
79 |
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
resized = image.resize((512, 512))
|
85 |
-
x = np.expand_dims(np.array(resized), axis=0)
|
86 |
-
y = model.predict(x)[0, :, :, 0]
|
87 |
|
88 |
-
|
89 |
|
90 |
-
|
91 |
-
|
92 |
-
mask_image = Image.fromarray(mask_bin)
|
93 |
|
94 |
-
|
|
|
45 |
# === Title ===
|
46 |
st.title("🕳️ Sinkhole Segmentation with EffV2-UNet")
|
47 |
|
48 |
+
# === Confidence threshold and predict trigger ===
|
49 |
+
st.sidebar.header("Segmentation Settings")
|
50 |
+
threshold = st.sidebar.slider("Confidence Threshold", 0.0, 1.0, 0.5, step=0.01)
|
51 |
|
52 |
+
# === Image input section ===
|
53 |
+
uploaded_image = st.file_uploader("📤 Upload an image", type=["png", "jpg", "jpeg", "tif", "tiff"])
|
54 |
+
|
55 |
+
# === Example selector with preview ===
|
56 |
example_dir = "examples"
|
57 |
example_files = sorted([
|
58 |
f for f in os.listdir(example_dir)
|
59 |
if f.lower().endswith((".jpg", ".jpeg", ".png", ".tif", ".tiff"))
|
60 |
])
|
61 |
|
62 |
+
selected_example_path = None
|
63 |
+
|
64 |
if example_files:
|
65 |
st.subheader("🖼️ Try with an Example Image")
|
66 |
+
cols = st.columns(min(len(example_files), 4))
|
|
|
67 |
for i, file in enumerate(example_files):
|
68 |
img_path = os.path.join(example_dir, file)
|
69 |
+
img_preview = Image.open(img_path).convert("RGB").resize((128, 128))
|
70 |
with cols[i % len(cols)]:
|
71 |
+
st.image(img_preview, caption=file, use_column_width=True)
|
72 |
+
if st.button(f"Use {file}", key=file):
|
73 |
+
selected_example_path = img_path
|
|
|
74 |
|
75 |
+
# === Set image to process ===
|
76 |
+
if selected_example_path:
|
77 |
+
uploaded_image = selected_example_path
|
78 |
+
|
79 |
+
# === Run prediction if button clicked ===
|
80 |
if uploaded_image:
|
81 |
if isinstance(uploaded_image, str):
|
82 |
image = Image.open(uploaded_image).convert("RGB")
|
|
|
85 |
|
86 |
st.image(image, caption="Input Image", use_column_width=True)
|
87 |
|
88 |
+
if st.button("Run Segmentation"):
|
89 |
+
resized = image.resize((512, 512))
|
90 |
+
x = np.expand_dims(np.array(resized), axis=0)
|
91 |
+
y = model.predict(x)[0, :, :, 0]
|
|
|
|
|
|
|
92 |
|
93 |
+
st.text(f"Prediction min/max: {y.min():.5f} / {y.max():.5f}")
|
94 |
|
95 |
+
mask_bin = (y > threshold).astype(np.uint8) * 255
|
96 |
+
mask_image = Image.fromarray(mask_bin)
|
|
|
97 |
|
98 |
+
st.image(mask_image, caption=f"Segmentation Mask (Threshold = {threshold:.2f})", use_column_width=True)
|