Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,45 +1,49 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import tensorflow as tf
|
| 3 |
import numpy as np
|
| 4 |
-
import
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
etiquetas = ['apple', 'car', 'cloud', 'moon', 'stop sign']
|
| 8 |
-
|
| 9 |
-
# Carga el modelo
|
| 10 |
model = tf.keras.models.load_model("quickdraw_model.keras")
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
#
|
| 17 |
-
image
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
def predict(image):
|
| 25 |
-
|
| 26 |
-
#
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
| 29 |
class_idx = np.argmax(preds)
|
| 30 |
-
return etiquetas[class_idx],
|
| 31 |
|
| 32 |
iface = gr.Interface(
|
| 33 |
fn=predict,
|
| 34 |
-
inputs=gr.Image(
|
| 35 |
outputs=[
|
| 36 |
gr.Label(num_top_classes=1, label="Predicci贸n"),
|
| 37 |
-
gr.Image(
|
| 38 |
-
|
| 39 |
],
|
| 40 |
-
title="QuickDraw API
|
| 41 |
-
description="
|
| 42 |
)
|
| 43 |
|
| 44 |
-
|
| 45 |
-
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import tensorflow as tf
|
| 3 |
import numpy as np
|
| 4 |
+
from PIL import Image, ImageOps
|
| 5 |
|
| 6 |
+
# Cargar el modelo
|
|
|
|
|
|
|
|
|
|
| 7 |
model = tf.keras.models.load_model("quickdraw_model.keras")
|
| 8 |
|
| 9 |
+
# Tus etiquetas en el mismo orden de entrenamiento (ajusta si usaste otro orden)
|
| 10 |
+
etiquetas = ['car', 'cloud', 'moon']
|
| 11 |
+
|
| 12 |
+
def preprocesar_imagen(image):
|
| 13 |
+
# Si viene con canal alfa, elimina alfa
|
| 14 |
+
if image.mode in ('RGBA', 'LA'):
|
| 15 |
+
image = image.convert('RGB')
|
| 16 |
+
# Convertir a escala de grises
|
| 17 |
+
image = image.convert('L')
|
| 18 |
+
# Invertir (fondo blanco, trazo negro)
|
| 19 |
+
image = ImageOps.invert(image)
|
| 20 |
+
# Redimensionar a 28x28, SIN suavizar
|
| 21 |
+
image = image.resize((28, 28), Image.NEAREST)
|
| 22 |
+
# Normalizar a 0-1
|
| 23 |
+
arr = np.array(image) / 255.0
|
| 24 |
+
# (Opcional) Reinvertir si tu modelo espera trazo blanco y fondo negro
|
| 25 |
+
# arr = 1 - arr
|
| 26 |
+
return arr
|
| 27 |
|
| 28 |
def predict(image):
|
| 29 |
+
arr = preprocesar_imagen(image)
|
| 30 |
+
# Mostrar lo que ve la IA
|
| 31 |
+
imagen_procesada = (arr * 255).astype(np.uint8)
|
| 32 |
+
# Redimensionar para la IA (aplanar)
|
| 33 |
+
entrada = arr.reshape(1, 784)
|
| 34 |
+
preds = model.predict(entrada)
|
| 35 |
class_idx = np.argmax(preds)
|
| 36 |
+
return etiquetas[class_idx], imagen_procesada
|
| 37 |
|
| 38 |
iface = gr.Interface(
|
| 39 |
fn=predict,
|
| 40 |
+
inputs=gr.Image(label="Dibuja o sube una imagen (fondo blanco, trazo negro)"),
|
| 41 |
outputs=[
|
| 42 |
gr.Label(num_top_classes=1, label="Predicci贸n"),
|
| 43 |
+
gr.Image(shape=(28, 28), label="Imagen preprocesada (lo que ve la IA)")
|
|
|
|
| 44 |
],
|
| 45 |
+
title="QuickDraw API",
|
| 46 |
+
description="API para reconocer dibujos estilo QuickDraw. Muestra la imagen preprocesada."
|
| 47 |
)
|
| 48 |
|
| 49 |
+
iface.launch()
|
|
|