Update Home.py
Browse files
Home.py
CHANGED
@@ -1,22 +1,57 @@
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
-
|
|
|
|
|
|
|
3 |
|
4 |
-
#
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
-
#
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
output = model(user_input, max_length=100, num_return_sequences=1)
|
20 |
-
st.success("Generated Text:")
|
21 |
-
st.write(output[0]['generated_text'])
|
22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
os.system("pip install tensorflow")
|
3 |
+
os.system("pip install scikit-learn")
|
4 |
+
|
5 |
import streamlit as st
|
6 |
+
import tensorflow as tf
|
7 |
+
import numpy as np
|
8 |
+
import pickle
|
9 |
+
from PIL import Image
|
10 |
|
11 |
+
# Constants
|
12 |
+
MODEL_PATH = "image_classification.h5"
|
13 |
+
LABEL_ENCODER_PATH = "le.pkl"
|
14 |
+
EXPECTED_SIZE = (64, 64) # Update this based on your model's input shape
|
15 |
|
16 |
+
def load_resources():
|
17 |
+
"""Load model and label encoder."""
|
18 |
+
model = tf.keras.models.load_model(MODEL_PATH)
|
19 |
+
with open(LABEL_ENCODER_PATH, "rb") as f:
|
20 |
+
label_encoder = pickle.load(f)
|
21 |
+
return model, label_encoder
|
22 |
|
23 |
+
# Load resources
|
24 |
+
model, label_encoder = load_resources()
|
25 |
+
|
26 |
+
def preprocess_image(image):
|
27 |
+
"""Resize image to match model input shape."""
|
28 |
+
image = image.resize(EXPECTED_SIZE) # Resize to match model input
|
29 |
+
image_array = np.array(image) # Convert to numpy array
|
30 |
+
image_array = np.expand_dims(image_array, axis=0) # Add batch dimension
|
31 |
+
return image_array
|
32 |
|
33 |
+
def predict(image):
|
34 |
+
"""Predict the class of the uploaded image."""
|
35 |
+
image_array = preprocess_image(image)
|
36 |
+
preds = model.predict(image_array)
|
37 |
+
class_index = np.argmax(preds)
|
38 |
+
return label_encoder.inverse_transform([class_index])[0]
|
39 |
+
|
40 |
+
# Streamlit UI
|
41 |
+
st.set_page_config(page_title="Image Classifier", layout="wide")
|
42 |
+
st.markdown("""
|
43 |
+
<h1 style='text-align: center; color: #4A90E2;'>🖼️ Image Classification App</h1>
|
44 |
+
<p style='text-align: center; font-size: 18px;'>Upload an image and let our model classify it for you!</p>
|
45 |
+
<hr>
|
46 |
+
""", unsafe_allow_html=True)
|
47 |
|
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}")
|