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

Update app.py

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