subek commited on
Commit
e48d285
·
verified ·
1 Parent(s): 8b6408c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -205
app.py CHANGED
@@ -6,114 +6,70 @@ from PIL import Image
6
  import base64
7
 
8
  st.set_page_config(
9
- page_title="Pneumonia Detection App",
10
- page_icon=":microscope:",
11
- layout="wide"
12
- )
13
 
14
  custom_style = """
15
- <style>
16
- .stApp {
17
- max-width: 1200px; /* Set maximum width for the app content */
18
- margin: 0 auto; /* Center the app content */
19
- padding: 20px; /* Add padding for better readability */
20
- }
21
- body {
22
- color: white;
23
- background-color: #2C3E50;
24
- }
25
- .stButton>button {
26
- background-color: #3498db !important;
27
- color: white !important;
28
- border-radius: 5px !important;
29
- }
30
- .download-buttons-container {
31
- display: flex; /* Use flexbox for the container */
32
- gap: 10px; /* Add space between buttons */
33
- }
34
- div[data-testid="stToolbar"] {
35
- visibility: hidden;
36
- height: 0%;
37
- position: fixed;
38
- }
39
- div[data-testid="stDecoration"] {
40
- visibility: hidden;
41
- height: 0%;
42
- position: fixed;
43
- }
44
- div[data-testid="stStatusWidget"] {
45
- visibility: hidden;
46
- height: 0%;
47
- position: fixed;
48
- }
49
- #MainMenu {
50
- visibility: hidden;
51
- height: 0%;
52
- }
53
- header {
54
- visibility: hidden;
55
- height: 0%;
56
- }
57
- footer {
58
- visibility: hidden;
59
- height: 0%;
60
- }
61
- </style>
62
- """
63
  st.markdown(custom_style, unsafe_allow_html=True)
64
 
65
-
66
- # Load the pre-trained model
67
- model = tf.keras.models.load_model('model.h5')
68
- # Define the target size for the model
69
  img_size = (224, 224)
70
 
71
- # Function to preprocess the image
72
  def preprocess_image(img):
73
  img = image.load_img(img, target_size=img_size)
74
- img_array = image.img_to_array(img)
75
- img_array = img_array / 255.0 # Normalize pixel values to between 0 and 1
76
  img_array = np.expand_dims(img_array, axis=0)
77
  return img_array
78
 
79
- # Function to make predictions
80
  def predict_image(img):
81
  img_array = preprocess_image(img)
82
- prediction = model.predict(img_array)
83
- prediction = np.squeeze(prediction, axis=0)
84
  return prediction
85
 
86
- # Function to display and provide a download link for an image
87
  def display_image_with_download(image_path, caption, download_text):
88
  image = Image.open(image_path)
89
  st.image(image, caption=caption, use_column_width=True)
90
 
91
- # Generate a download link
92
  with open(image_path, 'rb') as f:
93
  data = f.read()
94
  base64_data = base64.b64encode(data).decode('utf-8')
95
  href = f'<a href="data:application/octet-stream;base64,{base64_data}" download="{download_text}.jpg">Download {download_text}</a>'
96
  st.markdown(href, unsafe_allow_html=True)
97
 
98
-
99
-
100
- # Streamlit app
101
  def main():
102
-
103
-
104
  st.title("Pneumonia Detection")
105
 
106
- # Allow user to upload an image
107
  uploaded_file = st.file_uploader("Upload a chest X-ray image in JPG format...", type="jpg")
108
 
109
- # Example instructions
110
  st.markdown("""
111
  Example Instructions:
112
  - Upload a chest X-ray image in JPG format.
113
  - Or, download sample images below and check the predictions.
114
  """)
115
 
116
- # Provide links to download sample images
117
  st.write("**Download Sample Images:**")
118
 
119
  with st.markdown('<div class="download-buttons-container">', unsafe_allow_html=True):
@@ -121,152 +77,22 @@ def main():
121
  normal_download = st.button("Download Normal Image")
122
 
123
  if pneumonic_download:
124
- pneumonic_image_path = "test-pneumonia_028.jpg" # Replace with actual path
125
  display_image_with_download(pneumonic_image_path, "Pneumonic Image", "Pneumonic Image")
126
 
127
  if normal_download:
128
- normal_image_path = "test-normal_001.jpg" # Replace with actual path
129
  display_image_with_download(normal_image_path, "Normal Image", "Normal Image")
130
 
131
  if uploaded_file is not None:
132
  st.image(uploaded_file, caption="Uploaded Image", use_column_width=True)
133
  st.markdown('</div>', unsafe_allow_html=True)
134
 
135
-
136
- # Make predictions
137
  prediction = predict_image(uploaded_file)
138
 
139
- # Display the results
140
  st.write("**Prediction:**")
141
  class_label = "Pneumonia" if prediction > 0.5 else "Normal"
142
  st.write(f"The image is classified as **{class_label}**.")
143
-
144
  if __name__ == "__main__":
145
  main()
146
-
147
- # import streamlit as st
148
- # import tensorflow as tf
149
- # from tensorflow.keras.preprocessing import image
150
- # import numpy as np
151
- # from PIL import Image
152
- # import base64
153
-
154
- # st.set_page_config(
155
- # page_title="Pneumonia Detection App",
156
- # page_icon=":microscope:",
157
- # layout="wide"
158
- # )
159
-
160
- # custom_style = """
161
- # <style>
162
- # body {
163
- # color: white;
164
- # background-color: #2C3E50;
165
- # }
166
- # .stButton>button {
167
- # background-color: #3498db !important;
168
- # color: white !important;
169
- # border-radius: 5px !important;
170
- # }
171
- # .download-buttons-container {
172
- # display: flex; /* Use flexbox for the container */
173
- # gap: 10px; /* Add space between buttons */
174
- # margin-bottom: 20px; /* Add margin for better spacing */
175
- # }
176
- # div[data-testid="stToolbar"],
177
- # div[data-testid="stDecoration"],
178
- # div[data-testid="stStatusWidget"],
179
- # #MainMenu,
180
- # header,
181
- # footer {
182
- # visibility: hidden;
183
- # height: 0%;
184
- # }
185
- # </style>
186
- # """
187
- # st.markdown(custom_style, unsafe_allow_html=True)
188
-
189
- # # Load the pre-trained model
190
- # model = tf.keras.models.load_model('model.h5')
191
- # # Define the target size for the model
192
- # img_size = (224, 224)
193
-
194
-
195
- # # Function to preprocess the image
196
- # def preprocess_image(img):
197
- # img = image.load_img(img, target_size=img_size)
198
- # img_array = image.img_to_array(img)
199
- # img_array = img_array / 255.0 # Normalize pixel values to between 0 and 1
200
- # img_array = np.expand_dims(img_array, axis=0)
201
- # return img_array
202
-
203
-
204
- # # Function to make predictions
205
- # def predict_image(img):
206
- # img_array = preprocess_image(img)
207
- # prediction = model.predict(img_array)
208
- # prediction = np.squeeze(prediction, axis=0)
209
- # return prediction
210
-
211
-
212
- # # Function to display and provide a download link for an image
213
- # def display_image_with_download(image_path, caption, download_text):
214
- # image = Image.open(image_path)
215
- # st.image(image, caption=caption, use_column_width=True)
216
-
217
- # # Generate a download link
218
- # with open(image_path, 'rb') as f:
219
- # data = f.read()
220
- # base64_data = base64.b64encode(data).decode('utf-8')
221
- # href = f'<a href="data:application/octet-stream;base64,{base64_data}" download="{download_text}.jpg">Download {download_text}</a>'
222
- # st.markdown(href, unsafe_allow_html=True)
223
-
224
-
225
- # # Streamlit app
226
- # def main():
227
- # st.title("Pneumonia Detection")
228
-
229
- # # Allow user to upload an image
230
- # uploaded_file = st.file_uploader("Upload a chest X-ray image in JPG format...", type="jpg")
231
-
232
- # # Example instructions
233
- # st.markdown("""
234
- # Example Instructions:
235
- # - Upload a chest X-ray image in JPG format.
236
- # - Or, download sample images below and check the predictions.
237
- # """)
238
-
239
- # # Provide links to download sample images
240
- # st.write("**Download Sample Images:**")
241
-
242
- # # Container for download buttons
243
- # col1, col2 = st.beta_columns(2)
244
-
245
- # with col1:
246
- # pneumonic_download = st.button("Download Pneumonic Image")
247
-
248
- # with col2:
249
- # normal_download = st.button("Download Normal Image")
250
-
251
- # if pneumonic_download:
252
- # pneumonic_image_path = "test-pneumonia_028.jpg" # Replace with actual path
253
- # display_image_with_download(pneumonic_image_path, "Pneumonic Image", "Pneumonic Image")
254
-
255
- # if normal_download:
256
- # normal_image_path = "test-normal_001.jpg" # Replace with actual path
257
- # display_image_with_download(normal_image_path, "Normal Image", "Normal Image")
258
-
259
- # if uploaded_file is not None:
260
- # st.image(uploaded_file, caption="Uploaded Image", use_column_width=True)
261
-
262
- # # Make predictions
263
- # prediction = predict_image(uploaded_file)
264
-
265
- # # Display the results
266
- # st.write("**Prediction:**")
267
- # class_label = "Pneumonia" if prediction > 0.5 else "Normal"
268
- # st.write(f"The image is classified as **{class_label}**.")
269
-
270
-
271
- # if __name__ == "__main__":
272
- # main()
 
6
  import base64
7
 
8
  st.set_page_config(
9
+ page_title="Pneumonia Detection App",
10
+ page_icon=":microscope:",
11
+ layout="wide"
12
+ )
13
 
14
  custom_style = """
15
+ <style>
16
+ .stApp {
17
+ max-width: 1200px;
18
+ margin: 0 auto;
19
+ padding: 20px;
20
+ }
21
+ body {
22
+ color: white;
23
+ background-color: #2C3E50;
24
+ }
25
+ .stButton>button {
26
+ background-color: #3498db !important;
27
+ color: white !important;
28
+ border-radius: 5px !important;
29
+ }
30
+ .download-buttons-container {
31
+ display: flex;
32
+ gap: 10px;
33
+ }
34
+ </style>
35
+ """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  st.markdown(custom_style, unsafe_allow_html=True)
37
 
38
+ model = tf.keras.models.load_model('model.h5')
 
 
 
39
  img_size = (224, 224)
40
 
 
41
  def preprocess_image(img):
42
  img = image.load_img(img, target_size=img_size)
43
+ img_array = image.img_to_array(img) / 255.0
 
44
  img_array = np.expand_dims(img_array, axis=0)
45
  return img_array
46
 
 
47
  def predict_image(img):
48
  img_array = preprocess_image(img)
49
+ prediction = np.squeeze(model.predict(img_array), axis=0)
 
50
  return prediction
51
 
 
52
  def display_image_with_download(image_path, caption, download_text):
53
  image = Image.open(image_path)
54
  st.image(image, caption=caption, use_column_width=True)
55
 
 
56
  with open(image_path, 'rb') as f:
57
  data = f.read()
58
  base64_data = base64.b64encode(data).decode('utf-8')
59
  href = f'<a href="data:application/octet-stream;base64,{base64_data}" download="{download_text}.jpg">Download {download_text}</a>'
60
  st.markdown(href, unsafe_allow_html=True)
61
 
 
 
 
62
  def main():
 
 
63
  st.title("Pneumonia Detection")
64
 
 
65
  uploaded_file = st.file_uploader("Upload a chest X-ray image in JPG format...", type="jpg")
66
 
 
67
  st.markdown("""
68
  Example Instructions:
69
  - Upload a chest X-ray image in JPG format.
70
  - Or, download sample images below and check the predictions.
71
  """)
72
 
 
73
  st.write("**Download Sample Images:**")
74
 
75
  with st.markdown('<div class="download-buttons-container">', unsafe_allow_html=True):
 
77
  normal_download = st.button("Download Normal Image")
78
 
79
  if pneumonic_download:
80
+ pneumonic_image_path = "test-pneumonia_028.jpg"
81
  display_image_with_download(pneumonic_image_path, "Pneumonic Image", "Pneumonic Image")
82
 
83
  if normal_download:
84
+ normal_image_path = "test-normal_001.jpg"
85
  display_image_with_download(normal_image_path, "Normal Image", "Normal Image")
86
 
87
  if uploaded_file is not None:
88
  st.image(uploaded_file, caption="Uploaded Image", use_column_width=True)
89
  st.markdown('</div>', unsafe_allow_html=True)
90
 
 
 
91
  prediction = predict_image(uploaded_file)
92
 
 
93
  st.write("**Prediction:**")
94
  class_label = "Pneumonia" if prediction > 0.5 else "Normal"
95
  st.write(f"The image is classified as **{class_label}**.")
96
+
97
  if __name__ == "__main__":
98
  main()