File size: 1,159 Bytes
adf2111
 
8d9c7cb
8d504fb
 
8d9c7cb
59e3723
35cf55e
 
 
 
59e3723
8d504fb
59e3723
da33e50
 
 
8d9c7cb
8d504fb
8d9c7cb
8d504fb
 
 
8d9c7cb
 
 
 
 
8d504fb
8d9c7cb
 
 
 
59e3723
8d9c7cb
 
 
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
import gradio as gr
import tensorflow as tf
import requests
import cv2
import numpy as np

# Define a custom layer 'FixedDropout' without specifying 'name' argument
def fixed_dropout(x):
    return tf.keras.layers.Dropout(0.5)(x)

# Load the TensorFlow model while registering the custom layer
custom_objects = {'fixed_dropout': fixed_dropout}
tf_model_path = 'modelo_treinado.h5'  # Update with the path to your TensorFlow model
tf_model = tf.keras.models.load_model(tf_model_path, custom_objects=custom_objects)

class_labels = ["Normal", "Cataract"]

def predict(inp):
    # Use the TensorFlow model to predict Normal or Cataract
    img_array = cv2.cvtColor(np.array(inp), cv2.COLOR_RGB2BGR)
    img_array = cv2.resize(img_array, (224, 224))
    img_array = img_array / 255.0
    img_array = np.expand_dims(img_array, axis=0)

    prediction_tf = tf_model.predict(img_array)
    label_index = np.argmax(prediction_tf)
    confidence_tf = float(prediction_tf[0, label_index])

    return class_labels[label_index], confidence_tf

demo = gr.Interface(
    fn=predict,
    inputs=gr.inputs.Image(type="pil"),
    outputs=["label", "number"]
)

demo.launch()