Download src/streamlit_app.py from ESMATUGBA/Boundary-Detection-System: direct link, hf CLI and curl.
- Browser
- Download file 2.99 kB
-
https://huggingface.co/spaces/ESMATUGBA/Boundary-Detection-System/resolve/main/src/streamlit_app.py
- Command line
-
hf download hf://spaces/ESMATUGBA/Boundary-Detection-System/src/streamlit_app.py
-
curl -L -o streamlit_app.py https://huggingface.co/spaces/ESMATUGBA/Boundary-Detection-System/resolve/main/src/streamlit_app.py
2.99 kB
| 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) | |
| 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...") |