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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -25
app.py CHANGED
@@ -1,34 +1,40 @@
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()
 
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'
8
+ def fixed_dropout(x):
9
+ return tf.keras.layers.Dropout(0.5)(x)
10
+
11
+ # Function to register custom layers within a custom_object_scope
12
+ def register_custom_layers():
13
+ return tf.keras.utils.custom_object_scope({'FixedDropout': fixed_dropout})
14
+
15
+ # Load the TensorFlow model within the custom_object_scope
16
+ with register_custom_layers():
17
+ tf_model = tf.keras.models.load_model('modelo_treinado.h5')
18
 
 
19
  class_labels = ["Normal", "Cataract"]
20
 
 
21
  def predict(inp):
22
+ # Use the TensorFlow model to predict Normal or Cataract
23
+ img_array = cv2.cvtColor(np.array(inp), cv2.COLOR_RGB2BGR)
24
+ img_array = cv2.resize(img_array, (224, 224))
25
+ img_array = img_array / 255.0
26
+ img_array = np.expand_dims(img_array, axis=0)
27
+
28
+ prediction_tf = tf_model.predict(img_array)
29
+ label_index = np.argmax(prediction_tf)
30
+ confidence_tf = float(prediction_tf[0, label_index])
31
+
32
+ return class_labels[label_index], confidence_tf
33
+
34
+ demo = gr.Interface(
35
+ fn=predict,
36
+ inputs=gr.inputs.Image(type="pil"),
37
+ outputs=["label", "number"],
38
+ )
39
+
 
 
40
  demo.launch()