|
|
import streamlit as st
|
|
|
import numpy as np
|
|
|
import tensorflow as tf
|
|
|
from PIL import Image
|
|
|
import io
|
|
|
|
|
|
|
|
|
model = tf.keras.models.load_model('model.keras')
|
|
|
|
|
|
|
|
|
|
|
|
def preprocess_image(image):
|
|
|
|
|
|
if image.mode != 'RGB':
|
|
|
image = image.convert('RGB')
|
|
|
|
|
|
|
|
|
image = image.resize((224, 224))
|
|
|
image_array = np.array(image) / 255.0
|
|
|
image_array = np.expand_dims(image_array, axis=0)
|
|
|
return image_array
|
|
|
|
|
|
|
|
|
class_labels = ['Atelectasis', 'Cardiomegaly', 'Consolidation', 'Edema', 'Effusion',
|
|
|
'Emphysema', 'Fibrosis', 'Infiltration', 'Mass',
|
|
|
'Nodule', 'Pleural_Thickening', 'Pneumothorax']
|
|
|
|
|
|
|
|
|
|
|
|
st.title("Image Classification with Grad-CAM")
|
|
|
|
|
|
st.write("Upload an image to get predictions:")
|
|
|
|
|
|
|
|
|
uploaded_file = st.file_uploader("Choose an 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)
|
|
|
|
|
|
|
|
|
preprocessed_image = preprocess_image(image)
|
|
|
|
|
|
|
|
|
predictions = model.predict(preprocessed_image)[0]
|
|
|
|
|
|
|
|
|
top_predictions = [(label, prob) for label, prob in zip(class_labels, predictions) if prob > 0.5]
|
|
|
top_predictions = sorted(top_predictions, key=lambda x: x[1], reverse=True)[:3]
|
|
|
|
|
|
|
|
|
st.write("Predictions with probability greater than 0.5:")
|
|
|
for label, prob in top_predictions:
|
|
|
st.write(f"{label}: {prob*100:.2f}%")
|
|
|
percentage = int(prob * 100)
|
|
|
|
|
|
st.progress(percentage)
|
|
|
|
|
|
if not top_predictions:
|
|
|
st.write("No predictions with probability greater than 0.5.") |