Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from PIL import Image, ImageOps
|
| 3 |
+
import numpy as np
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
from keras.models import load_model
|
| 7 |
+
|
| 8 |
+
# Load the Keras model
|
| 9 |
+
model = load_model('gastrointestinal_model.h5', compile=False)
|
| 10 |
+
|
| 11 |
+
# Load class names
|
| 12 |
+
class_names = ['Normal', 'Ulcerative Colitis', 'Polyp', 'Esophagitis']
|
| 13 |
+
|
| 14 |
+
# Function to create plot
|
| 15 |
+
def create_plot(prediction, class_names):
|
| 16 |
+
df = pd.DataFrame(prediction, index=class_names, columns=['Confidence'])
|
| 17 |
+
df = df.sort_values(by='Confidence', ascending=False)
|
| 18 |
+
plt.figure(figsize=(8, 5))
|
| 19 |
+
plt.bar(df.index, df['Confidence'], color='blue')
|
| 20 |
+
plt.xlabel('Class')
|
| 21 |
+
plt.ylabel('Confidence Score')
|
| 22 |
+
plt.title('Classification Confidence Scores')
|
| 23 |
+
plt.xticks(rotation=45)
|
| 24 |
+
plt.tight_layout()
|
| 25 |
+
return plt
|
| 26 |
+
|
| 27 |
+
# Function to predict gastrointestinal conditions
|
| 28 |
+
def predict_gastrointestinal(img):
|
| 29 |
+
size = (224, 224)
|
| 30 |
+
image_PIL = ImageOps.fit(img, size, Image.LANCZOS)
|
| 31 |
+
image_array = np.asarray(image_PIL)
|
| 32 |
+
normalized_image_array = (image_array.astype(np.float32) / 127.0) - 1
|
| 33 |
+
data = np.expand_dims(normalized_image_array, axis=0)
|
| 34 |
+
|
| 35 |
+
prediction = model.predict(data)[0]
|
| 36 |
+
class_index = np.argmax(prediction)
|
| 37 |
+
predicted_class = class_names[class_index]
|
| 38 |
+
confidence_scores = prediction * 100
|
| 39 |
+
|
| 40 |
+
# Create plot
|
| 41 |
+
plot = create_plot(confidence_scores, class_names)
|
| 42 |
+
|
| 43 |
+
return predicted_class, plot
|
| 44 |
+
|
| 45 |
+
# Streamlit app
|
| 46 |
+
st.title("Gastrointestinal Classification Web App")
|
| 47 |
+
|
| 48 |
+
uploaded_file = st.file_uploader("Upload an image...", type=["jpg", "jpeg", "png"])
|
| 49 |
+
|
| 50 |
+
if uploaded_file is not None:
|
| 51 |
+
image = Image.open(uploaded_file)
|
| 52 |
+
st.image(image, caption='Uploaded Image', use_column_width=True)
|
| 53 |
+
st.write("Classifying...")
|
| 54 |
+
|
| 55 |
+
predicted_class, plot = predict_gastrointestinal(image)
|
| 56 |
+
st.write(f"Prediction: {predicted_class}")
|
| 57 |
+
st.pyplot(plot)
|
| 58 |
+
|
| 59 |
+
# Sample images
|
| 60 |
+
st.markdown("### Sample Images")
|
| 61 |
+
if st.button('Normal Sample'):
|
| 62 |
+
image = Image.open('normal_sample.jpg')
|
| 63 |
+
st.image(image, caption='Normal Sample Image', use_column_width=True)
|
| 64 |
+
st.write("Classifying...")
|
| 65 |
+
predicted_class, plot = predict_gastrointestinal(image)
|
| 66 |
+
st.write(f"Prediction: {predicted_class}")
|
| 67 |
+
st.pyplot(plot)
|
| 68 |
+
|
| 69 |
+
if st.button('Ulcerative Colitis Sample'):
|
| 70 |
+
image = Image.open('ulcerative_colitis_sample.jpg')
|
| 71 |
+
st.image(image, caption='Ulcerative Colitis Sample Image', use_column_width=True)
|
| 72 |
+
st.write("Classifying...")
|
| 73 |
+
predicted_class, plot = predict_gastrointestinal(image)
|
| 74 |
+
st.write(f"Prediction: {predicted_class}")
|
| 75 |
+
st.pyplot(plot)
|
| 76 |
+
|
| 77 |
+
if st.button('Polyp Sample'):
|
| 78 |
+
image = Image.open('polyp_sample.jpg')
|
| 79 |
+
st.image(image, caption='Polyp Sample Image', use_column_width=True)
|
| 80 |
+
st.write("Classifying...")
|
| 81 |
+
predicted_class, plot = predict_gastrointestinal(image)
|
| 82 |
+
st.write(f"Prediction: {predicted_class}")
|
| 83 |
+
st.pyplot(plot)
|
| 84 |
+
|
| 85 |
+
if st.button('Esophagitis Sample'):
|
| 86 |
+
image = Image.open('esophagitis_sample.jpg')
|
| 87 |
+
st.image(image, caption='Esophagitis Sample Image', use_column_width=True)
|
| 88 |
+
st.write("Classifying...")
|
| 89 |
+
predicted_class, plot = predict_gastrointestinal(image)
|
| 90 |
+
st.write(f"Prediction: {predicted_class}")
|
| 91 |
+
st.pyplot(plot)
|
| 92 |
+
|
| 93 |
+
# Educational content
|
| 94 |
+
st.markdown("### Learn More About Gastrointestinal Conditions")
|
| 95 |
+
st.markdown("""
|
| 96 |
+
- [Gastrointestinal Disorders Overview](https://www.mayoclinic.org/diseases-conditions/gastrointestinal-disorders/symptoms-causes/syc-20375441)
|
| 97 |
+
- [Preventing Gastrointestinal Conditions](https://www.niddk.nih.gov/health-information/digestive-diseases)
|
| 98 |
+
""")
|