osherr commited on
Commit
862a0c0
·
verified ·
1 Parent(s): a3961aa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -23
app.py CHANGED
@@ -45,30 +45,38 @@ model = load_model()
45
  # === Title ===
46
  st.title("🕳️ Sinkhole Segmentation with EffV2-UNet")
47
 
48
- # === File uploader ===
49
- uploaded_image = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg", "tif", "tiff"])
 
50
 
51
- # === Example selector ===
 
 
 
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)) # up to 4 per row
61
-
62
  for i, file in enumerate(example_files):
63
  img_path = os.path.join(example_dir, file)
64
- example_img = Image.open(img_path)
65
  with cols[i % len(cols)]:
66
- if st.button(file, key=file):
67
- uploaded_image = img_path # simulate upload
68
- image = example_img.convert("RGB")
69
- st.image(image, caption=f"Example: {file}", use_column_width=True)
70
 
71
- # === Prediction ===
 
 
 
 
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
- # Confidence threshold slider
81
- threshold = st.slider("Confidence Threshold", 0.0, 1.0, 0.5, step=0.01)
82
-
83
- # Preprocess and predict
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
- st.text(f"Prediction min/max: {y.min():.5f} / {y.max():.5f}")
89
 
90
- # Apply thresholding if needed
91
- mask_bin = (y > threshold).astype(np.uint8) * 255
92
- mask_image = Image.fromarray(mask_bin)
93
 
94
- st.image(mask_image, caption=f"Segmentation Mask (Threshold = {threshold:.2f})", use_column_width=True)
 
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)