Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
def predict_gastrointestinal(img):
|
| 2 |
np.set_printoptions(suppress=True)
|
| 3 |
data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
|
|
@@ -7,10 +35,6 @@ def predict_gastrointestinal(img):
|
|
| 7 |
normalized_image_array = (image_array.astype(np.float32) / 127.0) - 1
|
| 8 |
data[0] = normalized_image_array
|
| 9 |
prediction = model.predict(data)
|
| 10 |
-
|
| 11 |
-
# Debugging: Print raw prediction values
|
| 12 |
-
st.write("Raw prediction values:", prediction)
|
| 13 |
-
|
| 14 |
index = np.argmax(prediction)
|
| 15 |
class_name = class_names[index].strip()
|
| 16 |
confidence_score = prediction[0][index]
|
|
@@ -30,3 +54,20 @@ def predict_gastrointestinal(img):
|
|
| 30 |
prediction_text = f"The image shows signs of {class_name}."
|
| 31 |
|
| 32 |
return prediction_text, plot
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from PIL import Image, ImageOps
|
| 3 |
+
import numpy as np
|
| 4 |
+
import seaborn as sns
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
import pandas as pd
|
| 7 |
+
import tensorflow as tf # Use TensorFlow's Keras API
|
| 8 |
+
|
| 9 |
+
# Load the TensorFlow Keras model
|
| 10 |
+
model = tf.keras.models.load_model('gastrointestinal_model.h5', compile=False)
|
| 11 |
+
|
| 12 |
+
# Load class names
|
| 13 |
+
with open('labels.txt', 'r') as f:
|
| 14 |
+
class_names = f.readlines()
|
| 15 |
+
|
| 16 |
+
# Function to create plot
|
| 17 |
+
def create_plot(data):
|
| 18 |
+
sns.set_theme(style="whitegrid")
|
| 19 |
+
f, ax = plt.subplots(figsize=(5, 5))
|
| 20 |
+
sns.set_color_codes("pastel")
|
| 21 |
+
sns.barplot(x="Total", y="Labels", data=data, label="Total", color="b")
|
| 22 |
+
sns.set_color_codes("muted")
|
| 23 |
+
sns.barplot(x="Confidence Score", y="Labels", data=data, label="Confidence Score", color="b")
|
| 24 |
+
ax.legend(ncol=2, loc="lower right", frameon=True)
|
| 25 |
+
sns.despine(left=True, bottom=True)
|
| 26 |
+
return f
|
| 27 |
+
|
| 28 |
+
# Function to predict gastrointestinal conditions
|
| 29 |
def predict_gastrointestinal(img):
|
| 30 |
np.set_printoptions(suppress=True)
|
| 31 |
data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
|
|
|
|
| 35 |
normalized_image_array = (image_array.astype(np.float32) / 127.0) - 1
|
| 36 |
data[0] = normalized_image_array
|
| 37 |
prediction = model.predict(data)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
index = np.argmax(prediction)
|
| 39 |
class_name = class_names[index].strip()
|
| 40 |
confidence_score = prediction[0][index]
|
|
|
|
| 54 |
prediction_text = f"The image shows signs of {class_name}."
|
| 55 |
|
| 56 |
return prediction_text, plot
|
| 57 |
+
|
| 58 |
+
# Streamlit app
|
| 59 |
+
#st.title("Gastrointestinal Classification Web App")
|
| 60 |
+
######
|
| 61 |
+
st.write("Loaded class names:", class_names)
|
| 62 |
+
|
| 63 |
+
uploaded_file = st.file_uploader("Upload a gastrointestinal image...", type=["jpg", "jpeg", "png"])
|
| 64 |
+
|
| 65 |
+
if uploaded_file is not None:
|
| 66 |
+
image = Image.open(uploaded_file)
|
| 67 |
+
st.image(image, caption='Uploaded Image', use_column_width=True)
|
| 68 |
+
st.write("Classifying...")
|
| 69 |
+
|
| 70 |
+
prediction, plot = predict_gastrointestinal(image)
|
| 71 |
+
st.write(prediction)
|
| 72 |
+
st.pyplot(plot)
|
| 73 |
+
|