|
|
import streamlit as st |
|
|
from PIL import Image, ImageOps |
|
|
import numpy as np |
|
|
import pandas as pd |
|
|
import tensorflow as tf |
|
|
|
|
|
|
|
|
model = tf.keras.models.load_model('gastrointestinal_model.h5', compile=False) |
|
|
|
|
|
|
|
|
with open('labels.txt', 'r') as f: |
|
|
class_names = f.readlines() |
|
|
|
|
|
|
|
|
def predict_gastrointestinal(img): |
|
|
np.set_printoptions(suppress=True) |
|
|
data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32) |
|
|
size = (224, 224) |
|
|
image_PIL = ImageOps.fit(img, size, Image.LANCZOS) |
|
|
image_array = np.asarray(image_PIL) |
|
|
normalized_image_array = (image_array.astype(np.float32) / 127.0) - 1 |
|
|
data[0] = normalized_image_array |
|
|
prediction = model.predict(data) |
|
|
index = np.argmax(prediction) |
|
|
class_name = class_names[index].strip() |
|
|
confidence_score = prediction[0][index] |
|
|
other_class = [name for i, name in enumerate(class_names) if i != index][0].strip() |
|
|
|
|
|
result = { |
|
|
"Labels": [class_name, other_class], |
|
|
"Confidence Score": [confidence_score * 100, (1 - confidence_score) * 100], |
|
|
"Total": 100 |
|
|
} |
|
|
|
|
|
if class_name == "Normal": |
|
|
prediction_text = "The image is classified as Normal." |
|
|
else: |
|
|
prediction_text = f"The image shows signs of {class_name}." |
|
|
|
|
|
return prediction_text |
|
|
|
|
|
|
|
|
st.title("Gastrointestinal Classification Web App") |
|
|
|
|
|
st.write(""" |
|
|
Welcome to the Gastrointestinal Classification Web App! This tool allows you to upload images of gastrointestinal conditions and receive classification results. |
|
|
|
|
|
### How to Use: |
|
|
|
|
|
1. **Upload an Image**: Click the "Upload a gastrointestinal image..." button to select and upload an image file in JPG, JPEG, or PNG format. |
|
|
|
|
|
2. **View Classification**: Once the image is uploaded, it will be processed, and the app will classify it into one of the gastrointestinal conditions: Normal, Ulcerative Colitis, Polyp, or Esophagitis. |
|
|
|
|
|
3. **See Results**: The app will display the classification result along with a confidence score. |
|
|
|
|
|
### Next Steps: |
|
|
|
|
|
- **For Medical Advice**: If the classification suggests a potential issue, we recommend consulting with a healthcare professional for a thorough examination and diagnosis. |
|
|
|
|
|
- **Upload More Images**: Feel free to upload additional images if you wish to check more samples. |
|
|
|
|
|
- **Contact Support**: If you encounter any issues or have questions about the app, please contact our support team for assistance. |
|
|
|
|
|
Thank you for using our Gastrointestinal Classification Web App! |
|
|
""") |
|
|
|
|
|
uploaded_file = st.file_uploader("Upload a gastrointestinal image...", type=["jpg", "jpeg", "png"]) |
|
|
|
|
|
if uploaded_file is not None: |
|
|
image = Image.open(uploaded_file) |
|
|
st.image(image, caption='Uploaded Image', use_column_width=True) |
|
|
st.write("Classifying...") |
|
|
|
|
|
prediction = predict_gastrointestinal(image) |
|
|
st.write(prediction) |
|
|
|
|
|
st.write(""" |
|
|
**Next Steps:** |
|
|
|
|
|
- If the result indicates a condition of concern, please seek advice from a healthcare professional. |
|
|
- You can upload more images for further classification. |
|
|
- For any issues or questions, reach out to our support team. |
|
|
""") |
|
|
|