I am having trouble with this error (-215:Assertio

2020-06-22 19:44发布

问题:

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:

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

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

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.

回答1:

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

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.



回答2:

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



回答3:

One more possibility is that the image width or height might be zero:

print(image.shape())
# (300, 0) -> Incorrect!
# (0, 400) -> Incorrect!

# (300, 400) -> Correct!
# (400, 300) -> Correct!

This might happen when you try to resize an image after applying some image processing techniques (say, using OpenCV) and, height or width of your image may become 0.