fish-classifier / app.py
alperugurcan's picture
Update app.py
024d098 verified
import streamlit as st
import tensorflow as tf
import numpy as np
from PIL import Image
import requests
from io import BytesIO
import pandas as pd
# Move st.set_page_config() to the top
st.set_page_config(page_title="Fish Species Classifier", page_icon="🐠", layout="wide")
# Load model
@st.cache_resource
def load_model():
return tf.keras.models.load_model('fish_classification_model.h5')
model = load_model()
# Class names
class_names = ['Black Sea Sprat', 'Gilt Head Bream', 'Horse Mackerel', 'Red Mullet', 'Red Sea Bream', 'Sea Bass', 'Shrimp', 'Striped Red Mullet', 'Trout'] # Sınıf isimleri
# Function to get fish emoji
def get_fish_emoji(fish_name):
emoji_dict = {
'Black Sea Sprat': '🐟',
'Gilt Head Bream': '🐠',
'Horse Mackerel': '🐟',
'Red Mullet': '🐡',
'Red Sea Bream': '🐠',
'Sea Bass': '🐟',
'Shrimp': '🦐',
'Striped Red Mullet': '🐡',
'Trout': '🐟'
}
return emoji_dict.get(fish_name, '🐠')
# Add a background image
background_image = """
<style>
[data-testid="stAppViewContainer"] > .main {
background-image: url("https://images.unsplash.com/photo-1498574932731-e711f7092d07");
background-size: cover;
background-position: center center;
background-repeat: no-repeat;
background-attachment: local;
}
</style>
"""
st.markdown(background_image, unsafe_allow_html=True)
# Custom CSS for better styling
st.markdown("""
<style>
.big-font {
font-size:50px !important;
color: #0e1117;
text-align: center;
}
.result-font {
font-size:30px !important;
color: #0e1117;
text-align: center;
}
</style>
""", unsafe_allow_html=True)
# Title with emoji
st.markdown('<p class="big-font">🐠 Fish Species Classification 🐟</p>', unsafe_allow_html=True)
# File uploader
uploaded_file = st.file_uploader("Upload a fish image", type=["jpg", "jpeg", "png"])
# URL input
image_url = st.text_input("Or enter an image URL")
if uploaded_file is not None or image_url:
if uploaded_file is not None:
image = Image.open(uploaded_file)
else:
response = requests.get(image_url)
image = Image.open(BytesIO(response.content))
st.image(image, caption='Uploaded Image', use_column_width=True) # Yüklenen resmi göster
# Preprocess image
image = image.resize((224, 224))
image_array = np.array(image) / 255.0
image_array = np.expand_dims(image_array, axis=0) # Resmi ön işle
# Make prediction
prediction = model.predict(image_array)
predicted_class = class_names[np.argmax(prediction)]
confidence = np.max(prediction) # Tahmin yap
# Display result with emoji
st.markdown(f'<p class="result-font">Predicted fish species: {predicted_class} {get_fish_emoji(predicted_class)}</p>', unsafe_allow_html=True)
st.markdown(f'<p class="result-font">Confidence: {confidence:.2f}</p>', unsafe_allow_html=True)
# Display bar chart of probabilities
st.subheader("Prediction Probabilities")
prob_df = pd.DataFrame({'Species': class_names, 'Probability': prediction[0]})
prob_df = prob_df.sort_values('Probability', ascending=False).reset_index(drop=True)
st.bar_chart(prob_df.set_index('Species'))
# Add some information about the project
st.sidebar.title("About")
st.sidebar.info(
"This app uses a deep learning model to classify fish species. "
"Upload an image or provide a URL to get started!"
)
# Add a footer
st.markdown(
"""
<style>
#MainMenu {visibility: hidden;}
footer {visibility: hidden;}
.footer {
position: fixed;
left: 0;
bottom: 0;
width: 100%;
background-color: rgba(14, 17, 23, 0.5);
color: white;
text-align: center;
}
</style>
<div class="footer">
<p>Developed with ❤️ by AE</p>
</div>
""",
unsafe_allow_html=True
)