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
-
Halcyon Creates Anti-Ransomware Protection for Linux
As more Linux systems are targeted by ransomware, Halcyon is stepping up its protection.
-
Valve and Arch Linux Announce Collaboration
Valve and Arch have come together for two projects that will have a serious impact on the Linux distribution.
-
Hacker Successfully Runs Linux on a CPU from the Early ‘70s
From the office of "Look what I can do," Dmitry Grinberg was able to get Linux running on a processor that was created in 1971.
-
OSI and LPI Form Strategic Alliance
With a goal of strengthening Linux and open source communities, this new alliance aims to nurture the growth of more highly skilled professionals.
-
Fedora 41 Beta Available with Some Interesting Additions
If you're a Fedora fan, you'll be excited to hear the beta version of the latest release is now available for testing and includes plenty of updates.
-
AlmaLinux Unveils New Hardware Certification Process
The AlmaLinux Hardware Certification Program run by the Certification Special Interest Group (SIG) aims to ensure seamless compatibility between AlmaLinux and a wide range of hardware configurations.
-
Wind River Introduces eLxr Pro Linux Solution
eLxr Pro offers an end-to-end Linux solution backed by expert commercial support.
-
Juno Tab 3 Launches with Ubuntu 24.04
Anyone looking for a full-blown Linux tablet need look no further. Juno has released the Tab 3.
-
New KDE Slimbook Plasma Available for Preorder
Powered by an AMD Ryzen CPU, the latest KDE Slimbook laptop is powerful enough for local AI tasks.
-
Rhino Linux Announces Latest "Quick Update"
If you prefer your Linux distribution to be of the rolling type, Rhino Linux delivers a beautiful and reliable experience.