29

I am trying to apply a Keras' image classifier to my project, but down the road I got stuck with this. Though previously, with the same code I could use OpenCV to read and train images, but after switching to a new batch of images it got caught with the error. So my speculation is that there's something wrong with my file type:

This is from the batch that got the error:

traf.204.jpg: JPEG image data, JFIF standard 1.01, aspect ratio, density 1x1, segment length 16, baseline, precision 8, 480x294, frames 1

This is from the batch that didn't get caught with the error:

bear.290.jpg: JPEG image data, JFIF standard 1.01, aspect ratio, density 1x1, segment length 16, baseline, precision 8, 224x224, frames 3

But the file type seems to be exactly the same (except for the resolution). How can I fix this problem?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Tsoi Phu
  • 766
  • 2
  • 8
  • 11

9 Answers9

33

I was supposed to add a try/exception so my code could bypass "ugly" images:

try:
    path=os.path.join(mypath, n)
    img=cv2.imread(path, cv2.IMREAD_GRAYSCALE)
    img=cv2.resize(img, (img_rows, img_cols))

except Exception as e:
    print(str(e))

cv2.resize() was where it was supposed to catch the error since it couldn't resize a "broken" image.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Tsoi Phu
  • 766
  • 2
  • 8
  • 11
9

One more possibility: width or height of the image might be zero:

print(image.shape())
# (300, 0) -> Incorrect!
# (0, 400) -> Incorrect!

# (300, 400) -> Correct!
# (400, 300) -> Correct!

This might happen when you try to resize an image after applying some image processing techniques (say, using OpenCV) and, height or width of your image may become 0.

Scott
  • 4,974
  • 6
  • 35
  • 62
1

Just enter the precise image format. .jpg instead of .jpeg.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ansh Desai
  • 27
  • 1
1

In my case there were special characters in the file name. Like "ü", "ä", "/", etc.

I had to make a choice: Renaming automatically or just raise an error for the user to know, that the pictures have bad names.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mika
  • 11
  • 4
0

This issue is caused because after resizing an image the height or width becomes 0. I had tackled this by just printing the image name before resizing. So I can get to the root cause that due to which image this issue is occurring and just deleted that image from my dataset. Below is the code how I solved this using debugging:

# loop over the input images
for imagePath in imagePaths:
    # load the image, pre-process it, and store it in the data list
    print(imagePath)  #here I have printed the imagename
    image = cv2.imread(imagePath)
    image = cv2.resize(image, (28, 28))   # 28, 28
    image = img_to_array(image)
    data.append(image)
0

For me, I had my camera app open in Windows 10, and that's why I was getting that error. I shut it down and reran my code and it worked.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Huzaifa
  • 345
  • 4
  • 15
0

This issue could be due to wrong image type passed while resizing an image.
The solution is to add a check before resizing the image:

if(type(image) == type(None)):
    pass
else:
    image = cv2.resize(image, (h, w), interpolation=cv2.INTER_AREA)
sauravjoshi23
  • 837
  • 11
  • 9
0

I was facing a similar problem because I was reading multiple images from a directory and that directory had a pdf file(silly), so make sure your specified paths have only images.

suraj
  • 1
-1

Put double backslashes in your path inside the imread function.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131