Spaces:
Sleeping
Sleeping
| 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.vgg19 import preprocess_input | |
| from tensorflow.keras.applications.vgg16 import preprocess_input | |
| import os | |
| # --- KERAS DESERIALIZATION YAMASI --- | |
| import keras.src.saving.serialization_lib as serialization_lib | |
| _original_deserialize = serialization_lib.deserialize_keras_object | |
| def safe_deserialize(config, *args, **kwargs): | |
| # Eğer config bir sözlük ise ve içinde quantization_config varsa sil | |
| if isinstance(config, dict): | |
| 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 | |
| # ------------------------------------- | |
| # streamlit_app.py dosyasının bulunduğu klasörün yolunu alır | |
| BASE_DIR = os.path.dirname(os.path.abspath(__file__)) | |
| MODEL_PATH = os.path.join(BASE_DIR, 'Date_Fruit_VGG16.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) | |
| # VGG16 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('Hurma Çeşidi Tespiti (Date Fruit Detection)') | |
| st.write('Bir hurma fotoğrafı yükleyin, VGG16 modeli çeşidini tahmin etsin!') | |
| 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 = ["Ajwa", "Galaxy", "Medjool", "Meneifi", "Nabtat Ali", "Rutab", "Shaishe", "Sokari", "Sugaey"] | |
| # 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}**") |