Skin_Cancer_CNN / app.py
ananthakrishnan171's picture
Update app.py
f1f191d verified
Raw History Blame Contribute Delete
1.13 kB
import gradio as gr
import tensorflow as tf
import numpy as np
from PIL import Image
# Load the trained model
model = tf.keras.models.load_model('Skin Cancer.h5')
# Preprocess function to prepare image for model
def preprocess_image(img):
img = img.resize((224, 224)) # Resize as per model input
img = np.array(img) / 255.0 # Normalize
img = np.expand_dims(img, axis=0)
return img
# Prediction function
def predict(image):
img = preprocess_image(image)
prediction = model.predict(img)[0][0]
if prediction > 0.5:
result = "Cancer Detected ❌"
confidence = f"Confidence: {prediction*100:.2f}%"
else:
result = "No Cancer ✅"
confidence = f"Confidence: {(1 - prediction)*100:.2f}%"
return result, confidence
# Gradio UI
iface = gr.Interface(
fn=predict,
inputs=gr.Image(type="pil", label="Upload Skin Image"),
outputs=[gr.Text(label="Prediction"), gr.Text(label="Confidence")],
title="Skin Cancer Detection App",
description="Upload a skin image to detect whether it has cancer or not using CNN model."
)
# Launch app
iface.launch()