File size: 4,341 Bytes
82cd534
f4be969
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21eada6
 
 
 
 
 
 
 
 
f4be969
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1033ba9
f4be969
 
 
 
 
 
1033ba9
f4be969
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from PIL import Image
import numpy as np
import os

# --- SAYFA AYARLARI / PAGE CONFIG ---
st.set_page_config(page_title="Fruit & Veg Classifier", layout="wide", page_icon="🍎")

# --- MODEL YÜKLEME / LOAD MODEL (Titremeyi ve tekrar yüklemeyi önler) ---
@st.cache_resource
def load_my_model():
    model_path = "cnn_model.h5"
    if not os.path.exists(model_path):
        return None
    # Model yüklenirken hata oluşursa sessizce yönetir
    try:
        model = tf.keras.models.load_model(model_path)
        return model
    except:
        return None

model = load_my_model()

# --- SINIF İSİMLERİ (Notebook Sıralaması İle Birebir Aynı) ---
# İngilizce (Klasör isimleri)
class_names = [
    'apple', 'banana', 'beetroot', 'bell pepper', 'cabbage', 'capsicum', 
    'carrot', 'cauliflower', 'chilli pepper', 'corn', 'cucumber', 'eggplant', 
    'garlic', 'ginger', 'grapes', 'jalepeno', 'kiwi', 'lemon', 'lettuce', 
    'mango', 'onion', 'orange', 'paprika', 'pear', 'peas', 'pineapple', 
    'pomegranate', 'potato', 'raddish', 'soy beans', 'spinach', 'sweetcorn', 
    'sweetpotato', 'tomato', 'turnip', 'watermelon'
]

# Türkçe Karşılıkları
class_tr = [
    'Elma', 'Muz', 'Pancar', 'Dolmalık Biber', 'Lahana', 'Dolma Biber (Capsicum)', 
    'Havuç', 'Karnabahar', 'Acı Biber', 'Mısır', 'Salatalık', 'Patlıcan', 
    'Sarımsak', 'Zencefil', 'Üzüm', 'Jalapeno Biberi', 'Kivi', 'Limon', 'Marul', 
    'Mango', 'Soğan', 'Portakal', 'Kırmızı Toz Biber', 'Armut', 'Bezelye', 'Ananas', 
    'Nar', 'Patates', 'Turp', 'Soya Fasulyesi', 'Ispanak', 'Tatlı Mısır', 
    'Tatlı Patates', 'Domates', 'Şalgam', 'Karpuz'
]

# --- ARAYÜZ / UI ---
st.title("🍎 Fruit & Veg Classifier / Meyve ve Sebze Sınıflandırıcı")
st.write("36 different species / 36 farklı tür")
st.divider()

# Yan Panel / Sidebar
with st.sidebar:
    st.header("Project Info / Proje Bilgisi")
    st.info("Architecture: Custom 5-Layer CNN\n\nMimari: Özel 5 Katmanlı CNN")
    
    st.subheader("Species List / Tür Listesi")
    # Liste görünümü (İngilizce / Türkçe)
    for en, tr in zip(class_names, class_tr):
        st.write(f"• {en.capitalize()} / {tr}")

# Ana İçerik Alanı
if model is None:
    st.error("Model file 'cnn_model.h5' not found! Please upload the model file. / 'cnn_model.h5' dosyası bulunamadı!")
else:
    col1, col2 = st.columns([1, 1])

    with col1:
        st.subheader("Upload / Yükle 📤")
        uploaded_file = st.file_uploader("Choose a fruit/veg photo...", 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 / Analiz 🔍")
        
        if uploaded_file is not None:
            if st.button("Predict / Tahmin Et"):
                with st.spinner("Analyzing... / Analiz ediliyor..."):
                    # 1. Ön İşleme (Preprocessing)
                    img = image.resize((128, 128))
                    img_array = np.array(img).astype('float32')
                    
                    # 2. Normalizasyon (Hep aynı meyvenin çıkmasını engelleyen kritik adım)
                    img_array /= 255.0 
                    img_array = np.expand_dims(img_array, axis=0)
                    
                    # 3. Tahmin (Prediction)
                    preds = model.predict(img_array, verbose=0)
                    class_idx = np.argmax(preds[0])
                    confidence = np.max(preds[0]) * 100
                    
                    # 4. Sonuçları Göster
                    en_result = class_names[class_idx].capitalize()
                    tr_result = class_tr[class_idx]
                    
                    st.success(f"### Result / Sonuç: {en_result} / {tr_result}")
                    st.write(f"**Confidence / Güven:** %{confidence:.2f}")
                    st.progress(int(confidence))
                    
                    # Kutlama
                    st.balloons()
        else:
            st.info("Waiting for an image to analyze... / Analiz için resim bekleniyor...")

st.divider()
st.caption("Deep Learning Project - Fruit & Vegetable Detection")