Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +64 -38
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,66 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 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 |
+
IMG_SIZE = 128
|
| 7 |
+
|
| 8 |
+
# Class Labels
|
| 9 |
+
# Based on: {'ALB':0, 'YFT':1, 'SHARK':2, 'NoF':3, 'LAG':4, 'BET':5, 'OTHER':6, 'DOL':7}
|
| 10 |
+
LABELS = ['ALB', 'YFT', 'SHARK', 'NoF', 'LAG', 'BET', 'OTHER', 'DOL']
|
| 11 |
+
|
| 12 |
+
# Page Config
|
| 13 |
+
st.set_page_config(page_title="🐟 Fish Species Detection", page_icon="🐟")
|
| 14 |
+
|
| 15 |
+
# --- LOAD MODEL ---
|
| 16 |
+
@st.cache_resource
|
| 17 |
+
def load_model():
|
| 18 |
+
try:
|
| 19 |
+
model = tf.keras.models.load_model('src/fish.h5')
|
| 20 |
+
return model
|
| 21 |
+
except Exception as e:
|
| 22 |
+
st.error(f"Error loading model: {e}")
|
| 23 |
+
return None
|
| 24 |
+
|
| 25 |
+
model = load_model()
|
| 26 |
+
|
| 27 |
+
# --- IMAGE PREPROCESSING ---
|
| 28 |
+
def process_image(image):
|
| 29 |
+
image = image.resize((IMG_SIZE, IMG_SIZE))
|
| 30 |
+
img_array = np.array(image)
|
| 31 |
+
img_array = img_array / 255.0
|
| 32 |
+
img_array = np.expand_dims(img_array, axis=0)
|
| 33 |
+
return img_array
|
| 34 |
+
|
| 35 |
+
# --- USER INTERFACE ---
|
| 36 |
+
st.title("🐟 Fish Species Classification")
|
| 37 |
+
st.write("The Nature Conservancy Fisheries Monitoring Model")
|
| 38 |
+
|
| 39 |
+
uploaded_file = st.file_uploader("Upload a fish image.", type=["jpg", "png", "jpeg"])
|
| 40 |
|
| 41 |
+
if uploaded_file is not None:
|
| 42 |
+
image = Image.open(uploaded_file).convert('RGB')
|
| 43 |
+
st.image(image, caption='Uploaded Image', use_column_width=True)
|
| 44 |
+
|
| 45 |
+
if st.button('Predict'):
|
| 46 |
+
if model is None:
|
| 47 |
+
st.error("Model could not be loaded, prediction impossible.")
|
| 48 |
+
else:
|
| 49 |
+
with st.spinner('Analyzing...'):
|
| 50 |
+
# Process image
|
| 51 |
+
processed_img = process_image(image)
|
| 52 |
+
# Make prediction
|
| 53 |
+
predictions = model.predict(processed_img)
|
| 54 |
+
# Get the highest probability class
|
| 55 |
+
predicted_class_idx = np.argmax(predictions[0])
|
| 56 |
+
predicted_class = LABELS[predicted_class_idx]
|
| 57 |
+
confidence = np.max(predictions[0])
|
| 58 |
+
# Display Results
|
| 59 |
+
st.success(f"Prediction: **{predicted_class}**")
|
| 60 |
+
st.info(f"Confidence: **{confidence * 100:.2f}%**")
|
| 61 |
+
# Probability Distribution Chart
|
| 62 |
+
st.write("---")
|
| 63 |
+
st.write("Probability Distribution:")
|
| 64 |
+
# Create a dictionary for the chart
|
| 65 |
+
chart_data = {label: float(predictions[0][i]) for i, label in enumerate(LABELS)}
|
| 66 |
+
st.bar_chart(chart_data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|