Manith Marapperuma commited on
Commit
9b67c0d
·
verified ·
1 Parent(s): a8de88e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -21
app.py CHANGED
@@ -1,31 +1,39 @@
1
  import streamlit as st
2
- from tensorflow.keras.models import load_model # Use TF 2.x compatible import
3
- from tensorflow.keras.preprocessing import image
4
- from tensorflow.keras.applications.vgg16 import preprocess_input # Import preprocessing function
5
  import numpy as np
 
 
6
 
7
- def load_model():
8
- """Loads the pre-trained chest X-ray classification model."""
9
- return load_model('chest_xray.h5') # Assuming the model is saved as 'chest_xray.h5'
10
 
11
- # Cache the model loading process for efficiency
12
- model = st.cache(load_model, allow_output_mutation=True)()
13
 
14
- st.title("Pneumonia Detection App")
 
15
 
16
- uploaded_file = st.file_uploader("Upload a chest X-ray image", type=["jpg", "jpeg", "png"])
 
 
 
 
 
 
 
 
 
17
 
18
  if uploaded_file is not None:
19
- img = image.load_img(uploaded_file, target_size=(224, 224)) # Load image with target size
20
- x = image.img_to_array(img) # Convert image to array
21
- x = np.expand_dims(x, axis=0) # Add a dimension for batch processing
22
- img_data = preprocess_input(x) # Preprocess image for VGG16
23
-
24
- prediction = model.predict(img_data)
25
- result = int(prediction[0][0])
26
-
27
  if result == 0:
28
- st.write("**Person is Affected By PNEUMONIA**")
29
  else:
30
- st.write("**Result is Normal**")
31
-
 
1
  import streamlit as st
2
+ from keras.models import load_model
3
+ from keras.applications.vgg16 import preprocess_input
 
4
  import numpy as np
5
+ from PIL import Image
6
+ import tensorflow as tf
7
 
8
+ # Load your pre-trained model
9
+ model = load_model('chest_xray.h5')
 
10
 
11
+ st.title('Pneumonia Detection from Chest X-Ray Images')
 
12
 
13
+ # Create a file uploader to upload images
14
+ uploaded_file = st.file_uploader("Choose an X-ray image...", type=["jpg", "jpeg", "png"])
15
 
16
+ def predict(image):
17
+ # Preprocess the image to get it into the right format for the model
18
+ img = image.resize((224,224))
19
+ x = tf.keras.preprocessing.image.img_to_array(img)
20
+ x = np.expand_dims(x, axis=0)
21
+ img_data = preprocess_input(x)
22
+
23
+ # Make the prediction
24
+ classes = model.predict(img_data)
25
+ return int(classes[0][0])
26
 
27
  if uploaded_file is not None:
28
+ # Display the uploaded image
29
+ st.image(uploaded_file, caption='Uploaded X-ray Image', use_column_width=True)
30
+ st.write("")
31
+ st.write("Classifying...")
32
+ image = Image.open(uploaded_file)
33
+
34
+ # Predict and display the results
35
+ result = predict(image)
36
  if result == 0:
37
+ st.write("Person is Affected By PNEUMONIA")
38
  else:
39
+ st.write("Result is Normal")