File size: 2,130 Bytes
1f62bfc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20270ef
1f62bfc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from tensorflow.keras.models import load_model
from PIL import Image
import numpy as np

# Load the model
model = load_model('cnn_model.h5')

# Function to process the uploaded image
def process_image(img):
    img = img.convert("RGB")  # Convert to RGB
    img = img.resize((64, 64))  # Resize to match model input
    img = np.array(img) / 255.0  # Normalize pixel values
    img = np.expand_dims(img, axis=0)  # Expand dimensions for batch processing
    return img

# Streamlit App
st.set_page_config(page_title="Pneumonia Detector", page_icon="🩺", layout="centered")

# Apply custom styling
st.markdown(
    """
    <style>
        .main {background-color: #e3f2fd;}  /* Light blue background */
        h1 {color: #008080; text-align: center;}  /* Teal color title */
        .stButton button {background-color: #008080; color: white; font-size: 18px;} /* Stylish button */
        .result-box {padding: 10px; border-radius: 10px; font-size: 20px; text-align: center; font-weight: bold;}
    </style>
    """,
    unsafe_allow_html=True,
)

# Title
st.title("🩺 Pneumonia Detection System")
st.write("Upload a chest X-ray image to check if it indicates Pneumonia or is Normal.")

# File uploader
file = st.file_uploader("📷 Upload an Image", type=["jpg", "jpeg", "png"])

if file is not None:
    # Display uploaded image
    img = Image.open(file)
    st.image(img, caption="Uploaded Image")

    # Predict button
    if st.button("🔍 Predict"):
        with st.spinner("Analyzing... ⏳"):
            image = process_image(img)
            predictions = model.predict(image)
            predicted_class = np.argmax(predictions)

            # Class names
            class_names = ["NORMAL", "PNEUMONIA"]
            result_text = class_names[predicted_class]

            # Dynamic coloring for results
            color = "green" if predicted_class == 0 else "red"

            # Display result
            st.markdown(
                f'<div class="result-box" style="background-color: {color}; color: white;">{result_text}</div>',
                unsafe_allow_html=True,
            )