File size: 2,358 Bytes
40b9560
d5dd5e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40b9560
d5dd5e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import streamlit as st
import tensorflow as tf
import numpy as np
from PIL import Image

IMG_SIZE = 128 

# Class Labels
# Based on: {'ALB':0, 'YFT':1, 'SHARK':2, 'NoF':3, 'LAG':4, 'BET':5, 'OTHER':6, 'DOL':7}
LABELS = ['ALB', 'YFT', 'SHARK', 'NoF', 'LAG', 'BET', 'OTHER', 'DOL']

# Page Config
st.set_page_config(page_title="๐ŸŸ Fish Species Detection", page_icon="๐ŸŸ")

# --- LOAD MODEL ---
@st.cache_resource
def load_model():
    try:
        model = tf.keras.models.load_model('src/fish.h5')
        return model
    except Exception as e:
        st.error(f"Error loading model: {e}")
        return None

model = load_model()

# --- IMAGE PREPROCESSING ---
def process_image(image):
    image = image.resize((IMG_SIZE, IMG_SIZE))
    img_array = np.array(image)
    img_array = img_array / 255.0
    img_array = np.expand_dims(img_array, axis=0)
    return img_array

# --- USER INTERFACE ---
st.title("๐ŸŸ Fish Species Classification")
st.write("The Nature Conservancy Fisheries Monitoring Model")

uploaded_file = st.file_uploader("Upload a fish image.", type=["jpg", "png", "jpeg"])

if uploaded_file is not None:
    image = Image.open(uploaded_file).convert('RGB')
    st.image(image, caption='Uploaded Image', use_column_width=True)
    
    if st.button('Predict'):
        if model is None:
            st.error("Model could not be loaded, prediction impossible.")
        else:
            with st.spinner('Analyzing...'):
                # Process image
                processed_img = process_image(image)
                # Make prediction
                predictions = model.predict(processed_img)                
                # Get the highest probability class
                predicted_class_idx = np.argmax(predictions[0])
                predicted_class = LABELS[predicted_class_idx]
                confidence = np.max(predictions[0])
                # Display Results
                st.success(f"Prediction: **{predicted_class}**")
                st.info(f"Confidence: **{confidence * 100:.2f}%**")
                # Probability Distribution Chart
                st.write("---")
                st.write("Probability Distribution:")
                # Create a dictionary for the chart
                chart_data = {label: float(predictions[0][i]) for i, label in enumerate(LABELS)}
                st.bar_chart(chart_data)