FruitVegitable / src /streamlit_app.py
zaidasim232's picture
Update src/streamlit_app.py
02bb38d verified
Raw
History Blame Contribute Delete
2.91 kB
import streamlit as st
import numpy as np
from PIL import Image
import tensorflow as tf
from tensorflow.keras.models import load_model
from tensorflow.keras.applications.mobilenet import preprocess_input
import os
# --- KERAS DESERIALIZATION YAMASI (Hatanı Çözen Kısım) ---
import keras.src.saving.serialization_lib as serialization_lib
_original_deserialize = serialization_lib.deserialize_keras_object
def safe_deserialize(config, *args, **kwargs):
if isinstance(config, dict):
# Eğer katman konfigürasyonunda quantization_config varsa sil
if "config" in config and isinstance(config["config"], dict):
config["config"].pop("quantization_config", None)
config.pop("quantization_config", None)
return _original_deserialize(config, *args, **kwargs)
serialization_lib.deserialize_keras_object = safe_deserialize
# --------------------------------------------------------
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
MODEL_PATH = os.path.join(BASE_DIR, 'fruit_n_veg_MobileNet_son_model.keras')
model = load_model(MODEL_PATH)
def process_image(img):
# RGB formatına dönüştür (PNG şeffaflık kanallarını temizler)
img = img.convert('RGB')
img = img.resize((224, 224))
# Numpy dizisine çevir (float32 tipinde)
img_array = np.array(img, dtype=np.float32)
# MobileNet için özel ön işleme (RGB -> BGR dönüşümü ve ImageNet mean extraction yapar)
img_array = preprocess_input(img_array)
#img_array = img_array / 255.0 # Normalizasyon (0-1 aralığına getir)
# Modelin beklediği batch boyutunu ekle: (1, 224, 224, 3)
img_array = np.expand_dims(img_array, axis=0)
return img_array
st.title('Meyve ve Sebze Sınıflandırma (Fruit and Vegetable Classification)')
st.write('Bir meyve veya sebze fotoğrafı yükleyin, MobileNet modeli sınıflandırsın!')
file = st.file_uploader('Bir resim yükleyin', type=['jpg', 'jpeg', 'png'])
if file is not None:
img = Image.open(file)
st.image(img, caption='Yüklenen Resim', use_column_width=True)
# Resmi işle ve tahmine hazır hale getir
processed_img = process_image(img)
# Tahmin al
prediction = model.predict(processed_img)[0]
predicted_class_index = np.argmax(prediction)
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']
# Sonucu ekrana yazdır
st.subheader(f"Tahmin Edilen Sınıf: **{class_names[predicted_class_index]}**")
st.write(f"Modelin Güven Oranı: **%{prediction[predicted_class_index] * 100:.2f}**")