0

I have a dataset of training images and test images. What I want to do is feed in the training images and resize them to 150x150 size. Then, depending on the class name of the image file, append a label to the array 'y', which is my array of labels.

However, I get this error message:

OpenCV(4.2.0) /io/opencv/modules/imgproc/src/resize.cpp:4045: error: (-215:Assertion failed) !ssize.empty() in function 'resize'

Relevant Part of my code is as follows:

nrows = 150
ncolumns = 150
channels = 3

def read(imgarray):
    x = []
    y = []

    for image in imgarray:
        try:
            x.append(cv2.resize(cv2.imread(image, cv2.IMREAD_COLOR), (nrows,ncolumns), interpolation=cv2.INTER_CUBIC))
        except Exception as e:
            print(str(e))
        if 'chicken' in image:
            y.append(0)
        elif 'cat' in image:
            y.append(1)
        elif 'scoop' in image:
            y.append(2)

    return x,y
x,y = read(train_images) #train_images is composed of ~5400 images, of mixed sizes and image formats

Please can someone tell me why CV2 isn't 'seeing' the images and how I can get the images to be resized?

edit: an example image name is '../input/train/train/chicken (1438).jpg' and the image shape is (340,594, 3)

I am using a Kaggle kernel where my training images and testing images are stored in a directory called 'input'. Training images are in input/train/train/img.jpg and testing images are in input/test/img2.jpg.

Update: when I tried to display the images in train_images:

for image in imgarray:
        #print(image)
        image = mpimg.imread(image)
        showplot = plt.imshow(image)

I got this error:

<built-in function imread> returned NULL without setting an error

which is odd as this previous code worked perfectly fine, displaying the images:

import matplotlib.image as mpimg
for i in train_images[0:3]:
    img=mpimg.imread(i)
    imgplot = plt.imshow(img)
    plt.show()

Update: When I output the images that cause an error, I get this:

Please check this image, has some issues ../input/train/train/scoop (1360).jpg
OpenCV(4.2.0) /io/opencv/modules/imgproc/src/resize.cpp:4045: error: (-215:Assertion failed) !ssize.empty() in function 'resize'

so it seems that an image that should work doesn't for some reason enter image description here

This is one of the images that threw an error: enter image description here

Swalkr
  • 56
  • 7
  • Be careful about using `except Exception` like that, see https://stackoverflow.com/questions/54948548/what-is-wrong-with-using-a-bare-except. – AMC Mar 28 '20 at 18:24
  • you rules out the obvious, already? That is, print the name of the images as you process them in the loop and make sure each one of them is a good file. – Daemon Painter Mar 28 '20 at 18:25
  • @AMC Even when I didn't use try/except, I still got the error from CV2... – Swalkr Mar 28 '20 at 18:27
  • @DaemonPainter yes, they seem fine, so I'm quite confused – Swalkr Mar 28 '20 at 18:29
  • @DaemonPainter an example image name is '../input/train/train/chicken (1438).jpg' and the image shape is (340,594, 3) – Swalkr Mar 28 '20 at 18:33
  • @DaemonPainter thank you for your reply, my apologies - I don't understand, what do you mean by your sentence "Your script is in a folder that is parallel to /input, in the end."? – Swalkr Mar 28 '20 at 18:38
  • Please see [this question](https://stackoverflow.com/questions/52162004/i-am-having-trouble-with-this-error-215assertion-failed-ssize-empty-in-fu) as well. – Daemon Painter Mar 28 '20 at 18:42
  • @DaemonPainter I'm working in a Kaggle kernel, and as far as I know I only have this directory: input/train/train/img.jpg and input/test/img2.jpg. My apologies if I have got this wrong, I am new to using a Kaggle kernel. – Swalkr Mar 28 '20 at 18:43
  • @DaemonPainter I've had a look at that question, unfortunately it seems very similar to mine yet mine does not work whereas theirs does. – Swalkr Mar 28 '20 at 18:45
  • Not an expert on cv2, but should the resize tuple include the color dimension? (nrows,ncolumns, 3) – Stefan Mar 28 '20 at 18:58
  • @Stefan I added it in, no difference unfortunately – Swalkr Mar 28 '20 at 19:05
  • There is some image in the folder with unsupported format/ corrupted. – Zabir Al Nazi Mar 28 '20 at 19:06
  • Try to run in a loop and see where the bug happens, look into that image. – Zabir Al Nazi Mar 28 '20 at 19:06
  • @furcifer do you know which image formats are unsupported? I can delete those and see if it helps – Swalkr Mar 28 '20 at 19:09
  • It's hard to tell, sometimes there are just corrupted images. – Zabir Al Nazi Mar 28 '20 at 19:13

1 Answers1

0

Simply, ignore images with issues.

The way you are adding labels, even if some images are missed the labels will be added to the array.

nrows = 150
ncolumns = 150
channels = 3

def read(imgarray):
    x = []
    y = []

    for image in imgarray:
        try:
            im = cv2.resize(cv2.imread(image), (nrows,ncolumns)
            x.append(im)
            print(type(im))
            print(im.shape)
            if 'chicken' in image:
                y.append(0)
            elif 'cat' in image:
                y.append(1)
            elif 'scoop' in image:
                y.append(2)
        except Exception as e:
            print(str(e))
            print(f'Please check this image, has some issues {image}')


    return x,y

x,y = read(train_images) #train_images is composed of ~5400 images, of mixed sizes and ima
Zabir Al Nazi
  • 10,298
  • 4
  • 33
  • 60
  • I've tried your code and updated my question, it seems that a lot of images are throwing up errors – Swalkr Mar 28 '20 at 19:28
  • Can you post the errors? also post the screenshot, the snaps of the images which are throwing errors? – Zabir Al Nazi Mar 28 '20 at 19:36
  • Can you actually check this image as I said? Is it a valid image? Post a screenshot of the image, if it is not a valid image, there is nothing to do except ignoring those cases. – Zabir Al Nazi Mar 28 '20 at 20:14
  • It's definitely a valid image, so I'm puzzled by what the problem is that stops it being read – Swalkr Mar 30 '20 at 14:55