In this tutorial, we’ll learn a very straightforward approach for real-time face detection using python with a free and open-source package OpenCV and a fast and light weight Haar Cascade algorithm.
Haar Cascade Algorithm
Haar cascades algorithm was introduced in 2001 and were one of the most popular object detection algorithms in OpenCV at the time. Implementation of this algorithm is quite easy and it can run fast in real-time.
The cascades are nothing but a bunch of XML files that contain OpenCV data used for object detection. You just need to initialize your python code with any one of the available cascades you want, and then it does the detection job for you.
As face detection is a common task in computer vision, OpenCV offers a wide range of built-in cascades for detecting everything like: eyes, hands, legs, upper body, etc. There are even cascades for non-human things like Cat face detection.
Advantages of Haar Cascade
- Haar cascades are lightweight
- Extremely fast compared to other Object detection models such as YOLO
- Easy to implement
- Required less computing power
Install required libraries
You just need to install OpenCV library to run face recognition with haar cascades.
pip install opencv-python
Face recognition from images
In this section, we will see how we can detect faces in an image.
Download Cascade file
To use the Haar cascade classifier you need to download specific cascade files (OpenCV data) from this repo. Since I am performing face recognition, I have downloaded haarcascade_frontalface_default.xml file.
Now you are ready to run the following code to achieve face detection using haar cascade.
# Import OpenCV import cv2 # Define haar cascade classifier for face detection face_classifier = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') # Read input image to recognize face img = cv2.imread('input_image.jpg') # Convert image to gray scale OpenCV gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Detect face using haar cascade classifier faces_coordinates = face_classifier.detectMultiScale(gray_img) # Draw a rectangle around the faces for (x, y, w, h) in faces_coordinates: cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2) # Saving the image cv2.imwrite('face detection using haar cascade output.png', img) # show output image cv2.imshow('image', img) cv2.waitKey(0) cv2.destroyAllWindows()
In the above code:
- Line 5: Defining cascades for detecting faces
- Line 11: Converting input image to greyscale as Haar cascade works well for grey scale images
- Line 17: Extracting coordinates for detected faces
- Line 18: Drawing rectangle based on those coordinates
Note: If you are getting the below error, you need to update or install the latest OpenCV library
error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale
Face detection in python using a webcam
Now that you know how to implement face recognition using Haar Cascade classifier for an image, now let’s see how we can implement the same thing for a video or webcam.
To implement real-time face detection using python with webcam you just need to run the below code:
# Import OpenCV import cv2 # Define haar cascade classifier for face detection face_classifier = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') # Read webcam video cap = cv2.VideoCapture(0) while True: # Run video frame by frame read_ok, frame = cap.read() labels =  # Convert image to gray scale OpenCV gray_img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Detect face using haar cascade classifier faces_coordinates = face_classifier.detectMultiScale(gray_img) # Draw a rectangle around the faces for (x, y, w, h) in faces_coordinates: cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) cv2.imshow('Face Detector', frame) # Close video window by pressing 'x' if cv2.waitKey(1) & 0xFF == ord('x'): break cap.release() cv2.destroyAllWindows()
In the above code:
- Line 8: Reading webcam using OpenCV internal function
- Line 10: while loop to run forever
- Line 12-24: Passing same code inside while loop
- Line 27-28: Break the while loop by pressing “x” on the keyboard
Limitations of Haar Cascade Face detection
Though it is easy to implement real-time face detection using python OpenCV with webcam using Haar Cascade algorithm, it has some limitations.
- Prone to predict false positives: You can see in my output video shown above that haar cascade can easily predict any part of an image or video frame as a face. This algorithm works well with a single color background.
- Works only for front view: This algorithm works only for the front view of faces. You may see no prediction for a side view of your face. Maybe this is the reason why they named the XML file name as haarcascade_frontalface_default.xml
In this tutorial, you learned how to perform realtime face detection in python using a webcam with OpenCV and Haar Cascades.
Though Haar Cascade is a super lightweight algorithm, it has some limitations. In terms of accuracy, a slightly heavy YOLO algorithm is way better than haar cascades.
Hi there, I’m Anindya Naskar, Data Science Engineer. I created this website to show you what I believe is the best possible way to get your start in the field of Data Science.