Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,34 +1,40 @@
|
|
1 |
import gradio as gr
|
2 |
import tensorflow as tf
|
3 |
-
|
|
|
4 |
import numpy as np
|
5 |
|
6 |
-
#
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
-
# Definir as classes
|
10 |
class_labels = ["Normal", "Cataract"]
|
11 |
|
12 |
-
# Função de previsão
|
13 |
def predict(inp):
|
14 |
-
#
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
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()
|