ESMATUGBA commited on
Commit
f4be969
·
verified ·
1 Parent(s): 9509ad6

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +97 -12
src/streamlit_app.py CHANGED
@@ -1,5 +1,30 @@
1
 
2
- # app.py içindeki liste Notebook'taki çıktı ile AYNI sırada olmalı
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  class_names = [
4
  'apple', 'banana', 'beetroot', 'bell pepper', 'cabbage', 'capsicum',
5
  'carrot', 'cauliflower', 'chilli pepper', 'corn', 'cucumber', 'eggplant',
@@ -9,16 +34,76 @@ class_names = [
9
  'sweetpotato', 'tomato', 'turnip', 'watermelon'
10
  ]
11
 
12
- # Tahmin kısmında 'titreme' ve 'yanlış sonuç' engelleme:
13
- if st.button("Predict / Tahmin Et"):
14
- with st.spinner("Analyzing..."):
15
- img = image.resize((128, 128))
16
- img_array = np.array(img).astype('float32') / 255.0 # <--- UNUTMA!
17
- img_array = np.expand_dims(img_array, axis=0)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
- preds = model.predict(img_array)
20
- # Notebook'ta test ederken aldığın sonucun aynısını buraya yansıtır
21
- class_idx = np.argmax(preds[0])
22
- result = class_names[class_idx]
 
 
23
 
24
- st.success(f"Result: {result}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 ve tekrar yüklemeyi önler) ---
12
+ @st.cache_resource
13
+ def load_my_model():
14
+ model_path = "cnn_model.h5"
15
+ if not os.path.exists(model_path):
16
+ return None
17
+ # Model yüklenirken hata oluşursa sessizce yönetir
18
+ try:
19
+ model = tf.keras.models.load_model(model_path)
20
+ return model
21
+ except:
22
+ return None
23
+
24
+ model = load_my_model()
25
+
26
+ # --- SINIF İSİMLERİ (Notebook Sıralaması İle Birebir Aynı) ---
27
+ # İngilizce (Klasör isimleri)
28
  class_names = [
29
  'apple', 'banana', 'beetroot', 'bell pepper', 'cabbage', 'capsicum',
30
  'carrot', 'cauliflower', 'chilli pepper', 'corn', 'cucumber', 'eggplant',
 
34
  'sweetpotato', 'tomato', 'turnip', 'watermelon'
35
  ]
36
 
37
+ # Türkçe Karşılıkları
38
+ class_tr = [
39
+ 'Elma', 'Muz', 'Pancar', 'Dolmalık Biber', 'Lahana', 'Dolma Biber (Capsicum)',
40
+ 'Havuç', 'Karnabahar', 'Acı Biber', 'Mısır', 'Salatalık', 'Patlıcan',
41
+ 'Sarımsak', 'Zencefil', 'Üzüm', 'Jalapeno Biberi', 'Kivi', 'Limon', 'Marul',
42
+ 'Mango', 'Soğan', 'Portakal', 'Kırmızı Toz Biber', 'Armut', 'Bezelye', 'Ananas',
43
+ 'Nar', 'Patates', 'Turp', 'Soya Fasulyesi', 'Ispanak', 'Tatlı Mısır',
44
+ 'Tatlı Patates', 'Domates', 'Şalgam', 'Karpuz'
45
+ ]
46
+
47
+ # --- ARAYÜZ / UI ---
48
+ st.title("🍎 Fruit & Veg Classifier / Meyve ve Sebze Sınıflandırıcı")
49
+ st.write("36 different species / 36 farklı tür")
50
+ st.divider()
51
+
52
+ # Yan Panel / Sidebar
53
+ with st.sidebar:
54
+ st.header("Project Info / Proje Bilgisi")
55
+ st.info("Architecture: Custom 5-Layer CNN\n\nMimari: Özel 5 Katmanlı CNN")
56
+
57
+ st.subheader("Species List / Tür Listesi")
58
+ # Liste görünümü (İngilizce / Türkçe)
59
+ for en, tr in zip(class_names, class_tr):
60
+ st.write(f"• {en.capitalize()} / {tr}")
61
+
62
+ # Ana İçerik Alanı
63
+ if model is None:
64
+ st.error("Model file 'cnn_model.h5' not found! Please upload the model file. / 'cnn_model.h5' dosyası bulunamadı!")
65
+ else:
66
+ col1, col2 = st.columns([1, 1])
67
+
68
+ with col1:
69
+ st.subheader("Upload / Yükle 📤")
70
+ uploaded_file = st.file_uploader("Choose a fruit/veg photo...", type=["jpg", "jpeg", "png"])
71
 
72
+ if uploaded_file is not None:
73
+ image = Image.open(uploaded_file)
74
+ st.image(image, caption="Uploaded Image / Yüklenen Resim", use_container_width=True)
75
+
76
+ with col2:
77
+ st.subheader("Analysis / Analiz 🔍")
78
 
79
+ if uploaded_file is not None:
80
+ if st.button("Predict / Tahmin Et"):
81
+ with st.spinner("Analyzing... / Analiz ediliyor..."):
82
+ # 1. Ön İşleme (Preprocessing)
83
+ img = image.resize((128, 128))
84
+ img_array = np.array(img).astype('float32')
85
+
86
+ # 2. Normalizasyon (Hep aynı meyvenin çıkmasını engelleyen kritik adım)
87
+ img_array /= 255.0
88
+ img_array = np.expand_dims(img_array, axis=0)
89
+
90
+ # 3. Tahmin (Prediction)
91
+ preds = model.predict(img_array, verbose=0)
92
+ class_idx = np.argmax(preds[0])
93
+ confidence = np.max(preds[0]) * 100
94
+
95
+ # 4. Sonuçları Göster
96
+ en_result = class_names[class_idx].capitalize()
97
+ tr_result = class_tr[class_idx]
98
+
99
+ st.success(f"### Result / Sonuç: {en_result} / {tr_result}")
100
+ st.write(f"**Confidence / Güven:** %{confidence:.2f}")
101
+ st.progress(int(confidence))
102
+
103
+ # Kutlama
104
+ st.balloons()
105
+ else:
106
+ st.info("Waiting for an image to analyze... / Analiz için resim bekleniyor...")
107
+
108
+ st.divider()
109
+ st.caption("Deep Learning Project - Fruit & Vegetable Detection")