Spaces:
Sleeping
Sleeping
File size: 1,172 Bytes
adf2111 1a65837 8d504fb 8d9c7cb 1a65837 da33e50 8d9c7cb 1a65837 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 39 40 41 |
import gradio as gr
import tensorflow as tf
import requests
import cv2
import numpy as np
# Define a custom layer 'FixedDropout'
def fixed_dropout(x):
return tf.keras.layers.Dropout(0.5)(x)
# Function to register custom layers within a custom_object_scope
def register_custom_layers():
return tf.keras.utils.custom_object_scope({'FixedDropout': fixed_dropout})
# Load the TensorFlow model within the custom_object_scope
with register_custom_layers():
tf_model = tf.keras.models.load_model('modelo_treinado.h5')
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()
|