CoolFace
Apppublic

zeynepptkn/KitchenVision-AI

sourceHugging Facemitupdated 5mo agoView on Hugging Face
0likes
streamlit_app.py71 linesDownload Raw Back to src
1import streamlit as st2import tensorflow as tf3from tensorflow.keras import layers, models, optimizers4from tensorflow.keras.applications import EfficientNetB35from PIL import Image6import numpy as np7 8# --- Page Configuration ---9st.set_page_config(page_title="Kitchenware Classifier AI", layout="centered")10 11# Custom CSS for centering12st.markdown("""13    <style>14    .centered-text { text-align: center; }15    .stImage { display: flex; justify-content: center; }16    </style>17    """, unsafe_allow_html=True)18 19# --- 1. Header & Image ---20st.markdown("<h1 class='centered-text'>๐Ÿณ Kitchenware Recognition System</h1>", unsafe_allow_html=True)21st.image("https://bakeyy.com/cdn/shop/collections/kitchenware-bakeyy-com.jpg?v=1741775181", use_container_width=True)22 23# --- 2. Model Loading Section ---24@st.cache_resource25def load_my_model():26    # Rebuild the architecture to match training27    base_model = EfficientNetB3(weights='imagenet', include_top=False, input_shape=(300, 300, 3))28    base_model.trainable = False 29 30    model = models.Sequential([31        base_model,32        layers.GlobalAveragePooling2D(),33        layers.BatchNormalization(),34        layers.Dropout(0.3),35        layers.Dense(256, activation='relu'),36        layers.Dropout(0.2),37        layers.Dense(6, activation='softmax')38    ])39    40    # Load weights41    model.load_weights("src/mutfak_modeli_weights.weights.h5")42    return model43 44model = load_my_model()45class_names = ['cup', 'fork', 'glass', 'knife', 'plate', 'spoon']46 47# --- 3. File Upload & Inference ---48uploaded_file = st.file_uploader("Upload a kitchenware photo...", type=["jpg", "png", "jpeg"])49 50if uploaded_file is not None:51    # 1. Image Loading and Conversion (RGB fix for 4-channel PNGs)52    image = Image.open(uploaded_file).convert('RGB')53    st.image(image, caption='Uploaded Image', use_container_width=True)54    55    st.write("๐Ÿ”Ž **Analyzing the image...**")56    57    # 2. Preprocessing58    img = image.resize((300, 300))59    img_array = tf.keras.preprocessing.image.img_to_array(img)60    img_array = np.expand_dims(img_array, axis=0)61    # Important: EfficientNet specific preprocessing62    img_array = tf.keras.applications.efficientnet.preprocess_input(img_array)63 64    # 3. Prediction65    predictions = model.predict(img_array)66    pred_class = np.argmax(predictions)67    confidence = np.max(predictions) * 10068 69    # --- 4. Display Results ---70    st.success(f"### Prediction: **{class_names[pred_class].upper()}**")71    st.info(f"Confidence Level: **%{confidence:.2f}**")