Face Detection Using a real-time webcam and Implemented with Haar cascade Algorithm
Prerequisites – OpenCV (Basic), Python
Face Detection is one of the most popular mini-projects a beginner at ML (Machine Learning) can go for One such project is Basic face detection using Haar Cascade Algorithm. In this project, we will be using our webcam to detect the face in real-time.
What is Haar Cascade?
Haar Cascades are machine learning object detection algorithms that are used to identify faces in an image or a real-time video. The Harr Cascade algorithm uses edge or line detection features that were proposed by Viola and Jones in their research paper published in the year 2001.
How can we use this algorithm in our project?
Simple! We can simply download the XML file and save it in our project folder.
GitHub link to all Haar Cascades algorithms –
GitHub Link to the project –
Understanding project Algorithm:
Let us break down our project into easy steps and build an algorithm for the same.
- Importing OpenCV
- Importing XML file
- Allowing WebCam to capture video
- Capturing video in terms of frames
- Converting the image to greyscale
- Detecting Multi-scale faces
- Mentioning sides of the rectangle for face detection
- Displaying the detected Video
Since we have broken down the project into multiple sections, let’s start working on them!
STEP 1 – Importing OpenCV
We can install and set up OpenCV to python using the command prompt, type (pip install OpenCV-python). OpenCV provides a real-time optimized Computer Vision library, tools, and hardware and the same will be used in our project.
Type the below-mentioned command in the command prompt.
Create a new directory and create a new file named ‘Main.py’
In the python file import cv2
STEP 2 – Importing XML file
You can download the file using this link and save it in the project folder – https://github.com/opencv/opencv/blob/master/data/haarcascades/haarcascade_frontalface_default.xml
STEP 3 – Allowing WebCam to capture video
cv2.VideoCapture() allows the program to access the webcam only if the function has the parameter 0. In other cases, if rather than using webCam one wishes to detect faces using a video, then the parameter 0 in the function can be easily replaced with the video file name.
STEP 4 – Capturing video in terms of frames
A video when detected in real-time is divided into frames for the face recognition process
STEP 5 – Converting the image to greyscale
The project that we built today works on greyscale images and hence each frame that is been detected needs to be converted into greyscale before the face detection process begins.
STEP 6 – Detecting Multi-scale faces
Each Frame may or may not have the same scale i.e. size of the face detected and hence the function detectMultiScale() allows the program to detect faces of multiple scales
detectMultiScale (InputArray image, double scaleFactor=1.1, int minNeighbors=6)
to read more about its functions you can visit – https://docs.opencv.org/4.5.0/d1/de5/classcv_1_1CascadeClassifier.html
STEP 7 – Mentioning sides of the rectangle for face detection
This function helps us to mention the dimensions thickness and color of the rectangle that will be visible during the face detection.
cv2.rectangle(image, start_point, end_point, color, thickness)
STEP 8 – Displaying the detected frames in terms of real-time video
Yay! You have come so far. Now it’s time to display the frames that have been detected in form of real-time video.
waitKey() is used to mention that the screen cannot close until a key is pressed.
HEY! Make sure you use capital “K” in waitKey.
Let’s form a loop so that we don’t miss any frame!
Lastly, Releasing the entire captured frame in a video.
It’s fun that the project that we divide into 8 parts works seamlessly together and runs smoothly as a single real-time video!