File size: 3,069 Bytes
de1987e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | import streamlit as st
import numpy as np
import cv2
from tensorflow.keras.models import load_model
# 1. Page Configuration / Sayfa Ayarları (Centered layout seçildi)
st.set_page_config(page_title="CNN Boundary Detector", layout="centered")
# Sabitleme ve Titremeyi Önleme için CSS
st.markdown("""
<style>
.stImage > img {
border-radius: 8px;
border: 1px solid #ddd;
}
/* Sütunlar arasındaki boşluğu ve hizalamayı koru */
[data-testid="stHorizontalBlock"] {
align-items: center;
}
</style>
""", unsafe_allow_html=True)
@st.cache_resource
def load_my_model():
# Model ismini kendi dosya isminle değiştir (.h5 veya .keras)
return load_model("cnn_segmentation_model.keras", compile=False)
model = load_my_model()
# Header / Başlık (Ortalı)
st.markdown("<h1 style='text-align: center;'>🎯 Boundary Detection System</h1>", unsafe_allow_html=True)
st.markdown("<h3 style='text-align: center; color: gray;'>Kenar ve Sınır Tespit Sistemi</h3>", unsafe_allow_html=True)
st.write("---")
# 2. Upload Section / Yükleme Bölümü
uploaded_file = st.file_uploader("Upload Image / Resim Yükleyin", type=["jpg", "jpeg", "png"])
if uploaded_file is not None:
# Görüntü İşleme
file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
img = cv2.imdecode(file_bytes, 1)
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# Model Tahmini (168x168)
img_input = cv2.resize(img_rgb, (168, 168)) / 255.0
img_input = np.expand_dims(img_input, axis=0)
with st.spinner('Analyzing... / Analiz ediliyor...'):
pred = model.predict(img_input, verbose=0)[0]
mask = pred.squeeze()
# Notebook stili parlatma (Normalization)
mask_norm = (mask - mask.min()) / (mask.max() - mask.min() + 1e-7)
mask_255 = (mask_norm * 255).astype(np.uint8)
# Orijinal boyuta geri getir
mask_resized = cv2.resize(mask_255, (img_rgb.shape[1], img_rgb.shape[0]))
# Overlay (Yeşil Kenar)
overlay = img_rgb.copy()
# Eşik (Threshold) 120 olarak ayarlandı
overlay[mask_resized > 120] = [0, 255, 0]
final_blend = cv2.addWeighted(img_rgb, 0.7, overlay, 0.3, 0)
# 3. YAN YANA VE ORTALANMIŞ GÖSTERİM
col1, col2 = st.columns(2)
with col1:
st.markdown("<p style='text-align: center; font-weight: bold;'>Original / Orijinal</p>", unsafe_allow_html=True)
st.image(img_rgb, use_container_width=True)
with col2:
st.markdown("<p style='text-align: center; font-weight: bold;'>Prediction / Tahmin</p>", unsafe_allow_html=True)
st.image(final_blend, use_container_width=True)
# Opsiyonel: Siyah Beyaz Maske
st.write("---")
with st.expander("Show Binary Mask / İkili Maskeyi Göster"):
st.image(mask_resized, width=400, caption="Grayscale Output")
else:
st.info("Waiting for image upload... / Resim yüklenmesi bekleniyor...") |