Personal blog of Miguel Araujo

Raspberry Pi + SimpleCV + OpenCV + Raspicam CSI camera

In this Codemotion Madrid 2013, I’m giving a talk about Computer Vision. It’s going to be a beginner’s talk, an introduction to high level concepts of artificial vision using SimpleCV, OpenCV and Python. For making this talk more interesting I’m going to develop a software prototype, whose internals I will explain at the end of the talk, putting in play some of the concepts introduced during the talk, so people see a real application of things learned.

In the abstract of the talk, I stated I was going to run this prototype in a Raspberry Pi. I have some experience with the Raspi using USB cameras and compact cameras handled with libgphoto2, but many of these hog Pi’s CPU, their framerate is too slow, the camera is too expensive or too big.

raspicam picture

We’ve all heard of Raspberry CSI camera board or Raspicam. It’s specs are great and one of its great strengths is that is uses ~2% CPU. That’s crucial as I’m going to need every CPU cycle available for analyzing frames looking for patterns. The problem is that the camera is not USB, so I knew it wasn’t going to be an easy fit.

I couldn’t find a single tutorial on how to make Raspicam work with SimpleCV, only questions about it online. Actually, I only found one for OpenCV, a seven part tutorial by Pierre Raufast, the guy behind Magic Mirror project. The idea of this tutorial is to modify source code of raspivid and raspistill to use buffer memory of the camera to feed OpenCV image objects. This was a great step in making it work, but not an easy one, everyone can follow.

Then I searched for a driver that made things easier, one that made possible recognizing the camera as if it were a USB camera, a V4L2 (Video4Linux2) driver. I knew that if I didn’t find one, I may have to code my own and that was going to be a tough task. Luckily I found UV4L (UserVideo4Linux) project, with a recent release announcement for a CSI video driver, great! The author of this driver seems to be nicknamed RpiName, if someone can confirm it I will update the article.

So here is a detailed how-to explaining how to put all the pieces together. We will be using Raspbian, compiling OpenCV and using latest SimpleCV version, it’s been a long time since its last PyPi release to be honest. Beware that I’ve tried to make this the easiest to follow and some hard parts are automated through nice scripts.

We’ll need to download Raspbian and record it in our SD, I’m using version released 2013-07-26. I will take for granted you know how to do this step, if not you should check online, there’s plenty of tutorials and documentation.

After first boot and going through raspi config program, log in and then run:

sudo apt-get install python-pygame python-scipy python-numpy
sudo apt-get install python-pip

After that you can upgrade pip doing:

sudo pip install --upgrade pip

Now logut and then login again (Otherwise pip won’t be found in your path, as bash is most likely caching paths). Then run:

sudo pip install virtualenvwrapper

Now edit your .bashrc file using your favorite editor:

vim ~/.bashrc 

Add this line to the file:

source /usr/local/bin/

Reload .bashrc file with:

. .bashrc

Now, we are ready to create a virtual environment. We will create one that uses system site packages, because some of the tools that SimpleCV needs, are hard to install or compile and we will be using apt versions installed.

mkvirtualenv --system-site-packages hal

The name of my prototype is “Red Light Hal”, it’s still a secret what it is about :) come to the talk and if you can’t, don’t worry, there will be a post about it in the future.

Now we need to download a little script by Jay Rambhia, an active developer of SimpleCV, that publishes great tutorials online. This script we’ll do all the heavy lifting for preparing your system for download and compiling OpenCV 2.4.5 in your Raspbian, piece of cake. This is going to take easily 5 hours, so plan ahead:

chmod +x

Now let’s install SimpleCV latest version, we’ll need git for this:

sudo apt-get install git
git clone git://
cd SimpleCV
python install

If we now run a simplecv shell and do:

camera = Camera()

We will get this error:

/bin/sh: 1: lsof: not found
WARNING: caught exception: SystemError("Cannot identify '/dev/video0': 2, No such file or directory",)
WARNING: SimpleCV can't seem to find a camera on your system, or the drivers do not work with SimpleCV.

Now we need to install UV4L CSI driver, there are detailed intructions in this thread, so I won’t copy those here.

Somewhere in the internet you will read this driver doesn’t support video, well that was when it was released, if you read this Raspberry Pi forum thread, you will see the author state:

The new driver release from the repository also supports H264 video encoding.

From the last release on, the driver allows to control almost all the image properties on the fly, while the camera is in use, without having to reload the driver with new settings. These properties include image size, encoding, hue, saturation, vertical/orizontal flip, iso, etc…

These are great news! After following the steps described in installation instructions, we can try again with a simplecv shell:

SimpleCV:1> camera = Camera()
/bin/sh: 1: lsof: not found
/dev/video0 does not support memory mapping
munmap: Invalid argument 
munmap: Invalid argument
munmap: Invalid argument
munmap: Invalid argument
Unable to stop the stream.: Bad file descriptor
munmap: Invalid argument
munmap: Invalid argument
munmap: Invalid argument 
munmap: Invalid argument

Don’t worry about those errors, a camera.getImage() we’ll work, and that’s it! Now you have latest OpenCV and SimpleCV. Your raspicam working and all your CPU power available for Artificial Vision.

Now it’s your choice to use OpenCV, SimpleCV or both together, enjoy!

blog comments powered by Disqus