Spaces:
Runtime error
Runtime error
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +70 -38
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,72 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
| 3 |
-
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
""
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
|
| 10 |
-
If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
|
| 11 |
-
forums](https://discuss.streamlit.io).
|
| 12 |
-
|
| 13 |
-
In the meantime, below is an example of what you can do with just a few lines of code:
|
| 14 |
-
"""
|
| 15 |
-
|
| 16 |
-
num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
|
| 17 |
-
num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
|
| 18 |
-
|
| 19 |
-
indices = np.linspace(0, 1, num_points)
|
| 20 |
-
theta = 2 * np.pi * num_turns * indices
|
| 21 |
-
radius = indices
|
| 22 |
-
|
| 23 |
-
x = radius * np.cos(theta)
|
| 24 |
-
y = radius * np.sin(theta)
|
| 25 |
-
|
| 26 |
-
df = pd.DataFrame({
|
| 27 |
-
"x": x,
|
| 28 |
-
"y": y,
|
| 29 |
-
"idx": indices,
|
| 30 |
-
"rand": np.random.randn(num_points),
|
| 31 |
-
})
|
| 32 |
-
|
| 33 |
-
st.altair_chart(alt.Chart(df, height=700, width=700)
|
| 34 |
-
.mark_point(filled=True)
|
| 35 |
-
.encode(
|
| 36 |
-
x=alt.X("x", axis=None),
|
| 37 |
-
y=alt.Y("y", axis=None),
|
| 38 |
-
color=alt.Color("idx", legend=None, scale=alt.Scale()),
|
| 39 |
-
size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
|
| 40 |
-
))
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
os.environ["TF_USE_LEGACY_KERAS"] = "1"
|
| 3 |
+
|
| 4 |
import streamlit as st
|
| 5 |
+
import tensorflow as tf
|
| 6 |
+
from tensorflow.keras.applications.efficientnet import preprocess_input
|
| 7 |
+
from PIL import Image
|
| 8 |
+
import numpy as np
|
| 9 |
+
|
| 10 |
+
# -----------------------------------
|
| 11 |
+
# CONFIG
|
| 12 |
+
# -----------------------------------
|
| 13 |
+
IMG_SIZE = 300
|
| 14 |
+
CLASS_NAMES = ['cup', 'fork', 'glass', 'knife', 'plate', 'spoon']
|
| 15 |
+
|
| 16 |
+
# -----------------------------------
|
| 17 |
+
# MODEL LOADING
|
| 18 |
+
# -----------------------------------
|
| 19 |
+
@st.cache_resource
|
| 20 |
+
def load_model_file():
|
| 21 |
+
return tf.keras.models.load_model(
|
| 22 |
+
"src/kitchen1.keras",
|
| 23 |
+
compile=False
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
model = load_model_file()
|
| 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 |
+
|
| 35 |
+
if img.ndim == 2:
|
| 36 |
+
img = np.stack([img]*3, axis=-1)
|
| 37 |
+
elif img.shape[-1] == 4:
|
| 38 |
+
img = img[..., :3]
|
| 39 |
+
|
| 40 |
+
img = np.expand_dims(img, 0)
|
| 41 |
+
img = preprocess_input(img)
|
| 42 |
+
return img
|
| 43 |
+
|
| 44 |
+
# -----------------------------------
|
| 45 |
+
# UI LAYOUT
|
| 46 |
+
# -----------------------------------
|
| 47 |
+
st.title("🍽️ Kitchenware Classifier")
|
| 48 |
+
st.write("Upload an image to classify kitchen items using EfficientNetB3.")
|
| 49 |
+
|
| 50 |
+
uploaded = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
| 51 |
+
|
| 52 |
+
if uploaded:
|
| 53 |
+
img = Image.open(uploaded).convert("RGB")
|
| 54 |
+
|
| 55 |
+
col1, col2 = st.columns(2)
|
| 56 |
+
|
| 57 |
+
with col1:
|
| 58 |
+
st.image(img, caption="Uploaded Image", use_container_width=True)
|
| 59 |
+
|
| 60 |
+
with col2:
|
| 61 |
+
st.write("Classifying...")
|
| 62 |
+
|
| 63 |
+
image_tensor = process_image(img)
|
| 64 |
+
preds = model.predict(image_tensor)
|
| 65 |
+
|
| 66 |
+
predicted_index = np.argmax(preds)
|
| 67 |
+
predicted_class = CLASS_NAMES[predicted_index]
|
| 68 |
+
confidence = float(np.max(preds))
|
| 69 |
|
| 70 |
+
st.success(f"Prediction: **{predicted_class.upper()}**")
|
| 71 |
+
st.metric("Confidence", f"{confidence * 100:.2f}%")
|
| 72 |
+
st.progress(int(confidence * 100))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|