Spaces:
Sleeping
Sleeping
import gradio as gr | |
import tensorflow as tf | |
import numpy as np | |
from PIL import Image | |
from io import BytesIO | |
# Load your trained model | |
model = tf.keras.models.load_model("best_model_weights.h5") # Replace with the path to your saved model | |
# Define the image classification function | |
def classify_image(input_image): | |
# Preprocess the input image | |
input_image = Image.open(BytesIO(input_image)) | |
input_image = input_image.resize((img_width, img_height)) | |
input_image = np.array(input_image) / 255.0 # Normalize pixel values | |
# Make a prediction using the model | |
predictions = model.predict(np.expand_dims(input_image, axis=0)) | |
# Get the class label with the highest probability | |
class_index = np.argmax(predictions) | |
class_prob = predictions[0][class_index] | |
# Define class labels (you can replace these with your actual class labels) | |
class_labels = ["Normal", "Cataract"] | |
# Get the class label | |
class_label = class_labels[class_index] | |
return f"Predicted Class: {class_label} (Probability: {class_prob:.2f})" | |
# Define the Gradio interface | |
iface = gr.Interface( | |
fn=classify_image, | |
inputs=gr.inputs.Image(shape=(img_height, img_width)), | |
outputs="text", | |
live=True, | |
title="Image Classifier" | |
) | |
# Run the Gradio interface | |
iface.launch() | |