Spaces:
Runtime error
Runtime error
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +37 -37
src/streamlit_app.py
CHANGED
|
@@ -4,68 +4,68 @@ os.environ["TF_USE_LEGACY_KERAS"] = "1"
|
|
| 4 |
import streamlit as st
|
| 5 |
import numpy as np
|
| 6 |
from PIL import Image
|
| 7 |
-
|
| 8 |
|
| 9 |
-
from tensorflow.keras.models import load_model
|
| 10 |
-
from tensorflow.keras.applications.efficientnet import preprocess_input
|
| 11 |
-
|
| 12 |
-
# -----------------------------------
|
| 13 |
# CONFIG
|
| 14 |
-
# -----------------------------------
|
| 15 |
IMG_SIZE = 300
|
| 16 |
CLASS_NAMES = ['cup', 'fork', 'glass', 'knife', 'plate', 'spoon']
|
| 17 |
|
| 18 |
-
# -----------------------------------
|
| 19 |
# MODEL LOADING
|
| 20 |
-
# -----------------------------------
|
| 21 |
@st.cache_resource
|
| 22 |
def load_model_file():
|
| 23 |
-
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
# -----------------------------------
|
| 28 |
# IMAGE PREPROCESS
|
| 29 |
-
# -----------------------------------
|
| 30 |
def process_image(img):
|
| 31 |
img = img.resize((IMG_SIZE, IMG_SIZE))
|
| 32 |
img = np.array(img)
|
| 33 |
-
|
| 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 |
-
|
|
|
|
| 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 |
|
| 49 |
uploaded = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
| 50 |
|
| 51 |
if uploaded:
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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"Error: {e}")
|
| 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 |
|
| 45 |
uploaded = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
| 46 |
|
| 47 |
if uploaded:
|
| 48 |
+
try:
|
| 49 |
+
img = Image.open(uploaded).convert("RGB")
|
| 50 |
+
|
| 51 |
+
col1, col2 = st.columns(2)
|
| 52 |
+
|
| 53 |
+
with col1:
|
| 54 |
+
st.image(img, caption="Uploaded Image", use_container_width=True)
|
| 55 |
+
|
| 56 |
+
with col2:
|
| 57 |
+
st.write("Classifying...")
|
| 58 |
+
|
| 59 |
+
image_tensor = process_image(img)
|
| 60 |
+
preds = model.predict(image_tensor)
|
| 61 |
+
|
| 62 |
+
predicted_index = np.argmax(preds)
|
| 63 |
+
predicted_class = CLASS_NAMES[predicted_index]
|
| 64 |
+
confidence = float(np.max(preds))
|
| 65 |
+
|
| 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"Error: {e}")
|