import streamlit as st import cv2 import numpy as np import joblib import os import warnings from tensorflow.keras.models import load_model # Gereksiz sistem uyarılarını kapat / Silence system warnings warnings.filterwarnings('ignore') os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' # Sayfa Ayarları / Page Configuration st.set_page_config(page_title="Pencil Sketch & Face Recognition", layout="wide") # --- 1. MODELLERİ ÖNBELLEĞE ALARAK YÜKLE / LOAD MODELS WITH CACHING --- @st.cache_resource def load_all_models(): m1_path = 'model1_lbph.yml' m2_path = 'model2_knn.pkl' m3_path = 'model3_cnn.keras' try: # LBPH m1 = cv2.face.LBPHFaceRecognizer_create() m1.read(m1_path) # KNN (Version ignore) m2 = joblib.load(m2_path) # CNN (No compile to avoid optimizer errors) m3 = load_model(m3_path, compile=False) return m1, m2, m3 except Exception as e: return None, None, None # Modelleri yükle model1, model2, model3 = load_all_models() # --- 2. SOL PANEL (SIDEBAR) / SIDEBAR DESIGN --- st.sidebar.title("⚙️ Sistem Paneli / System Panel") st.sidebar.divider() if model1 is not None: st.sidebar.success(""" ### ✅ Durum / Status: **Modeller Yüklendi! / Models Loaded!** * **LBPH:** Aktif / Active * **KNN:** Aktif / Active * **CNN:** Aktif / Active """) st.sidebar.info(""" **ℹ️ Not / Note:** Resim yüklendiğinde 3 model aynı anda çalışır. (3 models will process simultaneously.) """) else: st.sidebar.error(""" ### ❌ Hata / Error: **Modeller Bulunamadı! / Models Not Found!** Lütfen dosyaları kontrol edin. """) st.sidebar.divider() st.sidebar.caption("🚀 Final Project - Computer Vision") # --- 3. ANA ARAYÜZ / MAIN INTERFACE --- st.title("🎨 Karakalem & Yüz Tanıma | Pencil Sketch & Face Recognition") st.write("---") # Karakalem Fonksiyonu / Sketch Function def get_sketch(image): gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) blur = cv2.GaussianBlur(gray, (5,5), 0) edges = cv2.Canny(blur, 10, 70) ret, mask = cv2.threshold(edges, 250, 255, cv2.THRESH_BINARY_INV) return mask # Dosya Yükleme / File Upload uploaded_file = st.file_uploader("Bir Resim Seçin / Choose an Image", type=["jpg", "png", "jpeg"]) if uploaded_file is not None: # Görüntüyü oku / Read image file_bytes = np.frombuffer(uploaded_file.read(), np.uint8) img = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR) if img is not None: # Görsel Sonuçlar / Visual Results col1, col2 = st.columns(2) with col1: st.subheader("🖼️ Orijinal / Original") # use_container_width yerine yeni standart olan width='stretch' kullanıldı st.image(cv2.cvtColor(img, cv2.COLOR_BGR2RGB), width=500) with col2: st.subheader("✍️ Karakalem / Sketch") sketch_res = get_sketch(img) st.image(sketch_res, width=500) # Tahmin Bölümü / Prediction Section if model1 is not None: st.divider() st.header("🤖 Model Analizleri / Model Analysis") t1, t2, t3 = st.columns(3) # LBPH Tahmin with t1: try: gray_lb = cv2.resize(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY), (200,200)) label, conf = model1.predict(gray_lb) st.metric("LBPH Sonucu", f"ID: {label}", f"Güven: {round(conf,1)}") except: st.error("LBPH Error") # KNN Tahmin with t2: try: gray_knn = cv2.resize(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY), (100,100)).flatten().reshape(1,-1) res2 = model2.predict(gray_knn) st.metric("KNN Sonucu", str(res2[0])) except: st.error("KNN Error") # CNN Tahmin with t3: try: gray_cnn = cv2.resize(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY), (64,64)) / 255.0 res3 = model3.predict(gray_cnn.reshape(1,64,64,1), verbose=0) st.metric("CNN Sınıf", np.argmax(res3)) except: st.error("CNN Error")