Solving Python OpenCV Error: Assertion failed (!empty()) in cv::CascadeClassifier::detectMultiScale ( when using face-detection )

Python OpenCV Error: Assertion failed (!empty()) in cv::CascadeClassifier::detectMultiScale ( when using face-detection ) is an error which occurs because OpenCV cannot find xml files when using cascade classifier.

I’m going to provide a detailed and clear explanation of why this error is happening and how to solve it, I am also going to present other ways to get rid of this problem for good.

Exploring Python OpenCV Error: Assertion failed (!empty()) in cv::CascadeClassifier::detectMultiScale ( when using face-detection )

This is an error which occurs because OpenCV cannot find xml files when using cascade classifier.

You should avoid mixing between different errors. The error message should look like the error message bellow.

                                                                       #
OpenCV Error: Assertion failed (!empty()) in cv::CascadeClassifier::detectMultiScale
                                                                       #

Bellow, we will describe how the error can be solved. With multiple possible methods.

Solution 1 : Correctly use absolute paths with xml files and cv2.CascadeClassifier

When OpenCV cannot find xml files when using cascade classifier, you will without a doubt end up with an error that looks a lot like the error we have here.

Maybe you are using cv2.CascadeClassifier() with the wrong path to the xml files like this

                                                                       #
myFace_cascade = cv2.CascadeClassifier('wrongPath\haarcascade_frontalface_default.xml')
                                                                       #

Or maybe you did not download the files at all. Or even if you have downloaded the XML files, maybe your xml files are corrupt.

The solution that has worked for me is pretty straightforward, download the xml files from the two links bellow :

https://raw.githubusercontent.com/Itseez/opencv/master/data/haarcascades/haarcascade_frontalface_default.xml

https://raw.githubusercontent.com/Itseez/opencv/master/data/haarcascades/haarcascade_eye.xml

When using cv2.CascadeClassifier , make sure you do it like this

                                                                       #
myEye_cascade = cv2.CascadeClassifier('C:\opencv\data\haarcascades\haarcascade_eye.xml')

myFace_cascade = cv2.CascadeClassifier('C:\opencv\data\haarcascades\
haarcascade_frontalface_default.xml')
                                                                       #

Make sure the paths to the xml files are correct.

You can try the method bellow for a different approach.

Solution 2 : Avoid using absolute paths with xml files and cascade classifier

You can also avoid downloading the xml files and choosing the path manually and choose to use the packaged path to the installed cascades.

So, instead of using the absolute path like in this line

                                                                       #
myEye_cascade = cv2.CascadeClassifier('C:\opencv\data\haarcascades\haarcascade_eye.xml')
                                                                       #

You can use the line bellow

                                                                       #
myEye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_eye.xml')
                                                                       #

and instead of using this Absolut path

                                                                       #
myFace_cascade = cv2.CascadeClassifier('C:\opencv\data\haarcascades\
haarcascade_frontalface_default.xml')
                                                                       #

You can use this line instead

                                                                       #
myFace_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
                                                                       #

If the methods above helped you, consider supporting us on Kofi, any help is appreciated.

Summing-up : 

This is the end of our article, I hope the solutions I presented worked for you, Learning Python is a fun journey, do not let the errors discourage you. Keep coding and cheers.

Thank you for reading, keep coding and cheers. If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/