DHEIVER commited on
Commit
a46bb01
1 Parent(s): dfdde84

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -43
app.py CHANGED
@@ -1,52 +1,43 @@
1
- import gradio as gr
2
  import tensorflow as tf
3
- from tensorflow.keras.models import load_model
4
- from tensorflow.keras.layers import Layer
5
- import numpy as np
6
  from PIL import Image
 
7
 
8
- # Define the custom 'FixedDropout' layer
9
- class FixedDropout(Layer):
10
- def __init__(self, rate, **kwargs):
11
- super(FixedDropout, self).__init__(**kwargs)
12
- self.rate = rate
13
-
14
- def call(self, inputs, training=None):
15
- if training is None:
16
- training = K.learning_phase()
17
-
18
- if training == 1:
19
- return K.dropout(inputs, self.rate)
20
- return inputs
21
-
22
- # Register the custom layer in a custom object scope
23
- custom_objects = {"FixedDropout": FixedDropout}
24
-
25
- # Load the TensorFlow model with the custom object scope
26
- tf_model_path = 'modelo_treinado.h5' # Update with the path to your model
27
- tf_model = load_model(tf_model_path, custom_objects=custom_objects)
28
 
29
- # Class labels for the model
30
  class_labels = ["Normal", "Cataract"]
31
 
32
- # Define a function for prediction
33
- def predict(image):
34
- # Preprocess the input image
35
- image = image.resize((224, 224)) # Adjust the size as needed
36
- image = np.array(image) / 255.0 # Normalize pixel values
37
- image = np.expand_dims(image, axis=0) # Add batch dimension
38
-
39
- # Make a prediction using the loaded TensorFlow model
40
- predictions = tf_model.predict(image)
41
-
42
- # Get the predicted class label
43
- predicted_label = class_labels[np.argmax(predictions)]
44
-
45
- return predicted_label
46
-
47
- # Create the Gradio interface
 
 
 
 
 
 
 
 
 
48
  gr.Interface(
49
  fn=predict,
50
- inputs=gr.Image(type="pil"),
51
- outputs=gr.Label(num_top_classes=2)
52
  ).launch()
 
1
+ import requests
2
  import tensorflow as tf
3
+ import gradio as gr
 
 
4
  from PIL import Image
5
+ import numpy as np
6
 
7
+ # Load your custom TensorFlow model. Update 'modelo_treinado.h5' with the path to your model.
8
+ tf_model_path = 'modelo_treinado.h5'
9
+ tf_model = tf.keras.models.load_model(tf_model_path)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
+ # Define your class labels.
12
  class_labels = ["Normal", "Cataract"]
13
 
14
+ def preprocess_image(image):
15
+ # Resize the image to the input size required by the model (e.g., 224x224).
16
+ image = image.resize((224, 224))
17
+ # Convert the PIL image to a NumPy array and normalize pixel values.
18
+ image = np.array(image) / 255.0
19
+ # Add a batch dimension to the image.
20
+ image = np.expand_dims(image, axis=0)
21
+ return image
22
+
23
+ def predict(inp):
24
+ # Preprocess the input image.
25
+ inp = preprocess_image(inp)
26
+ # Make predictions using your custom TensorFlow model.
27
+ predictions = tf_model.predict(inp)
28
+ # Get the class label with the highest confidence.
29
+ predicted_class = class_labels[np.argmax(predictions)]
30
+ # Get the confidence score of the predicted class.
31
+ confidence = float(predictions[0][np.argmax(predictions)])
32
+
33
+ # Create a dictionary with the predicted class and its confidence.
34
+ result = {predicted_class: confidence}
35
+
36
+ return result
37
+
38
+ # Create a Gradio interface.
39
  gr.Interface(
40
  fn=predict,
41
+ inputs=gr.inputs.Image(type="pil"),
42
+ outputs=gr.outputs.Label(num_top_classes=1)
43
  ).launch()