File size: 1,997 Bytes
9f6b670 2d9aada 9f6b670 87a1e68 9f6b670 87a1e68 9f6b670 ac157d0 9f6b670 87a1e68 9f6b670 87a1e68 9f6b670 87a1e68 9f6b670 |
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 |
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("""
<h1 style='text-align: center; color: #4A90E2;'>🖼️ Image Classification App</h1>
<p style='text-align: center; font-size: 18px;'>Upload an image and let our model classify it for you!</p>
<hr>
""", 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}")
|