import streamlit as st import numpy as np from tensorflow.keras.models import load_model from tensorflow.keras.preprocessing import image # Load the pre-trained Keras model for lung cancer classification model = load_model("./model/lung_cancer_detection_model.h5", compile=False) # Then, compile your model using the optimizer model.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"]) # Function to preprocess the uploaded image def predict_single_image(image_path, model, target_size=(128, 128)): # Load and preprocess the image img = image.load_img(image_path, target_size=target_size, color_mode="grayscale") img = image.img_to_array(img) img = np.expand_dims(img, axis=0) img /= 255.0 # Rescale the image # Predict the class probabilities probabilities = model.predict(img) # display(probabilities) # Determine the predicted class label predicted_class = "positive" if probabilities[0][0] > 0.5 else "negative" return predicted_class, probabilities[0][0] # Function to classify the uploaded image def classify_lung_cancer(img): # Call the function to predict the class label for the single image predicted_label, confidence = predict_single_image( img, model, target_size=(512, 512) ) # Print the prediction # print('Predicted Label:', predicted_label) # print('Confidence:', confidence) return f"Prediction: {predicted_label}\n(Confidence: {confidence:.2f})" # Streamlit app st.title("Lung Cancer Classification") st.write( "Upload an image and the model will classify it as positive or negative for lung cancer." ) uploaded_image = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"]) if uploaded_image is not None: # Display the uploaded image st.image(uploaded_image, caption="Uploaded Image.", use_column_width=True) # Classify the uploaded image if st.button("Classify"): predicted_label, confidence = classify_lung_cancer(uploaded_image) st.write(f"Prediction: {predicted_label}") st.write(f"Confidence: {confidence:.2f}")