To perform object or face detection, first you must load the pretrained XML file using OpenCV's CascadeClassifier class as follows:
CascadeClassifier faceDetector;
faceDetector.load(faceCascadeFilename);
This can load Haar or LBP detectors just by giving a different filename. A very common mistake when using this is to provide the wrong folder or filename, but depending on your build environment, the load() method will either return false or generate a C++ exception (and exit your program with an assert error). So it is best to surround the load() method with a try... catch block, and display an error message to the user if something went wrong. Many beginners skip checking for errors, but it is crucial to show a help message to the user when something did not load correctly; otherwise, you may spend a very long time debugging other parts of your code before eventually realizing something did not load. A simple error message can be displayed as follows:
CascadeClassifier faceDetector;
try {
faceDetector.load(faceCascadeFilename);
} catch (cv::Exception e) {}
if ( faceDetector.empty() ) {
cerr << "ERROR: Couldn't load Face Detector (";
cerr << faceCascadeFilename << ")!" << endl;
exit(1);
}