Spaces:
Sleeping
Sleeping
| 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 --- | |
| 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) |