Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +89 -37
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,92 @@
|
|
| 1 |
-
import
|
| 2 |
import numpy as np
|
| 3 |
import pandas as pd
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
""
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
|
| 10 |
-
If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
|
| 11 |
-
forums](https://discuss.streamlit.io).
|
| 12 |
-
|
| 13 |
-
In the meantime, below is an example of what you can do with just a few lines of code:
|
| 14 |
-
"""
|
| 15 |
-
|
| 16 |
-
num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
|
| 17 |
-
num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
|
| 18 |
-
|
| 19 |
-
indices = np.linspace(0, 1, num_points)
|
| 20 |
-
theta = 2 * np.pi * num_turns * indices
|
| 21 |
-
radius = indices
|
| 22 |
-
|
| 23 |
-
x = radius * np.cos(theta)
|
| 24 |
-
y = radius * np.sin(theta)
|
| 25 |
-
|
| 26 |
-
df = pd.DataFrame({
|
| 27 |
-
"x": x,
|
| 28 |
-
"y": y,
|
| 29 |
-
"idx": indices,
|
| 30 |
-
"rand": np.random.randn(num_points),
|
| 31 |
-
})
|
| 32 |
-
|
| 33 |
-
st.altair_chart(alt.Chart(df, height=700, width=700)
|
| 34 |
-
.mark_point(filled=True)
|
| 35 |
-
.encode(
|
| 36 |
-
x=alt.X("x", axis=None),
|
| 37 |
-
y=alt.Y("y", axis=None),
|
| 38 |
-
color=alt.Color("idx", legend=None, scale=alt.Scale()),
|
| 39 |
-
size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
|
| 40 |
-
))
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
import numpy as np
|
| 3 |
import pandas as pd
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import tensorflow as tf
|
| 6 |
+
|
| 7 |
+
# --- PAS DIT AAN ALS JE ANDERE KLASSEN HEBT ---
|
| 8 |
+
CLASS_NAMES = [
|
| 9 |
+
"Ajwa",
|
| 10 |
+
"Galaxy",
|
| 11 |
+
"Medjool",
|
| 12 |
+
"Meneifi",
|
| 13 |
+
"Nabtat Ali",
|
| 14 |
+
"Rutab",
|
| 15 |
+
"Shaishe",
|
| 16 |
+
"Sokari",
|
| 17 |
+
"Sugaey"
|
| 18 |
+
]
|
| 19 |
+
|
| 20 |
+
IMG_SIZE = (128, 128) # dezelfde grootte als in je model
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
@st.cache_resource
|
| 24 |
+
def load_model():
|
| 25 |
+
"""Laad het Keras-model één keer en cache het."""
|
| 26 |
+
model = tf.keras.models.load_model("date_fruit_model.h5")
|
| 27 |
+
return model
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def preprocess_image(image: Image.Image) -> np.ndarray:
|
| 31 |
+
"""
|
| 32 |
+
Maakt een PIL-image klaar voor het model:
|
| 33 |
+
- resize
|
| 34 |
+
- naar np.array
|
| 35 |
+
- normaliseren [0,1]
|
| 36 |
+
- batch-dimensie toevoegen
|
| 37 |
+
"""
|
| 38 |
+
image = image.convert("RGB") # voor de zekerheid
|
| 39 |
+
image = image.resize(IMG_SIZE)
|
| 40 |
+
arr = np.array(image, dtype="float32") / 255.0
|
| 41 |
+
arr = np.expand_dims(arr, axis=0) # shape: (1, 128, 128, 3)
|
| 42 |
+
return arr
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def main():
|
| 46 |
+
st.set_page_config(page_title="Date Fruit Classifier", layout="centered")
|
| 47 |
+
|
| 48 |
+
st.title("🍇 Date Fruit Classifier")
|
| 49 |
+
st.write("Upload een foto van een dadel en het model probeert de soort te raden.")
|
| 50 |
+
|
| 51 |
+
# Sidebar info
|
| 52 |
+
st.sidebar.header("Info")
|
| 53 |
+
st.sidebar.write("Model: Convolutional Neural Network (Keras/TensorFlow)")
|
| 54 |
+
st.sidebar.write(f"Aantal klassen: **{len(CLASS_NAMES)}**")
|
| 55 |
+
|
| 56 |
+
uploaded_file = st.file_uploader(
|
| 57 |
+
"Kies een afbeelding", type=["jpg", "jpeg", "png"]
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
if uploaded_file is not None:
|
| 61 |
+
# Toon de geüploade afbeelding
|
| 62 |
+
image = Image.open(uploaded_file)
|
| 63 |
+
st.image(image, caption="Geüploade afbeelding", use_container_width=True)
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
if st.button("Classificeer"):
|
| 67 |
+
with st.spinner("Bezig met voorspellen..."):
|
| 68 |
+
model = load_model()
|
| 69 |
+
input_arr = preprocess_image(image)
|
| 70 |
+
preds = model.predict(input_arr)[0] # shape: (n_classes,)
|
| 71 |
+
|
| 72 |
+
pred_idx = int(np.argmax(preds))
|
| 73 |
+
pred_name = CLASS_NAMES[pred_idx]
|
| 74 |
+
pred_conf = float(preds[pred_idx])
|
| 75 |
+
|
| 76 |
+
st.subheader("🔎 Voorspelling")
|
| 77 |
+
st.write(f"**Klasse:** {pred_name}")
|
| 78 |
+
st.write(f"**Vertrouwen:** {pred_conf:.2%}")
|
| 79 |
+
|
| 80 |
+
# Probabilities per klasse
|
| 81 |
+
st.subheader("📊 Waarschijnlijkheid per klasse")
|
| 82 |
+
probs_df = pd.DataFrame({
|
| 83 |
+
"Klasse": CLASS_NAMES,
|
| 84 |
+
"Score": preds
|
| 85 |
+
})
|
| 86 |
+
probs_df = probs_df.set_index("Klasse")
|
| 87 |
+
|
| 88 |
+
st.bar_chart(probs_df)
|
| 89 |
+
|
| 90 |
|
| 91 |
+
if __name__ == "__main__":
|
| 92 |
+
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|