import os os.system("pip install tensorflow") os.system("pip install scikit-learn") import streamlit as st import tensorflow as tf import numpy as np import pickle from PIL import Image # Constants MODEL_PATH = "image_classification.h5" LABEL_ENCODER_PATH = "le.pkl" EXPECTED_SIZE = (64, 64) # Update this based on your model's input shape def load_resources(): """Load model and label encoder.""" model = tf.keras.models.load_model(MODEL_PATH) with open(LABEL_ENCODER_PATH, "rb") as f: label_encoder = pickle.load(f) return model, label_encoder # Load resources model, label_encoder = load_resources() def preprocess_image(image): """Resize image to match model input shape.""" image = image.resize(EXPECTED_SIZE) # Resize to match model input image_array = np.array(image) # Convert to numpy array image_array = np.expand_dims(image_array, axis=0) # Add batch dimension return image_array def predict(image): """Predict the class of the uploaded image.""" image_array = preprocess_image(image) preds = model.predict(image_array) class_index = np.argmax(preds) return label_encoder.inverse_transform([class_index])[0] # Streamlit UI st.set_page_config(page_title="Image Classifier", layout="wide") st.markdown("""

🖼️ Image Classification App

Upload an image and let our model classify it for you!


""", unsafe_allow_html=True) st.sidebar.header("Upload Your Image") uploaded_file = st.sidebar.file_uploader("Choose an image", type=["jpg", "png", "jpeg"], help="Supported formats: JPG, PNG, JPEG") if uploaded_file: image = Image.open(uploaded_file) st.image(image, caption="📸 Uploaded Image", use_column_width=True) if st.button("🔍 Classify Image", use_container_width=True): prediction = predict(image) st.success(f"🎯 Predicted Class: {prediction}")