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("""
""", 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("
🎯 Boundary Detection System
", unsafe_allow_html=True)
st.markdown("Kenar ve Sınır Tespit Sistemi
", 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("Original / Orijinal
", unsafe_allow_html=True)
st.image(img_rgb, use_container_width=True)
with col2:
st.markdown("Prediction / Tahmin
", 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...")