Update app.py
Browse files
app.py
CHANGED
|
@@ -1,96 +1,103 @@
|
|
| 1 |
-
|
| 2 |
-
import streamlit as st
|
| 3 |
-
import tensorflow as tf
|
| 4 |
-
from PIL import Image
|
| 5 |
-
import numpy as np
|
| 6 |
-
import os
|
| 7 |
-
|
| 8 |
-
# --- SAYFA AYARLARI / PAGE CONFIG ---
|
| 9 |
-
st.set_page_config(page_title="Fruit & Veg Classifier", layout="wide", page_icon="🍎")
|
| 10 |
-
|
| 11 |
-
# --- MODEL YÜKLEME / LOAD MODEL ---
|
| 12 |
-
@st.cache_resource
|
| 13 |
-
def load_my_model():
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
#
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
'
|
| 28 |
-
'
|
| 29 |
-
'Garlic', 'Ginger'
|
| 30 |
-
'
|
| 31 |
-
'
|
| 32 |
-
'
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
#
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
st.
|
| 48 |
-
st.
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
with
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
if
|
| 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 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
st.caption("Deep Learning Course Project / Derin Öğrenme Kurs Projesi")
|
|
|
|
| 1 |
+
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import tensorflow as tf
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import numpy as np
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
# --- SAYFA AYARLARI / PAGE CONFIG ---
|
| 9 |
+
st.set_page_config(page_title="Fruit & Veg Classifier", layout="wide", page_icon="🍎")
|
| 10 |
+
|
| 11 |
+
# --- MODEL YÜKLEME / LOAD MODEL (Titremeyi önlemek için önbellekleme) ---
|
| 12 |
+
@st.cache_resource
|
| 13 |
+
def load_my_model():
|
| 14 |
+
model_path = "cnn_model.h5"
|
| 15 |
+
# Eğer model .keras formatındaysa ismi ona göre güncelleyin
|
| 16 |
+
if not os.path.exists(model_path):
|
| 17 |
+
return None
|
| 18 |
+
model = tf.keras.models.load_model(model_path)
|
| 19 |
+
return model
|
| 20 |
+
|
| 21 |
+
model = load_my_model()
|
| 22 |
+
|
| 23 |
+
# --- SINIF İSİMLERİ (İngilizce - Türkçe) / CLASS NAMES ---
|
| 24 |
+
# Veri setindeki orijinal (klasör) isimleri solda, Türkçe karşılıkları sağdadır
|
| 25 |
+
class_translations = {
|
| 26 |
+
'Apple': 'Elma', 'Banana': 'Muz', 'Beetroot': 'Pancar', 'Bell Pepper': 'Dolmalık Biber',
|
| 27 |
+
'Cabbage': 'Lahana', 'Capsicum': 'Yeşil Biber', 'Carrot': 'Havuç', 'Cauliflower': 'Karnabahar',
|
| 28 |
+
'Chilli Pepper': 'Acı Biber', 'Corn': 'Mısır', 'Cucumber': 'Salatalık', 'Eggplant': 'Patlıcan',
|
| 29 |
+
'Garlic': 'Sarımsak', 'Ginger': 'Zencefil', 'Grapes': 'Üzüm', 'Jalepeno': 'Jalapeno Biberi',
|
| 30 |
+
'Kiwi': 'Kivi', 'Lemon': 'Limon', 'Lettuce': 'Marul', 'Mango': 'Mango',
|
| 31 |
+
'Onion': 'Soğan', 'Orange': 'Portakal', 'Paprika': 'Kırmızı Toz Biber', 'Pear': 'Armut',
|
| 32 |
+
'Peas': 'Bezelye', 'Pineapple': 'Ananas', 'Pomegranate': 'Nar', 'Potato': 'Patates',
|
| 33 |
+
'Raddish': 'Turp', 'Soy Beans': 'Soya Fasulyesi', 'Spinach': 'Ispanak', 'Sweetcorn': 'Tatlı Mısır',
|
| 34 |
+
'Sweetpotato': 'Tatlı Patates', 'Tomato': 'Domates', 'Turnip': 'Şalgam', 'Watermelon': 'Karpuz'
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
# Orijinal klasör isimleri listesi (Tahmin için)
|
| 38 |
+
class_names = list(class_translations.keys())
|
| 39 |
+
|
| 40 |
+
# --- ARAYÜZ / UI ---
|
| 41 |
+
st.title("🍎 Fruit & Veg Classifier / Meyve ve Sebze Sınıflandırıcı")
|
| 42 |
+
st.write("Identify your produce / Ürünlerinizi tanımlayın.")
|
| 43 |
+
st.divider()
|
| 44 |
+
|
| 45 |
+
# --- YAN PANEL / SIDEBAR (Türkçe İsimler Eklendi) ---
|
| 46 |
+
with st.sidebar:
|
| 47 |
+
st.header("Project Info / Proje Bilgisi")
|
| 48 |
+
st.info("Custom 5-Layer CNN / Özel 5 Katmanlı CNN")
|
| 49 |
+
|
| 50 |
+
st.subheader("Species List / Tür Listesi")
|
| 51 |
+
# Türleri yan yana İngilizce / Türkçe olarak listeler
|
| 52 |
+
for eng, tr in class_translations.items():
|
| 53 |
+
st.write(f"• {eng} / {tr}")
|
| 54 |
+
|
| 55 |
+
# --- ANA İÇERİK / MAIN CONTENT ---
|
| 56 |
+
if model is None:
|
| 57 |
+
st.error("Model file 'cnn_model.h5' not found! / 'cnn_model.h5' dosyası bulunamadı!")
|
| 58 |
+
else:
|
| 59 |
+
col1, col2 = st.columns([1, 1])
|
| 60 |
+
|
| 61 |
+
with col1:
|
| 62 |
+
st.subheader("Upload / Yükle 📤")
|
| 63 |
+
uploaded_file = st.file_uploader("Choose a photo / Fotoğraf seçin...", type=["jpg", "jpeg", "png"])
|
| 64 |
+
|
| 65 |
+
if uploaded_file is not None:
|
| 66 |
+
image = Image.open(uploaded_file)
|
| 67 |
+
st.image(image, caption="Uploaded Image / Yüklenen Resim", use_container_width=True)
|
| 68 |
+
|
| 69 |
+
with col2:
|
| 70 |
+
st.subheader("Result / Sonuç 🔍")
|
| 71 |
+
|
| 72 |
+
if uploaded_file is not None:
|
| 73 |
+
# Butona basıldığında titreme yapmaması için tüm işlemler bu bloğun içinde kalmalı
|
| 74 |
+
if st.button("Predict / Tahmin Et"):
|
| 75 |
+
with st.spinner("Processing... / İşleniyor..."):
|
| 76 |
+
# Görüntü İşleme
|
| 77 |
+
img = image.resize((128, 128))
|
| 78 |
+
img_array = np.array(img).astype('float32')
|
| 79 |
+
|
| 80 |
+
# Normalizasyon
|
| 81 |
+
if img_array.max() > 1:
|
| 82 |
+
img_array /= 255.0
|
| 83 |
+
|
| 84 |
+
img_array = np.expand_dims(img_array, axis=0)
|
| 85 |
+
|
| 86 |
+
# Tahmin
|
| 87 |
+
predictions = model.predict(img_array, verbose=0)
|
| 88 |
+
score = np.max(predictions[0]) * 100
|
| 89 |
+
class_idx = np.argmax(predictions[0])
|
| 90 |
+
|
| 91 |
+
eng_res = class_names[class_idx]
|
| 92 |
+
tr_res = class_translations[eng_res]
|
| 93 |
+
|
| 94 |
+
# Sonuç Ekranı
|
| 95 |
+
st.success(f"### Result / Sonuç: {eng_res} / {tr_res}")
|
| 96 |
+
st.write(f"**Confidence / Güven:** %{score:.2f}")
|
| 97 |
+
st.progress(int(score))
|
| 98 |
+
st.balloons()
|
| 99 |
+
else:
|
| 100 |
+
st.info("Waiting for image... / Resim bekleniyor...")
|
| 101 |
+
|
| 102 |
+
st.divider()
|
| 103 |
st.caption("Deep Learning Course Project / Derin Öğrenme Kurs Projesi")
|