File size: 8,468 Bytes
f08cf5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
501e0be
 
 
 
 
 
 
f08cf5d
2b03bfc
f08cf5d
 
 
 
 
2b03bfc
 
501e0be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f08cf5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
501e0be
 
f08cf5d
501e0be
 
 
f08cf5d
501e0be
 
 
 
f08cf5d
501e0be
 
 
 
 
 
 
 
 
 
 
 
 
 
f08cf5d
 
501e0be
f08cf5d
501e0be
 
 
 
 
 
 
 
f08cf5d
501e0be
 
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# import streamlit as st
# import tensorflow as tf
# from tensorflow.keras.preprocessing import image
# import numpy as np
# from PIL import Image
# import base64

# hide_streamlit_style = """
#                 <style>
#                 div[data-testid="stToolbar"] {
#                 visibility: hidden;
#                 height: 0%;
#                 position: fixed;
#                 }
#                 div[data-testid="stDecoration"] {
#                 visibility: hidden;
#                 height: 0%;
#                 position: fixed;
#                 }
#                 div[data-testid="stStatusWidget"] {
#                 visibility: hidden;
#                 height: 0%;
#                 position: fixed;
#                 }
#                 #MainMenu {
#                 visibility: hidden;
#                 height: 0%;
#                 }
#                 header {
#                 visibility: hidden;
#                 height: 0%;
#                 }
#                 footer {
#                 visibility: hidden;
#                 height: 0%;
#                 }
#                 </style>
#                 """
# st.markdown(hide_streamlit_style, unsafe_allow_html=True)


# # Load the pre-trained model
# model = tf.keras.models.load_model('model.h5') 
# # Define the target size for the model
# img_size = (224, 224)

# # Function to preprocess the image
# def preprocess_image(img):
#     img = image.load_img(img, target_size=img_size)
#     img_array = image.img_to_array(img)
#     img_array = img_array / 255.0  # Normalize pixel values to between 0 and 1
#     img_array = np.expand_dims(img_array, axis=0)
#     return img_array

# # Function to make predictions
# def predict_image(img):
#     img_array = preprocess_image(img)
#     prediction = model.predict(img_array)
#     prediction = np.squeeze(prediction, axis=0)
#     return prediction

# # Function to display and provide a download link for an image
# def display_image_with_download(image_path, caption, download_text):
#     image = Image.open(image_path)
#     st.image(image, caption=caption, use_column_width=True)
    
#     # Generate a download link
#     with open(image_path, 'rb') as f:
#         data = f.read()
#         base64_data = base64.b64encode(data).decode('utf-8')
#         href = f'<a href="data:application/octet-stream;base64,{base64_data}" download="{download_text}.jpg">Download {download_text}</a>'
#         st.markdown(href, unsafe_allow_html=True)

# # Streamlit app
# def main():
        
#     st.title("Pneumonia Detection")

#     # Allow user to upload an image
#     uploaded_file = st.file_uploader("Upload a chest X-ray image in JPG format...", type="jpg")

#     # Example instructions
#     st.markdown("""
#         Example Instructions:
#         - Upload a chest X-ray image in JPG format.
#         - Or, download sample images below and check the predictions.
#     """)

#    # Provide links to download sample images
#     st.write("**Download Sample Images:**")
    
#     pneumonic_download = st.button("Download Pneumonic Image")
#     normal_download = st.button("Download Normal Image")

#     if pneumonic_download:
#         pneumonic_image_path = "test-pneumonia_028.jpg"  # Replace with actual path
#         display_image_with_download(pneumonic_image_path, "Pneumonic Image", "Pneumonic Image")

#     if normal_download:
#         normal_image_path = "test-normal_001.jpg"  # Replace with actual path
#         display_image_with_download(normal_image_path, "Normal Image", "Normal Image")

#     if uploaded_file is not None:
#         st.image(uploaded_file, caption="Uploaded Image", use_column_width=True)

#         # Make predictions
#         prediction = predict_image(uploaded_file)

#         # Display the results
#         st.write("**Prediction:**")
#         class_label = "Pneumonia" if prediction > 0.5 else "Normal"
#         st.write(f"The image is classified as **{class_label}**.")
        
# if __name__ == "__main__":
#     main()

import streamlit as st
import tensorflow as tf
from tensorflow.keras.preprocessing import image
import numpy as np
from PIL import Image
import base64

# Hide Streamlit menu and footer
hide_streamlit_style = """
    <style>
        #MainMenu {visibility: hidden;}
        footer {visibility: hidden;}
    </style>
    """
st.markdown(hide_streamlit_style, unsafe_allow_html=True)

# Load the pre-trained model
model = tf.keras.models.load_model('model.h5') 
# Define the target size for the model
img_size = (224, 224)

# Function to preprocess the image
def preprocess_image(img):
    img = image.load_img(img, target_size=img_size)
    img_array = image.img_to_array(img)
    img_array = img_array / 255.0  # Normalize pixel values to between 0 and 1
    img_array = np.expand_dims(img_array, axis=0)
    return img_array

# Function to make predictions
def predict_image(img):
    img_array = preprocess_image(img)
    prediction = model.predict(img_array)
    prediction = np.squeeze(prediction, axis=0)
    return prediction

# Function to display and provide a download link for an image
def display_image_with_download(image_path, caption, download_text):
    image = Image.open(image_path)
    st.image(image, caption=caption, use_column_width=True)
    
    # Generate a download link
    with open(image_path, 'rb') as f:
        data = f.read()
        base64_data = base64.b64encode(data).decode('utf-8')
        href = f'<a href="data:application/octet-stream;base64,{base64_data}" download="{download_text}.jpg">Download {download_text}</a>'
        st.markdown(href, unsafe_allow_html=True)

# Streamlit app
def main():
    # Set app title and page icon
    st.set_page_config(
        page_title="Pneumonia Detection App",
        page_icon=":microscope:",
        layout="wide"
    )

    # Add custom CSS for styling
    st.markdown("""
        <style>
            body {
                background-color: #f5f5f5;
            }
            .st-bw {
                background-color: #ffffff;
                box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
                border-radius: 10px;
                padding: 20px;
                margin-bottom: 20px;
            }
            .st-bw img {
                max-width: 100%;
                border-radius: 10px;
            }
            .st-bw a {
                color: #007bff;
            }
            .st-bw button {
                background-color: #007bff;
                color: #ffffff;
                border: none;
                padding: 10px 20px;
                font-size: 16px;
                border-radius: 5px;
                cursor: pointer;
            }
            .st-bw button:hover {
                background-color: #0056b3;
            }
        </style>
    """, unsafe_allow_html=True)

    # Display app title
    st.title("Pneumonia Detection App")

    # Allow user to upload an image
    uploaded_file = st.file_uploader("Upload a chest X-ray image in JPG format...", type="jpg", key="fileUploader")

    # Example instructions
    st.markdown("""
        **Example Instructions:**
        - Upload a chest X-ray image in JPG format.
        - Or, download sample images below and check the predictions.
    """)

    # Provide links to download sample images
    st.write("**Download Sample Images:**")
    
    pneumonic_download = st.button("Download Pneumonic Image")
    normal_download = st.button("Download Normal Image")

    if pneumonic_download:
        pneumonic_image_path = "test-pneumonia_028.jpg"  # Replace with actual path
        display_image_with_download(pneumonic_image_path, "Pneumonic Image", "Pneumonic Image")

    if normal_download:
        normal_image_path = "test-normal_001.jpg"  # Replace with actual path
        display_image_with_download(normal_image_path, "Normal Image", "Normal Image")

    if uploaded_file is not None:
        # Display the uploaded image in a styled container
        st.markdown('<div class="st-bw">', unsafe_allow_html=True)
        st.image(uploaded_file, caption="Uploaded Image", use_column_width=True)
        st.markdown('</div>', unsafe_allow_html=True)

        # Make predictions
        prediction = predict_image(uploaded_file)

        # Display the results
        st.write("**Prediction:**")
        class_label = "Pneumonia" if prediction > 0.5 else "Normal"
        st.write(f"The image is classified as **{class_label}**.")

if __name__ == "__main__":
    main()