reciouiikram's picture
Update app.py
42c4ab2 verified
Raw History Blame Contribute Delete
4.27 kB
import streamlit as st
import tensorflow as tf
from tensorflow.keras.models import load_model
from PIL import Image, ImageOps
import numpy as np
# Set the page title and layout
st.set_page_config(
page_title="Microorganism Classification 🦠",
layout="wide",
initial_sidebar_state="expanded"
)
# Add custom blue-themed CSS
st.markdown(
"""
<style>
body {
background-color:rgb(27, 54, 77);
}
.stApp {
background: linear-gradient(to right,rgb(123, 166, 247),rgb(1, 103, 236));
}
.stTitle {
color:rgb(169, 209, 240);
}
.stHeader {
color:rgb(155, 213, 247);
}
.stMarkdown {
color:rgb(170, 215, 240);
}
.stSidebar {
background-color:rgb(88, 143, 226);
}
.stButton > button {
background-color: #0288d1;
color: white;
border: none;
border-radius: 5px;
}
.stButton > button:hover {
background-color: #01579b;
}
</style>
<footer>
&copy; 2024 By: Ikram Recioui. All Rights Reserved.
</footer>
""",
unsafe_allow_html=True,
)
# Load the pre-trained model
@st.cache_resource
def load_trained_model():
return load_model(r"Micro_organisme_3_model.h5", compile=False)
model = load_trained_model()
# Define class labels
class_labels = [
"Bacteria",
"Lactobacillus",
"Micrococcus",
"Virus"
]
# Home page content
def home():
st.title("Welcome to the Microorganism Classification App πŸ¦ πŸ”¬")
st.markdown("This application helps classify microorganism images into four distinct categories using AI. πŸ¦ πŸ€–")
# Section: About Microorganisms
st.subheader("What are Microorganisms? πŸ€”")
st.markdown("""
Microorganisms are microscopic organisms, such as bacteria, viruses, and fungi, that are invisible to the naked eye.
They play significant roles in ecosystems, health, and industries. This app assists researchers and professionals by
automating the classification of microorganism images into four categories: Bacteria, Lactobacillus, Micrococcus, and Virus. πŸ€–βš™οΈ
""")
# Video
st.subheader("Learn More About Microorganisms πŸ“˜πŸ’‘")
st.video("https://www.youtube.com/watch?v=6OQVP8jacek")
# Additional description
st.markdown("""
### Features of this App βœ¨πŸ”:
- Upload your microorganism images.
- View AI predictions for 4 different categories:
1. **Bacteria**: A diverse group of prokaryotic microorganisms.
2. **Lactobacillus**: A genus of bacteria known for its role in fermentation.
3. **Micrococcus**: A genus of spherical, Gram-positive bacteria.
4. **Virus**: Submicroscopic infectious agents that replicate only inside living cells.
""")
# Prediction page content
def upload_and_predict():
st.title("Upload and Predict πŸ“€")
# File uploader for image upload
uploaded_file = st.file_uploader("Choose an image of a microorganism... πŸ–ΌοΈ", type=["jpg", "jpeg", "png"])
if uploaded_file is not None:
# Display the uploaded image
st.image(uploaded_file, caption="Uploaded Image")
# Load the image for prediction
image = Image.open(uploaded_file)
image = ImageOps.fit(image, (224, 224), Image.Resampling.LANCZOS)
image_array = np.asarray(image) / 255.0 # Normalize the image
image_array = np.expand_dims(image_array, axis=0) # Add batch dimension
# Make prediction
predictions = model.predict(image_array)
predicted_class = class_labels[np.argmax(predictions)]
confidence = np.max(predictions) * 100
# Display the prediction
st.subheader("Prediction Result πŸ“Š:")
st.markdown(f"**Predicted Class:** {predicted_class}")
st.markdown(f"**Confidence:** {confidence:.2f}%")
# Main function to control navigation
if __name__ == "__main__":
st.sidebar.title("Navigation")
page = st.sidebar.radio("Go to", ["Home", "Upload and Predict"])
if page == "Home":
home()
elif page == "Upload and Predict":
upload_and_predict()