skincancer / app.py
karouswissem's picture
Update app.py
525fff5 verified
import streamlit as st
import tensorflow as tf
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing import image
import numpy as np
from PIL import Image
# Set up Streamlit app
st.title("Skin Cancer Detection")
st.write("Upload an image to detect the type of skin lesion.")
# Load the trained model
model_path = "Model.h5"
model = load_model(model_path)
# Define class names
class_names = [
"actinic keratosis",
"basal cell carcinoma",
"dermatofibroma",
"melanoma",
"nevus",
"pigmented benign keratosis",
"seborrheic keratosis",
"squamous cell carcinoma",
"vascular lesion"
]
# Image preprocessing function
def preprocess_image(uploaded_file, img_height=224, img_width=224):
img = Image.open(uploaded_file).convert("RGB")
img = img.resize((img_width, img_height))
img_array = np.array(img) / 255.0 # Normalize the image
img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
return img_array
# File uploader for image
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
if uploaded_file is not None:
# Display uploaded image
st.image(uploaded_file, caption="Uploaded Image", use_column_width=True)
# Preprocess the image
img_array = preprocess_image(uploaded_file)
# Predict using the model
predictions = model.predict(img_array)
predicted_class_index = np.argmax(predictions[0])
predicted_class = class_names[predicted_class_index]
confidence = predictions[0][predicted_class_index]
# Display the prediction
st.write(f"### Predicted Class: {predicted_class}")
st.write(f"### Confidence: {confidence:.2%}")
# Display probabilities for all classes
st.write("### Class Probabilities:")
for i, class_name in enumerate(class_names):
st.write(f"{class_name}: {predictions[0][i]:.2%}")