Mikeztrada commited on
Commit
77363bf
verified
1 Parent(s): 4db37a0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -28
app.py CHANGED
@@ -1,45 +1,49 @@
1
  import gradio as gr
2
  import tensorflow as tf
3
  import numpy as np
4
- import cv2
5
 
6
- # Lista de etiquetas (ajusta seg煤n tus clases)
7
- etiquetas = ['apple', 'car', 'cloud', 'moon', 'stop sign']
8
-
9
- # Carga el modelo
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 colores: fondo negro, trazo blanco
21
- image = 1.0 - image
22
- return image # NO lo aplanes aqu铆, as铆 puedes mostrarlo
 
 
 
 
 
 
 
23
 
24
  def predict(image):
25
- image_proc = procesar_imagen(image)
26
- # Para el modelo, s铆 lo aplanas
27
- image_for_model = image_proc.reshape(1, 784)
28
- preds = model.predict(image_for_model)
 
 
29
  class_idx = np.argmax(preds)
30
- return etiquetas[class_idx], image_proc
31
 
32
  iface = gr.Interface(
33
  fn=predict,
34
- inputs=gr.Image(image_mode='L', label="Dibuja o sube una imagen (trazo negro, fondo blanco)"),
35
  outputs=[
36
  gr.Label(num_top_classes=1, label="Predicci贸n"),
37
- gr.Image(image_mode='L', label="Imagen preprocesada (lo que ve la IA)")
38
-
39
  ],
40
- title="QuickDraw API (con imagen preprocesada)",
41
- description="Sube tu dibujo (fondo blanco, trazo negro). La IA mostrar谩 la predicci贸n y la imagen final con la que realmente predice."
42
  )
43
 
44
- if __name__ == "__main__":
45
- iface.launch()
 
1
  import gradio as gr
2
  import tensorflow as tf
3
  import numpy as np
4
+ 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)
36
+ return etiquetas[class_idx], imagen_procesada
37
 
38
  iface = gr.Interface(
39
  fn=predict,
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(shape=(28, 28), label="Imagen preprocesada (lo que ve la IA)")
 
44
  ],
45
+ title="QuickDraw API",
46
+ description="API para reconocer dibujos estilo QuickDraw. Muestra la imagen preprocesada."
47
  )
48
 
49
+ iface.launch()