Spaces:
Runtime error
Runtime error
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +12 -11
src/streamlit_app.py
CHANGED
|
@@ -1,44 +1,48 @@
|
|
| 1 |
-
import os
|
| 2 |
-
os.environ["TF_USE_LEGACY_KERAS"] = "1"
|
| 3 |
-
|
| 4 |
import streamlit as st
|
| 5 |
import numpy as np
|
| 6 |
from PIL import Image
|
| 7 |
import tensorflow as tf
|
|
|
|
| 8 |
|
|
|
|
| 9 |
# CONFIG
|
|
|
|
| 10 |
IMG_SIZE = 300
|
| 11 |
CLASS_NAMES = ['cup', 'fork', 'glass', 'knife', 'plate', 'spoon']
|
| 12 |
|
|
|
|
| 13 |
# MODEL LOADING
|
|
|
|
| 14 |
@st.cache_resource
|
| 15 |
def load_model_file():
|
| 16 |
model_path = os.path.join(os.path.dirname(__file__), "kitchen1.keras")
|
| 17 |
-
|
| 18 |
return tf.keras.models.load_model(model_path, compile=False)
|
| 19 |
|
| 20 |
try:
|
| 21 |
model = load_model_file()
|
| 22 |
except Exception as e:
|
| 23 |
-
st.error(f"
|
| 24 |
st.stop()
|
| 25 |
|
|
|
|
| 26 |
# IMAGE PREPROCESS
|
|
|
|
| 27 |
def process_image(img):
|
| 28 |
img = img.resize((IMG_SIZE, IMG_SIZE))
|
| 29 |
img = np.array(img)
|
| 30 |
-
|
| 31 |
if img.ndim == 2:
|
| 32 |
img = np.stack([img]*3, axis=-1)
|
| 33 |
elif img.shape[-1] == 4:
|
| 34 |
img = img[..., :3]
|
| 35 |
|
| 36 |
img = np.expand_dims(img, 0)
|
| 37 |
-
|
| 38 |
img = tf.keras.applications.efficientnet.preprocess_input(img)
|
| 39 |
return img
|
| 40 |
|
|
|
|
| 41 |
# UI LAYOUT
|
|
|
|
| 42 |
st.title("🍽️ Kitchenware Classifier")
|
| 43 |
st.write("Upload an image to classify kitchen items using EfficientNetB3.")
|
| 44 |
|
|
@@ -47,7 +51,6 @@ uploaded = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
|
| 47 |
if uploaded:
|
| 48 |
try:
|
| 49 |
img = Image.open(uploaded).convert("RGB")
|
| 50 |
-
|
| 51 |
col1, col2 = st.columns(2)
|
| 52 |
|
| 53 |
with col1:
|
|
@@ -55,7 +58,6 @@ if uploaded:
|
|
| 55 |
|
| 56 |
with col2:
|
| 57 |
st.write("Classifying...")
|
| 58 |
-
|
| 59 |
image_tensor = process_image(img)
|
| 60 |
preds = model.predict(image_tensor)
|
| 61 |
|
|
@@ -66,6 +68,5 @@ if uploaded:
|
|
| 66 |
st.success(f"Prediction: **{predicted_class.upper()}**")
|
| 67 |
st.metric("Confidence", f"{confidence * 100:.2f}%")
|
| 68 |
st.progress(int(confidence * 100))
|
| 69 |
-
|
| 70 |
except Exception as e:
|
| 71 |
-
st.error(f"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import numpy as np
|
| 3 |
from PIL import Image
|
| 4 |
import tensorflow as tf
|
| 5 |
+
import os
|
| 6 |
|
| 7 |
+
# -----------------------------------
|
| 8 |
# CONFIG
|
| 9 |
+
# -----------------------------------
|
| 10 |
IMG_SIZE = 300
|
| 11 |
CLASS_NAMES = ['cup', 'fork', 'glass', 'knife', 'plate', 'spoon']
|
| 12 |
|
| 13 |
+
# -----------------------------------
|
| 14 |
# MODEL LOADING
|
| 15 |
+
# -----------------------------------
|
| 16 |
@st.cache_resource
|
| 17 |
def load_model_file():
|
| 18 |
model_path = os.path.join(os.path.dirname(__file__), "kitchen1.keras")
|
| 19 |
+
# TF 2.14'te de bu yöntem çalışır
|
| 20 |
return tf.keras.models.load_model(model_path, compile=False)
|
| 21 |
|
| 22 |
try:
|
| 23 |
model = load_model_file()
|
| 24 |
except Exception as e:
|
| 25 |
+
st.error(f"Model yüklenirken hata: {e}")
|
| 26 |
st.stop()
|
| 27 |
|
| 28 |
+
# -----------------------------------
|
| 29 |
# IMAGE PREPROCESS
|
| 30 |
+
# -----------------------------------
|
| 31 |
def process_image(img):
|
| 32 |
img = img.resize((IMG_SIZE, IMG_SIZE))
|
| 33 |
img = np.array(img)
|
|
|
|
| 34 |
if img.ndim == 2:
|
| 35 |
img = np.stack([img]*3, axis=-1)
|
| 36 |
elif img.shape[-1] == 4:
|
| 37 |
img = img[..., :3]
|
| 38 |
|
| 39 |
img = np.expand_dims(img, 0)
|
|
|
|
| 40 |
img = tf.keras.applications.efficientnet.preprocess_input(img)
|
| 41 |
return img
|
| 42 |
|
| 43 |
+
# -----------------------------------
|
| 44 |
# UI LAYOUT
|
| 45 |
+
# -----------------------------------
|
| 46 |
st.title("🍽️ Kitchenware Classifier")
|
| 47 |
st.write("Upload an image to classify kitchen items using EfficientNetB3.")
|
| 48 |
|
|
|
|
| 51 |
if uploaded:
|
| 52 |
try:
|
| 53 |
img = Image.open(uploaded).convert("RGB")
|
|
|
|
| 54 |
col1, col2 = st.columns(2)
|
| 55 |
|
| 56 |
with col1:
|
|
|
|
| 58 |
|
| 59 |
with col2:
|
| 60 |
st.write("Classifying...")
|
|
|
|
| 61 |
image_tensor = process_image(img)
|
| 62 |
preds = model.predict(image_tensor)
|
| 63 |
|
|
|
|
| 68 |
st.success(f"Prediction: **{predicted_class.upper()}**")
|
| 69 |
st.metric("Confidence", f"{confidence * 100:.2f}%")
|
| 70 |
st.progress(int(confidence * 100))
|
|
|
|
| 71 |
except Exception as e:
|
| 72 |
+
st.error(f"Hata: {e}")
|