![]() |
VOOZH | about |
Face detection is a fundamental computer vision task that involves locating human faces in images or video streams. OpenCV provides an efficient solution for this using Haar Cascade Classifiers. Haar Cascades are trained using two types of images:
After training, the classifier scans images at multiple scales to detect faces in real time. This approach is widely used in surveillance systems, attendance systems and camera-based applications.
In this section, we will implement face and eye detection using pre-trained Haar Cascade classifiers provided by OpenCV.
To begin, we import the necessary libraries like OpenCV, Numpy and Matplotlib.
Explanation:
Next, we load the pre-trained Haar Cascade XML files for face and eye detection.
You can download these classifier from this link.
Explanation:
Now, we define a function that detects faces in an image and draws rectangles around them.
Explanation:
Similarly, we create a function to detect eyes using the eye cascade classifier.
Explanation: The process is similar to face detection, rectangles are drawn around detected eyes using separate classifiers improves detection accuracy.
Now, we load an image on which face and eye detection will be performed.
Note: We are using a sample image "andrew.jpg", to download click here.
Output
Explanation:
After running the code you will see three images Face Detection, Eyes Detection and Face and Eyes Detection. These images will also be saved as face.jpg, eyes.jpg and face+eyes.jpg respectively.
Face Detection
Output
Eyes Detection
Output
Face and Eyes Detection
Output