File size: 4,165 Bytes
cff90be | 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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | import streamlit as st
import tensorflow as tf
from tensorflow.keras.applications import VGG16
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D, Dropout
from PIL import Image
import numpy as np
import os
# --- SAYFA AYARLARI ---
st.set_page_config(page_title="Bird Identifier / Kuş Tanımlayıcı", layout="wide", page_icon="🐦")
# --- MODEL YÜKLEME (TİTREMEYİ VE TEKRAR YÜKLEMEYİ ÖNLER) ---
@st.cache_resource
def load_bird_model():
# Modelin yanındaki dosyayı bulmak için tam yol tespiti
current_dir = os.path.dirname(os.path.abspath(__file__))
weights_path = os.path.join(current_dir, "bird_weights.weights.h5")
# Model Mimarisi (Kaggle'daki model2 ile birebir aynı olmalı)
base_model = VGG16(weights=None, include_top=False, input_shape=(128, 128, 3))
model = Sequential([
base_model,
GlobalAveragePooling2D(),
Dense(256, activation='relu'),
Dropout(0.6),
Dense(25, activation='softmax')
])
# Ağırlıkları yükle
if os.path.exists(weights_path):
model.load_weights(weights_path)
else:
# Eğer dosya ana dizindeyse doğrudan ismen yüklemeyi dene
model.load_weights("bird_weights.weights.h5")
return model
# Uygulama başladığında modeli yükle (Cache sayesinde titreme yapmaz)
try:
model = load_bird_model()
except Exception as e:
st.error(f"Model yüklenirken bir hata oluştu / Error loading model: {e}")
# --- KUŞ TÜRLERİ LİSTESİ ---
class_names = [
'Alexandrine Parakeet', 'Asian Green Bee-Eater', 'Baya Weaver', 'Black Drongo',
'Black-Crowned Night Heron', 'Blue-Throated Barbet', 'Brown-Headed Barbet',
'Cattle Egret', 'Common Kingfisher', 'Common Myna', 'Common Rosefinch',
'Common Tailorbird', 'Coppersmith Barbet', 'Grey Heron', 'Hoopoe',
'Indian Peafowl', 'Indian Roller', 'Indian Silverbill', 'Jungle Babbler',
'Little Egret', 'Pied Kingfisher', 'Purple Sunbird', 'Red-Wattled Lapwing',
'Slaty-Headed Parakeet', 'White-Throated Kingfisher'
]
# --- YAN PANEL (SIDEBAR) ---
with st.sidebar:
st.title("Settings / Ayarlar ⚙️")
st.divider()
st.subheader("Recognized Species / Tanınan Türler 🐦")
for name in sorted(class_names):
st.write(f"• {name}")
# --- ANA EKRAN (GÖRSEL DÜZEN) ---
st.title("Bird Species Classifier / Kuş Türü Sınıflandırıcı 🐦")
st.write("Identify 25 Indian bird species / 25 farklı Hint kuş türünü tanımlayın.")
st.divider()
col1, col2 = st.columns([1, 1])
with col1:
st.subheader("Upload Image / Resim Yükle 📤")
uploaded_file = st.file_uploader("Choose a bird photo / Bir kuş fotoğrafı seçin...", type=["jpg", "jpeg", "png"])
if uploaded_file is not None:
image = Image.open(uploaded_file)
st.image(image, caption="Uploaded Image / Yüklenen Resim", use_container_width=True)
with col2:
st.subheader("Analysis Results / Analiz Sonuçları 🔍")
if uploaded_file is not None:
# Tahmin butonu
if st.button("Predict / Tahmin Et"):
with st.spinner("Analyzing... / Analiz ediliyor..."):
# Görüntü Ön İşleme
img = image.resize((128, 128))
img_array = np.array(img).astype('float32') / 255.0
img_array = np.expand_dims(img_array, axis=0)
# Tahmin
preds = model.predict(img_array)
class_idx = np.argmax(preds[0])
confidence = np.max(preds[0]) * 100
# Sonuçların Yazdırılması
st.success(f"**Result / Sonuç:** {class_names[class_idx]}")
st.write(f"**Confidence / Güven:** %{confidence:.2f}")
st.progress(int(confidence))
# Kutlama (Balonlar)
st.balloons()
else:
st.info("Waiting for an image to analyze... / Analiz için resim bekleniyor...")
st.divider()
st.caption("Developed with TensorFlow & Streamlit | Indian Bird Dataset Project") |