cisemh commited on
Commit
a2c1754
·
verified ·
1 Parent(s): 1b42d3a

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -7
app.py CHANGED
@@ -18,23 +18,34 @@ labels = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight"
18
 
19
  model = tf.keras.models.load_model("number_recognition_model_colab.keras")
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
 
40
  # Print the predictions
@@ -46,8 +57,7 @@ def predict(img):
46
  print(f"Error during prediction: {e}")
47
  return {"Error": str(e)}
48
 
49
-
50
  label = gr.Label(num_top_classes=3)
51
 
52
  interface = gr.Interface(fn=predict, inputs="sketchpad", outputs=label, title=title, description=head, article=ref)
53
- interface.launch()
 
18
 
19
  model = tf.keras.models.load_model("number_recognition_model_colab.keras")
20
 
 
21
  def predict(img):
22
  try:
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 shape and type of the input image
28
+ print(f"Initial image type: {type(img)}, shape: {img.shape}")
29
+
30
+ # Ensure the image is in grayscale and has a single channel
31
+ if img.ndim == 3 and img.shape[-1] == 3:
32
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
33
+ elif img.ndim == 2:
34
  img = np.expand_dims(img, axis=-1)
35
 
36
+ # Print the shape of the grayscale image
37
+ print(f"Grayscale image shape: {img.shape}")
38
 
39
  # Resize the image
40
  img = cv2.resize(img, (img_size, img_size))
41
+
42
+ # Normalize the image
43
+ img = img.astype('float32') / 255.0
44
  img = img.reshape(1, img_size, img_size, 1)
45
+
46
+ # Print the shape after resizing and normalizing
47
+ print(f"Processed image shape: {img.shape}")
48
+
49
  preds = model.predict(img)[0]
50
 
51
  # Print the predictions
 
57
  print(f"Error during prediction: {e}")
58
  return {"Error": str(e)}
59
 
 
60
  label = gr.Label(num_top_classes=3)
61
 
62
  interface = gr.Interface(fn=predict, inputs="sketchpad", outputs=label, title=title, description=head, article=ref)
63
+ interface.launch(debug=True)