简体   繁体   中英

Why is CV2 not reading the images?

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在此处输入图片说明

This is one of the images that threw an error: 在此处输入图片说明

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM