Akshay-Vs commited on
Commit
6a18290
1 Parent(s): 4a1e3f2

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -0
app.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from PIL import Image
3
+ import tensorflow as tf
4
+ from tensorflow.keras.preprocessing.image import img_to_array
5
+ import numpy as np
6
+
7
+ path_to_model = ""
8
+
9
+ # Load the trained model
10
+ model = tf.keras.models.load_model(path_to_model)
11
+
12
+ # Set the default height and width of the uploaded image
13
+ IMG_HEIGHT = 180
14
+ IMG_WIDTH = 180
15
+
16
+ # Define a function to preprocess the image
17
+ def preprocess_image(image):
18
+ # Resize the image to the expected input size of the model
19
+ img = image.resize((IMG_HEIGHT, IMG_WIDTH))
20
+ # Convert the image to a numpy array
21
+ img_array = img_to_array(img)
22
+ # Normalize the pixel values
23
+ img_array = img_array / 255.0
24
+ # Reshape the image array to match the input shape of the model
25
+ img_array = np.reshape(img_array, (1, IMG_HEIGHT, IMG_WIDTH, 3))
26
+ return img_array
27
+
28
+ # Create the Streamlit app
29
+ def main():
30
+ # Set the title and a brief description
31
+ st.title('Pneumonia Classification')
32
+ st.write('This app classifies an uploaded chest X-ray image as normal or pneumonia using a trained deep learning model.')
33
+
34
+ # Add an image upload option
35
+ uploaded_image = st.file_uploader('Upload an image', type=['jpg', 'jpeg', 'png'])
36
+
37
+ # If an image has been uploaded, display it and make a prediction
38
+ if uploaded_image is not None:
39
+ # Display the uploaded image
40
+ image = Image.open(uploaded_image)
41
+ st.image(image, caption='Uploaded Image', use_column_width=True)
42
+
43
+ # Preprocess the image for the model
44
+ input_image = preprocess_image(image)
45
+
46
+ # Make a prediction using the model
47
+ prediction = model.predict(input_image)
48
+
49
+ # Display the prediction
50
+ if prediction[0][0] > prediction[0][1]:
51
+ st.write('Prediction: Normal')
52
+ else:
53
+ st.write('Prediction: Pneumonia')
54
+
55
+ if __name__ == '__main__':
56
+ main()