Blog Post

How to install OpenCV on Raspberry Pi and do Face Tracking


How to  install OpenCV on Raspberry Pi

To  install OpenCV on Raspberry Pi, you need Raspberry Pi ( Pi2 preferably) , Pi camera, and internet connection to  Raspberry Pi.

Begin with latest Raspberry Pi Image from

Copy this image to correct size SD Card. To do this correctly  please refers the detailed instructions at

Now since your SD card is ready, Boot your Pi. First thing you want to do is  Enable Camera.
To do this run

sudo raspi-config

and chose Enable Camera (menu 6) also chose the correct speed for your Raspberry Pi from Overclock option (menu 8).

Reboot your Raspberry Pi. Now its is time to Install  python wrapper for OpenCV, to do this run 

sudo apt-get install python-opencv
sudo pip install imutils

 Now we are almost done. Let's test the Pi camera connected to Raspberry Pi for proper operation. For this on command prompt  run,

raspistill -o cam.jpg

The red light on the Pi camera will come on and a picture will be stored in cam.jpg file. Now we are all set to do some serious face tracking using Raspberry Pi. You can download the face tracking sample code using haar cascade from here.
You can run this code from Raspberry Pi command line.

python haarcascade_frontalface_default.xml

If the face is found in an image captured by camera, python  program will print   "Found 1 faces!."  Below is brief description of how the code work.

from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2
import sys
import imutils

# Get user supplied values
cascPath = sys.argv[1]

# Create the haar cascade
faceCascade = cv2.CascadeClassifier(cascPath) # initialize the camera and grab a reference to the raw camera capture
camera = PiCamera()
camera.resolution = (160, 120)
camera.framerate = 32
rawCapture = PiRGBArray(camera, size=(160, 120))

This imports the required libraries and open the haar cascade file
Raspberry pi camera is intialized and the streme of images is collectd from camera one by one. Resolution is selected to be 162X120 for fast detection of faces.

image = frame.array
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

each image is picked from the camera and is loaded in nmupy array. Image is  then converted to a grayscale image. This gray scale image is fed to  facedetection alogorithm, with some minimum size setting

faces = faceCascade.detectMultiScale(
    minSize=(30, 30),
    flags =

for (x, y, w, h) in faces:, (x+w/2, y+h/2), int((w+h)/3), (255, 255, 255), 1)

if any valid faces are  found in image, function will return the list of faces with positions. We draw a white circle around each face and display results.

Raspberry Pi Face detection

Comments (1)

Stephen B 10.06.2020
Is it possible to install openCV on a Raspberry Pi running ev3dev image for use with PiStorms-v2? If so can you tell me how to go about it?
Please Login to add your comments

Featured Posts

The mindsensors Grove Sensor Adapter makes it easy to use a variety of unique Grove sensors with the...

Read more

The mindsensors Grove Sensor Adapter makes it easy to use a variety of unique Grove sensors with the...

Read more


  • What do you use to program your FRC Robot?