Mikeztrada commited on
Commit
908977e
verified
1 Parent(s): 81f06e2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -15
app.py CHANGED
@@ -1,15 +1,9 @@
1
  import gradio as gr
2
  import numpy as np
3
  from PIL import Image
4
- import tensorflow as tf
5
-
6
- # Cargar modelo
7
- model = tf.keras.models.load_model("quickdraw_model.keras")
8
-
9
- # etiquetas usadas en el entrenamiento
10
- etiquetas = ['apple', 'banana', 'bed', 'carrot', 'laptop']
11
 
12
  def preprocesar_imagen(image):
 
13
  if isinstance(image, np.ndarray):
14
  image = Image.fromarray(image)
15
  image = image.convert('L')
@@ -17,20 +11,23 @@ def preprocesar_imagen(image):
17
  arr = np.array(image) / 255.0
18
  arr_bin = (arr < 0.5).astype(np.float32)
19
  arr_bin_4d = arr_bin.reshape(1, 28, 28, 1)
20
- return arr_bin_4d
21
 
22
  def predict(image):
23
- x = preprocesar_imagen(image)
24
- preds = model.predict(x)
25
  class_idx = np.argmax(preds)
26
- return {etiquetas[class_idx]: float(preds[0][class_idx])}
27
 
28
  iface = gr.Interface(
29
  fn=predict,
30
- inputs=gr.Image(label="Dibuja o sube una imagen"),
31
- outputs=gr.Label(label="Predicci贸n"),
32
- title="Predicci贸n QuickDraw",
33
- description="Dibuja o sube una imagen para clasificarla."
 
 
 
34
  )
35
 
36
  iface.launch()
 
1
  import gradio as gr
2
  import numpy as np
3
  from PIL import Image
 
 
 
 
 
 
 
4
 
5
  def preprocesar_imagen(image):
6
+ # El canvas da una imagen, NO vectores, as铆 que procesamos como antes
7
  if isinstance(image, np.ndarray):
8
  image = Image.fromarray(image)
9
  image = image.convert('L')
 
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, arr_bin
15
 
16
  def predict(image):
17
+ arr, img_proc = preprocesar_imagen(image)
18
+ preds = model.predict(arr)
19
  class_idx = np.argmax(preds)
20
+ return {etiquetas[class_idx]: float(preds[0][class_idx])}, img_proc
21
 
22
  iface = gr.Interface(
23
  fn=predict,
24
+ inputs=gr.Sketchpad(shape=(256, 256), label="Dibuja aqu铆", brush=10, bg_color="white"),
25
+ outputs=[
26
+ gr.Label(num_top_classes=1, label="Predicci贸n"),
27
+ gr.Image(label="Imagen preprocesada (0-1)")
28
+ ],
29
+ title="Dibujo estilo QuickDraw",
30
+ description="Dibuja, se procesa como 28x28 para la IA."
31
  )
32
 
33
  iface.launch()