webcm1First Step is to install OpenCV 3 on your Raspberry Pi. I also prefer Python 3, so will concentrate only on this version in this post. This post of Adrian Rosebrock gives very good guidance on how to install OpenCV 3.

At the time I wanted to install OpenCV I had to install the version 3.1.0 for which I am listing in the following just the steps I did and recommend the reader to have a look at Adrian’s post for more details.

Install Dependencies

Grab the OpenCV source code

I had some problems to get opencv_contrib installed. But after some tries it suddenly worked.
So try a few times and make sure you did not misspell something.

Setup Python

Now open ~/.profile for example with

and add to the end of this file the following lines:

Reload ~/.profile with

Create and start your Python virtual environment

Compile and install OpenCV

As Adrian is showing in his post make sure compiling OpenCV for Python 3 “.virtualenvs” in the highlighted paths as shown in below image. Then you know that your virtual environment is set up correctly. Again if there are issues at this point I recommend Adrian’s post for more details.

Now comes the part that takes the longest time (about 1.5 hours depending on the Raspberry Pi version you are using) – the actual compilation of OpenCV on your Pi:

This worked flawlessly on the Raspberry Pi 2, but on the Pi 3 I got errors probably due to race condition issues. So I removed the build and installed it again using the one core approach (this might in some cases be necessary also for the Raspberry Pi 2):

Finally install OpenCV on your system:

Finishing the install for Python 3

Check to see if OpenCV is installed with

where you should see cv2.cpyhon-34m.so.


Verifying your OpenCV 3 install

Starting Python you should see following:

If this all worked you are all set for your Dropbox based home-surveillance system.
If you see errors, please consult Adrian’s post for troubleshooting.

Video Streaming with OpenCV

The OpenCV 3 system you just installed is a powerful image processing library that can be used for many purposes related to imaging.
As an introduction I want to present here a simple object-oriented (OO) Python script that allows capture and saving of videos. The OO setup simplifies the addition of more functions to process the video stream and also allows the easy addition of more cameras, by just adding more camera “instances”.
Save following code for example as “piCameraClass.py”:

In this Python code the camera functions are encapsulated in a class. This allows in the main program main() to easily start multiple camera instances if desired. It also makes the program easier oversee-able as the camera functions can be controlled by different member functions of this class. Here implemented is only one the following member functions:

  • __init__ and __del__ : The __init__ method is the “constructor” of this class which is called once at the instantiation of the class (in main() with cam1 = CameraInst() ). The constructor initializes a few class-internal parameters, activates the camera with the openCV method VideoCapture() and defines the output format to save the video to the file output.avi .
    The destructor __del__ releases the camera (which important to avoid crashing the camera controller) and closes all windows opened by this Python script using the openCV function destroyAllWindows() .
  • captureVideo() : This function captures singles frames with the read() method. The captured frame is transferred to a gray-level image using the openCV function cvtColor() with the argument COLOR_BGR2GRAY. Finally the image is displayed with the openCV function imshow() .
  • saveVideo : This function just saves the uses the write() method to save each captured image in the file output.avi.

main() is a function outside of the class CameraInst() . This is the function that instantiates the class using the name cam1 and uses this in an endless-loop to call the frma capture function of the class captureVideo() to capture frames and generate a video and also calls saveVideo() to save the video.

My favorite main-call in a Python function is using if __name__==”__main__” , which gives you the flexibility to use the code in this file (piCameraClass.py) ither directly as the main code or by using this code as a module that is called from another Python script.

Because OpenCV is installed in the virtual environment cv, following has to be done to run this script:

The last command is the call to start the script. To stop the execution click on the video window and press “q”.


In this first part of this home surveillance series we are installing the basic tools to capture and to process images and video streams. OpenCV is a powerful tool-set to process images, which can be readily applied for home and any other surveillance.
In the following parts of this series I want to present an extended Python script that allows the detection of intruders. This script will allow the capture of videos and images of the scene at detection of motion. The videos and images can then be automatically uploaded to cloud-servers such as Dropbox to be view-able from anywhere in the world if you have access to the internet. Finally I will show you how to stream a life video to a website which you also can access from anywhere in the world.

As usual I am looking forward to hearing from you, your thoughts and comments. Please leave me your input or questions in below form and I will try to answer as soon as I can