Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,31 +5,20 @@ 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)
|
|
@@ -40,7 +29,7 @@ iface = gr.Interface(
|
|
| 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(
|
| 44 |
],
|
| 45 |
title="QuickDraw API",
|
| 46 |
description="API para reconocer dibujos estilo QuickDraw. Muestra la imagen preprocesada."
|
|
|
|
| 5 |
|
| 6 |
# Cargar el modelo
|
| 7 |
model = tf.keras.models.load_model("quickdraw_model.keras")
|
| 8 |
+
etiquetas = ['car', 'cloud', 'moon'] # Cambia si tu modelo tiene otras
|
|
|
|
|
|
|
| 9 |
|
| 10 |
def preprocesar_imagen(image):
|
|
|
|
| 11 |
if image.mode in ('RGBA', 'LA'):
|
| 12 |
image = image.convert('RGB')
|
|
|
|
| 13 |
image = image.convert('L')
|
|
|
|
| 14 |
image = ImageOps.invert(image)
|
|
|
|
| 15 |
image = image.resize((28, 28), Image.NEAREST)
|
|
|
|
| 16 |
arr = np.array(image) / 255.0
|
|
|
|
|
|
|
| 17 |
return arr
|
| 18 |
|
| 19 |
def predict(image):
|
| 20 |
arr = preprocesar_imagen(image)
|
|
|
|
| 21 |
imagen_procesada = (arr * 255).astype(np.uint8)
|
|
|
|
| 22 |
entrada = arr.reshape(1, 784)
|
| 23 |
preds = model.predict(entrada)
|
| 24 |
class_idx = np.argmax(preds)
|
|
|
|
| 29 |
inputs=gr.Image(label="Dibuja o sube una imagen (fondo blanco, trazo negro)"),
|
| 30 |
outputs=[
|
| 31 |
gr.Label(num_top_classes=1, label="Predicci贸n"),
|
| 32 |
+
gr.Image(label="Imagen preprocesada (lo que ve la IA)") # <--- SIN shape
|
| 33 |
],
|
| 34 |
title="QuickDraw API",
|
| 35 |
description="API para reconocer dibujos estilo QuickDraw. Muestra la imagen preprocesada."
|