Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,14 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
from PIL import Image
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
def preprocesar_imagen(image):
|
| 6 |
-
#
|
| 7 |
if isinstance(image, np.ndarray):
|
| 8 |
image = Image.fromarray(image)
|
| 9 |
image = image.convert('L')
|
|
@@ -11,23 +16,21 @@ def preprocesar_imagen(image):
|
|
| 11 |
arr = np.array(image) / 255.0
|
| 12 |
arr_bin = (arr < 0.5).astype(np.float32)
|
| 13 |
arr_bin_4d = arr_bin.reshape(1, 28, 28, 1)
|
| 14 |
-
return arr_bin_4d
|
| 15 |
|
| 16 |
def predict(image):
|
| 17 |
-
|
| 18 |
-
preds = model.predict(
|
| 19 |
class_idx = np.argmax(preds)
|
| 20 |
-
|
|
|
|
| 21 |
|
| 22 |
iface = gr.Interface(
|
| 23 |
fn=predict,
|
| 24 |
-
inputs=gr.
|
| 25 |
-
outputs=
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
],
|
| 29 |
-
title="Dibujo estilo QuickDraw",
|
| 30 |
-
description="Dibuja, se procesa como 28x28 para la IA."
|
| 31 |
)
|
| 32 |
|
| 33 |
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import tensorflow as tf
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
+
# Carga el modelo entrenado
|
| 7 |
+
model = tf.keras.models.load_model("quickdraw_model.keras")
|
| 8 |
+
etiquetas = ['apple', 'banana', 'bed', 'carrot', 'laptop']
|
| 9 |
+
|
| 10 |
def preprocesar_imagen(image):
|
| 11 |
+
# Convierte la imagen a escala de grises, 28x28 y binariza
|
| 12 |
if isinstance(image, np.ndarray):
|
| 13 |
image = Image.fromarray(image)
|
| 14 |
image = image.convert('L')
|
|
|
|
| 16 |
arr = np.array(image) / 255.0
|
| 17 |
arr_bin = (arr < 0.5).astype(np.float32)
|
| 18 |
arr_bin_4d = arr_bin.reshape(1, 28, 28, 1)
|
| 19 |
+
return arr_bin_4d
|
| 20 |
|
| 21 |
def predict(image):
|
| 22 |
+
x = preprocesar_imagen(image)
|
| 23 |
+
preds = model.predict(x)
|
| 24 |
class_idx = np.argmax(preds)
|
| 25 |
+
confidences = {etiquetas[i]: float(preds[0][i]) for i in range(len(etiquetas))}
|
| 26 |
+
return confidences
|
| 27 |
|
| 28 |
iface = gr.Interface(
|
| 29 |
fn=predict,
|
| 30 |
+
inputs=gr.Image(label="Dibuja o sube una imagen (fondo blanco, trazo negro)"),
|
| 31 |
+
outputs=gr.Label(label="Predicci贸n"),
|
| 32 |
+
title="QuickDraw - Clasificador de Dibujos",
|
| 33 |
+
description="Dibuja o sube una imagen y te dir谩 si es una apple, banana, bed, carrot o laptop."
|
|
|
|
|
|
|
|
|
|
| 34 |
)
|
| 35 |
|
| 36 |
iface.launch()
|