File size: 3,469 Bytes
078f2ff
9fc0f31
 
 
 
 
 
 
 
 
 
 
 
 
078f2ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9fc0f31
 
 
078f2ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146

# -----------------------------
# Install pydicom if not present
# -----------------------------
import subprocess
import sys

try:
    import pydicom
except ImportError:
    subprocess.check_call([sys.executable, "-m", "pip", "install", "pydicom"])
    import pydicom


import streamlit as st
import numpy as np
import requests
import cv2
import tempfile

# -----------------------------
# Backend API URL
# -----------------------------
API_URL = "https://viveksardey-pneumoniadetectionpredictionbackend.hf.space/v1/predict"

IMG_SIZE = 224

st.title("🩺 Pneumonia Detection from Chest X-ray (DICOM)")

st.write("""
Upload a **Chest X-ray DICOM (.dcm)** file.
The image will be converted to **NumPy (.npy)** format and sent to the backend model.
""")

# -----------------------------
# Upload file
# -----------------------------
uploaded_file = st.file_uploader(
    "Upload DICOM Chest X-ray",
    type=["dcm"]
)

# -----------------------------
# Convert DICOM → Image
# -----------------------------
def dicom_to_image(dicom_file):

    dicom = pydicom.dcmread(dicom_file)
    img = dicom.pixel_array

    return img


# -----------------------------
# Preprocess image
# -----------------------------
def preprocess_image(img):

    # Resize image
    img = cv2.resize(img, (IMG_SIZE, IMG_SIZE))

    # Normalize
    img = img.astype("float32")

    if img.max() > 0:
        img = img / img.max()

    return img


# -----------------------------
# If file uploaded
# -----------------------------
if uploaded_file is not None:

    try:

        img = dicom_to_image(uploaded_file)

        st.subheader("X-ray Preview")
        st.image(img, caption="Uploaded DICOM Image", use_column_width=True)

        processed_img = preprocess_image(img)

        npy_array = np.array(processed_img)

        st.success("DICOM successfully converted to NumPy format")

    except Exception as e:

        st.error(f"Error processing DICOM file: {e}")


# -----------------------------
# Predict button
# -----------------------------
if st.button("Predict Pneumonia"):

    if uploaded_file is None:

        st.warning("Please upload a DICOM image first")

    else:

        try:

            with st.spinner("Running Pneumonia Detection..."):

                # Save temporary npy file
                with tempfile.NamedTemporaryFile(suffix=".npy", delete=False) as tmp:

                    np.save(tmp.name, npy_array)
                    tmp_path = tmp.name

                # Send request to backend API
                with open(tmp_path, "rb") as f:

                    response = requests.post(
                        API_URL,
                        files={"file": f}
                    )

                if response.status_code == 200:

                    result = response.json()

                    predicted_class = result["Predicted_Class"]
                    probability = result["Probability_Pneumonia"]

                    st.subheader("Prediction Result")

                    if predicted_class == "Pneumonia":
                        st.error("⚠️ Pneumonia Detected")
                    else:
                        st.success("✅ Normal Lungs")

                    st.write(f"Probability of Pneumonia: **{probability:.4f}**")

                else:

                    st.error(f"Prediction failed. Status Code: {response.status_code}")

        except Exception as e:

            st.error(f"Error during prediction: {e}")