Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +42 -21
src/streamlit_app.py
CHANGED
|
@@ -1,11 +1,11 @@
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
from PIL import Image
|
| 4 |
-
import onnxruntime as ort
|
| 5 |
|
| 6 |
# ====== MODEL SETTINGS ======
|
| 7 |
-
MODEL_PATH = "cnn_largefish_model.
|
| 8 |
-
IMG_SIZE = 64 # jouw
|
| 9 |
|
| 10 |
CLASS_NAMES = [
|
| 11 |
'House Mackerel',
|
|
@@ -19,37 +19,55 @@ CLASS_NAMES = [
|
|
| 19 |
'Red Sea Bream'
|
| 20 |
]
|
| 21 |
|
|
|
|
|
|
|
| 22 |
@st.cache_resource
|
| 23 |
-
def
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
input_name = session.get_inputs()[0].name
|
| 29 |
-
return session, input_name
|
| 30 |
|
| 31 |
def preprocess_image(image: Image.Image) -> np.ndarray:
|
|
|
|
| 32 |
image = image.convert("RGB")
|
| 33 |
image = image.resize((IMG_SIZE, IMG_SIZE))
|
| 34 |
arr = np.array(image).astype("float32") / 255.0
|
| 35 |
-
arr = np.expand_dims(arr, axis=0)
|
| 36 |
return arr
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
|
|
|
| 40 |
x = preprocess_image(image)
|
| 41 |
-
preds =
|
| 42 |
pred_idx = int(np.argmax(preds))
|
| 43 |
pred_class = CLASS_NAMES[pred_idx]
|
| 44 |
pred_conf = float(preds[pred_idx])
|
| 45 |
return pred_class, pred_conf, preds
|
| 46 |
|
| 47 |
-
st.set_page_config(page_title="Fish Classifier", page_icon="🐟")
|
| 48 |
|
| 49 |
-
|
| 50 |
-
st.
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
if uploaded_file is not None:
|
| 55 |
image = Image.open(uploaded_file)
|
|
@@ -57,12 +75,15 @@ if uploaded_file is not None:
|
|
| 57 |
|
| 58 |
if st.button("Classify"):
|
| 59 |
with st.spinner("Bezig met voorspellen..."):
|
| 60 |
-
pred_class, pred_conf, preds =
|
| 61 |
|
| 62 |
st.subheader("Voorspelling")
|
| 63 |
st.write(f"**{pred_class}** met **{pred_conf:.2%}** zekerheid.")
|
| 64 |
|
|
|
|
|
|
|
| 65 |
st.subheader("Class probabilities")
|
| 66 |
-
st.bar_chart(
|
|
|
|
| 67 |
else:
|
| 68 |
-
st.info("➡️ Upload eerst een afbeelding.")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import tensorflow as tf
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
|
|
|
| 5 |
|
| 6 |
# ====== MODEL SETTINGS ======
|
| 7 |
+
MODEL_PATH = "cnn_largefish_model.h5" # op HuggingFace gewoon in de root neerzetten
|
| 8 |
+
IMG_SIZE = 64 # jouw resize in Kaggle
|
| 9 |
|
| 10 |
CLASS_NAMES = [
|
| 11 |
'House Mackerel',
|
|
|
|
| 19 |
'Red Sea Bream'
|
| 20 |
]
|
| 21 |
|
| 22 |
+
|
| 23 |
+
# ====== FUNCTIES ======
|
| 24 |
@st.cache_resource
|
| 25 |
+
def load_model():
|
| 26 |
+
"""Laad het Keras-model één keer en cache het."""
|
| 27 |
+
model = tf.keras.models.load_model(MODEL_PATH)
|
| 28 |
+
return model
|
| 29 |
+
|
|
|
|
|
|
|
| 30 |
|
| 31 |
def preprocess_image(image: Image.Image) -> np.ndarray:
|
| 32 |
+
"""Resize + normaliseer afbeelding naar hetzelfde formaat als training."""
|
| 33 |
image = image.convert("RGB")
|
| 34 |
image = image.resize((IMG_SIZE, IMG_SIZE))
|
| 35 |
arr = np.array(image).astype("float32") / 255.0
|
| 36 |
+
arr = np.expand_dims(arr, axis=0) # shape: (1, 64, 64, 3)
|
| 37 |
return arr
|
| 38 |
|
| 39 |
+
|
| 40 |
+
def predict_image(model, image: Image.Image):
|
| 41 |
+
"""Voorspel klasse + probabilities voor één afbeelding."""
|
| 42 |
x = preprocess_image(image)
|
| 43 |
+
preds = model.predict(x)[0] # shape: (9,)
|
| 44 |
pred_idx = int(np.argmax(preds))
|
| 45 |
pred_class = CLASS_NAMES[pred_idx]
|
| 46 |
pred_conf = float(preds[pred_idx])
|
| 47 |
return pred_class, pred_conf, preds
|
| 48 |
|
|
|
|
| 49 |
|
| 50 |
+
# ====== STREAMLIT UI ======
|
| 51 |
+
st.set_page_config(
|
| 52 |
+
page_title="Fish Classifier",
|
| 53 |
+
page_icon="🐟",
|
| 54 |
+
)
|
| 55 |
|
| 56 |
+
st.title("🐟 Large-Scale Fish Classifier")
|
| 57 |
+
st.write(
|
| 58 |
+
"""
|
| 59 |
+
Upload een afbeelding van een vis uit de dataset
|
| 60 |
+
en het model voorspelt de soort.
|
| 61 |
+
"""
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
# Model alvast laden (toont spinner)
|
| 65 |
+
with st.spinner("Model laden..."):
|
| 66 |
+
model = load_model()
|
| 67 |
+
|
| 68 |
+
uploaded_file = st.file_uploader(
|
| 69 |
+
"Upload een afbeelding", type=["jpg", "jpeg", "png"]
|
| 70 |
+
)
|
| 71 |
|
| 72 |
if uploaded_file is not None:
|
| 73 |
image = Image.open(uploaded_file)
|
|
|
|
| 75 |
|
| 76 |
if st.button("Classify"):
|
| 77 |
with st.spinner("Bezig met voorspellen..."):
|
| 78 |
+
pred_class, pred_conf, preds = predict_image(model, image)
|
| 79 |
|
| 80 |
st.subheader("Voorspelling")
|
| 81 |
st.write(f"**{pred_class}** met **{pred_conf:.2%}** zekerheid.")
|
| 82 |
|
| 83 |
+
# Probabilities plotten als bar chart
|
| 84 |
+
prob_dict = {CLASS_NAMES[i]: float(preds[i]) for i in range(len(CLASS_NAMES))}
|
| 85 |
st.subheader("Class probabilities")
|
| 86 |
+
st.bar_chart(prob_dict)
|
| 87 |
+
|
| 88 |
else:
|
| 89 |
+
st.info("➡️ Upload eerst een afbeelding (jpg/jpeg/png).")
|