|
|
| import streamlit as st |
| import tensorflow as tf |
| from PIL import Image |
| import numpy as np |
| import os |
|
|
| |
| st.set_page_config(page_title="Fruit & Veg Classifier", layout="wide", page_icon="🍎") |
|
|
| |
| @st.cache_resource |
| def load_my_model(): |
| model_path = "cnn_model.h5" |
| if not os.path.exists(model_path): |
| return None |
| |
| try: |
| model = tf.keras.models.load_model(model_path) |
| return model |
| except: |
| return None |
|
|
| model = load_my_model() |
|
|
| |
| |
| 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' |
| ] |
|
|
| |
| 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' |
| ] |
|
|
| |
| st.title("🍎 Fruit & Veg Classifier / Meyve ve Sebze Sınıflandırıcı") |
| st.write("36 different species / 36 farklı tür") |
| st.divider() |
|
|
| |
| 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") |
| |
| for en, tr in zip(class_names, class_tr): |
| st.write(f"• {en.capitalize()} / {tr}") |
|
|
| |
| 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..."): |
| |
| img = image.resize((128, 128)) |
| img_array = np.array(img).astype('float32') |
| |
| |
| img_array /= 255.0 |
| img_array = np.expand_dims(img_array, axis=0) |
| |
| |
| preds = model.predict(img_array, verbose=0) |
| class_idx = np.argmax(preds[0]) |
| confidence = np.max(preds[0]) * 100 |
| |
| |
| 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)) |
| |
| |
| 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") |