• Register
0 votes
4k views

Problem :

I am beginner to opencv and keras on python. Currently I am trying to apply the Keras' image classifier to my existing project, but I am facing below error.

Error: (-215:assertion failed) !ssize.empty() in function 'cv::resize'

Earlier with the same code I was able to use OpenCV to read and train my images, but recently I switching to the new batch of images and so I am facing above error. So I guess there is something wrong with the file type

Below is from a batch which 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

Below is from a batch that was error free:

bear.290.jpg: JPEG image data, JFIF standard 1.01, aspect ratio, density 1x1, segment length 16, baseline, precision 8, 224x224, frames 3

My file type are exactly the same so now I am confused. Can somebody help me in fixing my problem ?

2.3k points

Please log in or register to answer this question.

2 Answers

0 votes

Solution :

I have gone through your question and I think solution for your problem is pretty simple.

You just need to add a try/exception so your code can bypass problematic images:

try:
    mypath=os.path.join(mypath,n)
    myimg=cv2.imread(mypath, cv2.IMREAD_GRAYSCALE)
   my img=cv2.resize(myimg, (img_rows,img_cols))

except Exception as e:
    print(str(e))

Hope it fixes your problem

5k points
0 votes

You might want to check bounds of detected object’s box.

I faced the same error and the cause was that bounds were either negative or more than image size. ( Simply put, a portion of detected object was lying outside image. )


edited