ESMATUGBA commited on
Commit
82cd534
·
verified ·
1 Parent(s): ce31277

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +94 -38
src/streamlit_app.py CHANGED
@@ -1,40 +1,96 @@
1
- import altair as alt
2
- import numpy as np
3
- import pandas as pd
4
  import streamlit as st
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
- """
7
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
40
- ))
 
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
+ # Model dosya adının 'cnn_model.h5' olduğundan emin olun
15
+ model_path = "cnn_model.h5"
16
+ model = tf.keras.models.load_model(model_path)
17
+ return model
18
+
19
+ try:
20
+ model = load_my_model()
21
+ except Exception as e:
22
+ st.error(f"Model dosyası bulunamadı! / Model file not found!: {e}")
23
+
24
+ # --- SINIF İSİMLERİ / CLASS NAMES ---
25
+ # Veri setindeki klasör sırasına göre burayı güncelleyin
26
+ class_names = [
27
+ 'Apple', 'Banana', 'Beetroot', 'Bell Pepper', 'Cabbage', 'Capsicum',
28
+ 'Carrot', 'Cauliflower', 'Chilli Pepper', 'Corn', 'Cucumber', 'Eggplant',
29
+ 'Garlic', 'Ginger', 'Grapes', 'Jalepeno', 'Kiwi', 'Lemon', 'Lettuce',
30
+ 'Mango', 'Onion', 'Orange', 'Paprika', 'Pear', 'Peas', 'Pineapple',
31
+ 'Pomegranate', 'Potato', 'Raddish', 'Soy Beans', 'Spinach', 'Sweetcorn',
32
+ 'Sweetpotato', 'Tomato', 'Turnip', 'Watermelon'
33
+ ]
34
+
35
+ # --- ARAYÜZ / UI ---
36
+ st.title("🍎 Fruit & Veg Classifier / Meyve ve Sebze Sınıflandırıcı")
37
+ st.write("Identify your produce using Deep Learning / Derin Öğrenme ile ürünlerinizi tanımlayın.")
38
+ st.divider()
39
+
40
+ # Yan Panel / Sidebar
41
+ with st.sidebar:
42
+ st.header("Project Info / Proje Bilgisi")
43
+ st.info("""
44
+ **EN:** This model uses a custom 5-layer CNN architecture trained on fruit and vegetable images.
45
+ \n**TR:** Bu model, meyve ve sebze görüntüleri üzerinde eğitilmiş 5 katmanlı özel bir CNN mimarisi kullanır.
46
+ """)
47
+ st.subheader("Species / Türler")
48
+ st.write(", ".join(class_names))
49
+
50
+ # Ana İçerik / Main Content
51
+ col1, col2 = st.columns([1, 1])
52
+
53
+ with col1:
54
+ st.subheader("Upload / Yükle 📤")
55
+ uploaded_file = st.file_uploader("Choose a photo / Bir fotoğraf seçin...", type=["jpg", "jpeg", "png"])
56
+
57
+ if uploaded_file is not None:
58
+ image = Image.open(uploaded_file)
59
+ st.image(image, caption="Uploaded Image / Yüklenen Resim", use_container_width=True)
60
+
61
+ with col2:
62
+ st.subheader("Result / Sonuç 🔍")
63
+
64
+ if uploaded_file is not None:
65
+ if st.button("Predict / Tahmin Et"):
66
+ with st.spinner("Processing... / İşleniyor..."):
67
+ # Ön İşleme / Preprocessing (Modelin eğitildiği boyuta göre: 128x128)
68
+ img = image.resize((128, 128))
69
+ img_array = np.array(img)
70
+
71
+ # Eğer model 0-1 arası normalizasyon istiyorsa:
72
+ if img_array.max() > 1:
73
+ img_array = img_array.astype('float32') / 255.0
74
+
75
+ img_array = np.expand_dims(img_array, axis=0)
76
+
77
+ # Tahmin / Prediction
78
+ predictions = model.predict(img_array)
79
+ score = np.max(predictions[0]) * 100
80
+ class_idx = np.argmax(predictions[0])
81
+ result = class_names[class_idx]
82
+
83
+ # Sonuç Ekranı / Result Display
84
+ st.success(f"### {result}")
85
+ st.write(f"**Confidence / Güven:** %{score:.2f}")
86
+
87
+ # İlerleme Çubuğu / Progress Bar
88
+ st.progress(int(score))
89
+
90
+ # Kutlama / Celebration
91
+ st.balloons()
92
+ else:
93
+ st.write("Please upload an image to see the result. / Sonucu görmek için lütfen bir resim yükleyin.")
94
 
95
+ st.divider()
96
+ st.caption("Deep Learning Course Project / Derin Öğrenme Kurs Projesi")