jasmcaus / opencv-course

Learn OpenCV in 4 Hours - Code used in my Python and OpenCV course on freeCodeCamp.
https://youtu.be/oXlwWbU8l2o
MIT License
1.11k stars 955 forks source link

(-215:Assertion failed) s >= 0 in function 'cv::setSize' #10

Closed vivekthedev closed 3 years ago

vivekthedev commented 3 years ago

I was following your Youtube tutorial but got stuck in this particular problem. I searched a lot but couldn't find any solution.

Video with Timestamp - https://youtu.be/oXlwWbU8l2o?t=10728

Traceback (most recent call last): File "face_recognize.py", line 43, in <module> face_recognizer.train(features, labels) cv2.error: OpenCV(4.4.0) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-8ely825f\opencv\modules\core\src\matrix.cpp:235: error: (-215:Assertion failed) s >= 0 in function 'cv::setSize'

jasmcaus commented 3 years ago

Could you verify that features and labels are both Numpy arrays (use type(features) and type(labels)) and check that they both have the same number of elements?

vivekthedev commented 3 years ago

Could you verify that features and labels are both Numpy arrays (use type(features) and type(labels)) and check that they both have the same number of elements?

My code is here https://github.com/bradtoxic/open-cv-face-detection/blob/main/face-detection.py Line 37 and Line 38 for features and labels

jasmcaus commented 3 years ago

LGTM. However, still, verify the above. Comment out lines 37-47 and add the following:

print(features.shape)
print(type(features))
print(labels.shape)
print(type(labels)) 

Let me know what you get

vivekthedev commented 3 years ago

LGTM. However, still, verify the above. Comment out lines 37-47 and add the following:

print(features.shape)
print(type(features))
print(labels.shape)
print(type(labels)) 

Let me know what you get

Traceback (most recent call last): File "d:\Work\py\opencv\advanced\face_recognize.py", line 36, in <module> print(features.shape) AttributeError: 'list' object has no attribute 'shape'

This error popped up.

jasmcaus commented 3 years ago

Ah yes, my bad. Replace the .shape with len(). Like: len(features) and len(labels

vivekthedev commented 3 years ago

Ah yes, my bad. Replace the .shape with len(). Like: len(features) and len(labels

Ok 24 <class 'list'> 24 <class 'list'>

I have 24 Images (Faces in my faces folder)

jasmcaus commented 3 years ago

Your issue is here: https://github.com/bradtoxic/open-cv-face-detection/blob/7263af8229394674371c28c1a9be8c637714bd4e/face-detection.py#L29

You've written

gray[x:y+h, x:x+w]

instead of

faces_roi = gray[y:y+h, x:x+w]

You're attempting to access a negative index, which is not possible.

At first, I thought it had something to do with a NoneType return, so hence the above tests.

vivekthedev commented 3 years ago

Your issue is here: https://github.com/bradtoxic/open-cv-face-detection/blob/7263af8229394674371c28c1a9be8c637714bd4e/face-detection.py#L29

You've written

gray[x:y+h, x:x+w]

instead of

faces_roi = gray[y:y+h, x:x+w]

You're attempting to access a negative index, which is not possible.

At first, I thought it had something to do with a NoneType return, so hence the above tests.

It's working now. Thank you so much