Spaces:
Sleeping
Sleeping
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() | |