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