You are on page 1of 2

Projects

Camera case
Put your Pi camera in a
protective case to keep
it safe when it's set up
as a motion detector

Take it apart
With modular components, it’s easy
to disassemble and allows for various
configurations to suit your needs

Set up a Pi motion detector


Use the Raspberry Pi camera to detect motion and take a photo of the cause for home security or
time-lapse photography

Resources
T
he Raspberry Pi camera is an excellent piece The code is fairly simple as well, plus you’ll be able to
of kit for many projects. With a decent sensor, learn some special methods of storing photos.
SD card with up-to-date video capabilities and great Python support from
version of Raspbian
picamera Python module
picamera, there’s plenty you can do with it. You can
easily do surveillance and time-lapse photography with “Combine time-lapse
Raspberry Pi camera module
the camera – but what if you could combine the two?
Having the camera take photos when motion occurs
reduces the strain on the Pi for surveillance and can
photography and
create a better time-lapse for more human situations. surveillance”
94 Raspberry Pi for Beginners
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.

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. (See Fig 1.)

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 Q Fig 1. – Position the camera module's ribbon so that the silver side is facing the HDMI port
inspect the images. Install them both with:
001 $ sudo apt-get install python-picamera image and inputting it into a stream we’ve year, time.month, time.day, time.hour,
python-imaging-tk created using io.BytesIO. It’s stored in memory time.minute, time.second)
and then compared with the previous image camera.resolution = (width, height)
that has been saved later on in the code – this camera.capture(filename)
determines whether or not a new photo should print “Captured %s” % filename
be taken.
image1, buffer1 = compare()
Full Code Listing:
timestamp = time.time()
import io
import os while (True):
import picamera
import time image2, buffer2 = compare()
from datetime import datetime
from PIL import Image changedpixels = 0
for x in xrange(0, 100):
camera = picamera.PiCamera() for y in xrange(0, 75):
04 Position your camera pixdiff = abs(buffer1[x,y][1]
Where are you placing your camera? Does the difference = 20 - buffer2[x,y][1])
Raspberry Pi have adequate access to power? pixels = 100 if pixdiff > difference:
Are you controlling it with SSH or will it be in changedpixels += 1
range of a display and keyboard/mouse? Set up width = 1280
your Raspberry Pi accordingly and take a sample height = 960 if changedpixels > pixels:
picture to make sure it’s in the right location with: timestamp = time.time()
def compare(): newimage(width, height)
001 $ raspistill -o test.jpg camera.resolution = (100, 75)
stream = io.BytesIO() image1 = image2
05 Tweak the sensitivity camera.capture(stream, format = ‘bmp’) buffer1 = buffer2
In our code, difference and pixels are used to stream.seek(0)
determine when a picture should be taken. The
difference variable is the amount a pixel needs
im = Image.open(stream)
buffer = im.load()
“Having the camera
to change in colour to count as a change, while
pixels is the number of these changed pixels that
stream.close()
return im, buffer take photos when
will be used to determine whether or not enough
motion has been created to take a picture. def newimage(width, height): motion occurs
06 Cross the stream
time = datetime.now()
filename = “motion-%04d%02d%02d- reduces the strain
To make the comparison, we’re taking a sample %02d%02d%02d.jpg” % (time.
on the Raspberry Pi”
Raspberry Pi for Beginners 95

You might also like