Download src/streamlit_app.py from ESMATUGBA/Fish-Classification-System: direct link, hf CLI and curl.
- Browser
- Download file 3.68 kB
-
https://huggingface.co/spaces/ESMATUGBA/Fish-Classification-System/resolve/cdb045f97435b23dcf4f4cbb08359bae53dc663c/src/streamlit_app.py
- Command line
-
hf download hf://spaces/ESMATUGBA/Fish-Classification-System@cdb045f97435b23dcf4f4cbb08359bae53dc663c/src/streamlit_app.py
-
curl -L -o streamlit_app.py https://huggingface.co/spaces/ESMATUGBA/Fish-Classification-System/resolve/cdb045f97435b23dcf4f4cbb08359bae53dc663c/src/streamlit_app.py
3.68 kB
| import streamlit as st | |
| import tensorflow as tf | |
| import numpy as np | |
| import cv2 | |
| from PIL import Image | |
| from tensorflow.keras.applications import MobileNetV2 | |
| from tensorflow.keras.models import Sequential | |
| from tensorflow.keras.layers import GlobalAveragePooling2D, Dense, Dropout | |
| # --- 1. SAYFA AYARLARI / PAGE CONFIG --- | |
| st.set_page_config(page_title="Fish Classifier / Balık Sınıflandırıcı", page_icon="🐟", layout="wide") | |
| # --- 2. MODEL YÜKLEME / LOAD MODEL --- | |
| def load_my_model(): | |
| # Mimariyi manuel kuruyoruz (Sürüm çakışmasını önlemek için) | |
| base_model = MobileNetV2(weights='imagenet', include_top=False, input_shape=(170, 170, 3)) | |
| base_model.trainable = False | |
| model = Sequential([ | |
| base_model, | |
| GlobalAveragePooling2D(), | |
| Dense(256, activation='relu'), | |
| Dropout(0.5), | |
| Dense(9, activation='softmax') | |
| ]) | |
| # Ağırlıkları yükle | |
| try: | |
| model.load_weights('fish_transfer_model.h5') | |
| except: | |
| model = tf.keras.models.load_model('fish_transfer_model.h5') | |
| return model | |
| model = load_my_model() | |
| # --- 3. SÖZLÜK / DICTIONARY --- | |
| translate = { | |
| 'Black Sea Sprat': 'Karadeniz Çaça', | |
| 'Gilt-Head Bream': 'Çipura', | |
| 'Hourse Mackerel': 'İstavrit', | |
| 'Red Mullet': 'Barbun', | |
| 'Red Sea Bream': 'Mercan', | |
| 'Sea Bass': 'Levrek', | |
| 'Shrimp': 'Karides', | |
| 'Striped Red Mullet': 'Tekir', | |
| 'Trout': 'Alabalık' | |
| } | |
| class_labels = sorted(list(translate.keys())) | |
| # --- 4. SOL PANEL / SIDEBAR --- | |
| with st.sidebar: | |
| st.title("🔍 Species List / Tür Listesi") | |
| st.write("Supported Fish Types / Desteklenen Balıklar:") | |
| for en, tr in translate.items(): | |
| st.write(f"🔹 **{en}** / {tr}") | |
| st.markdown("---") | |
| st.info("Model: MobileNetV2\n\nAccuracy / Doğruluk: %99") | |
| # --- 5. ANA EKRAN / MAIN SCREEN --- | |
| st.title("🐟 Fish Classification System / Akıllı Balık Tanımlama") | |
| st.subheader("Deep Learning Project / Derin Öğrenme Projesi") | |
| # Sabit konteyner (Titremeyi önler) | |
| main_container = st.container() | |
| with main_container: | |
| uploaded_file = st.file_uploader("Upload an image... / Bir resim yükleyin...", type=["jpg", "png", "jpeg"]) | |
| if uploaded_file is not None: | |
| col1, col2 = st.columns([1, 1]) | |
| # Sol Kolon: Resim | |
| image = Image.open(uploaded_file) | |
| col1.image(image, caption="Uploaded Image / Yüklenen Resim", use_container_width=True) | |
| # Sağ Kolon: Tahmin | |
| with col2: | |
| st.write("### Analysis Result / Analiz Sonucu") | |
| # --- ÖN İŞLEME / PREPROCESSING --- | |
| img = np.array(image.convert('RGB')) | |
| img = cv2.resize(img, (170, 170)) | |
| img = img / 255.0 | |
| img = np.expand_dims(img, axis=0) | |
| # --- TAHMİN / PREDICTION --- | |
| preds = model.predict(img) | |
| idx = np.argmax(preds) | |
| prob = np.max(preds) * 100 | |
| label_en = class_labels[idx] | |
| label_tr = translate[label_en] | |
| # --- GÖRSEL KUTLAMA / CELEBRATION --- | |
| st.success(f"**Result / Sonuç:** {label_en} / {label_tr}") | |
| st.balloons() # Balonlar burada uçuyor! 🎈 | |
| st.metric(label="Confidence / Güven Oranı", value=f"%{prob:.2f}") | |
| # Olasılık Grafiği / Chart | |
| st.write("Probabilities / Olasılıklar:") | |
| chart_data = {f"{k} / {translate[k]}": float(preds[0][i]) for i, k in enumerate(class_labels)} | |
| st.bar_chart(chart_data) | |
| # --- 6. ALT BİLGİ / FOOTER --- | |
| st.markdown("---") |