Spaces:
Runtime error
Runtime error
File size: 1,967 Bytes
6fb15cb e8c5da0 6fb15cb 8fa027f 82d6933 494d69f 6fb15cb 8fa027f 6fb15cb e8c5da0 6fb15cb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | import os
os.environ["TF_USE_LEGACY_KERAS"] = "1"
import streamlit as st
import numpy as np
from PIL import Image
from tensorflow import keras
from tensorflow.keras.models import load_model
from tensorflow.keras.applications.efficientnet import preprocess_input
# -----------------------------------
# CONFIG
# -----------------------------------
IMG_SIZE = 300
CLASS_NAMES = ['cup', 'fork', 'glass', 'knife', 'plate', 'spoon']
# -----------------------------------
# MODEL LOADING
# -----------------------------------
@st.cache_resource
def load_model_file():
return keras.models.load_model("src/kitchen1.keras", compile=False)
model = load_model_file()
# -----------------------------------
# IMAGE PREPROCESS
# -----------------------------------
def process_image(img):
img = img.resize((IMG_SIZE, IMG_SIZE))
img = np.array(img)
if img.ndim == 2:
img = np.stack([img]*3, axis=-1)
elif img.shape[-1] == 4:
img = img[..., :3]
img = np.expand_dims(img, 0)
img = preprocess_input(img)
return img
# -----------------------------------
# UI LAYOUT
# -----------------------------------
st.title("🍽️ Kitchenware Classifier")
st.write("Upload an image to classify kitchen items using EfficientNetB3.")
uploaded = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
if uploaded:
img = Image.open(uploaded).convert("RGB")
col1, col2 = st.columns(2)
with col1:
st.image(img, caption="Uploaded Image", use_container_width=True)
with col2:
st.write("Classifying...")
image_tensor = process_image(img)
preds = model.predict(image_tensor)
predicted_index = np.argmax(preds)
predicted_class = CLASS_NAMES[predicted_index]
confidence = float(np.max(preds))
st.success(f"Prediction: **{predicted_class.upper()}**")
st.metric("Confidence", f"{confidence * 100:.2f}%")
st.progress(int(confidence * 100)) |