Elegbede's picture
Update app.py
c476e3f
raw
history blame
No virus
1.14 kB
import gradio as gr
import tensorflow as tf
from tensorflow.keras.models import load_model
import numpy as np
from tensorflow.keras.preprocessing import image
def predict_input_image(img):
# Normalize the image by cropping (center crop)
h, w = img.size
crop_start_x = (w - 224) // 2
crop_start_y = (h - 224) // 2
img = img.crop((crop_start_x, crop_start_y, crop_start_x + 224, crop_start_y + 224))
img = tf.image.resize(img, [224,224])
img = np.expand_dims(img, axis = 0)
my_model = load_model('Brain_Tumor_Model.h5')
# Set a threshold for binary classification
threshold = 0.5
# Make predictions using your model
predictions = my_model.predict(img)
# Convert predictions to binary (0 or 1) based on the threshold
binary_prediction = 'Tumor Detected' if predictions[0][0] > threshold else 'No Tumor Detected'
# Print or use the binary prediction as needed
print("Prediction:", binary_prediction)
# Define Gradio interface
iface = gr.Interface(
fn=predict_input_image,
inputs= 'image',
outputs="text",
)
# Launch the interface
iface.launch()