Spaces:
Sleeping
Sleeping
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) | |
if confidence < 0.5: | |
confidence = abs(1 - confidence) | |
return (predicted_label, confidence) | |
# Streamlit app | |
st.title("Lung Cancer Classification") | |
st.write( | |
"Upload an image and click 'Classify' to predict if it's positive or negative for lung cancer." | |
) | |
# Display the uploaded image | |
uploaded_image = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"]) | |
# Classify the uploaded image | |
if st.button("Classify"): | |
if uploaded_image is not None: | |
predicted_label, confidence = classify_lung_cancer(uploaded_image) | |
# Style the prediction text | |
if predicted_label == "POSITIVE": | |
prediction_style = "color: red; font-size: 24px; text-transform: uppercase; border: 2px solid red;" | |
else: | |
prediction_style = "color: green; font-size: 24px; text-transform: uppercase; border: 2px solid green;" | |
st.markdown( | |
f"<p style='{prediction_style}'>Prediction: {predicted_label}</p>", | |
unsafe_allow_html=True, | |
) | |
st.write(f"Confidence: {confidence:.2f}") | |
st.progress(int(confidence * 100)) | |
st.image(uploaded_image, caption="Uploaded Image.", use_column_width=True) | |