Spaces:
Sleeping
Sleeping
import gradio as gr | |
import tensorflow as tf | |
import cv2 | |
import numpy as np | |
def preprocess_image(img): | |
# Resize the image to the target size (256x256) | |
img = cv2.resize(img, (256, 256)) | |
# Center crop to 224x224 | |
h, w, _ = img.shape | |
crop_start_x = (w - 224) // 2 | |
crop_start_y = (h - 224) // 2 | |
img = img[crop_start_y:crop_start_y + 224, crop_start_x:crop_start_x + 224] | |
# Normalize the image | |
img = img / 255.0 | |
# Convert BGR to RGB | |
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) | |
# Expand dimensions to match model input shape | |
img = np.expand_dims(img, axis=0) | |
return img | |
def predict_input_image(img): | |
# Preprocess the input image | |
img = preprocess_image(img) | |
img = tf.image.resize(img, [224,224]) | |
img = np.expand_dims(img, axis = 0) | |
# Load the pre-trained model | |
model = tf.keras.models.load_model('Tumor_Model.h5') | |
# Make predictions | |
prediction = model.predict(img) | |
result = 'No Tumor Detected' if prediction[0][0] > 0.5 else 'Tumor detected' | |
return f"Prediction: {result}" | |
# Define Gradio interface | |
iface = gr.Interface( | |
fn=predict_input_image, | |
inputs=gr.Image(type="numpy"), | |
outputs="text", | |
) | |
# Launch the interface | |
iface.launch() |