-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
93 lines (73 loc) · 2.43 KB
/
app.py
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
import streamlit as st
import tensorflow as tf
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing import image
import numpy as np
import cv2
# Set page configuration
st.set_page_config(
page_title="Pneumonia Classifier",
page_icon=":medical_thermometer:",
layout="wide"
)
# Load the pre-trained model
@st.cache_resource
def load_pneumonia_model():
model = load_model('pneumonia_cnn_model.h5')
return model
# Preprocess the image
def preprocess_image(uploaded_file):
# Read the image
img = image.load_img(uploaded_file, target_size=(64, 64), color_mode='grayscale')
# Convert to array
img_array = image.img_to_array(img)
# Normalize the image
img_array = img_array / 255.0
# Expand dimensions to create batch
img_array = np.expand_dims(img_array, axis=0)
return img_array
# Prediction function
def predict_pneumonia(model, img_array):
# Make prediction
prediction = model.predict(img_array)
# Convert to percentage
probability = prediction[0][0] * 100
# Classify
if probability > 50:
return f"Pneumonia Detected (Probability: {probability:.2f}%)", probability
else:
return f"Normal (Probability: {(100-probability):.2f}%)", 100-probability
# Main Streamlit app
def main():
# Title
st.title("Pneumonia Classification from Chest X-Ray")
# Sidebar
st.sidebar.header("About the App")
st.sidebar.info(
"This app uses a Convolutional Neural Network (CNN) "
"to classify chest X-ray images as Normal or Pneumonia."
)
# Load model
model = load_pneumonia_model()
# File uploader
uploaded_file = st.file_uploader(
"Upload a Chest X-Ray Image",
type=["jpg", "jpeg", "png"]
)
if uploaded_file is not None:
# Display the uploaded image
st.image(uploaded_file, caption="Uploaded Chest X-Ray", width=300)
# Preprocess the image
img_array = preprocess_image(uploaded_file)
# Prediction
if st.button("Classify Image"):
# Make prediction
result, probability = predict_pneumonia(model, img_array)
# Display results
st.subheader("Prediction Results")
st.write(result)
# Progress bar
st.progress(int(probability)/100)
# Run the app
if __name__ == "__main__":
main()