Spaces:
Sleeping
Sleeping
Malik Sahab
commited on
Commit
•
b74374a
1
Parent(s):
80eec88
Added Streamlit and Model file
Browse files- app.py +58 -2
- model/lung_cancer_detection_model.h5 +3 -0
app.py
CHANGED
@@ -1,4 +1,60 @@
|
|
1 |
import streamlit as st
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import numpy as np
|
3 |
+
from tensorflow.keras.models import load_model
|
4 |
+
from tensorflow.keras.preprocessing import image
|
5 |
|
6 |
+
# Load the pre-trained Keras model for lung cancer classification
|
7 |
+
model = load_model("./model/lung_cancer_detection_model.h5", compile=False)
|
8 |
+
|
9 |
+
# Then, compile your model using the optimizer
|
10 |
+
model.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"])
|
11 |
+
|
12 |
+
|
13 |
+
# Function to preprocess the uploaded image
|
14 |
+
def predict_single_image(image_path, model, target_size=(128, 128)):
|
15 |
+
# Load and preprocess the image
|
16 |
+
img = image.load_img(image_path, target_size=target_size, color_mode="grayscale")
|
17 |
+
img = image.img_to_array(img)
|
18 |
+
img = np.expand_dims(img, axis=0)
|
19 |
+
img /= 255.0 # Rescale the image
|
20 |
+
|
21 |
+
# Predict the class probabilities
|
22 |
+
probabilities = model.predict(img)
|
23 |
+
# display(probabilities)
|
24 |
+
|
25 |
+
# Determine the predicted class label
|
26 |
+
predicted_class = "positive" if probabilities[0][0] > 0.5 else "negative"
|
27 |
+
|
28 |
+
return predicted_class, probabilities[0][0]
|
29 |
+
|
30 |
+
|
31 |
+
# Function to classify the uploaded image
|
32 |
+
def classify_lung_cancer(img):
|
33 |
+
# Call the function to predict the class label for the single image
|
34 |
+
predicted_label, confidence = predict_single_image(
|
35 |
+
img, model, target_size=(512, 512)
|
36 |
+
)
|
37 |
+
|
38 |
+
# Print the prediction
|
39 |
+
# print('Predicted Label:', predicted_label)
|
40 |
+
# print('Confidence:', confidence)
|
41 |
+
return f"Prediction: {predicted_label}\n(Confidence: {confidence:.2f})"
|
42 |
+
|
43 |
+
|
44 |
+
# Streamlit app
|
45 |
+
st.title("Lung Cancer Classification")
|
46 |
+
st.write(
|
47 |
+
"Upload an image and the model will classify it as positive or negative for lung cancer."
|
48 |
+
)
|
49 |
+
|
50 |
+
uploaded_image = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
51 |
+
|
52 |
+
if uploaded_image is not None:
|
53 |
+
# Display the uploaded image
|
54 |
+
st.image(uploaded_image, caption="Uploaded Image.", use_column_width=True)
|
55 |
+
|
56 |
+
# Classify the uploaded image
|
57 |
+
if st.button("Classify"):
|
58 |
+
predicted_label, confidence = classify_lung_cancer(uploaded_image)
|
59 |
+
st.write(f"Prediction: {predicted_label}")
|
60 |
+
st.write(f"Confidence: {confidence:.2f}")
|
model/lung_cancer_detection_model.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:384d3ed0f01155e64ffabf362d5df13fc425c22a1de8696c0976090adb04882b
|
3 |
+
size 710743384
|