Malaria_Cell_Detection / src /streamlit_app.py
HarunDemircioglu11's picture
Update src/streamlit_app.py
dc67056 verified
import streamlit as st
from PIL import Image
import numpy as np
from tensorflow.keras.models import load_model
st.set_page_config(page_title="Sıtma Sınıflandırıcı", page_icon="🦟")
st.title("🦟 Sıtma Resmi Sınıflandırma")
st.write("Bir mikroskop görüntüsü yükleyin, sıtma olup olmadığını tahmin edelim.")
# MODEL YÜKLEME
model_path = "src/myn_cnn_model.h5"
try:
model = load_model(model_path, compile=False)
st.success(f"✅ Model yüklendi: `{model_path}`")
except Exception as e:
st.error(f"❌ Model yüklenemedi: {e}")
st.stop()
# SINIF ADLARI (Senin sıralamana göre değiştir)
class_names = ["Sıtma", "Sıtma Değil"] # Eğer model ters sıralıysa değiştir
# RESİM YÜKLEYİCİ
file = st.file_uploader("📷 Mikroskop Görüntüsü Seçin", type=["jpg", "jpeg", "png"])
if file:
try:
image = Image.open(file).convert("RGB")
st.image(image, caption="Yüklenen Görsel", use_container_width=True)
# MODELİN INPUT BOYUTUNU AL
input_shape = model.input_shape[1:3] # Örneğin (170, 170)
st.write(f"Modelin beklediği görsel boyutu: {input_shape}")
# GÖRSELİ AYARLA
img = image.resize(input_shape)
img = np.array(img)
if img.shape != (*input_shape, 3):
st.error(f"Görsel boyutu beklenmeyen formatta: {img.shape}")
st.stop()
img = img / 255.0
img = np.expand_dims(img, axis=0)
# TAHMİN ET
prediction = model.predict(img)
# MODELİN SOFTMAX Mİ SİGMOID Mİ OLDUĞUNU ANLA
if prediction.shape[-1] == 1:
predicted_class = int(prediction[0][0] > 0.5)
st.success(f"🧪 Tahmin: **{class_names[predicted_class]}**")
st.write(f"Sıtma olasılığı: {prediction[0][0]:.4f}")
elif prediction.shape[-1] == 2:
predicted_class = int(np.argmax(prediction))
st.success(f"🧪 Tahmin: **{class_names[predicted_class]}**")
st.subheader("📈 Sınıf Skorları:")
for i, score in enumerate(prediction[0]):
st.write(f"{class_names[i]}: {score:.4f}")
else:
st.error("⚠️ Model çıktısı beklenmeyen formatta.")
st.stop()
except Exception as e:
st.error(f"🚫 Görsel işleme veya tahmin hatası: {e}")