Motion Detecting CCTV

 

What is Computer Vision?

How can we make a computer 'see'?

As sumarised by Wikipedia, computer vision is a robot analogue of human vision in which information about the environment is received by one or more video cameras and processed by computer. The field of computer vision can open up lots of doors to processing and analyzing what we see in our webcams. To be able to analyze webcam data, however, we need some type of computer vision software. Luckily there’s an open source version called OpenCV, that has tons of features and support. It has a steep learning curve, though, but luckily there’s a simplified version of it called SimpleCV that makes it relatively easy to get started in the world of computer vision.



Step 1

Installing SimpleCV

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

exit()

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:

error

If so, then you will need to install an extra set of drivers before you can get it to work.


Step 2

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.


Step 3

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”

mkdir simplecv
cd simplecv
nano 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

python simplecv.py

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.


Step 4

Detecting Motion

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:


Step 5

Automatically E-mailing And Streaming Results

The last thing to make our program complete is the ability to live stream and send us an email whenever motion is detected. In order to do this, we need to edit the e-mail program we created in our last video. So make a copy of it and put it, rename it to “py_gmailer.py” and put it in your project folder. What we need to do is turn the primary portion of the code into a definition called “gmail” that is passed a variable called “png_img” which will be our image attachment. Here’s what the new py_gmailer code should look like:

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:


Step 6

Going Further

motion20160308-181410-1

The code in the last step is functional, but it is very basic and very buggy. Feel free to add and tweak it to customize your needs and make it more stable. SimpleCV has a lot of cool features you can add to make this a really nice motion detection program. Below is a more full featured version of the code I created. You’re more than welcome to use it as inspiration.