cisemh commited on
Commit
f715dac
·
verified ·
1 Parent(s): a08ac70

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -12
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
- # Convert the input image to a NumPy array if needed
24
- if not isinstance(img, np.ndarray):
25
- img = np.array(img)
26
-
27
- # Print the type and shape of the image
28
- print(type(img), img.shape)
29
-
30
- # Resize the image
31
- img = cv2.resize(img, (img_size, img_size))
32
- img = img.reshape(1, img_size, img_size, 1)
33
- preds = model.predict(img)[0]
34
- return {label: float(pred) for label, pred in zip(labels, preds)}
 
 
 
 
 
 
 
 
 
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)