Update src/streamlit_app.py
Browse files- src/streamlit_app.py +16 -15
src/streamlit_app.py
CHANGED
|
@@ -1,21 +1,28 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
from PIL import Image
|
| 3 |
import numpy as np
|
| 4 |
-
import
|
| 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 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
class_names = [
|
| 14 |
'Rutab', 'Meneifi', 'Sokari', 'Galaxy', 'Shaishe',
|
| 15 |
'Medjool', 'Ajwa', 'Nabtat Ali', 'Sugaey'
|
| 16 |
]
|
| 17 |
|
| 18 |
-
#
|
| 19 |
def image_to_base64(image_bytes):
|
| 20 |
return base64.b64encode(image_bytes).decode("utf-8")
|
| 21 |
|
|
@@ -28,29 +35,23 @@ def process_image(img):
|
|
| 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
|
| 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 |
-
|
| 51 |
-
prediction = model.predict(
|
| 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"
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
+
from PIL import Image
|
| 4 |
import base64
|
| 5 |
+
import io
|
| 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 |
+
st.title("📷 Hurma Resmi Sınıflandırma")
|
| 11 |
+
st.write("Lütfen bir hurma resmi yükleyin ve hangi tür olduğunu tahmin edelim.")
|
| 12 |
+
|
| 13 |
+
# --- MODEL ---
|
| 14 |
+
try:
|
| 15 |
+
model = load_model("src/dates_classifier_model.h5")
|
| 16 |
+
except Exception as e:
|
| 17 |
+
st.error(f"Model yüklenemedi: {e}")
|
| 18 |
+
st.stop()
|
| 19 |
|
| 20 |
class_names = [
|
| 21 |
'Rutab', 'Meneifi', 'Sokari', 'Galaxy', 'Shaishe',
|
| 22 |
'Medjool', 'Ajwa', 'Nabtat Ali', 'Sugaey'
|
| 23 |
]
|
| 24 |
|
| 25 |
+
# --- IMAGE SESSION ---
|
| 26 |
def image_to_base64(image_bytes):
|
| 27 |
return base64.b64encode(image_bytes).decode("utf-8")
|
| 28 |
|
|
|
|
| 35 |
img = np.expand_dims(img, axis=0)
|
| 36 |
return img
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
if "image_data" not in st.session_state:
|
| 39 |
st.session_state.image_data = None
|
| 40 |
|
| 41 |
+
uploaded_file = st.file_uploader("Resim Seç (.jpg, .jpeg, .png)", type=["jpg", "jpeg", "png"])
|
| 42 |
|
|
|
|
| 43 |
if uploaded_file is not None:
|
| 44 |
st.session_state.image_data = image_to_base64(uploaded_file.read())
|
| 45 |
|
|
|
|
| 46 |
if st.session_state.image_data:
|
| 47 |
try:
|
| 48 |
img = base64_to_image(st.session_state.image_data)
|
| 49 |
st.image(img, caption="Yüklenen Resim", use_column_width=True)
|
| 50 |
|
| 51 |
+
processed_img = process_image(img)
|
| 52 |
+
prediction = model.predict(processed_img)
|
| 53 |
predicted_class = np.argmax(prediction)
|
| 54 |
|
| 55 |
st.success(f"Tahmin: **{class_names[predicted_class]}**")
|
| 56 |
except Exception as e:
|
| 57 |
+
st.error(f"Fotoğraf işlenemedi: {e}")
|