Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
@@ -20,18 +20,27 @@ model = tf.keras.models.load_model("number_recognition_model_colab.keras")
|
|
20 |
|
21 |
|
22 |
def predict(img):
|
23 |
-
|
24 |
-
|
25 |
-
img
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
|
37 |
label = gr.Label(num_top_classes=3)
|
|
|
20 |
|
21 |
|
22 |
def predict(img):
|
23 |
+
try:
|
24 |
+
# Convert the input image to a NumPy array if needed
|
25 |
+
if not isinstance(img, np.ndarray):
|
26 |
+
img = np.array(img)
|
27 |
+
|
28 |
+
# Ensure the image has a single channel (grayscale)
|
29 |
+
if img.ndim == 2:
|
30 |
+
img = np.expand_dims(img, axis=-1)
|
31 |
+
|
32 |
+
# Print the type and shape of the image
|
33 |
+
print(type(img), img.shape)
|
34 |
+
|
35 |
+
# Resize the image
|
36 |
+
img = cv2.resize(img, (img_size, img_size))
|
37 |
+
img = img.reshape(1, img_size, img_size, 1)
|
38 |
+
preds = model.predict(img)[0]
|
39 |
+
return {label: float(pred) for label, pred in zip(labels, preds)}
|
40 |
+
except Exception as e:
|
41 |
+
# Print the exception to the console
|
42 |
+
print(f"Error during prediction: {e}")
|
43 |
+
return {"Error": str(e)}
|
44 |
|
45 |
|
46 |
label = gr.Label(num_top_classes=3)
|