File size: 1,427 Bytes
a46bb01
adf2111
a46bb01
dfdde84
a46bb01
8d9c7cb
a46bb01
 
 
da33e50
a46bb01
da33e50
 
a46bb01
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32e9f67
 
a46bb01
 
32e9f67
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
import requests
import tensorflow as tf
import gradio as gr
from PIL import Image
import numpy as np

# Load your custom TensorFlow model. Update 'modelo_treinado.h5' with the path to your model.
tf_model_path = 'modelo_treinado.h5'
tf_model = tf.keras.models.load_model(tf_model_path)

# Define your class labels.
class_labels = ["Normal", "Cataract"]

def preprocess_image(image):
    # Resize the image to the input size required by the model (e.g., 224x224).
    image = image.resize((224, 224))
    # Convert the PIL image to a NumPy array and normalize pixel values.
    image = np.array(image) / 255.0
    # Add a batch dimension to the image.
    image = np.expand_dims(image, axis=0)
    return image

def predict(inp):
    # Preprocess the input image.
    inp = preprocess_image(inp)
    # Make predictions using your custom TensorFlow model.
    predictions = tf_model.predict(inp)
    # Get the class label with the highest confidence.
    predicted_class = class_labels[np.argmax(predictions)]
    # Get the confidence score of the predicted class.
    confidence = float(predictions[0][np.argmax(predictions)])
    
    # Create a dictionary with the predicted class and its confidence.
    result = {predicted_class: confidence}
    
    return result

# Create a Gradio interface.
gr.Interface(
    fn=predict,
    inputs=gr.inputs.Image(type="pil"),
    outputs=gr.outputs.Label(num_top_classes=1)
).launch()