Sridhar027's picture
Update src/streamlit_app.py
8b3a8c8 verified
Raw
History Blame Contribute Delete
1.53 kB
import streamlit as st
import tensorflow as tf
from PIL import Image
import numpy as np
import base64
from tensorflow.keras.models import load_model as keras_load_model
# Set Streamlit page config
st.set_page_config(page_title="Vegetable Classifier", page_icon="🥦", layout="centered")
# Optional: Set background color
def set_bg_color(color="#f0fff0"):
st.markdown(f"""<style>
.stApp {{
background-color: {color};
}}
</style>""", unsafe_allow_html=True)
# Optional: Background image
def add_bg_image(image_file):
with open(image_file, "rb") as f:
encoded = base64.b64encode(f.read()).decode()
st.markdown(f"""
<style>
.stApp {{
background-image: url("data:image/png;base64,{encoded}");
background-size: cover;
}}
</style>
""", unsafe_allow_html=True)
# Class labels
class_names = [
'Bean', 'Bitter_Gourd', 'Bottle_Gourd', 'Brinjal', 'Broccoli',
'Cabbage', 'Capsicum', 'Carrot', 'Cauliflower', 'Cucumber',
'Papaya', 'Potato', 'Pumpkin', 'Radish', 'Tomato'
]
# Load model safely
@st.cache_resource
def load_model_safe():
try:
model = load_model("vegetable_cnn_improved (2).h5", compile=False)
return model
except Exception as e:
st.error(f"❌ Error loading model: {e}")
return None
# Load the model
model = load_model_safe()
# UI
set_bg_color()
st.markdown("<h1 style='text-align:center;'>🥦 Vegetable Image Classifier 🥕</h1>", unsafe_allow_html=True)