|
import streamlit as st |
|
import numpy as np |
|
from tensorflow.keras.models import load_model |
|
from tensorflow.keras.preprocessing import image |
|
|
|
|
|
model = load_model("./model/lung_cancer_detection_model.h5", compile=False) |
|
|
|
|
|
model.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"]) |
|
|
|
|
|
|
|
def predict_single_image(image_path, model, target_size=(128, 128)): |
|
|
|
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 |
|
|
|
|
|
probabilities = model.predict(img) |
|
|
|
|
|
|
|
predicted_class = "POSITIVE" if probabilities[0][0] > 0.5 else "NEGATIVE" |
|
|
|
return predicted_class, probabilities[0][0] |
|
|
|
|
|
|
|
def classify_lung_cancer(img): |
|
|
|
predicted_label, confidence = predict_single_image( |
|
img, model, target_size=(512, 512) |
|
) |
|
|
|
|
|
|
|
|
|
if confidence < 0.5: |
|
confidence = abs(1 - confidence) |
|
return (predicted_label, confidence) |
|
|
|
|
|
|
|
st.title("Lung Cancer Classification") |
|
st.write( |
|
"Upload an image and click 'Classify' to predict if it's positive or negative for lung cancer." |
|
) |
|
|
|
|
|
uploaded_image = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"]) |
|
|
|
|
|
if st.button("Classify"): |
|
if uploaded_image is not None: |
|
predicted_label, confidence = classify_lung_cancer(uploaded_image) |
|
|
|
|
|
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) |
|
|