Face and Object Detection using Python and OpenCV

For those who want to do object or face detection, the standard way to do it is with OpenCV. OpenCV is a programming library for Object recognition and is very flexible. You can train it to recognize any number of different objects. For the purpose of this tutorial, I will be using Python3. However, you can use other programming languages with OpenCV as well.


1. Pip
Pip is not nesicarily a prerequisite. But it makes it a lot easier. Below are instructions on how to install Pip on OSX, LinuxA Family of Operating Systems based on UNIX, and WindowsA Computer Operating System made by Microsoft Corporation.

LinuxA Family of Operating Systems based on UNIX and OSX:
Open a terminal window and run: curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py

WindowsA Computer Operating System made by Microsoft Corporation:
Installing on Windows is a very similar process to OSX and Linux, you just don’t have access to curl. So start by downloading the get-pip.py script here: https://bootstrap.pypa.io/get-pip.py
Then run:
python3 https://bootstrap.pypa.io/get-pip.py

2. Opencv
If you are using pythonPython is an interpreted, high-level, cross-platform program... 3:
Pip3 install opencv-python

If you are using PythonPython is an interpreted, high-level, cross-platform program... 2:
Pip install opencv-python

The above command(s) will install the latest version of OpenCV. As of today, that is version 3.4.

3. Pre-trained Model
OpenCV comes with pre-trained models for various scenarios. Using a pre-trained model is much easier than training one yourself. For the purposes of this demonstration, download this url:

There are additinoal pretrained models here: https://github.com/opencv/opencv/tree/master/data/haarcascades

Running the script

After your prerequisites are installed, you can write your code. At the bottom of this article is an example script that will capture from your webcam and draw a rectangle around any detected faces. Make sure that you save the xml file you downloaded in the previous step to the same folder as the pythonPython is an interpreted, high-level, cross-platform program... script below.

The XML file I mentioned will detect all full faces. However, there are a variety of other pre-trained models you can use. There is one that will detect partial faces, another that will detect smiles, etc…. You can use the same script below with the different models to detect different things. In a future article, we will go over training your own model.

Below is the script I mentioned. You can copy and paste it into a file called face.py. Then you can run it my typing python3 face.py


When you are done running the above script, you can exit by pressing the ‘Q’ button on your keyboard.