DHEIVER commited on
Commit
32e9f67
·
1 Parent(s): 8ddfbaa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -32
app.py CHANGED
@@ -1,44 +1,34 @@
1
  import gradio as gr
2
  import tensorflow as tf
3
- import cv2
 
4
  import numpy as np
5
 
6
- # Define the custom layer 'FixedDropout'
7
- class FixedDropout(tf.keras.layers.Layer):
8
- def __init__(self, rate, **kwargs):
9
- super(FixedDropout, self).__init__(**kwargs)
10
- self.rate = rate
11
-
12
- def call(self, inputs, training=None):
13
- return tf.keras.layers.Dropout(self.rate)(inputs, training=training)
14
-
15
- # Load the TensorFlow model with custom layer handling
16
- def load_model_with_custom_objects(model_path):
17
- with tf.keras.utils.custom_object_scope({'FixedDropout': FixedDropout}):
18
- model = tf.keras.models.load_model(model_path)
19
- return model
20
-
21
  tf_model_path = 'modelo_treinado.h5' # Update with the path to your TensorFlow model
22
- tf_model = load_model_with_custom_objects(tf_model_path)
23
 
 
24
  class_labels = ["Normal", "Cataract"]
25
 
26
- # Define a Gradio interface
27
- def classify_image(input_image):
28
- # Preprocess the input image
29
- input_image = cv2.resize(input_image, (224, 224)) # Resize the image to match the model's input size
30
- input_image = np.expand_dims(input_image, axis=0) # Add batch dimension
31
- input_image = input_image / 255.0 # Normalize pixel values (assuming input range [0, 255])
32
 
33
- # Make predictions using the loaded model
34
- predictions = tf_model.predict(input_image)
35
- class_index = np.argmax(predictions, axis=1)[0]
36
- predicted_class = class_labels[class_index]
37
 
38
- return predicted_class
 
39
 
40
- # Create a Gradio interface
41
- input_image = gr.inputs.Image(shape=(224, 224, 3)) # Define the input image shape
42
- output_label = gr.outputs.Label() # Define the output label
43
 
44
- gr.Interface(fn=classify_image, inputs=input_image, outputs=output_label).launch()
 
 
 
 
 
 
1
  import gradio as gr
2
  import tensorflow as tf
3
+ from tensorflow.keras.models import load_model
4
+ from PIL import Image
5
  import numpy as np
6
 
7
+ # Load the TensorFlow model
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  tf_model_path = 'modelo_treinado.h5' # Update with the path to your TensorFlow model
9
+ tf_model = load_model(tf_model_path)
10
 
11
+ # Class labels for the model
12
  class_labels = ["Normal", "Cataract"]
13
 
14
+ # Define a function for prediction
15
+ def predict(image):
16
+ # Preprocess the input image (resize and normalize)
17
+ image = image.resize((224, 224)) # Adjust the size as needed
18
+ image = np.array(image) / 255.0 # Normalize pixel values
19
+ image = np.expand_dims(image, axis=0) # Add batch dimension
20
 
21
+ # Make a prediction using the loaded TensorFlow model
22
+ predictions = tf_model.predict(image)
 
 
23
 
24
+ # Get the predicted class label
25
+ predicted_label = class_labels[np.argmax(predictions)]
26
 
27
+ return predicted_label
 
 
28
 
29
+ # Create the Gradio interface
30
+ gr.Interface(
31
+ fn=predict,
32
+ inputs=gr.Image(type="pil"),
33
+ outputs=gr.Label(num_top_classes=2),
34
+ ).launch()