Spaces:
Sleeping
Sleeping
Update src/app.py
Browse files- src/app.py +68 -89
src/app.py
CHANGED
|
@@ -1,89 +1,68 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
# ====== MODEL SETTINGS ======
|
| 7 |
-
MODEL_PATH = "cnn_largefish_model.
|
| 8 |
-
IMG_SIZE = 64 # jouw
|
| 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 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
def preprocess_image(image: Image.Image) -> np.ndarray:
|
| 32 |
-
|
| 33 |
-
image = image.
|
| 34 |
-
|
| 35 |
-
arr = np.
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
pred_class
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
st.
|
| 57 |
-
|
| 58 |
-
""
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
)
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 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).")
|
|
|
|
| 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 # jouw trainingsgrootte
|
| 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 |
+
@st.cache_resource
|
| 23 |
+
def load_session():
|
| 24 |
+
session = ort.InferenceSession(
|
| 25 |
+
MODEL_PATH,
|
| 26 |
+
providers=["CPUExecutionProvider"]
|
| 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 |
+
def predict(image: Image.Image):
|
| 39 |
+
session, input_name = load_session()
|
| 40 |
+
x = preprocess_image(image)
|
| 41 |
+
preds = session.run(None, {input_name: x})[0][0]
|
| 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 |
+
st.title("🐟 Large-Scale Fish Classifier (ONNX)")
|
| 50 |
+
st.write("Upload een afbeelding en het model voorspelt de soort.")
|
| 51 |
+
|
| 52 |
+
uploaded_file = st.file_uploader("Upload een afbeelding", type=["jpg", "jpeg", "png"])
|
| 53 |
+
|
| 54 |
+
if uploaded_file is not None:
|
| 55 |
+
image = Image.open(uploaded_file)
|
| 56 |
+
st.image(image, caption="Geüploade afbeelding", use_column_width=True)
|
| 57 |
+
|
| 58 |
+
if st.button("Classify"):
|
| 59 |
+
with st.spinner("Bezig met voorspellen..."):
|
| 60 |
+
pred_class, pred_conf, preds = predict(image)
|
| 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({CLASS_NAMES[i]: float(preds[i]) for i in range(len(CLASS_NAMES))})
|
| 67 |
+
else:
|
| 68 |
+
st.info("➡️ Upload eerst een afbeelding.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|