Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +96 -38
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,98 @@
|
|
| 1 |
-
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 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 |
+
# --- Fish Classifier (CNN from scratch) ---
|
|
|
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import tensorflow as tf
|
| 6 |
+
from tensorflow.keras.models import load_model
|
| 7 |
+
|
| 8 |
+
st.set_page_config(
|
| 9 |
+
page_title="Fish Classifier (CNN – No Transfer Learning)",
|
| 10 |
+
layout="centered"
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
# ------------------ SETTINGS ------------------
|
| 14 |
+
MODEL_PATH = "src/best_fish_cnn.keras"
|
| 15 |
+
IMG_SIZE = (224, 224)
|
| 16 |
+
|
| 17 |
+
CLASS_NAMES = [
|
| 18 |
+
"Black Sea Sprat",
|
| 19 |
+
"Gilt-Head Bream",
|
| 20 |
+
"Horse Mackerel",
|
| 21 |
+
"Red Mullet",
|
| 22 |
+
"Red Sea Bream",
|
| 23 |
+
"Sea Bass",
|
| 24 |
+
"Shrimp",
|
| 25 |
+
"Striped Red Mullet",
|
| 26 |
+
"Trout"
|
| 27 |
+
]
|
| 28 |
+
|
| 29 |
+
# ------------------ CACHED MODEL LOADING ------------------
|
| 30 |
+
@st.cache_resource
|
| 31 |
+
def load_model_cached():
|
| 32 |
+
return load_model(MODEL_PATH, compile=False)
|
| 33 |
+
|
| 34 |
+
model = load_model_cached()
|
| 35 |
+
|
| 36 |
+
# ------------------ HELPER FUNCTIONS ------------------
|
| 37 |
+
def preprocess_image(image: Image.Image) -> np.ndarray:
|
| 38 |
+
"""Resize and normalize image for the CNN model."""
|
| 39 |
+
image = image.convert("RGB")
|
| 40 |
+
image = image.resize(IMG_SIZE)
|
| 41 |
+
img_array = np.array(image).astype("float32") / 255.0 # weil du mit rescale=1./255 trainiert hast
|
| 42 |
+
img_array = np.expand_dims(img_array, axis=0) # (1, 224, 224, 3)
|
| 43 |
+
return img_array
|
| 44 |
+
|
| 45 |
+
def predict_image(image: Image.Image):
|
| 46 |
+
"""Run model prediction and return class index and probabilities."""
|
| 47 |
+
img_batch = preprocess_image(image)
|
| 48 |
+
preds = model.predict(img_batch)
|
| 49 |
+
probs = preds[0]
|
| 50 |
+
pred_index = int(np.argmax(probs))
|
| 51 |
+
pred_class = CLASS_NAMES[pred_index]
|
| 52 |
+
return pred_class, probs
|
| 53 |
+
|
| 54 |
+
# ------------------ STREAMLIT UI ------------------
|
| 55 |
+
st.set_page_config(
|
| 56 |
+
page_title="Fish Classifier (CNN – From Scratch)",
|
| 57 |
+
layout="centered"
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
st.title("🐟 Fish Species Classifier (CNN From Scratch)")
|
| 61 |
+
st.write(
|
| 62 |
+
"Upload a fish image and the model will predict one of **9 fish species**. "
|
| 63 |
+
"This model is a custom Convolutional Neural Network trained from scratch."
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
st.sidebar.header("About this app")
|
| 67 |
+
st.sidebar.markdown(
|
| 68 |
+
"""
|
| 69 |
+
**Model:** CNN from scratch
|
| 70 |
+
**Input size:** 224×224
|
| 71 |
+
**Classes:** 9 fish species
|
| 72 |
+
**Author:** Enes Yamac
|
| 73 |
+
"""
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
uploaded_file = st.file_uploader(
|
| 77 |
+
"Upload a fish image (JPG/PNG)",
|
| 78 |
+
type=["jpg", "jpeg", "png"]
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
if uploaded_file is not None:
|
| 82 |
+
image = Image.open(uploaded_file)
|
| 83 |
+
st.image(image, caption="Uploaded image", use_column_width=True)
|
| 84 |
+
|
| 85 |
+
with st.spinner("Predicting..."):
|
| 86 |
+
pred_class, probs = predict_image(image)
|
| 87 |
+
|
| 88 |
+
st.subheader("Prediction")
|
| 89 |
+
st.write(f"**Predicted class:** {pred_class}")
|
| 90 |
+
|
| 91 |
+
# Show probabilities for all classes
|
| 92 |
+
st.subheader("Class probabilities")
|
| 93 |
+
prob_dict = {cls: float(p) for cls, p in zip(CLASS_NAMES, probs)}
|
| 94 |
+
|
| 95 |
+
st.bar_chart(prob_dict)
|
| 96 |
|
| 97 |
+
else:
|
| 98 |
+
st.info("Please upload an image to start.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|