Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app5.py +88 -0
- cnn_largefish_model.onnx +3 -0
app5.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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.onnx"
|
| 8 |
+
IMG_SIZE = 64
|
| 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 |
+
@st.cache_resource
|
| 24 |
+
def load_session():
|
| 25 |
+
"""
|
| 26 |
+
Laad het ONNX-model één keer in een ONNX Runtime sessie.
|
| 27 |
+
"""
|
| 28 |
+
session = ort.InferenceSession(
|
| 29 |
+
MODEL_PATH,
|
| 30 |
+
providers=["CPUExecutionProvider"],
|
| 31 |
+
)
|
| 32 |
+
input_name = session.get_inputs()[0].name
|
| 33 |
+
return session, input_name
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def preprocess_image(image: Image.Image) -> np.ndarray:
|
| 37 |
+
"""
|
| 38 |
+
Resize + normaliseer naar (1, 64, 64, 3) met waarden 0–1.
|
| 39 |
+
"""
|
| 40 |
+
image = image.convert("RGB")
|
| 41 |
+
image = image.resize((IMG_SIZE, IMG_SIZE))
|
| 42 |
+
arr = np.array(image).astype("float32") / 255.0
|
| 43 |
+
arr = np.expand_dims(arr, axis=0) # (1, 64, 64, 3)
|
| 44 |
+
return arr
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def predict(image: Image.Image):
|
| 48 |
+
"""
|
| 49 |
+
Run één voorspelling via ONNX Runtime.
|
| 50 |
+
"""
|
| 51 |
+
session, input_name = load_session()
|
| 52 |
+
x = preprocess_image(image)
|
| 53 |
+
|
| 54 |
+
# ONNX Runtime geeft een list terug; [0] is de output tensor
|
| 55 |
+
preds = session.run(None, {input_name: x})[0][0] # shape: (9,)
|
| 56 |
+
|
| 57 |
+
pred_idx = int(np.argmax(preds))
|
| 58 |
+
pred_class = CLASS_NAMES[pred_idx]
|
| 59 |
+
pred_conf = float(preds[pred_idx])
|
| 60 |
+
|
| 61 |
+
return pred_class, pred_conf, preds
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
# === STREAMLIT UI ===
|
| 65 |
+
st.set_page_config(page_title="Large-Scale Fish Classifier", page_icon="🐟")
|
| 66 |
+
|
| 67 |
+
st.title("🐟 Large-Scale Fish Classifier")
|
| 68 |
+
st.write("Upload een afbeelding van een vis en het model voorspelt de soort.")
|
| 69 |
+
|
| 70 |
+
uploaded_file = st.file_uploader("Upload een afbeelding", type=["jpg", "jpeg", "png"])
|
| 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)
|
| 79 |
+
|
| 80 |
+
st.subheader("Voorspelling")
|
| 81 |
+
st.write(f"**{pred_class}** met **{pred_conf:.2%}** zekerheid.")
|
| 82 |
+
|
| 83 |
+
st.subheader("Class probabilities")
|
| 84 |
+
st.bar_chart(
|
| 85 |
+
{CLASS_NAMES[i]: float(preds[i]) for i in range(len(CLASS_NAMES))}
|
| 86 |
+
)
|
| 87 |
+
else:
|
| 88 |
+
st.info("➡️ Upload eerst een afbeelding (jpg/jpeg/png).")
|
cnn_largefish_model.onnx
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:0363ff3a5a2b98a411c9ccb71b54ff5827f952bfbb2fc1d55bb2d1a49bc5144d
|
| 3 |
+
size 12841638
|