Spaces:
Sleeping
Sleeping
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,89 +0,0 @@
|
|
| 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',
|
| 12 |
-
'Black Sea Sprat',
|
| 13 |
-
'Sea Bass',
|
| 14 |
-
'Red Mullet',
|
| 15 |
-
'Trout',
|
| 16 |
-
'Striped Red Mullet',
|
| 17 |
-
'Shrimp',
|
| 18 |
-
'Gilt-Head Bream',
|
| 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)
|
| 74 |
-
st.image(image, caption="Geüploade afbeelding", use_column_width=True)
|
| 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).")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|