Motion Detecting CCTV
What is Computer Vision?
How can we make a computer 'see'?
As sumarised by Wikipedia, computer vision is
The obvious first step before we can use SimpleCV is to actually install it. In order to do that, there are a few pre-requisites we need to install first. So with your Raspberry Pi booted up, and with a terminal open, type in these commands to get everything installed:
sudo apt-get update sudo apt-get install ipython python-opencv python-scipy python-numpy python-setuptools sudo pip install https://github.com/sightmachine/SimpleCV/zipball/master sudo pip install pyparsing svgwrite simplecv
The last command should launch the SimpleCV interface. To test it out, you can type
camera = Camera()
to exit type
Depending on the camera you have, you should see a thumbnail of your webcam. Please note that the only way to see the thumbnail is to be logged into the Pi directly. If you’re remoted in, you probably won’t see anything. If you are using a Raspicam, like I am, then you may get an error that looks like this:
If so, then you will need to install an extra set of drivers before you can get it to work.
Installing the UV4L Driver
In order to get the Raspberry Pi Camera to work with SimpleCV (or any other software for that matter), you’ll need to install the UV4L driver. It isn’t too difficult, but finding this fix certainly took me quite a bit of googling…
wget http://www.linux-projects.org/listing/uv4l_repo/lrkey.asc && sudo apt-key add ./lrkey.asc sudo nano /etc/apt/sources.list #add this to the bottom of the sources list deb http://www.linux-projects.org/listing/uv4l_repo/raspbian/ wheezy main sudo apt-get update sudo apt-get install -y uv4l uv4l-raspicam uv4l-raspicam-extras uv4l-server sudo service uv4l-_raspicam start
After starting it, you should be able to run the simplecv command again and test the camera. Hopefully this time it will work.
Begin Writing the Program
Now that we have SimpleCV installed and working, we can start writing our code. I began by creating a project folder and then creating a new Python file called “simplecv.py”
Then you can add this code to it:
You can save it by typing “Ctrl + X”, “y” for yes, and then “enter” to save the file. Then you can run it by typing
The result should be a new window that briefly opens up with an image from your webcam and then immediately closes. But if you go into your project file folder, you should see that it also saved a copy of the image there.
A cool thing you can do to test some of the SimpleCV functionality is to add different filters to your image. You can see a few sample filters in the code below.
The next step would be to have a consistent camera feed and to save images only within parameters that we specify. In the code below, we set up a loop to constantly get a new webcam image, and another loop that looks for a left mouse click to save the image.
That’s a good start to SimpleCV, but it’s not detecting motion yet. In the next step, we’ll see how to do just that.
An awesome feature of SimpleCV is to perform arithmetic functions on images. For example, if you wanted to find what the difference was between two images, you could literally subtract one from the other and see the differences. So what we can do with our script is take a still from our webcam, wait half a second, and then take another still. This way we have two images to compare against each other so that we can constantly check for changes. To make the changes more apparent, we can convert the images to grayscale, and then binarize the differences (which takes out the gray and makes everything either black or white).
Another feature of SimpleCV is the ability to use the NumPy matrix system to store all the image pixel values as an array. By doing that, we can do more calculations on the images, such as getting the mean average of the image differences. After getting the mean, we can then set a custom threshold so that if the amount of change is past our threshold, then we can assume motion is detected and save our image. Here’s the code from that:
Automatically E-mailing And Streaming Results
Now we can import our py_gmailer program into our script and pass the image filename to the gmail definition that we created. So that you aren’t constantly getting bombarded by an image every time motion is detected, we can make a timer system that waits a specified amount of time before the e-mail is sent.
And finally, adding streaming capabilities is as simple as creating a Jpegstreamer variable and saving the camera image to it. Here is the final simplecv.py program: