Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,53 +1,7 @@
|
|
| 1 |
-
# import gradio as gr
|
| 2 |
-
# import tensorflow as tf
|
| 3 |
-
# import numpy as np
|
| 4 |
-
|
| 5 |
-
# # Defina a camada personalizada FixedDropout
|
| 6 |
-
# class FixedDropout(tf.keras.layers.Dropout):
|
| 7 |
-
# def _get_noise_shape(self, inputs):
|
| 8 |
-
# if self.noise_shape is None:
|
| 9 |
-
# return self.noise_shape
|
| 10 |
-
# symbolic_shape = tf.shape(inputs)
|
| 11 |
-
# noise_shape = [symbolic_shape[axis] if shape is None else shape
|
| 12 |
-
# for axis, shape in enumerate(self.noise_shape)]
|
| 13 |
-
# return tuple(noise_shape)
|
| 14 |
-
|
| 15 |
-
# # Registre a camada personalizada FixedDropout
|
| 16 |
-
# tf.keras.utils.get_custom_objects()['FixedDropout'] = FixedDropout
|
| 17 |
-
|
| 18 |
-
# # Carregue seu modelo TensorFlow treinado
|
| 19 |
-
# model = tf.keras.models.load_model('modelo_treinado.h5')
|
| 20 |
-
|
| 21 |
-
# # Defina uma função para fazer previsões
|
| 22 |
-
# def classify_image(input_image):
|
| 23 |
-
# # Redimensione a imagem para as dimensões corretas (192x256)
|
| 24 |
-
# input_image = tf.image.resize(input_image, (192, 256)) # Redimensione para as dimensões esperadas
|
| 25 |
-
# input_image = (input_image / 255.0) # Normalize para [0, 1]
|
| 26 |
-
# input_image = np.expand_dims(input_image, axis=0) # Adicione a dimensão de lote
|
| 27 |
-
|
| 28 |
-
# # Faça a previsão usando o modelo
|
| 29 |
-
# prediction = model.predict(input_image)
|
| 30 |
-
|
| 31 |
-
# # Assumindo que o modelo retorna probabilidades para duas classes, você pode retornar a classe com a maior probabilidade
|
| 32 |
-
# class_index = np.argmax(prediction)
|
| 33 |
-
# class_labels = ["Normal", "Cataract"] # Substitua pelas suas etiquetas de classe reais
|
| 34 |
-
# predicted_class = class_labels[class_index]
|
| 35 |
-
|
| 36 |
-
# return predicted_class
|
| 37 |
-
|
| 38 |
-
# # Crie uma interface Gradio
|
| 39 |
-
# input_interface = gr.Interface(
|
| 40 |
-
# fn=classify_image,
|
| 41 |
-
# inputs="image", # Especifique o tipo de entrada como "image"
|
| 42 |
-
# outputs="text" # Especifique o tipo de saída como "text"
|
| 43 |
-
# )
|
| 44 |
-
|
| 45 |
-
# # Inicie o aplicativo Gradio
|
| 46 |
-
# input_interface.launch()
|
| 47 |
-
|
| 48 |
import gradio as gr
|
| 49 |
import tensorflow as tf
|
| 50 |
import numpy as np
|
|
|
|
| 51 |
|
| 52 |
# Defina a camada personalizada FixedDropout
|
| 53 |
class FixedDropout(tf.keras.layers.Dropout):
|
|
@@ -82,14 +36,22 @@ def classify_image(input_image):
|
|
| 82 |
|
| 83 |
# Retorne a classe prevista e as probabilidades das classes
|
| 84 |
class_probabilities = {class_labels[i]: round(float(prediction[0][i]), 4) for i in range(len(class_labels))}
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
# Crie uma interface Gradio
|
| 88 |
input_interface = gr.Interface(
|
| 89 |
fn=classify_image,
|
| 90 |
inputs="image", # Especifique o tipo de entrada como "image"
|
| 91 |
-
outputs=["
|
| 92 |
-
output_labels=["
|
| 93 |
)
|
| 94 |
|
| 95 |
# Inicie o aplicativo Gradio
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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):
|
|
|
|
| 36 |
|
| 37 |
# Retorne a classe prevista e as probabilidades das classes
|
| 38 |
class_probabilities = {class_labels[i]: round(float(prediction[0][i]), 4) for i in range(len(class_labels))}
|
| 39 |
+
|
| 40 |
+
# Crie uma imagem composta com o rótulo de previsão
|
| 41 |
+
output_image = Image.fromarray((input_image[0] * 255).astype('uint8'))
|
| 42 |
+
draw = ImageDraw.Draw(output_image)
|
| 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 output_image, class_probabilities
|
| 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=["image", "text"], # Especifique dois tipos de saída: imagem e texto
|
| 54 |
+
output_labels=["Output Image", "Class Probabilities"] # Rotule as saídas
|
| 55 |
)
|
| 56 |
|
| 57 |
# Inicie o aplicativo Gradio
|