Face Detection Using OpenCV(Python)

Computer vision is all the rage in the machine learning and deep learning community these days. And one of the most popular applications of this domain is face detection.

What is Face Detection???

Face detection is the ability of computer technology to identify people’s faces within digital images. Face detection applications employ algorithms focused on detecting human faces within larger images that may contain landscapes, objects etc.

In order to work, face detection applications use machine learning algorithms to detect human faces within images of any size. The larger images might contain numerous objects that aren’t facing such as landscapes, objects, animals, buildings and other parts of humans (e.g. legs, shoulders and arms).

Facial detection/recognition technology was previously associated with only security sector but today there is active expansion into other industries including retail, marketing, healthcare etc.

How Face Detection Works???

While the process is somewhat complex, face detection algorithms often begin by searching for human eyes. Eyes constitute what is known as a valley region and are one of the easiest features to detect. Once eyes are detected, the algorithm might then attempt to detect facial regions including eyebrows, the mouth, nose, nostrils, and the iris. Once the algorithm surmises that it has detected a facial region, it can then apply additional tests to validate whether it has, in fact, detected a face.


OpenCV (Open Source Computer Vision Library) is an open source computer vision and machine learning library. This was built to give a common structure for computer vision applications and to influence the use of machine interpretation in the commercial products like facial recognition which is extensively use in today’s world. OpenCV has more than 2,500 optimized algorithms. These algorithm can be used to detect and identify faces, objects, track moving objects in a video or in an image, follow eye movements etc.

OpenCV has been written in C++ and has templated interface that work with STL containers (Standard Template Library: it is a set of C++ template classes to provide common programming data structure and functions like list, array, stack etc). OpenCV has C++, Python, Java and Matlab interfaces and supports Windows, Linux, Mac OS and also Android.

Haar Cascade:

A Haar Cascade is a classifier which is used to detect the object for which it has been trained for. The Haar cascade is trained by superimposing the positive image over a set of images. This type of 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 for. This cascade makes it easier to build a model. One just needs to pre-define the Haar cascade which are available on github repository. Moreover one can make their own cascade file.

How to develop your own Haar Cascade??

  1. Collect positive and negative images related to detection.For example for Face Detection positive images are the faces and negative images are the background images like lamppost,car,etc.Refer to my github repository for downloading samples.
  2. After completion of first step use object_maker.bat for saving the dimensions of faces . Press Space to save and Enter for next image.
  3. Run samples_creation.bat to get a file in folder vector after completion of step 2.
  4. Run harrtraining.bat to get 0 to n-1 Cascades.
  5. Copy Cascades to Cascade2xml/data folder and run covert.bat to get your own Harr Cascade in XML format.
  6. Your Detector is ready.

So, Let’s Start :

  1. Installing required Libraries:

2. Importing the Libraries:

3. Reading the image. Here firstly i am creating a my Haar Cascade object,image object, and then converting the colored image to gray Scale and printing its dimension of face.

4. Detecting the Face by making the rectangle:

Original Vs Detected Image:


The source code of this is available on my github repository.

For any queries related to my project contact me over my email id: powerakash8@gmail.com

Closing Note: I hope this blog will help you to build your own Face Detection system.




We are here building a community here.

Recommended from Medium

Does machine learning produce mental representations?

Training an Agent to beat Grid World

Top Twitter Topics by Data Scientists #43

Image Recognition & Transfer Learning — Great performance with limited data — Keras/TensorFlow

WSDM — KKBox’s Music Recommendation Challange

Face and Facial Landmarks Detection

A New First Order Hold!

Objection detection based on SSD Caffe model

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store


Datascience Intern @ Highradius Corporation Ltd.

More from Medium

Text Classification using TensorFlow and Keras

Introduction to Kafka

AVL Tree Implementation