|
|
import streamlit as st |
|
|
import numpy as np |
|
|
import tensorflow as tf |
|
|
from PIL import Image |
|
|
import cv2 as cv |
|
|
import io |
|
|
|
|
|
|
|
|
model = tf.keras.models.load_model('model.keras') |
|
|
|
|
|
|
|
|
def preprocess_image(image): |
|
|
|
|
|
image_array = np.array(image) |
|
|
|
|
|
|
|
|
image_array = cv.GaussianBlur(image_array, (9, 9), 0) |
|
|
|
|
|
|
|
|
clahe = cv.createCLAHE(clipLimit=3, tileGridSize=(10, 10)) |
|
|
clahe_image = clahe.apply(image_array) |
|
|
|
|
|
|
|
|
clahe_image = cv.cvtColor(clahe_image, cv.COLOR_GRAY2RGB) |
|
|
|
|
|
|
|
|
clahe_image = (clahe_image - clahe_image.min()) / (clahe_image.max() - clahe_image.min()) |
|
|
|
|
|
|
|
|
image_resized = cv.resize(clahe_image, (224, 224)) |
|
|
|
|
|
|
|
|
image_array = np.expand_dims(image_resized, axis=0).astype(np.float32) |
|
|
|
|
|
return image_array |
|
|
|
|
|
|
|
|
class_labels = ['Atelectasis', 'Cardiomegaly', 'Consolidation', 'Edema', 'Effusion', |
|
|
'Emphysema', 'Fibrosis', 'Infiltration', 'Mass', |
|
|
'Nodule', 'Pleural_Thickening', 'Pneumothorax'] |
|
|
|
|
|
|
|
|
st.title("Chest X-ray Classification") |
|
|
|
|
|
|
|
|
uploaded_file = st.file_uploader("Upload a Chest X-ray image...", type=["jpg", "jpeg", "png"]) |
|
|
|
|
|
|
|
|
col1, col2 = st.columns(2) |
|
|
|
|
|
if uploaded_file is not None: |
|
|
|
|
|
image = Image.open(uploaded_file) |
|
|
with col1: |
|
|
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] |
|
|
|
|
|
with col2: |
|
|
|
|
|
if not top_predictions: |
|
|
st.write("No diseases found with probability greater than 50%.") |
|
|
else: |
|
|
st.write("Predicted Disease(s):") |
|
|
for label, prob in top_predictions: |
|
|
st.write(f"{label}: {prob*100:.2f}%") |
|
|
percentage = int(prob * 100) |
|
|
st.progress(percentage) |