File size: 1,305 Bytes
adf2111
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()