Spaces:
Sleeping
Sleeping
File size: 1,543 Bytes
760e4cc e91a706 760e4cc c72ca3b 760e4cc fd3df99 77363bf 6e37720 77363bf 55ba783 77363bf 55ba783 e91a706 55ba783 8b0ce87 c72ca3b e91a706 020d3b5 c72ca3b e91a706 c72ca3b 760e4cc 77363bf e3b289c e91a706 e3b289c 77363bf e91a706 760e4cc 77363bf | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | import gradio as gr
import tensorflow as tf
import numpy as np
from PIL import Image
# Cargar modelo
model = tf.keras.models.load_model("quickdraw_model.keras")
etiquetas = ['apple', 'banana', 'bed', 'carrot', 'laptop'] # Ajusta según tu modelo
def preprocesar_imagen(image):
if isinstance(image, np.ndarray):
image = Image.fromarray(image)
if image.mode in ('RGBA', 'LA'):
image = image.convert('RGB')
image = image.convert('L') # escala de grises
image = image.resize((28, 28), Image.NEAREST)
arr = np.array(image) / 255.0
# Umbral bajo: todo menor o igual a 0.2 será negro (0), lo demás blanco (1)
arr_bin = np.where(arr <= 0.2, 0.0, 1.0).astype(np.float32)
arr_bin_4d = arr_bin.reshape(1, 28, 28, 1)
return arr_bin_4d, arr_bin
def predict(image):
try:
arr, img_preprocesada = preprocesar_imagen(image)
preds = model.predict(arr)
class_idx = np.argmax(preds)
return {etiquetas[class_idx]: float(preds[0][class_idx])}, img_preprocesada
except Exception as e:
return f"Error: {str(e)}", None
iface = gr.Interface(
fn=predict,
inputs=gr.Image(label="Dibuja o sube una imagen (fondo blanco, trazo negro)"),
outputs=[
gr.Label(num_top_classes=1, label="Predicción"),
gr.Image(label="Imagen preprocesada (0-1 en escala de grises)")
],
title="QuickDraw API",
description="API para reconocer dibujos estilo QuickDraw. Muestra la imagen preprocesada en escala 0-1."
)
iface.launch()
|