This website uses cookies

This website uses cookies to give you the best and most relevant experience. By continuing to browse this site, you are agreeing to our use of cookies. Learn More.

Using Python and OpenCV for Face Detection From a Live Camera

OpenCV is an open source software library which consists of a comprehensive set of optimised computer vision and machine learning algorithms that can be used to enhance machine perception of the physical world such as face recognition, object identification, human action classification, object movement tracking, image stitching, red eye removal and much more. In this fun experiment, it attempts to identify the faces of people in a live video stream. If it is able to recognise them, it will display the name of the person and a number for the confidence level (lower is better). Otherwise, it would classify them as "intruders".

How the code is organised

Python 2.7, numpy and opencv (v2 at the time of writing) are required to run this application which consists of 4 python scripts:

  1. Launches the application and presents the command line user interface.
  2. Takes a few pictures of known persons of interest standing alone in front of the camera. It prompts to enter the person name (avoid spaces) and saves the pictures in a folder named after that person. This procedure should be repeated to setup additional known persons.
  3. Processes the pictures of each known person and associates a label (the person's name) to each set.
  4. Tries to identify faces based on the known persons pictures that were used to train it.

The source code is available here.

User Interface

The application can be run by typing the command below in a terminal window after switching to the directory containing the above scripts:


A command line interface with 4 options are then shown to the user:

  1. take photo: Takes pictures of known people and associate a label (the name of the person) with that picture set. The application will prompt to enter the name of the person. The user has to sit in front of the camera and make natural facial expressions while it takes about 30 pictures which are saved in a directory named after that person. Selecting this option for the same user multiple times simply adds more pictures to his directory and increases the accuracy of the identification process. To add a different known person, simply repeat this process with a different name and person sitting in front of the camera.
  2. train system: Allows the system to digest and classify the pictures into an internal representation. It will then use this classification to identify faces when the system is armed if anything resembling a face comes into its field of view.
  3. arm system: Starts the camera in surveillance mode and tries to recognise faces based on the classification created using the pictures of known persons. It draws a rectangle around the faces with the person's name if it is able to identify the face. Otherwise it displays the label "intruder". It also displays the confidence level as a number - the lower the number the higher the confidence level.
  4. exit: Exits the application
User interface

Armed System in Action

The image below shows the armed system in surveillance mode with two persons in its field of view. It recognises only one of them and assigns a label and confidence level.

Camera in surveillance mode


A couple of enhancements can be made to this application:

  • Increase the accuracy (confidence level)
  • Detecting faces when they are slightly tilted
  • Detecting features other than faces

An interesting use case is to set it up as a surveillance camera and adapt it to send sms, emails, notification (with attached images) based on certain events such as intruder detection (unrecognised faces), motion detection (with sensors) using a raspberry pi thereby creating a smart surveillance camera.

Additional Resources

For detailed explanations of uses of OpenCV and image recognition, please refer to these links: