Spaces:
Sleeping
Sleeping
import streamlit as st | |
import tensorflow as tf | |
from PIL import Image | |
import numpy as np | |
import zipfile | |
import os | |
# Function to load the model from the zip file | |
def load_model_from_zip(zip_file_path): | |
with zipfile.ZipFile(zip_file_path, 'r') as zip_ref: | |
zip_ref.extractall('.') # Extract to the current directory | |
# Load the SavedModel directly from the current directory (root) | |
model = tf.keras.models.load_model('.') | |
return model | |
# Load the model | |
model = load_model_from_zip('my_christmas_tree_model.zip') | |
# Streamlit UI | |
st.title("Christmas Tree Classifier") | |
st.write("Upload an image of a Christmas tree to classify it:") | |
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"]) | |
if uploaded_file is not None: | |
# Display the uploaded image | |
image = Image.open(uploaded_file) | |
st.image(image, caption="Uploaded Image.", use_column_width=True) | |
# Preprocess the image | |
image = image.resize((150, 150)) # Resize to match your model's input shape | |
image_array = np.array(image) / 255.0 # Normalize | |
image_array = np.expand_dims(image_array, axis=0) # Add batch dimension | |
# Make prediction | |
prediction = model.predict(image_array) | |
# Interpret prediction (assuming binary classification) | |
class_names = ['Undecorated', 'Decorated'] # Update with your actual class names | |
predicted_class_index = 1 if prediction[0][0] >= 0.5 else 0 # Adjust threshold if needed | |
predicted_class = class_names[predicted_class_index] | |
# Display the prediction | |
st.write(f"Prediction: **{predicted_class}**") |