Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import tensorflow as tf
|
3 |
+
import numpy as np
|
4 |
+
from PIL import Image
|
5 |
+
import pandas as pd
|
6 |
+
import matplotlib.pyplot as plt
|
7 |
+
|
8 |
+
# Load the trained model
|
9 |
+
model_path = "flower-model.keras"
|
10 |
+
model = tf.keras.models.load_model(model_path)
|
11 |
+
|
12 |
+
# Define the core prediction function
|
13 |
+
def predict_flower(image):
|
14 |
+
# Preprocess image
|
15 |
+
image = image.resize((150, 150)) # Resize the image to 150x150
|
16 |
+
image = image.convert('RGB') # Ensure image has 3 channels
|
17 |
+
image = np.array(image)
|
18 |
+
image = np.expand_dims(image, axis=0) # Add batch dimension
|
19 |
+
|
20 |
+
# Predict
|
21 |
+
prediction = model.predict(image)
|
22 |
+
|
23 |
+
# Apply softmax to get probabilities for each class
|
24 |
+
probabilities = tf.nn.softmax(prediction, axis=1)
|
25 |
+
|
26 |
+
# Map probabilities to Flower classes
|
27 |
+
class_names = ['daisy', 'dandelion', 'rose','sunflower','tulip']
|
28 |
+
probabilities_dict = {flower_class: round(float(probability), 2) for flower_class, probability in zip(class_names, probabilities.numpy()[0])}
|
29 |
+
|
30 |
+
return probabilities_dict
|
31 |
+
|
32 |
+
# Streamlit interface
|
33 |
+
st.title("Bluemen erkenner")
|
34 |
+
st.write("Welche Blume wächst in ihrem Garten?")
|
35 |
+
|
36 |
+
# Upload image
|
37 |
+
uploaded_image = st.file_uploader("Lade dein Bild hoch...", type=["jpg", "png"])
|
38 |
+
|
39 |
+
if uploaded_image is not None:
|
40 |
+
image = Image.open(uploaded_image)
|
41 |
+
st.image(image, caption='Uploaded Image.', use_column_width=True)
|
42 |
+
st.write("")
|
43 |
+
st.write("Identifiezieren...")
|
44 |
+
|
45 |
+
predictions = predict_flower(image)
|
46 |
+
|
47 |
+
# Display predictions as a DataFrame
|
48 |
+
st.write("### Prediction Probabilities")
|
49 |
+
df = pd.DataFrame(predictions.items(), columns=["Flower", "Probability"])
|
50 |
+
st.dataframe(df)
|
51 |
+
|
52 |
+
|
53 |
+
# Example images
|
54 |
+
st.sidebar.title("Examples")
|
55 |
+
example_images = ["Blume/rose.png", "Blume/sunflower.png", "Blume/dandelion.png"]
|
56 |
+
for example_image in example_images:
|
57 |
+
st.sidebar.image(example_image, use_column_width=True)
|