A Haar feature-based cascade classifier detects objects through successive classification stages built from Haar-like image features. For face detection, machine learning trains the cascade with positive images containing faces and negative images without them. section 9.3.1
A practical OpenCV detection workflow is:
- Load a pretrained XML model shipped in OpenCV's data directory. section 9.3.1
- Read a camera image, convert it to grayscale, and equalize its histogram to improve contrast. section 9.3.1
- Call detectMultiScale to locate faces and return rectangles containing their coordinates and dimensions. section 9.3.1
- Use smaller images when necessary to make detection faster for live video. section 9.6
>>> CLASSIFIER_PATH = f'{CV2_DIR}/data/haarcascade_frontalface_default.xml'
Locate the pretrained frontal-face XML file inside the installed OpenCV package before constructing the classifier. section 9.3.1
Detecting a face in a camera photo
Create a CascadeClassifier from the pretrained model, read photo.jpg, convert it to grayscale, equalize the histogram, and call detectMultiScale. A 640×480 image can produce one detected rectangle such as [215, 105, 268, 268]. section 9.3.1 section 9.6
