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.
MJPG-Streamer
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 "input_file.so -f /tmp/stream -n pic.jpg"-o "output_http.so -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.
Conclusions
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.
Infos
- Surveillance camera security: http://blog.nruns.com/blog/2013/10/28/Surveillance-Cameras-Security-Sergio/
- PiCam: http://www.raspberrypi.org/camera
- Motion: http://www.lavrsen.dk/foswiki/bin/view/Motion/WebHome
- Sample code for motion detection: http://www.raspberrypi.org/phpBB3/viewtopic.php?t=45235
- Example of a webcam application: http://www.raspberrypi.org/phpBB3/viewtopic.php?t=56209
- Installing an MJPEG server: http://blog.miguelgrinberg.com/post/how-to-build-and-run-mjpg-streamer-on-the-raspberry-pi
« Previous 1 2
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Plasma 6.3 Ready for Public Beta Testing
Plasma 6.3 will ship with KDE Gear 24.12.1 and KDE Frameworks 6.10, along with some new and exciting features.
-
Budgie 10.10 Scheduled for Q1 2025 with a Surprising Desktop Update
If Budgie is your desktop environment of choice, 2025 is going to be a great year for you.
-
Firefox 134 Offers Improvements for Linux Version
Fans of Linux and Firefox rejoice, as there's a new version available that includes some handy updates.
-
Serpent OS Arrives with a New Alpha Release
After months of silence, Ikey Doherty has released a new alpha for his Serpent OS.
-
HashiCorp Cofounder Unveils Ghostty, a Linux Terminal App
Ghostty is a new Linux terminal app that's fast, feature-rich, and offers a platform-native GUI while remaining cross-platform.
-
Fedora Asahi Remix 41 Available for Apple Silicon
If you have an Apple Silicon Mac and you're hoping to install Fedora, you're in luck because the latest release supports the M1 and M2 chips.
-
Systemd Fixes Bug While Facing New Challenger in GNU Shepherd
The systemd developers have fixed a really nasty bug amid the release of the new GNU Shepherd init system.
-
AlmaLinux 10.0 Beta Released
The AlmaLinux OS Foundation has announced the availability of AlmaLinux 10.0 Beta ("Purple Lion") for all supported devices with significant changes.
-
Gnome 47.2 Now Available
Gnome 47.2 is now available for general use but don't expect much in the way of newness, as this is all about improvements and bug fixes.
-
Latest Cinnamon Desktop Releases with a Bold New Look
Just in time for the holidays, the developer of the Cinnamon desktop has shipped a new release to help spice up your eggnog with new features and a new look.