Python 我遇到了这个错误(-215:Assertion failed)!ssize.empty() in function 'resize' in opencv

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52162004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 20:02:45  来源:igfitidea点击:

I am having trouble with this error (-215:Assertion failed) !ssize.empty() in function 'resize' in opencv

pythonopencvkeras

提问by Tsoi Phu

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:

我正在尝试将 Keras 的图像分类器应用于我的项目,但在此过程中我陷入了困境。虽然以前使用相同的代码我可以使用 OpenCV 来读取和训练图像,但是在切换到一批新图像后,它被错误捕获。所以我的猜测是我的文件类型有问题:

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

traf.204.jpg:JPEG 图像数据,JFIF 标准 1.01,纵横比,密度 1x1,段长 16,基线,精度 8,480x294,帧 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

bear.290.jpg:JPEG 图像数据,JFIF 标准 1.01,纵横比,密度 1x1,段长 16,基线,精度 8,224x224,帧 3

But the file type seems to be exactly the same (except for the resolution). Please help me with this problem, I am still new to opencv and keras on python.

但是文件类型似乎完全一样(分辨率除外)。请帮我解决这个问题,我还是 Python 上的 opencv 和 keras 的新手。

回答by Tsoi Phu

Thanks guys, I figured it out, I was supposed to add a try/exception so my code could bypass "ugly" images:

谢谢大家,我想通了,我应该添加一个 try/exception 以便我的代码可以绕过“丑陋”的图像:

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 caught the error since it can't resize a "broken" image.

cv2.resize() 是它应该捕获错误的地方,因为它无法调整“损坏”图像的大小。

回答by Ansh Desai

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

只需输入精确的图像格式。.jpg 而不是 .jpeg