So, In this post, we are going to learn about Face Detection using OpenCV and Python. We will discuss a very popular method called Haar Cascades for face detection. We are using OpenCV – a popular library for Computer Vision.

Firstly, make sure that you have already installed the OpenCV library in your system. If you want to know how to install the system and how to use the basic methods of OpenCV then, I have already posted the article earlier Get Started with Image Processing using OpenCV-Python. In that post, you will come to know the basic steps of OpenCV.

OpenCV uses machine learning algorithms to find the face within the image or in real-time video. It is not as easy to find out the face from the pictures because it has many variations, and it doesn’t depend on a single face. So, we have thousands of small features which help to search for faces. The algorithm helps to identify the help of small patterns or features and makes the task easier. These tasks we called the Classifiers.

Ok, so let’s begin with more details and an understanding of the code.


1️⃣ OpenCV Cascade

Before using the Haar Cascade Classifiers through code, we need to understand that, What is Cascade?

Something arranged or occurring in a series or in a succession of stages.

  • In OpenCV, it works like this. It breaks the problem into multiple stages and applies one by one feature on the detecting face.
  • If the window fails in the initial stage and the picture will return a negative value then, algorithms won’t waste the time to match with the next 6000 features.
  • If it passes, apply the second stage of features and continue the process. The window which passes all stages is a face region.

It seems to be tricky, but it is not. It is an easy method. OpenCV comes with the trainer as well as the detector. So for detection, we have already pre-trained classifiers for Face, Eyes, Smile, etc. And we can also train our classifiers for anything like cars, animals, non-human things, etc.

2️⃣ Understanding the code

Let’s understand the code step by step, We have two variations of code

  • For Pictures
  • For Real-Time webcam

So, let’s start and make the face and eye detector.

#Step1: You need to collect the resources that we are using for detection. Firstly, you need to download the pre-trained classifiers from the Official repository.

You need to download two XML haascascade classifiers:

#Step2: For input, we need to load an image (or webcam video). After this, we are ready to use those Classifiers and our input image to detect the face and eyes.

#Step3: Add the OpenCV library and load the XML Classifier files in the code.

Now, we need to create and load the cascade and initialize it with the face classifiers. It will load the face or eye data into memory. Note, we have the XML Classifiers files for Face and Eye, which will be used to detect the face and eye from the given input.

#Step4: Now load the input and understand for different types of input

◼ For Pictures
  • Loads the input image and converts it into the grayscale (only works on grayscale images). Here we have the image of NASA group members, which we are using as input.
  • Now, to detect the face, we have a function called detectMultiScale(), which detects the object. It is called over the cascade, which we had already classified.

It takes three arguments.

  • The first argument is the grayscale image, which is mostly used in OpenCV.
  • The second one is scaleFactor specifies how much the image size is reduced with each scale. Suppose we have a face with which is closer to the camera. So, to show the face normal, we reduce the size.
  • The third argument is minNeighbors, which defines how many objects are detected near the current one before creating the face region.

If a face is found, it will return the list of coordinates of the rectangular region, which we can use to draw the rectangle around the face.

Finally, we display the results with face detected regions using imshow()

📔 Full code for your reference:

nasa_face_detection
Detecting faces of NASA Group Members

◼ For Webcam
  • Everything would be the same as for the picture only the difference is we need to load the video using webCam and run the loop by infinite times to load the frames in Real-Time.
  • Captures Video:
  • Read Frame by Frame: The read() function will be read one frame from the video on each loop. And do their remaining task and return the results.
  • Take frame as an image and apply the same functions to detect the face earlier in the “For Picture” section.

Only the addition is for eye detection, as we are detecting eyes also in the code. So, we find the faces in the picture (frame). If faces are found, it returns the positions of detected faces in the Rectangular region.

Once we get these locations (face region), we can create an ROI (Region of Interest) for the face (since eyes are always on the face) and apply eye detection using an eye cascade on this ROI.

  • Finally, we displays the results with face detected regions using imshow() and release the cam after pressing ‘Q’.

📔 Full code for your reference:

webcam face detect
Output through Webcam

3️⃣ Final Comments

Okay, So I hope you understand each part of the code. One thing that you should remember is, you will also create your classifiers for object detection like Smile faces, Identify Person, Detect Cars, Animals, etc. You can train the dataset and use that to detect the object and also used the existing datasets.

In the next article, I will give you more examples of object detection using OpenCV. So stay tuned!!


That’s all about Face Detection using OpenCV & Python. Hope you like it👍 and if you find this useful then hit the like button below and don’t forget to share this with your friends or on social media. And If you have any queries, please do ask in the comments section or anything that you want through mail then contact.

Thanks😊

Related Articles:

Also Read: