DHEIVER commited on
Commit
2133a73
·
1 Parent(s): 59e3723

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -28
app.py CHANGED
@@ -1,37 +1,34 @@
1
  import gradio as gr
2
  import tensorflow as tf
3
- import requests
4
- import cv2
5
  import numpy as np
6
 
7
- # Define a custom layer 'FixedDropout' without specifying 'name' argument
8
- def fixed_dropout(x):
9
- return tf.keras.layers.Dropout(0.5)(x)
10
-
11
- # Load the TensorFlow model while registering the custom layer
12
- custom_objects = {'fixed_dropout': fixed_dropout}
13
- tf_model_path = 'modelo_treinado.h5' # Update with the path to your TensorFlow model
14
- tf_model = tf.keras.models.load_model(tf_model_path, custom_objects=custom_objects)
15
 
 
16
  class_labels = ["Normal", "Cataract"]
17
 
 
18
  def predict(inp):
19
- # Use the TensorFlow model to predict Normal or Cataract
20
- img_array = cv2.cvtColor(np.array(inp), cv2.COLOR_RGB2BGR)
21
- img_array = cv2.resize(img_array, (224, 224))
22
- img_array = img_array / 255.0
23
- img_array = np.expand_dims(img_array, axis=0)
24
-
25
- prediction_tf = tf_model.predict(img_array)
26
- label_index = np.argmax(prediction_tf)
27
- confidence_tf = float(prediction_tf[0, label_index])
28
-
29
- return class_labels[label_index], confidence_tf
30
-
31
- demo = gr.Interface(
32
- fn=predict,
33
- inputs=gr.inputs.Image(type="pil"),
34
- outputs=["label", "number"]
35
- )
36
-
 
 
37
  demo.launch()
 
1
  import gradio as gr
2
  import tensorflow as tf
3
+ from PIL import Image
 
4
  import numpy as np
5
 
6
+ # Carregar o modelo TensorFlow
7
+ model = tf.keras.models.load_model('modelo_treinado.h5')
 
 
 
 
 
 
8
 
9
+ # Definir as classes
10
  class_labels = ["Normal", "Cataract"]
11
 
12
+ # Função de previsão
13
  def predict(inp):
14
+ # Pré-processamento da imagem para adequá-la ao modelo TensorFlow
15
+ img = np.array(inp)
16
+ img = tf.image.resize(img, (224, 224))
17
+ img = img / 255.0 # Normalização, se necessário
18
+ img = tf.expand_dims(img, axis=0)
19
+
20
+ # Fazer previsão com o modelo TensorFlow
21
+ predictions = model.predict(img)
22
+
23
+ # Obter a classe com a maior probabilidade
24
+ predicted_class = class_labels[np.argmax(predictions)]
25
+
26
+ return {predicted_class: float(predictions[0, np.argmax(predictions)])}
27
+
28
+ # Criar a interface Gradio
29
+ demo = gr.Interface(fn=predict,
30
+ inputs=gr.inputs.Image(type="pil"),
31
+ outputs=gr.outputs.Label(),
32
+ )
33
+
34
  demo.launch()