Update app.py
Browse files
app.py
CHANGED
@@ -48,6 +48,62 @@ st.markdown("""
|
|
48 |
st.sidebar.header("Upload Your Image")
|
49 |
uploaded_file = st.sidebar.file_uploader("Choose an image", type=["jpg", "png", "jpeg"], help="Supported formats: JPG, PNG, JPEG")
|
50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
if uploaded_file:
|
52 |
image = Image.open(uploaded_file)
|
53 |
st.image(image, caption="📸 Uploaded Image", use_column_width=True)
|
|
|
48 |
st.sidebar.header("Upload Your Image")
|
49 |
uploaded_file = st.sidebar.file_uploader("Choose an image", type=["jpg", "png", "jpeg"], help="Supported formats: JPG, PNG, JPEG")
|
50 |
|
51 |
+
if uploaded_file:
|
52 |
+
image = Image.open(uploaded_file)
|
53 |
+
st.image(image, caption="📸 Uploaded Image", use_column_width=True)
|
54 |
+
|
55 |
+
if st.button("🔍 Classify Image", use_container_width=True):
|
56 |
+
prediction = predict(image)
|
57 |
+
st.success(f"🎯 Predicted Class: {prediction}")import os
|
58 |
+
os.system("pip install tensorflow")
|
59 |
+
os.system("pip install scikit-learn")
|
60 |
+
|
61 |
+
import streamlit as st
|
62 |
+
import tensorflow as tf
|
63 |
+
import numpy as np
|
64 |
+
import pickle
|
65 |
+
from PIL import Image
|
66 |
+
|
67 |
+
# Constants
|
68 |
+
MODEL_PATH = "image_classification.h5"
|
69 |
+
LABEL_ENCODER_PATH = "le.pkl"
|
70 |
+
EXPECTED_SIZE = (64, 64) # Update this based on your model's input shape
|
71 |
+
|
72 |
+
def load_resources():
|
73 |
+
"""Load model and label encoder."""
|
74 |
+
model = tf.keras.models.load_model(MODEL_PATH)
|
75 |
+
with open(LABEL_ENCODER_PATH, "rb") as f:
|
76 |
+
label_encoder = pickle.load(f)
|
77 |
+
return model, label_encoder
|
78 |
+
|
79 |
+
# Load resources
|
80 |
+
model, label_encoder = load_resources()
|
81 |
+
|
82 |
+
def preprocess_image(image):
|
83 |
+
"""Resize image to match model input shape."""
|
84 |
+
image = image.resize(EXPECTED_SIZE) # Resize to match model input
|
85 |
+
image_array = np.array(image) # Convert to numpy array
|
86 |
+
image_array = np.expand_dims(image_array, axis=0) # Add batch dimension
|
87 |
+
return image_array
|
88 |
+
|
89 |
+
def predict(image):
|
90 |
+
"""Predict the class of the uploaded image."""
|
91 |
+
image_array = preprocess_image(image)
|
92 |
+
preds = model.predict(image_array)
|
93 |
+
class_index = np.argmax(preds)
|
94 |
+
return label_encoder.inverse_transform([class_index])[0]
|
95 |
+
|
96 |
+
# Streamlit UI
|
97 |
+
st.set_page_config(page_title="Image Classifier", layout="wide")
|
98 |
+
st.markdown("""
|
99 |
+
<h1 style='text-align: center; color: #4A90E2;'>🖼️ Image Classification App</h1>
|
100 |
+
<p style='text-align: center; font-size: 18px;'>Upload an image and let our model classify it for you!</p>
|
101 |
+
<hr>
|
102 |
+
""", unsafe_allow_html=True)
|
103 |
+
|
104 |
+
st.sidebar.header("Upload Your Image")
|
105 |
+
uploaded_file = st.sidebar.file_uploader("Choose an image", type=["jpg", "png", "jpeg"], help="Supported formats: JPG, PNG, JPEG")
|
106 |
+
|
107 |
if uploaded_file:
|
108 |
image = Image.open(uploaded_file)
|
109 |
st.image(image, caption="📸 Uploaded Image", use_column_width=True)
|