Code Error in OpenCV

when I am testing this model on multiple faces at one time, it is slow to recognize faces and moreover I’m very frequently getting this error

Traceback (most recent call last):
File “face_recognition.py”, line 93, in
face_section = cv2.resize(face_section,(100,100))
cv2.error: OpenCV(4.1.0) C:\projects\opencv-python\opencv\modules\imgproc\src\resize.cpp:3718:
error: (-215:Assertion failed) !ssize.empty() in function ‘cv::resize’

[ WARN:0] terminating async callback

I couldn’t understand why this error is coming.
Some problem in cv2.reshape() ???
It doesn’t happen every time, How to resolve this for permanent.

This error is raised when you provide an empty array to the cv2.resize function. Please provide your code so that i can guide you properly.
If am not wrong about what you might have done, i guess, when your are localising faces, there will be some frame in which the algorithm is not able to detect face, at that time, the face_section is empty, hence when calling you are getting this error. If this is the case, a simple if face_section is not None: statement above this function call would fix this error.

Thanks :slight_smile:

But how can this be possible that face_section is empty (does not have any face) if this would be the case then faces variable initially would have length 0 and skipped the loop iteration.
first, we are taking all the faces in “faces” variable, and if it’s len is 0 it skip the iteration and reiterate to find faces array.
if we got some faces in array, then face_section will also have some face. then there is no point of getting empty in face_section. then how this cv2.resize() is causing error…

Code is :- https://ide.codingblocks.com/s/87655

I’ve add this to my code (if face_section is not None:)

Hello @mohituniyal2010, as i said in my answer that it was a guess into what you might have done wrong. Here in your code i can see that when there is no face then the loop is reiterated without going into that portion of code that raises the error.
Hence, the problem is arising when you are actually slicing the array, that is all that i can see in your code that might induce the empty error.
Can you please tell me, does the error has gone after introducing the if statment. Also, at what scenerios were the error coming at the first place, my guess again is when the face came too close to the camera or when it went too close to the boundary. Could you please confirm this and get back here.

Thanks :slight_smile:

Hi,
even after using if face_section is not None the error still persists because of the empty error caused by slicing statement. To avoid this problem is used try except block.

Coming to your ques, at what scenarios were the error coming in the first place.

I couldn’t figure it out, because it works very slow in my laptop, it lags very much while recognizing the faces, so I can’t tell when the error actually occurred, I get response 2-3 seconds late.

Face detection works very smooth, but recognizing face lags a lot.

Q-> Is it because of the knn algorithm, that calculates the distance of an image with all other images having 30000features on every frame.

Hi @mohituniyal2010,
Could you please try removing the try except statement with if np.all(np.array(face_section.shape)):.
Also, its not because of the knn. face_section is the problem here.
You can also try printing face_section.shape to see whether the shapes are approriate or not. Most probably, you can see a 0 shape array when the error occurs.

Thanks :slight_smile:

correct, getting shape (67,0,3) for face_section, that was causing error.
if np.all(np.array(face_section.shape)): this statement solved the problem,
I got it now…
Thank you very much :slight_smile:

i also got this problem. can i know what is the solution? thank you!

cv2.error: OpenCV(4.2.0) /io/opencv/modules/imgproc/src/resize.cpp:4045: error: (-215:Assertion failed) !ssize.empty() in function ‘resize’