Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
#
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
| 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))
|