File size: 2,731 Bytes
501e0be
 
 
 
 
 
 
80ca849
e48d285
665a17d
e48d285
 
80ca849
 
e48d285
d9ad2d7
 
 
 
 
 
 
 
e48d285
 
d9ad2d7
e48d285
80ca849
2b03bfc
b749b00
501e0be
 
 
 
e48d285
501e0be
 
 
 
 
e48d285
501e0be
 
 
 
 
8b6408c
501e0be
 
 
 
 
 
9aad5f1
c0a0b60
501e0be
b7f89b1
501e0be
 
c0a0b60
501e0be
 
 
 
1c99073
8b6408c
1c99073
 
501e0be
 
e48d285
501e0be
 
f26202f
 
 
 
501e0be
 
8b6408c
 
501e0be
 
 
 
 
e48d285
501e0be
 
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
import streamlit as st
import tensorflow as tf
from tensorflow.keras.preprocessing import image
import numpy as np
from PIL import Image
import base64

st.set_page_config(
    page_title="Pneumonia Detection App",
    page_icon=":lungs:",
    layout="wide"
)

custom_style = """
<style>
    div[data-testid="stToolbar"],
    div[data-testid="stDecoration"],
    div[data-testid="stStatusWidget"],
    #MainMenu,
    header,
    footer {
        visibility: hidden;
        height: 0%;
    }
</style>

"""
st.markdown(custom_style, unsafe_allow_html=True)

model = tf.keras.models.load_model('fixed_model.h5')
img_size = (224, 224)

def preprocess_image(img):
    img = image.load_img(img, target_size=img_size)
    img_array = image.img_to_array(img) / 255.0
    img_array = np.expand_dims(img_array, axis=0)
    return img_array

def predict_image(img):
    img_array = preprocess_image(img)
    prediction = np.squeeze(model.predict(img_array), axis=0)
    return prediction

def display_image_with_download(image_path, caption, download_text):
    image = Image.open(image_path)
    st.image(image, caption=caption, use_column_width=True)
    
    with open(image_path, 'rb') as f:
        data = f.read()
        base64_data = base64.b64encode(data).decode('utf-8')
        href = f'<a href="data:application/octet-stream;base64,{base64_data}" download="{download_text}.jpg">Download {download_text}</a>'
        st.markdown(href, unsafe_allow_html=True)

def main():
    st.title("Pneumonia Detection")

    uploaded_file = st.file_uploader("Upload a chest X-ray image...", type=["jpg", "png", "jpeg"])

    st.markdown("""
        Example Instructions:
        - Upload a chest X-ray image in JPG format.
        - Or, download sample images below and check the predictions.
    """)

    st.write("**Download/View Sample Images:**")
    
    normal_download = st.button("View Normal Image")
    pneumonic_download = st.button("View Pneumonic Image")

    if normal_download:
        normal_image_path = "test-normal_001.jpg"
        display_image_with_download(normal_image_path, "Normal Image", "Normal Image")

    if pneumonic_download:
        pneumonic_image_path = "test-pneumonia_028.jpg"
        display_image_with_download(pneumonic_image_path, "Pneumonic Image", "Pneumonic Image")

    if uploaded_file is not None:
        st.image(uploaded_file, caption="Uploaded Image", use_column_width=True)
        st.markdown('</div>', unsafe_allow_html=True)

        prediction = predict_image(uploaded_file)

        st.write("**Prediction:**")
        class_label = "Pneumonia" if prediction > 0.5 else "Normal"
        st.write(f"The image is classified as **{class_label}**.")

if __name__ == "__main__":
    main()