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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -6
app.py CHANGED
@@ -7,14 +7,17 @@ from PIL import Image
7
  model = tf.keras.models.load_model("mnist_model.h5")
8
 
9
  def predict(image):
10
- # Convert numpy array (from Sketchpad) to PIL
11
- image = Image.fromarray(image.astype("uint8"), "RGB")
12
-
13
- # Convert to grayscale and resize
14
- image = image.convert("L").resize((28,28))
 
 
 
15
  image = np.array(image) / 255.0
16
  image = image.reshape(1, 28, 28, 1)
17
-
18
  # Predict
19
  prediction = model.predict(image)
20
  return str(np.argmax(prediction))
 
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)
20
+
21
  # Predict
22
  prediction = model.predict(image)
23
  return str(np.argmax(prediction))