Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Raspberry Pi for Secret Agents

You're reading from  Raspberry Pi for Secret Agents

Product type Book
Published in Apr 2013
Publisher Packt
ISBN-13 9781849695787
Pages 152 pages
Edition 1st Edition
Languages
Concepts
Author (1):
Stefan Sjogelid Stefan Sjogelid
Profile icon Stefan Sjogelid

Table of Contents (12) Chapters

Chapter 3. Webcam and Video Wizardry

Aha, good! Still with us, our sly grasshopper is! For our second day of spy class, we'll switch our gear of perception from sound to sight. We're going to show you how to get the most out of your webcam, help you secure your perimeter, and then end it on a high note with some mindless mischief.

Setting up your camera


Go ahead, plug in your webcam and boot up the Pi; we'll take a closer look at what makes it tick.

Note

If you experimented with the dwc_otg.speed parameter to improve the audio quality during the previous chapter, you should change it back now by changing its value from 1 to 0, as chances are that your webcam will perform worse or will not perform at all, because of the reduced speed of the USB ports.

Meet the USB Video Class drivers and Video4Linux

Just as the Advanced Linux Sound Architecture (ALSA) system provides kernel drivers and a programming framework for your audio gadgets, there are two important components involved in getting your webcam to work under Linux:

  • The Linux USB Video Class (UVC) drivers provide the low-level functions for your webcam, which are in accordance with a specification followed by most webcams produced today.

  • Video4Linux (V4L) is a video capture framework used by applications that record video from webcams, TV tuners, and other video-producing...

Capturing your target on film


All right, let's see what your sneaky glass eye can do!

We'll be using an excellent piece of software called MJPG-streamer for all our webcam capturing needs. Unfortunately, it's not available as an easy-to-install package for Raspbian, so we will have to download and build this software ourselves.

Often when we compile software from source code, the application we're building will want to make use of code libraries and development headers. Our MJPG-streamer application, for example, would like to include functionality for dealing with JPEG images and Video4Linux devices.

  1. Install the libraries and headers for JPEG and V4L by typing in the following command:

    pi@raspberrypi ~ $ sudo apt-get install libjpeg8-dev libv4l-dev
    
  2. Next, we're going to download the MJPG-streamer source code using the following command:

    pi@raspberrypi ~ $ wget http://mjpg-streamer.svn.sourceforge.net/viewvc/mjpg-streamer/mjpg-streamer/?view=tar -O mjpg-streamer.tar.gz
    

    The wget utility is an...

Detecting an intruder and setting off an alarm


Let's dive right in to the wonderful world of motion detection!

The basic idea of motion detection is pretty simple from a computer's point of view—the motion detection software processes a continuous stream of images and analyzes the positions of the pixels that make up the image. If a group of contiguous pixels above a certain threshold starts to change from one frame to the next, that must be something moving. The tricky part of motion detection is weeding out false positives triggered by naturally occurring changes in light and weather conditions.

  1. We'll be working with a motion detection application called Motion. Install it using the usual command:

    pi@raspberrypi ~ $ sudo apt-get install motion
    
  2. With Motion installed, the next step is to create a configuration file for our webcam. The Motion installation puts a sample configuration file inside the /etc/motion directory. We will use this configuration file as a template and modify it for our...

Turning your TV on or off using the Pi


For this example, we are relying on a technology called Consumer Electronics Control (CEC), which is a feature of the HDMI standard for sending control messages to your home electronics equipment.

To help us send these messages, we'll need a software package called libCEC. Unfortunately, the libCEC version that is currently part of the Raspbian package repository doesn't actually support the Raspberry Pi, so we'll need to build our own software from source code.

  1. Before building the software, we will need to add some developer headers and code libraries that libCEC relies on:

    pi@raspberrypi ~ $ sudo apt-get install autoconf libtool libudev-dev liblockdev1-dev
    
  2. Next, we check out the libCEC source code from the project's Git repository:

    pi@raspberrypi ~ $ git clone git://github.com/Pulse-Eight/libcec.git
    
  3. Now we enter the source directory and build the software using the following sequence of commands:

    pi@raspberrypi ~ $ cd libcec
    pi@raspberrypi ~/libcec...

Scheduling video recording or staging a playback scare


At this stage, you already know all the individual techniques used for this example. It's simply a matter of combining what you've learned so far to achieve the effect you want.

We'll try to illustrate a bit of everything with one sweet prank: you will prepare your Pi at home, take it over to your friend's house, and sneakily hook it up with the living room TV. In the middle of the night, the TV will turn itself on and a creepy video of your choice will start to play. This freaky incident might repeat itself a couple of times during the night, or we could take the prank to phase two: whenever someone walks into the room, their presence is detected and the video is played.

Let's start prepping the Pi! We will assume that no network connection is available at your friend's house, so we'll have to create a new ~/autorun.sh script to perform our prank, together with an at timer in /etc/rc.local that starts counting down when the Pi is plugged...

Summary


In this chapter, we got acquainted with the two components involved in webcam handling under Linux—the USB Video Class drivers and the Video4Linux framework. We learned how to obtain important information about our webcam's capabilities; we also learned a bit about pixel formats, image resolution, and frame rates.

We proceeded to set up an MJPG-streamer video feed, accessible directly via a web browser or through VLC media player, which we could also use to record the stream for permanent storage.

Then we dove head first into motion detection systems with the introduction of the Motion application. We learned how to create an initial configuration suitable for verifying and tweaking the motion detection mechanism, and how to set off alarms upon detection. After a successful first run, a second configuration was made, which added evidence collection capabilities; we also explored how to view that evidence. Not content with letting any unused webcams in the home go to waste, we explored...

lock icon The rest of the chapter is locked
You have been reading a chapter from
Raspberry Pi for Secret Agents
Published in: Apr 2013 Publisher: Packt ISBN-13: 9781849695787
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}