File size: 2,606 Bytes
80eec88
b74374a
 
 
80eec88
b74374a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
626eea0
b74374a
 
 
 
 
 
 
 
 
 
 
 
 
 
626eea0
 
 
b74374a
 
 
 
 
626eea0
b74374a
 
626eea0
b74374a
 
626eea0
 
 
b74374a
626eea0
 
 
 
 
 
 
 
 
 
 
b74374a
626eea0
 
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
65
66
67
68
69
70
71
72
73
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)