Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,33 +1,41 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import tensorflow as tf
|
| 3 |
import numpy as np
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
etiquetas = [
|
| 7 |
|
| 8 |
-
# Cargar el modelo
|
| 9 |
model = tf.keras.models.load_model("quickdraw_model.keras")
|
| 10 |
|
| 11 |
-
def
|
| 12 |
-
|
| 13 |
-
import numpy as np
|
| 14 |
-
# Si la imagen es RGB, conviértela a escala de grises
|
| 15 |
if image.ndim == 3:
|
| 16 |
image = np.mean(image, axis=2)
|
| 17 |
-
#
|
| 18 |
-
image = cv2.resize(image, (28, 28))
|
|
|
|
| 19 |
image = image / 255.0
|
|
|
|
|
|
|
|
|
|
| 20 |
image = image.reshape(1, 784)
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
class_idx = np.argmax(preds)
|
| 23 |
return etiquetas[class_idx]
|
| 24 |
|
| 25 |
iface = gr.Interface(
|
| 26 |
fn=predict,
|
| 27 |
-
inputs=gr.Image(image_mode='L'),
|
| 28 |
outputs="label",
|
| 29 |
title="QuickDraw API",
|
| 30 |
-
description="API para reconocer dibujos estilo QuickDraw"
|
| 31 |
)
|
| 32 |
|
| 33 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import tensorflow as tf
|
| 3 |
import numpy as np
|
| 4 |
+
import cv2
|
| 5 |
|
| 6 |
+
# Tu lista de etiquetas (ajusta según tus clases y orden de entrenamiento)
|
| 7 |
+
etiquetas = ['apple', 'car', 'cloud', 'moon', 'stop sign']
|
| 8 |
|
| 9 |
+
# Cargar el modelo previamente entrenado (.keras)
|
| 10 |
model = tf.keras.models.load_model("quickdraw_model.keras")
|
| 11 |
|
| 12 |
+
def procesar_imagen(image):
|
| 13 |
+
# Convertir a escala de grises si es necesario
|
|
|
|
|
|
|
| 14 |
if image.ndim == 3:
|
| 15 |
image = np.mean(image, axis=2)
|
| 16 |
+
# Redimensionar a 28x28
|
| 17 |
+
image = cv2.resize(image, (28, 28), interpolation=cv2.INTER_AREA)
|
| 18 |
+
# Normalizar (0-1)
|
| 19 |
image = image / 255.0
|
| 20 |
+
# Invertir: fondo negro, trazo blanco
|
| 21 |
+
image = 1.0 - image
|
| 22 |
+
# Aplanar
|
| 23 |
image = image.reshape(1, 784)
|
| 24 |
+
return image
|
| 25 |
+
|
| 26 |
+
def predict(image):
|
| 27 |
+
image_proc = procesar_imagen(image)
|
| 28 |
+
preds = model.predict(image_proc)
|
| 29 |
class_idx = np.argmax(preds)
|
| 30 |
return etiquetas[class_idx]
|
| 31 |
|
| 32 |
iface = gr.Interface(
|
| 33 |
fn=predict,
|
| 34 |
+
inputs=gr.Image(shape=(224, 224), image_mode='L', label="Dibuja o sube una imagen (trazo negro, fondo blanco)"),
|
| 35 |
outputs="label",
|
| 36 |
title="QuickDraw API",
|
| 37 |
+
description="API para reconocer dibujos estilo QuickDraw entrenado por ti"
|
| 38 |
)
|
| 39 |
|
| 40 |
+
if __name__ == "__main__":
|
| 41 |
+
iface.launch()
|