File size: 2,113 Bytes
80eec88
b74374a
 
 
80eec88
b74374a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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}")