The Raspberry Pi as a motion-sensing webcam

Motion Detection

The fine art of surveillance is fully automatic motion detection and response to unexpected movement of the monitored scene.

The methods used thus far (i.e., continuous recording and storage of images) create tons of uninteresting photos or hours of boring video material. The criterion for "interesting" is defined by surveillance specialists as containing movement within or significant changes in the observed scene that result in highly divergent images. Normally, you would want the software to delete images or video sequences in real time that do not, or only marginally, differ from the previous image.

My experiences with the Motion tool [3] and classical webcams have been good. Motion is based on the standard Video4Linux video stack and associated device files. In the lab, unfortunately, the PiCam did not support Motion. However, a little research revealed a Python script [4] that does the job just as well.

The software initially takes a picture, adds a timestamp to the file name, and stores the file. The script then compares two consecutive images in a low-resolution stream. If the changes exceed a threshold, the Pi stores a high-resolution image. The image quality will depend strongly on the available light.

For successful monitoring, you still need a solution that makes a live video stream available to multiple clients, preferably in a web browser. Help is at hand, either in the form of the ancient but popular Flash or state-of-the-art HTML5 with video support.

Even easier, however, is a variant with Motion JPEG (MJPEG), wherein a video is simply series of JPEG files. At the request of a client, the server sends back a series of images that the browser shows as a movie.


In a setup at the Raspberry Pi website [5], a tutorial describes how a combination of MJPG-streamer and the PiCam works. The MJPG-streamer code is not in the standard Raspbian repository, so you need to compile the components from the source code, at least for a data source and HTTP output.

You would then launch raspistill with the timelapse option and save the image thus generated in a folder that MJPG-streamer monitors. A possible call to Raspistill would be:

raspistill --nopreview -w 640 -h 480 -q 5 -o /tmp/stream/pic.jpg -tl 100 -t 9999999-th 0:0:0 &

In this case, the resolution can be modified by changing the -w and -h parameters. The user starts MJPG-streamer (after installing according to instructions [6]) as follows:

LD_LIBRARY_PATH=/usr/local/lib mjpg_streamer -i " -f /tmp/stream -n pic.jpg"-o " -w /usr/local/www"

If you add the line

<img src="http://SERVER:8080/?action=stream"/>

to your HTML (with your SERVER information), you can view it on a separate website.


The options for building a network camera to monitor a room are many. In contrast to cameras with embedded Linux, you will find it easier to set up a hardened system yourself.

The admin of a full-fledged Linux installation can define iptables rules that govern the flow of data through a VPN and take many other precautions. In terms of price, cheaper alternatives might be available, but they do not offer the flexibility or security the Raspberry Pi solution provides.

The experiments described here are by no means the end of the line. For example, an infrared version of the PiCam is now available for night shots, and you could redirect the stream to a major streaming platform that could then serve up the data in various formats and resolutions.

Overall, the trickiest part of the project is packaging the whole thing. Although you can buy professional cases for outdoor installations, if you are not satisfied with an off-the-shelf solution, you might have to put your crafting skills to the test.

The Author

Konstantin Agouros works for n.runs AG as a consultant for network security. His main focus is on telecommunications providers. His book DNS/DHCP (in German) is published by Open Source Press.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Raspberry Pi Pet Camera

    A Raspberry Pi, a Pi-compatible camera, and a mesh VPN are all you need to watch your pets from afar.

  • motionEyeOS

    Set up a professional video surveillance system armed with motionEyeOS and a Raspberry Pi.

  • Motion Detection

    The motion detector software, Motion, monitors the video signal from one or multiple cameras and is able to detect whether a significant part of the picture has changed, record and track movement, or launch arbitrary external commands to trigger other actions.

  • DIY Lenticular Camera

    You can take lenticular images with a homemade camera to re-create the "wiggle" pictures of your childhood.

  • Security Lessons: Secure Video

    Unless your surveillance cameras are wired directly to a video board, they are notoriously insecure. We show you how to keep your cameras from being compromised.

comments powered by Disqus