Introduction: RasPi + OpenCV = Face Tracking

This instructable will teach you everything you need to know about installing your new RasPi Camera to your Raspberry Pi and implement OpenCV's Face tracking libraries. The tutorial will not assume that you know how to program or understand the inner workings of a Raspberry Pi. Intermediate users can easily use the tutorial. Beginners should be able to use this tutorial but may run into problems along the way. The time to complete is approximately 10hrs due to the slow processing power of the Raspberry Pi and the size of OpenCV. It will be easier if you use a usb webcam to start out then go back and install the necessary libraries for the Raspi Camera. By the end of the tutorial you will have a fully functioning OpenCV installation on your Raspberry Pi capable of face tracking and much much more.

Materials Needed

  • Raspberry Pi
  • SD Card
  • RasPi Camera Module
  • Keyboard
  • Mouse
  • Micro USB Charger
  • Ethernet cord
  • HDMI cord
  • Monitor

Step 1: Install Operating System to SD Card

Download OS Image -

Installation Guides

Step 2: Connect Raspberry Pi

  1. Connect Ethernet cord to port
  2. Connect Keyboard and Mouse to USB ports
  3. Insert SD card
  4. Connect HDMI cord to monitor
  5. Connect HDMI cord to Raspberry Pi
  6. Power on device by connecting micro USB

Step 3: First Time Setup

Connect the Camera module to the CSI port on the Raspberry Pi computer. Configure the Raspberry Pi computer On first boot, you will be presented with the ‘raspi-config’ menu.

  1. Type the Following into the terminal
    1. sudo apt-get update
    2. sudo apt-get upgrade -y
    3. sudo reboot


default credentials are pi with the password raspberry

Step 4: (Optional) Connect to Raspberry Pi Over Local Internet

This will allow you to use your Raspberry Pi from another computer.

SSH -link

Step 5: Install OpenCv and Required Libraries

  1. Open a terminal and type the following lines. We are installing some python libraries this step should take approx 3 minutes.
    1. sudo apt-get install python-pygame python-scipy python-numpy
    2. sudo apt-get install python-pip
  2. Upgrade pip. approx 2 minutes.
    1. sudo pip install --upgrade pip
  3. You will need to log out and log back in.
    1. Click "Menu" in top right corner
    2. Click "Shutdown" at the bottom of list
    3. Select log out
    4. Log back in by typing "stratx" followed by the return key
  4. Install a virtual environment. approx 2 minutes.
    1. sudo pip install virtualenvwrapper
  5. Edit bashrc file using your favorite text editor, I will be using vi
    1. vi ~/.bashrc
    2. type "i" to enter insert mode
    3. paste the following on any line without quotes "source /usr/local/bin/"
  6. Reload bash file by typing the following in the terminal. approx 1 minute
    1. . .bashrc
  7. Make a virtual enviroment, I named mine env you may name it what ever you please
    1. mkvirtualenv --system-site-packages env
  8. Download the following script from Jay Rambhia, The script is outdated but we can work around this. aprox 20 minutes.
    1. wget
    2. chmod +x
    3. ./
  9. The terminal will yell at you saying there is a problem just ignore this and enter the following commands. This step will easily take 5-10 hours so grab yourself a nice book to read.
    1. wget -O
    2. unzip
    3. cd opencv-2.4.11
    4. mkdir build
    5. cd build
    7. make -j4
    8. sudo make install
    9. sudo sh -c 'echo "/usr/local/lib" > /etc/'
    10. sudo ldconfig
  10. Follow the instructions linked to install UV4L
  11. Fix some outdated dependencies
    1. apt-get remove libopencv-core2.3
    2. export PYTHONPATH=/usr/local/lib/python2.7/site-packages/
    3. export LD_PRELOAD=/usr/lib/uv4l/uv4lext/armv6l/

Step 6: (Optional) - Install SimpleCv

  1. Installation approx 5 minutes
    1. sudo apt-get install git
    2. git clone git://
    3. cd SimpleCV
    4. sudo python install
  2. To Test
    1. simplecv
    2. camera = Camera()

Step 7: Face Tracking

Once everything is installed you may run the OpenCV sample code provided in the directory you installed to.

To run python scripts you must type python then the name of the script.

After all your hard work and patience you should backup your operating system so you won't need to spend 10hrs reinstalling if something goes wrong.

Further projects and information can be found at