File size: 1,655 Bytes
eac8db7 26360ef eac8db7 51638a9 eac8db7 b5fa37c eac8db7 |
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 |
import gradio as gr
import numpy as np
from PIL import Image
from keras.models import load_model
# Load your pre-trained model (make sure the model file is in the same directory)
model = load_model('brain_tumor_model.h5')
# Function to process image and make predictions
def predict_image(image):
# Resize the image
img = image.resize((128, 128))
# Convert the image to a NumPy array
img = np.array(img)
# Check if the image has 3 color channels
if img.shape == (128, 128): # If grayscale, convert to RGB
img = np.stack((img,) * 3, axis=-1)
# Add a batch dimension
img = np.expand_dims(img, axis=0)
# Make the prediction
prediction = model.predict(img)
# Get the predicted class and confidence level
predicted_class = np.argmax(prediction)
confidence = np.max(prediction)
# Return the results
if predicted_class == 0:
return f'No tumor detected. Confidence: {confidence:.2f}'
else:
return f'Tumor detected. Confidence: {confidence:.2f}'
# Create custom CSS for background color
css = """
body {
background-color: #f0f4f7;
}
"""
# Create the Gradio interface
iface = gr.Interface(
fn=predict_image,
inputs=gr.Image(type="pil"),
outputs=gr.Textbox(),
title="Brain Tumor Detection AI App",
description="Upload a Magnetic Resonance Imaging (MRI) scan image to detect brain tumors.",
css=css, # Apply the custom background color
#theme="dark", # Apply a dark theme to the interface
flagging_options=["Incorrect Diagnosis", "Image Not Clear", "Model Error"], # Add flagging options
)
# Launch the interface
iface.launch() |