Upload 2 files
Browse files- app.py +131 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
from tensorflow.keras.models import load_model
|
| 4 |
+
from PIL import Image, ImageOps
|
| 5 |
+
import numpy as np
|
| 6 |
+
|
| 7 |
+
# Set the page title and layout
|
| 8 |
+
st.set_page_config(
|
| 9 |
+
page_title="Microorganism Classification π¦ ",
|
| 10 |
+
layout="wide",
|
| 11 |
+
initial_sidebar_state="expanded"
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
# Add custom blue-themed CSS
|
| 15 |
+
st.markdown(
|
| 16 |
+
"""
|
| 17 |
+
<style>
|
| 18 |
+
body {
|
| 19 |
+
background-color:rgb(27, 54, 77);
|
| 20 |
+
}
|
| 21 |
+
.stApp {
|
| 22 |
+
background: linear-gradient(to right,rgb(123, 166, 247),rgb(1, 103, 236));
|
| 23 |
+
}
|
| 24 |
+
.stTitle {
|
| 25 |
+
color:rgb(169, 209, 240);
|
| 26 |
+
}
|
| 27 |
+
.stHeader {
|
| 28 |
+
color:rgb(155, 213, 247);
|
| 29 |
+
}
|
| 30 |
+
.stMarkdown {
|
| 31 |
+
color:rgb(170, 215, 240);
|
| 32 |
+
}
|
| 33 |
+
.stSidebar {
|
| 34 |
+
background-color:rgb(88, 143, 226);
|
| 35 |
+
}
|
| 36 |
+
.stButton > button {
|
| 37 |
+
background-color: #0288d1;
|
| 38 |
+
color: white;
|
| 39 |
+
border: none;
|
| 40 |
+
border-radius: 5px;
|
| 41 |
+
}
|
| 42 |
+
.stButton > button:hover {
|
| 43 |
+
background-color: #01579b;
|
| 44 |
+
}
|
| 45 |
+
</style>
|
| 46 |
+
<footer>
|
| 47 |
+
© 2024 By: Ikram Recioui. All Rights Reserved.
|
| 48 |
+
</footer>
|
| 49 |
+
""",
|
| 50 |
+
unsafe_allow_html=True,
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
# Load the pre-trained model
|
| 54 |
+
@st.cache_resource
|
| 55 |
+
def load_trained_model():
|
| 56 |
+
return load_model(r"Micro_organisme_3_model.h5", compile=False)
|
| 57 |
+
|
| 58 |
+
model = load_trained_model()
|
| 59 |
+
|
| 60 |
+
# Define class labels
|
| 61 |
+
class_labels = [
|
| 62 |
+
"Bacteria",
|
| 63 |
+
"Lactobacillus",
|
| 64 |
+
"Micrococcus",
|
| 65 |
+
"Virus"
|
| 66 |
+
]
|
| 67 |
+
|
| 68 |
+
# Home page content
|
| 69 |
+
def home():
|
| 70 |
+
st.title("Welcome to the Microorganism Classification App π¦ π¬")
|
| 71 |
+
st.markdown("This application helps classify microorganism images into four distinct categories using AI. π¦ π€")
|
| 72 |
+
|
| 73 |
+
# Section: About Microorganisms
|
| 74 |
+
st.subheader("What are Microorganisms? π€")
|
| 75 |
+
st.markdown("""
|
| 76 |
+
Microorganisms are microscopic organisms, such as bacteria, viruses, and fungi, that are invisible to the naked eye.
|
| 77 |
+
They play significant roles in ecosystems, health, and industries. This app assists researchers and professionals by
|
| 78 |
+
automating the classification of microorganism images into four categories: Bacteria, Lactobacillus, Micrococcus, and Virus. π€βοΈ
|
| 79 |
+
""")
|
| 80 |
+
|
| 81 |
+
# Video
|
| 82 |
+
st.subheader("Learn More About Microorganisms ππ‘")
|
| 83 |
+
st.video("https://www.youtube.com/watch?v=6OQVP8jacek")
|
| 84 |
+
|
| 85 |
+
# Additional description
|
| 86 |
+
st.markdown("""
|
| 87 |
+
### Features of this App β¨π:
|
| 88 |
+
- Upload your microorganism images.
|
| 89 |
+
- View AI predictions for 4 different categories:
|
| 90 |
+
1. **Bacteria**: A diverse group of prokaryotic microorganisms.
|
| 91 |
+
2. **Lactobacillus**: A genus of bacteria known for its role in fermentation.
|
| 92 |
+
3. **Micrococcus**: A genus of spherical, Gram-positive bacteria.
|
| 93 |
+
4. **Virus**: Submicroscopic infectious agents that replicate only inside living cells.
|
| 94 |
+
""")
|
| 95 |
+
|
| 96 |
+
# Prediction page content
|
| 97 |
+
def upload_and_predict():
|
| 98 |
+
st.title("Upload and Predict π€")
|
| 99 |
+
|
| 100 |
+
# File uploader for image upload
|
| 101 |
+
uploaded_file = st.file_uploader("Choose an image of a microorganism... πΌοΈ", type=["jpg", "jpeg", "png"])
|
| 102 |
+
|
| 103 |
+
if uploaded_file is not None:
|
| 104 |
+
# Display the uploaded image
|
| 105 |
+
st.image(uploaded_file, caption="Uploaded Image")
|
| 106 |
+
|
| 107 |
+
# Load the image for prediction
|
| 108 |
+
image = Image.open(uploaded_file)
|
| 109 |
+
image = ImageOps.fit(image, (224, 224), Image.Resampling.LANCZOS)
|
| 110 |
+
image_array = np.asarray(image) / 255.0 # Normalize the image
|
| 111 |
+
image_array = np.expand_dims(image_array, axis=0) # Add batch dimension
|
| 112 |
+
|
| 113 |
+
# Make prediction
|
| 114 |
+
predictions = model.predict(image_array)
|
| 115 |
+
predicted_class = class_labels[np.argmax(predictions)]
|
| 116 |
+
confidence = np.max(predictions) * 100
|
| 117 |
+
|
| 118 |
+
# Display the prediction
|
| 119 |
+
st.subheader("Prediction Result π:")
|
| 120 |
+
st.markdown(f"**Predicted Class:** {predicted_class}")
|
| 121 |
+
st.markdown(f"**Confidence:** {confidence:.2f}%")
|
| 122 |
+
|
| 123 |
+
# Main function to control navigation
|
| 124 |
+
if __name__ == "__main__":
|
| 125 |
+
st.sidebar.title("Navigation")
|
| 126 |
+
page = st.sidebar.radio("Go to", ["Home", "Upload and Predict"])
|
| 127 |
+
|
| 128 |
+
if page == "Home":
|
| 129 |
+
home()
|
| 130 |
+
elif page == "Upload and Predict":
|
| 131 |
+
upload_and_predict()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
tensorflow
|
| 3 |
+
pillow
|
| 4 |
+
numpy
|