Josephus67 commited on
Commit
634fbce
·
verified ·
1 Parent(s): 8d6ca0f

switched from using sketchpad

Browse files
Files changed (1) hide show
  1. app.py +6 -11
app.py CHANGED
@@ -7,13 +7,9 @@ from PIL import Image
7
  model = tf.keras.models.load_model("mnist_model.h5")
8
 
9
  def predict(image):
10
- # Extract numpy array if dict
11
- if isinstance(image, dict) and "image" in image:
12
- image = image["image"]
13
-
14
- # Convert to PIL
15
- image = Image.fromarray(image.astype("uint8")).convert("L").resize((28, 28))
16
-
17
  # Normalize
18
  image = np.array(image) / 255.0
19
  image = image.reshape(1, 28, 28, 1)
@@ -22,12 +18,11 @@ def predict(image):
22
  prediction = model.predict(image)
23
  return str(np.argmax(prediction))
24
 
25
- # Gradio interface
26
  iface = gr.Interface(
27
  fn=predict,
28
- inputs=gr.Sketchpad(),
29
- outputs="label",
30
- live=True
31
  )
32
 
33
  iface.launch()
 
7
  model = tf.keras.models.load_model("mnist_model.h5")
8
 
9
  def predict(image):
10
+ # Ensure PIL image, convert to grayscale, resize
11
+ image = image.convert("L").resize((28, 28))
12
+
 
 
 
 
13
  # Normalize
14
  image = np.array(image) / 255.0
15
  image = image.reshape(1, 28, 28, 1)
 
18
  prediction = model.predict(image)
19
  return str(np.argmax(prediction))
20
 
21
+ # Gradio interface (upload instead of sketchpad)
22
  iface = gr.Interface(
23
  fn=predict,
24
+ inputs=gr.Image(type="pil", image_mode="L", shape=(28,28)),
25
+ outputs="label"
 
26
  )
27
 
28
  iface.launch()