Face Detection Using Haar Cascade Algorithm

9+

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.

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 Haar Cascade algorithm uses edge or line detection features that are proposed by Viola and Jones within their research paper named “Rapid Object Detection employing Boosted Cascade of Simple Features”  

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 algorithm – 

https://github.com/opencv/opencv/tree/master/data/haarcascades

GitHub Link to the project –

https://github.com/MansiiMishra/Face-Detection-From-an-Image

Understanding project Algorithm:

Let us break down our project into easy steps and build an algorithm for the same.

  • Importing OpenCV
  • Importing XML file
  • Importing test Image
  • Converting the image to greyscale
  • Detecting Multi-scale faces
  • Mentioning sides of the rectangle for face detection
  • Displaying the detected image

Since we have broken down the project into multiple sections, let’s start working on them!

STEP 1 – Importing OpenCV

In the command prompt type (pip install OpenCV-python) using this command, we can install and set up OpenCV to 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 – Importing the test image.

imread() is a method of OpenCV to read the input. Similarly, imshow() is a method to display the processed input in the form of output. 

STEP 4 – Converting to grey Scale.

This project works on images that are in greyscale and hence we convert the image to greyscale for ease of face detection.

STEP 5 – Detecting Multi-scale faces.

This function allows to detect objects of different sizes in the input image and hence an image with multi people with different sizes of the face can also be detected. Parameters of the functions are – 

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

To read more about the functions of OpenCV you can read this detailed documentation.

https://docs.opencv.org/master/d6/d00/tutorial_py_root.html

STEP 6 – 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 7 – Displaying the detected image.

Yay! You have come so far. Now it’s time to display the image that has been detected. We also add the feature to close the image tab only when a key is pressed.

waitKey() is used to mention that the screen cannot close until a key is pressed.

HEY! Make sure you use capital “K” in waitKey.

Final Code:

About the Author

Self-Exploring my path to being a data analyst, One step at a time. Exploring. Learning. Writing.

close
9+

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

DMCA.com Protection Status