Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CV2 Image Error: error: (-215:Assertion failed) !ssize.empty() in function 'cv::resize'

I'm making an image classifier with TensorFlow and Python, but I'm having an error reading images with CV2. I'm very new to CV2, and I haven't been able to find anything that sufficiently solves my problem. Can anyone explain how to solve this?

def train_data_with_label():
  train_images = []
  for i in tqdm(os.listdir(train_data)):
    path = os.path.join(train_data, i)
    img = cv2.imread(path, 3)
    img = cv2.resize(img, (64,64))
    train_images.append([np.array(img), one_hot_label(i)])
  shuffle(train_images)
  return train_images
  
def test_data_with_label():
  test_images = []
  for i in tqdm(os.listdir(test_data)):
    path = os.path.join(test_data, i)
    img = cv2.imread(path, 3)
    img = cv2.resize(img, (64,64))
    test_images.append([np.array(img), one_hot_label(i)])
  shuffle(test_images)
  return test_images

This is the error I got:

Using TensorFlow backend.
  0%|                                                                                            | 0/2 [00:00<?, ?it/s]
Traceback (most recent call last):
  File "retrain.py", line 47, in <module>
    training_images = train_data_with_label()
  File "retrain.py", line 32, in train_data_with_label
    img = cv2.resize(img, (64,64))
cv2.error: OpenCV(4.1.1) C:\projects\opencv-python\opencv\modules\imgproc\src\resize.cpp:3720: error: (-215:Assertion failed) !ssize.empty() in function 'cv::resize'
like image 558
Will Downs Avatar asked Oct 04 '19 20:10

Will Downs


People also ask

How to fix CV2 error if image size is invalid?

Note if you remove the cv2.resize (), an exception will not be thrown. To fix your problem, you can use cv2.error and a try/except block to catch the exception import cv2 image = cv2.imread ('noexist.jpg') try: resize = cv2.resize (image, (64,64)) except cv2.error as e: print ('Invalid frame!') cv2.waitKey ()

Why is my resize not getting the image I want?

This type of error also takes place because the resize is unable to get the image in simple the directory of the image may be wrong.In my case I left the forward slash during providing the location of file and this error took place after I put the slash problem was solved.

Why is my IMG variable empty?

Your img variable is empty, you probably didn’t load the image correctly. Try printing your img.shape after you load your image. Then have a look at the path you specified, there is probably something wrong with it.

What does “ssize empty” mean?

have you found the solution I’m not sure, as the OP did not post any code for context, but I’d guess that !ssize.empty () means that reading in the image resulted in an empty object. And what was the solution?


2 Answers

Your imread() call failed. It returned None.

When you try to imread() a file that doesn't exist, or it's not a proper picture file, imread signals that by returning None (in Python), instead of a proper numpy array.

Now, when you pass this None to resize(), resize() notices and throws this error. It tries to check if the argument is a proper array, and that it's not an empty array. In this case, your argument isn't even an array, but the error is the same.

How to fix this: check if the result of imread() is None. If it is None, reading failed. Just continue the loop on to the next picture file name.

img = cv2.imread(path)
if img is None:
    continue

by the way: the 3 in the second argument to imread means IMREAD_COLOR | IMREAD_ANYDEPTH

like image 64
Christoph Rackwitz Avatar answered Sep 22 '22 18:09

Christoph Rackwitz


check if there _DS_Store file in the folder you're trying to iterate in the for loop, as it is not a image file cv2.resize() is not able to resize it.

like image 34
radhakrishnan rayaprolu Avatar answered Sep 22 '22 18:09

radhakrishnan rayaprolu