-1

Image comes from the front end in PIL I preprocess it but it giving me a different shape than expected.

my code is

def preprocess(img):
    img = np.array(img)
    resized = cv2.resize(img, (254, 254))
    img = tf.keras.preprocessing.image.img_to_array(resized)/255
    img = np.array([img])
    
    return img

this is a pil image

<PIL.JpegImagePlugin.JpegImageFile image mode=L size=2144x1805 at 0x229615E2488>

And when I preprocess it, it gives this shape

the shape of the test image is  (1, 254, 254, 1)

and when I try the preprocess code outside of my project it works fine.

Hamza.S
  • 1,319
  • 9
  • 18

1 Answers1

-1

This means that your Network needs an image not in grayscale colors and you're unconsciously trimming that info. I suggest you to try resized = cv2.resize(img, (254, 254, 3)) instead of resized = cv2.resize(img, (254, 254)). Because otherwise your telling to the resize method implicitly that you want an image on grayscale and you'll miss that last dimension.

  • 2
    this answer contains an actual error in the usage of `resize`. don't post answers if you neither know how those APIs need to be used nor bother to look the documentation up. – Christoph Rackwitz Sep 07 '21 at 20:13
  • I'm sorry for that, I must admit there's been a while since I used opencv. @ChristophRackwitz please, help the guy for me. – Luis Ernesto Martinez Sep 30 '21 at 00:24