Update src/streamlit_app.py
Browse files- src/streamlit_app.py +33 -28
src/streamlit_app.py
CHANGED
|
@@ -1,51 +1,56 @@
|
|
| 1 |
-
import numpy as np
|
| 2 |
-
from tensorflow.keras.models import load_model
|
| 3 |
-
from PIL import Image
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
| 5 |
import io
|
|
|
|
|
|
|
| 6 |
|
| 7 |
st.set_page_config(page_title="Hurma Sınıflandırıcı", layout="centered")
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
model = load_model(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
# Görseli işle
|
| 13 |
def process_image(img):
|
| 14 |
img = img.resize((224, 224))
|
| 15 |
img = np.array(img) / 255.0
|
| 16 |
img = np.expand_dims(img, axis=0)
|
| 17 |
return img
|
| 18 |
|
| 19 |
-
|
| 20 |
-
st.
|
| 21 |
-
st.write('Lütfen bir hurma fotoğrafı yükleyin. Hangi tür olduğunu tahmin edelim.')
|
| 22 |
|
| 23 |
-
# Session
|
| 24 |
-
if "
|
| 25 |
-
st.session_state.
|
| 26 |
|
| 27 |
-
|
| 28 |
-
file = st.file_uploader("Bir Resim Seçin", type=["jpg", "jpeg", "png"])
|
| 29 |
|
| 30 |
-
#
|
| 31 |
-
if
|
| 32 |
-
st.session_state.
|
| 33 |
|
| 34 |
-
# Görsel
|
| 35 |
-
if st.session_state.
|
| 36 |
try:
|
| 37 |
-
img =
|
| 38 |
st.image(img, caption="Yüklenen Resim", use_column_width=True)
|
| 39 |
|
| 40 |
processed = process_image(img)
|
| 41 |
prediction = model.predict(processed)
|
| 42 |
predicted_class = np.argmax(prediction)
|
| 43 |
|
| 44 |
-
class_names
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
]
|
| 48 |
-
|
| 49 |
-
st.success(f"✅ Tahmin: **{class_names[predicted_class]}**")
|
| 50 |
-
except:
|
| 51 |
-
st.error("⚠️ Görsel işlenemedi. Lütfen geçerli bir .jpg veya .png dosyası yükleyin.")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import numpy as np
|
| 4 |
import io
|
| 5 |
+
import base64
|
| 6 |
+
from tensorflow.keras.models import load_model
|
| 7 |
|
| 8 |
st.set_page_config(page_title="Hurma Sınıflandırıcı", layout="centered")
|
| 9 |
|
| 10 |
+
# Model yükle
|
| 11 |
+
model = load_model("src/dates_classifier_model.h5")
|
| 12 |
+
|
| 13 |
+
class_names = [
|
| 14 |
+
'Rutab', 'Meneifi', 'Sokari', 'Galaxy', 'Shaishe',
|
| 15 |
+
'Medjool', 'Ajwa', 'Nabtat Ali', 'Sugaey'
|
| 16 |
+
]
|
| 17 |
+
|
| 18 |
+
# Base64 saklama
|
| 19 |
+
def image_to_base64(image_bytes):
|
| 20 |
+
return base64.b64encode(image_bytes).decode("utf-8")
|
| 21 |
+
|
| 22 |
+
def base64_to_image(base64_str):
|
| 23 |
+
return Image.open(io.BytesIO(base64.b64decode(base64_str))).convert("RGB")
|
| 24 |
|
|
|
|
| 25 |
def process_image(img):
|
| 26 |
img = img.resize((224, 224))
|
| 27 |
img = np.array(img) / 255.0
|
| 28 |
img = np.expand_dims(img, axis=0)
|
| 29 |
return img
|
| 30 |
|
| 31 |
+
st.title("📷 Hurma Resmi Sınıflandırma")
|
| 32 |
+
st.write("Lütfen bir hurma resmi yükleyin.")
|
|
|
|
| 33 |
|
| 34 |
+
# Session state ile güvenli saklama
|
| 35 |
+
if "image_data" not in st.session_state:
|
| 36 |
+
st.session_state.image_data = None
|
| 37 |
|
| 38 |
+
uploaded_file = st.file_uploader("Resim Seçin (.jpg, .png)", type=["jpg", "jpeg", "png"])
|
|
|
|
| 39 |
|
| 40 |
+
# Yeni yükleme varsa base64 sakla
|
| 41 |
+
if uploaded_file is not None:
|
| 42 |
+
st.session_state.image_data = image_to_base64(uploaded_file.read())
|
| 43 |
|
| 44 |
+
# Görsel işleme
|
| 45 |
+
if st.session_state.image_data:
|
| 46 |
try:
|
| 47 |
+
img = base64_to_image(st.session_state.image_data)
|
| 48 |
st.image(img, caption="Yüklenen Resim", use_column_width=True)
|
| 49 |
|
| 50 |
processed = process_image(img)
|
| 51 |
prediction = model.predict(processed)
|
| 52 |
predicted_class = np.argmax(prediction)
|
| 53 |
|
| 54 |
+
st.success(f"Tahmin: **{class_names[predicted_class]}**")
|
| 55 |
+
except Exception as e:
|
| 56 |
+
st.error(f"Hata oluştu: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|