|
|
|
import os |
|
os.environ["STREAMLIT_HOME"] = "/tmp" |
|
os.environ["STREAMLIT_BROWSER_GATHER_USAGE_STATS"] = "false" |
|
|
|
import streamlit as st |
|
from huggingface_hub import hf_hub_download |
|
from tensorflow.keras.models import load_model |
|
from tensorflow.keras.preprocessing import image |
|
import numpy as np |
|
from PIL import Image |
|
|
|
|
|
@st.cache_resource |
|
def load_model_from_hf(): |
|
model_path = hf_hub_download( |
|
repo_id="1Codephoenix/fish-freshness-model", |
|
filename="fish_freshness_model_retrained_final.keras" |
|
) |
|
return load_model(model_path) |
|
|
|
model = load_model_from_hf() |
|
|
|
|
|
class_names = ['Fresh', 'Moderately Fresh', 'Spoiled'] |
|
custom_messages = { |
|
'Fresh': ( |
|
"β
**Fresh Fish Detected**\n" |
|
"- Age: Less than 1 day\n" |
|
"- Bright eyes, red gills, firm flesh\n" |
|
"- Safe to eat raw or cooked" |
|
), |
|
'Moderately Fresh': ( |
|
"β οΈ **Moderately Fresh**\n" |
|
"- Age: 2β3 days\n" |
|
"- Slight odor, softer texture\n" |
|
"- Should be cooked thoroughly" |
|
), |
|
'Spoiled': ( |
|
"π« **Spoiled or Unsafe Fish**\n" |
|
"- Age: 4+ days or preserved\n" |
|
"- Strong odor, dull eyes, mushy flesh\n" |
|
"- Unsafe to consume" |
|
) |
|
} |
|
|
|
|
|
st.set_page_config(page_title="Fish Freshness Classifier", page_icon="π") |
|
st.title("π AI-Powered Fish Freshness Classifier") |
|
st.subheader("Upload a fish image to predict its freshness level") |
|
|
|
uploaded_file = st.file_uploader("Choose a fish image", type=["jpg", "jpeg", "png"]) |
|
|
|
if uploaded_file: |
|
try: |
|
|
|
img = Image.open(uploaded_file).convert("RGB") |
|
img_resized = img.resize((224, 224)) |
|
img_array = image.img_to_array(img_resized) |
|
img_array = np.expand_dims(img_array / 255.0, axis=0) |
|
|
|
|
|
prediction = model.predict(img_array) |
|
predicted_index = np.argmax(prediction) |
|
predicted_class = class_names[predicted_index] |
|
confidence = prediction[0][predicted_index] |
|
|
|
|
|
st.image(img, caption="Uploaded Fish Image", use_column_width=True) |
|
st.markdown(f"### π― Prediction: **{predicted_class}** ({confidence * 100:.2f}%)") |
|
st.markdown(custom_messages[predicted_class]) |
|
|
|
|
|
st.subheader("π Prediction Confidence:") |
|
for i, label in enumerate(class_names): |
|
st.write(f"- {label}: {prediction[0][i] * 100:.2f}%") |
|
|
|
except Exception as e: |
|
st.error(f"β Error processing image: {e}") |
|
|