Python OpenCV Face Detection

In this Python OpenCV lesson we are going to learn about Python OpenCV Face Detection, so OpenCV comes with a trainer as well as detector. If you want to train your own classifier for any object like car, planes etc. you can use OpenCV to create one. in this lesson we want to use Haar Cascade Classifier and it is a trained data.

 

 

What Is Haar Cascade ?

A Haar Cascade is basically a classifier which is used to detect the object for which it has been trained for, from the source. The Haar Cascade is trained by super imposing the positive image over a set of negative images. The training is generally done on a server and on various stages. Better results are obtained by using high quality images and increasing the amount of stages for which the classifier is trained.

 

We can also use predefined Haar Cascades which are available on Github

 

 

This is the complete code for this lesson.

 

 

This is for loading our Haar Cascade Classifier that we have already copied in our directory.

 

 

In here we are going to detect the faces, we can use detectMultiScale() and you need to give some parameters.

 

detecMultiScale() function is for detecting objects if it finds a face in the image it will return in the form of x,y,w,h. and it needs some parameters.

ScaleFactor: This parameter is for specifying  how much the image size is reduced at each image scale.

minNeighbors: Parameter specifying how many neighbors each candidate rectangle should have to retain it, this parameter will affect the quality of the detected faces.

 

 

 

In this code we want to draw rectangle to the faces in the image.

 

 

 

Run the complete code and this will be the result.

Python OpenCV Face Detection
Python OpenCV Face Detection

 

 

Thank you for visiting my website! If you enjoyed the free courses, please consider supporting my works on Patreon.