PacktPublishing/OpenCV-3-x-with-Python-By-Example

SystemError: <class 'cv2.CascadeClassifier'> returned a result with an error set

mayanktiwariiiitdmj opened this issue ยท 16 comments

Hello,

I am a beginner in OpenCV Python. I am getting the following error during the execution of the ear_detection.py program. Please help me.

**cv2.error: OpenCV(4.1.1) C:\projects\opencv-python\opencv\modules\objdetect\src\cascadedetect.cpp:570: error: (-2:Unspecified error) in function 'bool _cdecl cv::HaarEvaluator::Feature::re
ad(const class cv::FileNode &,const class cv::Size
&)'

Invalid HAAR feature (expected: 'rw.r.y + rw.r.height <= H'), where
'rw.r.y + rw.r.height' is 18
must be less than or equal to
'H' is 12

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "init_ear_detection.py", line 4, in
left_ear_cascade = cv2.CascadeClassifier('./cascade_files/haarcascade_mcs_leftear.xml')
SystemError: <class 'cv2.CascadeClassifier'> returned a result with an error set**

Same issue I'm facing by using the new self trained classifier or if i'm keeping that xml in the same project folder(root directory of my project)

Same problem here, but I am using a mouth classifier

i think you downloaded the broken haarcascade file

i think you downloaded the broken haarcascade file

How do you fix that? I have tried all the available mouth cascades available on the internet

mouth_cascade = cv2.CascadeClassifier(r'C:\Python37\Lib\site-packages\cv2\data\haarcascade_mcs_mouth.xml')

This line of code worked for me.
You don't need to download or copy the .xml files. You can simply use the packaged path to the installed cascades.

Even this can be simplified, instead of trying to figure out where the .xml files are, a property of cv2 can be used.

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')

becomes

face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_eye.xml')

Even this can be simplified, instead of trying to figure out where the .xml files are, a property of cv2 can be used.

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')

becomes

face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_eye.xml')

is there any mouth cascade then?
I tried
mouth_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_mouth_mcs.xml') but still returns an error for me

Run this code
mouth_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_mcs_mouth.xml')
test = mouth_cascade.load('haarcascade_mcs_mouth.xml')
print(test)

If the test returns False, it means the script is failing to read the .XML file.

You can try keeping the .XML file in the same directory as your python script and see if it works out.
mouth_cascade = cv2.CascadeClassifier('haarcascade_mcs_mouth.xml')

mouth_cascade = cv2.CascadeClassifier(r'C:\Python37\Lib\site-packages\cv2\data\haarcascade_mcs_mouth.xml')

This line of code worked for me.
You don't need to download or copy the .xml files. You can simply use the packaged path to the installed cascades.

@abdulahad01 Try this as well.

@Akshat1305 thank you, your solution works in my code

@abdulahad01 hi did your mouth cascade work? im having the same problem and i do not know hot to fix it.

@Akshat1305

mouth_cascade = cv2.CascadeClassifier(r'C:\Python37\Lib\site-packages\cv2\data\haarcascade_mcs_mouth.xml')

This line of code worked for me.
You don't need to download or copy the .xml files. You can simply use the packaged path to the installed cascades.

Hi i have tried this line of code but it still didnt work, and i do not know whats wrong. i am using python in jupyter lab btw.

Even this can be simplified, instead of trying to figure out where the .xml files are, a property of cv2 can be used.

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')

becomes

face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml') eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_eye.xml')

when i try this one i got assertion error.
and i provided proper path of my directory.

Hello,

I am a beginner in OpenCV Python. I am getting the following error during the execution of the ear_detection.py program. Please help me.

**cv2.error: OpenCV(4.1.1) C:\projects\opencv-python\opencv\modules\objdetect\src\cascadedetect.cpp:570: error: (-2:Unspecified error) in function 'bool _cdecl cv::HaarEvaluator::Feature::re ad(const class cv::FileNode &,const class cv::Size &)'

Invalid HAAR feature (expected: 'rw.r.y + rw.r.height <= H'), where
'rw.r.y + rw.r.height' is 18
must be less than or equal to
'H' is 12

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File "init_ear_detection.py", line 4, in left_ear_cascade = cv2.CascadeClassifier('./cascade_files/haarcascade_mcs_leftear.xml') SystemError: <class 'cv2.CascadeClassifier'> returned a result with an error set**

you can try this one.
left_ear_cascade = cv2.CascadeClassifier('./cascade_files\haarcascade_mcs_leftear.xml')
this work for me

Does anyone have the Swimming People Cascades XML file?

I searched for it everywhere but all are broken.