sreesaiarjun commited on
Commit
2a9d41f
·
verified ·
1 Parent(s): 96f4b51

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -0
app.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from tensorflow.keras.models import load_model
3
+ from tensorflow.keras.preprocessing import image
4
+ import numpy as np
5
+
6
+ # Load the model and weights
7
+ model_path = "Pneumonia_detection_using_CNN.h5"
8
+ weights_path = "Pneumonia_detection_using_CNN.weights.h5"
9
+
10
+ model = load_model(model_path)
11
+ model.load_weights(weights_path)
12
+
13
+ # Streamlit app
14
+ st.title('Pneumonia Detection App')
15
+
16
+ # File uploader for image input
17
+ uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png"])
18
+
19
+ if uploaded_file is not None:
20
+ # Display the uploaded image
21
+ st.image(uploaded_file, caption='Uploaded Image', use_column_width=True)
22
+
23
+ # Check if predict button is clicked
24
+ col1, col2, col3 = st.columns([1, 4, 1]) # Adjust column width ratios as needed
25
+ with col2:
26
+ if st.button('Predict', key='predict_button'):
27
+ # Load and preprocess the image
28
+ img = image.load_img(uploaded_file, target_size=(224, 224))
29
+ img_array = image.img_to_array(img)
30
+ img_array = np.expand_dims(img_array, axis=0)
31
+
32
+ # Make a prediction
33
+ prediction = model.predict(img_array)
34
+
35
+ # Display the prediction with confidence level in large highlighted text
36
+ class_names = ['Normal', 'Pneumonia']
37
+ predicted_class = class_names[np.argmax(prediction)]
38
+ confidence_level = np.max(prediction) * 100 # Convert probability to percentage
39
+
40
+ # Set text color based on prediction
41
+ if predicted_class == 'Normal':
42
+ text_color = 'green'
43
+ else:
44
+ text_color = 'red'
45
+
46
+ # Display prediction and confidence level in large highlighted text
47
+ st.markdown(f'<p style="font-size:32px; color:{text_color};">Prediction: {predicted_class}</p>', unsafe_allow_html=True)
48
+ st.markdown(f'<p style="font-size:32px; color:{text_color};">Confidence: {confidence_level:.2f}%</p>', unsafe_allow_html=True)