本文介绍了CV2图像错误:错误:(-215:声明失败)函数'cv :: resize'中的!ssize.empty()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用TensorFlow和Python进行图像分类,但是使用CV2读取图像时出现错误.我是CV2的新手,但是我找不到能够充分解决我问题的方法.谁能解释如何解决这个问题?

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

这是我得到的错误:

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'

推荐答案

检查您要在for循环中尝试迭代的文件夹中是否存在 _DS_Store 文件,因为它不是图像文件cv2.resize()无法调整大小.

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.

这篇关于CV2图像错误:错误:(-215:声明失败)函数'cv :: resize'中的!ssize.empty()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 11:23