Update src/streamlit_app.py
Browse files- src/streamlit_app.py +80 -38
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,82 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
|
| 10 |
-
If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
|
| 11 |
-
forums](https://discuss.streamlit.io).
|
| 12 |
-
|
| 13 |
-
In the meantime, below is an example of what you can do with just a few lines of code:
|
| 14 |
-
"""
|
| 15 |
-
|
| 16 |
-
num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
|
| 17 |
-
num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
|
| 18 |
-
|
| 19 |
-
indices = np.linspace(0, 1, num_points)
|
| 20 |
-
theta = 2 * np.pi * num_turns * indices
|
| 21 |
-
radius = indices
|
| 22 |
-
|
| 23 |
-
x = radius * np.cos(theta)
|
| 24 |
-
y = radius * np.sin(theta)
|
| 25 |
-
|
| 26 |
-
df = pd.DataFrame({
|
| 27 |
-
"x": x,
|
| 28 |
-
"y": y,
|
| 29 |
-
"idx": indices,
|
| 30 |
-
"rand": np.random.randn(num_points),
|
| 31 |
-
})
|
| 32 |
-
|
| 33 |
-
st.altair_chart(alt.Chart(df, height=700, width=700)
|
| 34 |
-
.mark_point(filled=True)
|
| 35 |
-
.encode(
|
| 36 |
-
x=alt.X("x", axis=None),
|
| 37 |
-
y=alt.Y("y", axis=None),
|
| 38 |
-
color=alt.Color("idx", legend=None, scale=alt.Scale()),
|
| 39 |
-
size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
|
| 40 |
-
))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import numpy as np
|
| 3 |
+
import cv2
|
| 4 |
+
from tensorflow.keras.models import load_model
|
| 5 |
+
|
| 6 |
+
# 1. Page Configuration / Sayfa Ayarları (Centered layout seçildi)
|
| 7 |
+
st.set_page_config(page_title="CNN Boundary Detector", layout="centered")
|
| 8 |
+
|
| 9 |
+
# Sabitleme ve Titremeyi Önleme için CSS
|
| 10 |
+
st.markdown("""
|
| 11 |
+
<style>
|
| 12 |
+
.stImage > img {
|
| 13 |
+
border-radius: 8px;
|
| 14 |
+
border: 1px solid #ddd;
|
| 15 |
+
}
|
| 16 |
+
/* Sütunlar arasındaki boşluğu ve hizalamayı koru */
|
| 17 |
+
[data-testid="stHorizontalBlock"] {
|
| 18 |
+
align-items: center;
|
| 19 |
+
}
|
| 20 |
+
</style>
|
| 21 |
+
""", unsafe_allow_html=True)
|
| 22 |
+
|
| 23 |
+
@st.cache_resource
|
| 24 |
+
def load_my_model():
|
| 25 |
+
# Model ismini kendi dosya isminle değiştir (.h5 veya .keras)
|
| 26 |
+
return load_model("cnn_segmentation_model.keras", compile=False)
|
| 27 |
+
|
| 28 |
+
model = load_my_model()
|
| 29 |
+
|
| 30 |
+
# Header / Başlık (Ortalı)
|
| 31 |
+
st.markdown("<h1 style='text-align: center;'>🎯 Boundary Detection System</h1>", unsafe_allow_html=True)
|
| 32 |
+
st.markdown("<h3 style='text-align: center; color: gray;'>Kenar ve Sınır Tespit Sistemi</h3>", unsafe_allow_html=True)
|
| 33 |
+
st.write("---")
|
| 34 |
+
|
| 35 |
+
# 2. Upload Section / Yükleme Bölümü
|
| 36 |
+
uploaded_file = st.file_uploader("Upload Image / Resim Yükleyin", type=["jpg", "jpeg", "png"])
|
| 37 |
+
|
| 38 |
+
if uploaded_file is not None:
|
| 39 |
+
# Görüntü İşleme
|
| 40 |
+
file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
|
| 41 |
+
img = cv2.imdecode(file_bytes, 1)
|
| 42 |
+
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 43 |
+
|
| 44 |
+
# Model Tahmini (168x168)
|
| 45 |
+
img_input = cv2.resize(img_rgb, (168, 168)) / 255.0
|
| 46 |
+
img_input = np.expand_dims(img_input, axis=0)
|
| 47 |
+
|
| 48 |
+
with st.spinner('Analyzing... / Analiz ediliyor...'):
|
| 49 |
+
pred = model.predict(img_input, verbose=0)[0]
|
| 50 |
+
mask = pred.squeeze()
|
| 51 |
+
|
| 52 |
+
# Notebook stili parlatma (Normalization)
|
| 53 |
+
mask_norm = (mask - mask.min()) / (mask.max() - mask.min() + 1e-7)
|
| 54 |
+
mask_255 = (mask_norm * 255).astype(np.uint8)
|
| 55 |
+
|
| 56 |
+
# Orijinal boyuta geri getir
|
| 57 |
+
mask_resized = cv2.resize(mask_255, (img_rgb.shape[1], img_rgb.shape[0]))
|
| 58 |
+
|
| 59 |
+
# Overlay (Yeşil Kenar)
|
| 60 |
+
overlay = img_rgb.copy()
|
| 61 |
+
# Eşik (Threshold) 120 olarak ayarlandı
|
| 62 |
+
overlay[mask_resized > 120] = [0, 255, 0]
|
| 63 |
+
final_blend = cv2.addWeighted(img_rgb, 0.7, overlay, 0.3, 0)
|
| 64 |
+
|
| 65 |
+
# 3. YAN YANA VE ORTALANMIŞ GÖSTERİM
|
| 66 |
+
col1, col2 = st.columns(2)
|
| 67 |
+
|
| 68 |
+
with col1:
|
| 69 |
+
st.markdown("<p style='text-align: center; font-weight: bold;'>Original / Orijinal</p>", unsafe_allow_html=True)
|
| 70 |
+
st.image(img_rgb, use_container_width=True)
|
| 71 |
+
|
| 72 |
+
with col2:
|
| 73 |
+
st.markdown("<p style='text-align: center; font-weight: bold;'>Prediction / Tahmin</p>", unsafe_allow_html=True)
|
| 74 |
+
st.image(final_blend, use_container_width=True)
|
| 75 |
+
|
| 76 |
+
# Opsiyonel: Siyah Beyaz Maske
|
| 77 |
+
st.write("---")
|
| 78 |
+
with st.expander("Show Binary Mask / İkili Maskeyi Göster"):
|
| 79 |
+
st.image(mask_resized, width=400, caption="Grayscale Output")
|
| 80 |
|
| 81 |
+
else:
|
| 82 |
+
st.info("Waiting for image upload... / Resim yüklenmesi bekleniyor...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|