hb-setosys commited on
Commit
7b7ada0
·
verified ·
1 Parent(s): d70585e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -3
app.py CHANGED
@@ -8,12 +8,25 @@ model = tf.keras.models.load_model("denis_mnist_cnn_model.h5")
8
 
9
  # Preprocessing function for images
10
  def preprocess_image(image):
 
 
 
 
 
 
 
 
11
  # Resize the image to 28x28 as expected by the model
12
- image = np.array(image)
13
  image = tf.image.resize(image, (28, 28)) # Resize to 28x28
 
 
14
  image = tf.image.grayscale_to_rgb(image) # Convert grayscale to RGB (3 channels)
15
- image = image / 255.0 # Normalize pixel values to [0, 1]
16
- image = np.expand_dims(image, axis=0) # Add batch dimension
 
 
 
 
17
  return image
18
 
19
  # Function to make predictions
 
8
 
9
  # Preprocessing function for images
10
  def preprocess_image(image):
11
+ # Convert PIL image to a tensor
12
+ image = tf.convert_to_tensor(image)
13
+
14
+ # Check if the image has a single channel (grayscale) and reshape if needed
15
+ if image.shape[-1] != 1:
16
+ # Convert to grayscale if the image is not in grayscale format (e.g., RGB)
17
+ image = tf.image.rgb_to_grayscale(image)
18
+
19
  # Resize the image to 28x28 as expected by the model
 
20
  image = tf.image.resize(image, (28, 28)) # Resize to 28x28
21
+
22
+ # Convert grayscale to RGB (3 channels)
23
  image = tf.image.grayscale_to_rgb(image) # Convert grayscale to RGB (3 channels)
24
+
25
+ # Normalize pixel values to [0, 1]
26
+ image = image / 255.0
27
+
28
+ # Add batch dimension (model expects batch of images)
29
+ image = tf.expand_dims(image, axis=0)
30
  return image
31
 
32
  # Function to make predictions