Tested on:RPi3; Raspbian Jessie; Python 2.7
Prerequisites:Raspberry Pi with Raspbian Jessie and internet connection (see steps 1-7 in the Introduction)
Hardware:Raspberry Pi camera module (optional)

OpenCV primer: Face detection with the Raspberry Pi

Real face recognition is complicated and requires a large set of training images. However, face detection and feature identification is simple with a Raspberry Pi and the computer vision library OpenCV. This tutorial will not explain face detection methods; it just gives everything required for starting experiments. A very good resource is the OpenCV documentation which also has a profund tutorial on face recognition.
First, the OpenCV machine vision library will be installed (which takes quite a few hours even on the RPi3); then, a simple script for finding face features is presented.

Installing OpenCV
The installation advice is a wrap-up of a very cool OpenCV installation tutorial by Adrian Rosebrock. Visit PyImageSearch for many more great tutorials on machine vision!
After installing an up-to-date Raspbian Jessie, expand the filesystem by entering

in the terminal and confirming the first menu point.
Then, install all dependencies by running

in the terminal.
Download and install OpenCV and some additional packages with

This will take a few hours.

Extract features with OpenCV
Download the Script and sample image with

Running the Script requires the Raspbian Desktop. If you haven’t already booted into the Desktop, enter

and open the terminal. The script supports two modes: it either takes an image file as command line option or makes a snapshot with the camera attached. Then, it extracts some features. For the static mode, simply enter

which yields

Haar features extracted


Alternatively, install the camera module and enter

Finding more features: Haar Cascades
Features (any kind of objects, not only parts of faces) can be detected with Haar cascades. These are trained classifiers which uses large samples of fitting and non-fitting images for feature recognition. Thorsten Ball published an excellent tutorial on training Haar classifiers.
This is quite a task, so lets simply add a classifier someone else already trained – e.g. Alereimondo has some cool face Haar cascades.
Download and install a nose Haar cascade with

Open the script with

and find the lines

Remove the hashtag in front of the lines, then save, exit and rerun the script with

haar cascade nose


Yay, a nose was detected!
Fitting the parameters of detectMultiScale is very important for obtaining good results. In
the parameter minSize defines the minimum size of objects which can be classified as nose. If minSize is set to a bigger value, the nose might not be recognized; if it is set to a smaller value, other objects might be classified as nose.

The complete script
Click on Full Script to show all of the code.

camera module tutorial: http://www.knight-of-pi.org/raspberry-pi-camera-and-night-vision-with-pinoir
OpenCV installation tutorial: http://www.pyimagesearch.com/2015/10/26/how-to-install-opencv-3-on-raspbian-jessie/
OpenCV website: http://opencv.org/
OpenCV Documentation: http://docs.opencv.org/master/d7/d8b/tutorial_py_face_detection.html
Tutorial: training Haar classifiers: http://coding-robin.de/2013/07/22/train-your-own-opencv-haar-classifier.html
OpenCV installation tutorial: http://www.pyimagesearch.com/2015/10/26/how-to-install-opencv-3-on-raspbian-jessie/
Alereimondo Haar cascades: http://alereimondo.no-ip.org/OpenCV/34
OpenCV tutorial on face recognition: http://docs.opencv.org/2.4/modules/contrib/doc/facerec/facerec_tutorial.html

Knight of Pi

Johannes Bergs aka Knight of Pi. Diploma in Bioinformatics, some Webdesign and Python coding then. Living in the beautiful city of Vienna.


  1. Hi there, thanks for the tutorial.
    The script throws a couple of errors for me, namely:

    Traceback (most recent call last):
    File “opencv_face_features.py”, line 65, in
    File “opencv_face_features.py”, line 30, in extract_features
    minSize=(30, 30),
    TypeError: Required argument ‘image’ (pos 1) not found

    Any ideas why this might be?

    • Hi!
      It seems as if the installation is broken for Raspbian Stretch, could that be your issue?
      I didn’t run the whole installation, but building the dependencies did run without errors for Jessie but not for Stretch.

  2. Good Tutorial. Had following comments :
    1. you have used for loop on x,y,w,h will this differ for different things ?
    2. there is no /home/pi/opencv-3.0.0/data/haarcascades/Nariz.xml file. it throws error


Time limit is exhausted. Please reload CAPTCHA.