Flower / app.py
mansesa3's picture
Upload app.py
f794c70 verified
raw
history blame
1.94 kB
import streamlit as st
import tensorflow as tf
import numpy as np
from PIL import Image
import pandas as pd
import matplotlib.pyplot as plt
# Load the trained model
model_path = "flower-model.keras"
model = tf.keras.models.load_model(model_path)
# Define the core prediction function
def predict_flower(image):
# Preprocess image
image = image.resize((150, 150)) # Resize the image to 150x150
image = image.convert('RGB') # Ensure image has 3 channels
image = np.array(image)
image = np.expand_dims(image, axis=0) # Add batch dimension
# Predict
prediction = model.predict(image)
# Apply softmax to get probabilities for each class
probabilities = tf.nn.softmax(prediction, axis=1)
# Map probabilities to Flower classes
class_names = ['daisy', 'dandelion', 'rose','sunflower','tulip']
probabilities_dict = {flower_class: round(float(probability), 2) for flower_class, probability in zip(class_names, probabilities.numpy()[0])}
return probabilities_dict
# Streamlit interface
st.title("Bluemen erkenner")
st.write("Welche Blume wächst in ihrem Garten?")
# Upload image
uploaded_image = st.file_uploader("Lade dein Bild hoch...", type=["jpg", "png"])
if uploaded_image is not None:
image = Image.open(uploaded_image)
st.image(image, caption='Uploaded Image.', use_column_width=True)
st.write("")
st.write("Identifiezieren...")
predictions = predict_flower(image)
# Display predictions as a DataFrame
st.write("### Prediction Probabilities")
df = pd.DataFrame(predictions.items(), columns=["Flower", "Probability"])
st.dataframe(df)
# Example images
st.sidebar.title("Examples")
example_images = ["Blume/rose.png", "Blume/sunflower.png", "Blume/dandelion.png"]
for example_image in example_images:
st.sidebar.image(example_image, use_column_width=True)