Raspberry Pi motion detection
Use the Raspberry Pi camera to detect motion and take a photo of the cause for home security or time-lapse photography
The Raspberry Pi camera is an excellent piece of kit for many projects. With a decent sensor, video capabilities and great Python support from picamera, there’s plenty you can do with it. In the past we’ve done surveillance and time- lapse photography with the camera – but what if you could combine the two? Having the camera take photos when motion occurs reduces the strain on the Raspberry Pi for surveillance and can create a better time-lapse for more human situations. The code is fairly simple as well, plus you’ll be able to learn some special methods of storing photos.
Step 01 Enable the camera module
Before we start, make sure to update your Raspberry Pi and its firmware with apt-get update and apt-get upgrade followed by rpi-update. Once that’s done, enter the raspi- config by typing exactly that in the terminal and choosing ‘Enable’ in the Enable Camera option. Hit Finish to reboot.
Step 02 Attach the camera
Turn the Raspberry Pi off either during the reboot process or afterwards. Disconnect the power and find the slim port between the ethernet port and the HDMI port. Lift up the fastener and slot in the camera module’s ribbon, with the silver side facing towards the HDMI port.
Step 03 Install the Python modules
The code requires a couple of extra Python modules to work. First, the all-important picamera so we can better control the camera, along with the Python Image Library so we can inspect the images. Install them both with:
$ sudo apt-get install python-picamera python- imaging-tk
Step 04 Position your camera
Where are you placing your camera? Does the Raspberry Pi have adequate access to power? Are you controlling it with SSH or will it be in range of a display and keyboard/mouse? Set up your Raspberry Pi accordingly and take a sample picture to make sure it’s in the right location with:
$ raspistill -o test.jpg
Step 05 Tweak the sensitivity
In our code, difference and pixels are used to determine when a picture should be taken. The difference variable is the amount a pixel needs to change in colour to count as a change, while pixels is the number of these changed pixels that will be used to determine whether or not enough motion has been created to take a picture.
Step 06 Cross the stream
To make the comparison, we’re taking a sample image and inputting it into a stream we’ve created using io.BytesIO. It’s stored in memory and then compared with the previous image that has been saved later on in the code – this determines whether or not a new photo should be taken.
Full code listing
import io import os import picamera import time from datetime import datetime from PIL import Image camera = picamera.PiCamera() difference = 20 pixels = 100 width = 1280 height = 960 def compare(): camera.resolution = (100, 75) stream = io.BytesIO() camera.capture(stream, format = ‘bmp’) stream.seek(0) im = Image.open(stream) buffer = im.load() stream.close() return im, buffer def newimage(width, height): time = datetime.now() filename = “motion-%04d%02d%02d-%02d%02d%02d.jpg” % (time.year, time.month, time.day, time.hour, time.minute, time.second) camera.resolution = (width, height) camera.capture(filename) print “Captured %s” % filename image1, buffer1 = compare() timestamp = time.time() while (True): image2, buffer2 = compare() changedpixels = 0 for x in xrange(0, 100): for y in xrange(0, 75): pixdiff = abs(buffer1[x,y] - buffer2[x,y]) if pixdiff > difference: changedpixels += 1 if changedpixels > pixels: timestamp = time.time() newimage(width, height) image1 = image2 buffer1 = buffer2
Want to do more with your Raspberry Pi? Try out the RasPi mag for only 69p an issue, with loads of projects and things to do with your Pi every month.