Mikeztrada commited on
Commit
e91a706
verified
1 Parent(s): 91a9a60

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -13
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import gradio as gr
2
  import tensorflow as tf
3
  import numpy as np
4
- from PIL import Image, ImageOps
5
 
6
  # Cargar modelo
7
  model = tf.keras.models.load_model("quickdraw_model.keras")
@@ -12,35 +12,32 @@ def preprocesar_imagen(image):
12
  image = Image.fromarray(image)
13
  if image.mode in ('RGBA', 'LA'):
14
  image = image.convert('RGB')
15
-
16
  image = image.resize((28, 28), Image.NEAREST)
17
  arr = np.array(image) / 255.0
18
- arr_bin = (arr < 0.5).astype(np.uint8) # binariza, 1=negro, 0=blanco
19
- img_binarizada = Image.fromarray((arr_bin * 255).astype(np.uint8), mode='L')
20
- arr_bin = arr_bin.reshape(1, 28, 28, 1).astype(np.float32)
21
- return arr_bin, img_binarizada
22
-
23
-
24
 
25
  def predict(image):
26
  try:
27
- arr, img_procesada_pil = preprocesar_imagen(image)
28
  preds = model.predict(arr)
29
  class_idx = np.argmax(preds)
30
- return {etiquetas[class_idx]: float(preds[0][class_idx])}, img_procesada_pil
31
  except Exception as e:
32
  return f"Error: {str(e)}", None
33
 
34
-
35
  iface = gr.Interface(
36
  fn=predict,
37
  inputs=gr.Image(label="Dibuja o sube una imagen (fondo blanco, trazo negro)"),
38
  outputs=[
39
  gr.Label(num_top_classes=1, label="Predicci贸n"),
40
- gr.Image(label="Imagen preprocesada (lo que ve la IA)")
41
  ],
42
  title="QuickDraw API",
43
- description="API para reconocer dibujos estilo QuickDraw. Muestra la imagen preprocesada."
44
  )
45
 
46
  iface.launch()
 
1
  import gradio as gr
2
  import tensorflow as tf
3
  import numpy as np
4
+ from PIL import Image
5
 
6
  # Cargar modelo
7
  model = tf.keras.models.load_model("quickdraw_model.keras")
 
12
  image = Image.fromarray(image)
13
  if image.mode in ('RGBA', 'LA'):
14
  image = image.convert('RGB')
15
+ image = image.convert('L')
16
  image = image.resize((28, 28), Image.NEAREST)
17
  arr = np.array(image) / 255.0
18
+ arr_bin = (arr < 0.5).astype(np.float32) # Binariza: 0 o 1 en float32
19
+ arr_bin_4d = arr_bin.reshape(1, 28, 28, 1)
20
+ # Para mostrar, devolvemos el arreglo 2D 0-1
21
+ return arr_bin_4d, arr_bin
 
 
22
 
23
  def predict(image):
24
  try:
25
+ arr, img_preprocesada = preprocesar_imagen(image)
26
  preds = model.predict(arr)
27
  class_idx = np.argmax(preds)
28
+ return {etiquetas[class_idx]: float(preds[0][class_idx])}, img_preprocesada
29
  except Exception as e:
30
  return f"Error: {str(e)}", None
31
 
 
32
  iface = gr.Interface(
33
  fn=predict,
34
  inputs=gr.Image(label="Dibuja o sube una imagen (fondo blanco, trazo negro)"),
35
  outputs=[
36
  gr.Label(num_top_classes=1, label="Predicci贸n"),
37
+ gr.Image(label="Imagen preprocesada (0-1 en escala de grises)")
38
  ],
39
  title="QuickDraw API",
40
+ description="API para reconocer dibujos estilo QuickDraw. Muestra la imagen preprocesada en escala 0-1."
41
  )
42
 
43
  iface.launch()