File size: 3,849 Bytes
cdb045f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
108
109
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 ---
@st.cache_resource
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("---")
st.caption("Developed by Batman / Gotham City Data Labs 🦇")