CoolFace
Apppublic

sreesaiarjun/Pneumonia-Detection-using-CNN

sourceHugging Faceapache-2.0updated 1y agoView on Hugging Face
0likes
app.py45 linesDownload Raw Back to root
1import streamlit as st2from tensorflow.keras.models import load_model3from tensorflow.keras.preprocessing import image4import numpy as np5 6# Load the model and weights7model_path = "Pneumonia_detection_using_CNN.h5"8weights_path = "Pneumonia_detection_using_CNN.weights.h5"9 10model = load_model(model_path)11model.load_weights(weights_path)12 13# Streamlit app14st.title('Pneumonia Detection App')15 16# File uploader for image input17uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png", "webp"])18 19if uploaded_file is not None:20    # Display the uploaded image21    st.image(uploaded_file, caption='Uploaded Image', use_column_width=True)22    23    if st.button('Predict', key='predict_button'):24        # Load and preprocess the image25        img = image.load_img(uploaded_file, target_size=(224, 224))26        img_array = image.img_to_array(img)27        img_array = np.expand_dims(img_array, axis=0)28 29        # Make a prediction30        prediction = model.predict(img_array)31 32        # Display the prediction with confidence level in large highlighted text33        class_names = ['Normal', 'Pneumonia']34        predicted_class = class_names[np.argmax(prediction)]35        confidence_level = np.max(prediction) * 100  # Convert probability to percentage36 37        # Set text color based on prediction38        if predicted_class == 'Normal':39            text_color = 'green'40        else:41            text_color = 'red'42 43        # Display prediction and confidence level in large highlighted text44        st.markdown(f'<p style="font-size:32px; color:{text_color};">Prediction: {predicted_class}</p>', unsafe_allow_html=True)45        st.markdown(f'<p style="font-size:32px; color:{text_color};">Confidence: {confidence_level:.2f}%</p>', unsafe_allow_html=True)