File size: 1,198 Bytes
46a2006
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ce1d5b2
46a2006
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import streamlit as st
import numpy as np
import tensorflow as tf
import pydicom
from PIL import Image
import io

@st.cache_resource
def load_model():
    return tf.keras.models.load_model("best_cnn_model.h5")

model = load_model()
IMG_SIZE = 128

st.title("Pneumonia Detection from Chest X-ray")
st.write("Upload a chest X-ray image (DICOM format)")

uploaded_file = st.file_uploader("Upload DICOM Image", type=None)

def preprocess_dicom_bytes(dicom_bytes):
    dicom = pydicom.dcmread(io.BytesIO(dicom_bytes))
    img = dicom.pixel_array.astype("float32")

    img = img / np.max(img)

    img = Image.fromarray((img * 255).astype(np.uint8))
    img = img.resize((IMG_SIZE, IMG_SIZE))

    img = np.array(img) / 255.0
    img = img.reshape(1, IMG_SIZE, IMG_SIZE, 1)
    return img

if uploaded_file:
    try:
        image = preprocess_dicom_bytes(uploaded_file.read())
        prob = model.predict(image)[0][0]

        label = "Pneumonia" if prob >= 0.45 else "No Pneumonia"

        st.success("Prediction complete")
        st.write(f"**Predicted Class:** {label}")
        st.write(f"**Probability:** {prob:.2f}")

    except Exception as e:
        st.error(f"Error processing file: {e}")