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
-
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.
-
Armbian 24.11 Released with Expanded Hardware Support
If you've been waiting for Armbian to support OrangePi 5 Max and Radxa ROCK 5B+, the wait is over.
-
SUSE Renames Several Products for Better Name Recognition
SUSE has been a very powerful player in the European market, but it knows it must branch out to gain serious traction. Will a name change do the trick?
-
ESET Discovers New Linux Malware
WolfsBane is an all-in-one malware that has hit the Linux operating system and includes a dropper, a launcher, and a backdoor.
-
New Linux Kernel Patch Allows Forcing a CPU Mitigation
Even when CPU mitigations can consume precious CPU cycles, it might not be a bad idea to allow users to enable them, even if your machine isn't vulnerable.
-
Red Hat Enterprise Linux 9.5 Released
Notify your friends, loved ones, and colleagues that the latest version of RHEL is available with plenty of enhancements.
-
Linux Sees Massive Performance Increase from a Single Line of Code
With one line of code, Intel was able to increase the performance of the Linux kernel by 4,000 percent.
-
Fedora KDE Approved as an Official Spin
If you prefer the Plasma desktop environment and the Fedora distribution, you're in luck because there's now an official spin that is listed on the same level as the Fedora Workstation edition.