BeyzaTopbas commited on
Commit
bf220f0
·
verified ·
1 Parent(s): 70fc779

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +83 -34
src/streamlit_app.py CHANGED
@@ -1,40 +1,89 @@
1
- import altair as alt
2
- import numpy as np
3
- import pandas as pd
4
  import streamlit as st
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
- """
7
- # Welcome to Streamlit!
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 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).")