Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,21 +1,9 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import tensorflow as tf
|
| 3 |
import numpy as np
|
|
|
|
| 4 |
from PIL import Image, ImageDraw, ImageFont
|
| 5 |
|
| 6 |
-
# Defina a camada personalizada FixedDropout
|
| 7 |
-
class FixedDropout(tf.keras.layers.Dropout):
|
| 8 |
-
def _get_noise_shape(self, inputs):
|
| 9 |
-
if self.noise_shape is None:
|
| 10 |
-
return self.noise_shape
|
| 11 |
-
symbolic_shape = tf.shape(inputs)
|
| 12 |
-
noise_shape = [symbolic_shape[axis] if shape is None else shape
|
| 13 |
-
for axis, shape in enumerate(self.noise_shape)]
|
| 14 |
-
return tuple(noise_shape)
|
| 15 |
-
|
| 16 |
-
# Registre a camada personalizada FixedDropout
|
| 17 |
-
tf.keras.utils.get_custom_objects()['FixedDropout'] = FixedDropout
|
| 18 |
-
|
| 19 |
# Carregue seu modelo TensorFlow treinado
|
| 20 |
model = tf.keras.models.load_model('modelo_treinado.h5')
|
| 21 |
|
|
@@ -34,24 +22,26 @@ def classify_image(input_image):
|
|
| 34 |
class_labels = ["Normal", "Cataract"] # Substitua pelas suas etiquetas de classe reais
|
| 35 |
predicted_class = class_labels[class_index]
|
| 36 |
|
| 37 |
-
#
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
#
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
font = ImageFont.load_default()
|
| 44 |
-
label_text = f"Predicted Class: {predicted_class}"
|
| 45 |
-
draw.text((10, 10), label_text, (255, 0, 0), font=font)
|
| 46 |
|
| 47 |
-
return
|
| 48 |
|
| 49 |
# Crie uma interface Gradio
|
| 50 |
input_interface = gr.Interface(
|
| 51 |
fn=classify_image,
|
| 52 |
inputs="image", # Especifique o tipo de entrada como "image"
|
| 53 |
-
outputs=
|
| 54 |
-
|
| 55 |
)
|
| 56 |
|
| 57 |
# Inicie o aplicativo Gradio
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import tensorflow as tf
|
| 3 |
import numpy as np
|
| 4 |
+
import cv2
|
| 5 |
from PIL import Image, ImageDraw, ImageFont
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
# Carregue seu modelo TensorFlow treinado
|
| 8 |
model = tf.keras.models.load_model('modelo_treinado.h5')
|
| 9 |
|
|
|
|
| 22 |
class_labels = ["Normal", "Cataract"] # Substitua pelas suas etiquetas de classe reais
|
| 23 |
predicted_class = class_labels[class_index]
|
| 24 |
|
| 25 |
+
# Crie uma imagem composta com a caixa de identificação de objeto e o rótulo de previsão
|
| 26 |
+
output_image = (input_image[0] * 255).astype('uint8')
|
| 27 |
+
output_image_with_box = output_image.copy()
|
| 28 |
+
|
| 29 |
+
# Desenhe uma caixa de identificação de objeto no output_image_with_box (apenas como exemplo)
|
| 30 |
+
if predicted_class == "Cataract": # Adicione sua lógica para desenhar a caixa com base na classe
|
| 31 |
+
cv2.rectangle(output_image_with_box, (50, 50), (150, 150), (0, 255, 0), 2) # Exemplo de caixa verde
|
| 32 |
|
| 33 |
+
# Escreva o rótulo de previsão no output_image_with_box
|
| 34 |
+
font = cv2.FONT_HERSHEY_SIMPLEX
|
| 35 |
+
cv2.putText(output_image_with_box, f"Predicted Class: {predicted_class}", (10, 30), font, 0.7, (0, 0, 255), 2)
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
return output_image_with_box
|
| 38 |
|
| 39 |
# Crie uma interface Gradio
|
| 40 |
input_interface = gr.Interface(
|
| 41 |
fn=classify_image,
|
| 42 |
inputs="image", # Especifique o tipo de entrada como "image"
|
| 43 |
+
outputs="image", # Especifique o tipo de saída como "image"
|
| 44 |
+
live=True
|
| 45 |
)
|
| 46 |
|
| 47 |
# Inicie o aplicativo Gradio
|