mansesa3 commited on
Commit
dcd0170
·
verified ·
1 Parent(s): a94bfc3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -57
app.py CHANGED
@@ -1,57 +1,50 @@
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)
 
1
+ import streamlit as st
2
+ import tensorflow as tf
3
+ import numpy as np
4
+ from PIL import Image
5
+
6
+ # Load the trained model
7
+ model_path = "flower-model.keras"
8
+ model = tf.keras.models.load_model(model_path)
9
+
10
+ # Define the core prediction function
11
+ def predict_flower(image):
12
+ # Preprocess image
13
+ image = image.resize((150, 150)) # Resize the image to 150x150
14
+ image = image.convert('RGB') # Ensure image has 3 channels
15
+ image = np.array(image)
16
+ image = np.expand_dims(image, axis=0) # Add batch dimension
17
+
18
+ # Predict
19
+ prediction = model.predict(image)
20
+
21
+ # Apply softmax to get probabilities for each class
22
+ probabilities = tf.nn.softmax(prediction, axis=1)
23
+
24
+ # Map probabilities to Flower classes
25
+ class_names = ['daisy', 'dandelion', 'rose','sunflower','tulip']
26
+ probabilities_dict = {flower_class: round(float(probability), 2) for flower_class, probability in zip(class_names, probabilities.numpy()[0])}
27
+
28
+ return probabilities_dict
29
+
30
+ # Streamlit interface
31
+ st.title("Der Bluemenerkenner")
32
+ st.write("Welche Blume wächst in ihrem Garten?")
33
+
34
+ # Upload image
35
+ uploaded_image = st.file_uploader("Lade dein Bild hoch...", type=["jpg", "png"])
36
+
37
+ if uploaded_image is not None:
38
+ image = Image.open(uploaded_image)
39
+ st.image(image, caption='Hochgeladenes Bild.', use_column_width=True)
40
+ st.write("")
41
+ st.write("Identifizieren...")
42
+
43
+ predictions = predict_flower(image)
44
+
45
+ # Find the flower with the highest probability
46
+ highest_probability_flower = max(predictions, key=predictions.get)
47
+ highest_probability_value = predictions[highest_probability_flower]
48
+
49
+ # Display the flower with the highest probability
50
+ st.write(f"Es ist eine: **{highest_probability_flower}** mit einer Wahrscheinlichkeit von **{highest_probability_value * 100}%**.")