Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,29 +5,29 @@ from PIL import Image, ImageOps
|
|
| 5 |
|
| 6 |
# Cargar modelo
|
| 7 |
model = tf.keras.models.load_model("quickdraw_model.keras")
|
| 8 |
-
etiquetas = ['car', 'cloud', 'moon'] #
|
| 9 |
|
| 10 |
def preprocesar_imagen(image):
|
| 11 |
-
|
| 12 |
-
|
|
|
|
| 13 |
if image.mode in ('RGBA', 'LA'):
|
| 14 |
image = image.convert('RGB')
|
| 15 |
image = image.convert('L')
|
| 16 |
image = ImageOps.invert(image)
|
| 17 |
image = image.resize((28, 28), Image.NEAREST)
|
| 18 |
arr = np.array(image) / 255.0
|
| 19 |
-
return arr
|
| 20 |
|
| 21 |
def predict(image):
|
| 22 |
try:
|
| 23 |
-
arr = preprocesar_imagen(image)
|
| 24 |
-
imagen_procesada = (arr * 255).astype(np.uint8)
|
| 25 |
entrada = arr.reshape(1, 784)
|
| 26 |
preds = model.predict(entrada)
|
| 27 |
class_idx = np.argmax(preds)
|
| 28 |
-
|
|
|
|
| 29 |
except Exception as e:
|
| 30 |
-
# Esto muestra el error en la interfaz de Gradio
|
| 31 |
return f"Error: {str(e)}", None
|
| 32 |
|
| 33 |
iface = gr.Interface(
|
|
|
|
| 5 |
|
| 6 |
# Cargar modelo
|
| 7 |
model = tf.keras.models.load_model("quickdraw_model.keras")
|
| 8 |
+
etiquetas = ['car', 'cloud', 'moon'] # Ajusta seg煤n tu modelo
|
| 9 |
|
| 10 |
def preprocesar_imagen(image):
|
| 11 |
+
# Si la imagen viene como np.ndarray, convi茅rtela a PIL
|
| 12 |
+
if isinstance(image, np.ndarray):
|
| 13 |
+
image = Image.fromarray(image)
|
| 14 |
if image.mode in ('RGBA', 'LA'):
|
| 15 |
image = image.convert('RGB')
|
| 16 |
image = image.convert('L')
|
| 17 |
image = ImageOps.invert(image)
|
| 18 |
image = image.resize((28, 28), Image.NEAREST)
|
| 19 |
arr = np.array(image) / 255.0
|
| 20 |
+
return arr, image # Regresamos el array (para el modelo) y el PIL (para mostrar)
|
| 21 |
|
| 22 |
def predict(image):
|
| 23 |
try:
|
| 24 |
+
arr, img_procesada_pil = preprocesar_imagen(image)
|
|
|
|
| 25 |
entrada = arr.reshape(1, 784)
|
| 26 |
preds = model.predict(entrada)
|
| 27 |
class_idx = np.argmax(preds)
|
| 28 |
+
# Devolvemos: predicci贸n, imagen preprocesada (como PIL)
|
| 29 |
+
return {etiquetas[class_idx]: float(preds[0][class_idx])}, img_procesada_pil
|
| 30 |
except Exception as e:
|
|
|
|
| 31 |
return f"Error: {str(e)}", None
|
| 32 |
|
| 33 |
iface = gr.Interface(
|