File size: 1,688 Bytes
adf2111
 
1a65837
8d504fb
8d9c7cb
ef2cffe
 
 
 
 
 
 
8ddfbaa
ef2cffe
 
 
 
 
 
 
6a96309
ef2cffe
da33e50
 
 
2d907f6
 
 
 
 
 
1a65837
2d907f6
 
 
 
1a65837
2d907f6
1a65837
2d907f6
 
 
1a65837
8ddfbaa
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
45
import gradio as gr
import tensorflow as tf
import cv2
import numpy as np

# Define the custom layer 'FixedDropout'
class FixedDropout(tf.keras.layers.Layer):
    def __init__(self, rate, **kwargs):
        super(FixedDropout, self).__init__(**kwargs)
        self.rate = rate

    def call(self, inputs, training=None):
        return tf.keras.layers.Dropout(self.rate)(inputs, training=training)

# Load the TensorFlow model with custom layer handling
def load_model_with_custom_objects(model_path):
    with tf.keras.utils.custom_object_scope({'FixedDropout': FixedDropout}):
        model = tf.keras.models.load_model(model_path)
    return model

tf_model_path = 'modelo_treinado.h5'  # Update with the path to your TensorFlow model
tf_model = load_model_with_custom_objects(tf_model_path)

class_labels = ["Normal", "Cataract"]

# Define a Gradio interface
def classify_image(input_image):
    # Preprocess the input image
    input_image = cv2.resize(input_image, (224, 224))  # Resize the image to match the model's input size
    input_image = np.expand_dims(input_image, axis=0)  # Add batch dimension
    input_image = input_image / 255.0  # Normalize pixel values (assuming input range [0, 255])

    # Make predictions using the loaded model
    predictions = tf_model.predict(input_image)
    class_index = np.argmax(predictions, axis=1)[0]
    predicted_class = class_labels[class_index]

    return predicted_class

# Create a Gradio interface
input_image = gr.inputs.Image(shape=(224, 224, 3))  # Define the input image shape
output_label = gr.outputs.Label()  # Define the output label

gr.Interface(fn=classify_image, inputs=input_image, outputs=output_label).launch()