Chapter 16. Raspberry Pi Portable Webcam

In this chapter you learn how to take still pictures and record video with your Raspberry Pi. Perhaps you want to investigate time lapse photography, install a baby monitor, set up a security camera, deploy a bird feeder cam, or simply snap interesting images.

Prior to May 2013, when the Raspberry Pi Foundation introduced the Raspberry Pi Camera Board, enthusiasts did their best to use their USB webcams with their Raspberry Pi units. To be sure, this chapter teaches you how to use the Pi with third-party cameras. However, I must tell you that the Raspberry Pi Camera Board is pretty slick!

I conclude this lesson with step-by-step instructions and best practice advice for putting your Raspberry Pi on battery and therefore freeing you up to take your new webcam wherever you need to take it. Let’s get started!

About the Raspberry Pi Camera Board

As you know, the Model A and Model B boards include a Camera Serial Interface 2 (CSI-2) camera connector, shown in Figure 16.1. The interface is labeled S5 and is located between the USB and HDMI ports on the Pi PCB.

Image

FIGURE 16.1 The MIPI CS-2 camera interface on a Model A board

Initially the Foundation said nothing as to whether it would create a camera to connect to this interface, leaving Raspberry Pi enthusiasts to speculate as to how they could access the CSI-2 interface through hardware hacking. Other Pi users simply plugged in their USB-connected cameras; we cover that idea later in this chapter.

This situation cleared up in May 2013 when the Raspberry Pi Foundation announced a $25 accessory called the Raspberry Pi Camera Board, which does in fact connect to the Pi through the CSI-2 interface.

The Raspberry Pi camera board is available through the typical channels:

Image RS Components: http://is.gd/6ol2Gq

Image Premier Farnell/Element 14: http://is.gd/xJSkbQ

Physically, the CSI connector (the one shown in Figure 16.1) implements a 15-pin flex ribbon cable. One end connects to the CSI-2 interface on the Raspberry Pi PCB. The other end of that cable is soldered directly to the Raspberry Pi Camera Board, as shown in Figure 16.2.

Image

FIGURE 16.2 Raspberry Pi Camera Board PCB

The specs for this board (also called Camera Module) are listed in Table 16.1.

Image

TABLE 16.1 Raspberry Pi Camera Board Specifications

I think you’ll find that the Raspberry Pi camera board specs compare favorably with those of, say, the iPhone 4 from summer 2010. Not too shabby! Actually, I found that the Camera Board shoots better video than many entry-level webcams I’ve used in the past.

The Camera Board ships in an anti-static bag enclosed by a minimalist paper box. When you handle the Camera Board, be careful not to kink the ribbon cable—it is on the delicate side. You also want to avoid touching the camera lens to avoid fingerprint smudges.

With no further ado, let’s get your new Raspberry Pi Camera Board set up and start snapping some pictures and recording some video!

Installing and Configuring the Raspberry Pi Camera Board

Getting the Raspberry Pi Camera Board up and running consists of two phases:

Image Preparing the Pi and installing the camera driver

Image Physically installing the camera

As usual, in this chapter I assume you’re using the official Raspbian Linux distribution.

Now that you have installed the necessary device drivers and readied the Raspberry Pi to use the Camera Board, let’s connect the add-on to the Pi’s PCB.

Alrighty then! As you can see in Figure 16.5, the Raspberry Pi and the Camera Board are now (hopefully) a functional unit.

Image

FIGURE 16.5 The Raspberry Pi and the Camera Board add-on make a nice pair, don’t they?

Using the Camera Board

The Raspberry Pi Foundation provides two command-line utilities for using the Camera Board:

Image raspistill: Used to take still images in both JPEG and RAW formats

Image raspivid: Used to record video by using the H.264 codec

In the next section I cover how to use raspistill to take still pictures. In the section that follows I turn your attention to shooting full-motion video (FMV) using raspivid.

Capturing Still Pictures

Let’s begin by obtaining some command-line syntax help:

raspistill | less

Figure 16.6 shows you the screen output for raspistill; this should serve as a nice reference for you.

Image

FIGURE 16.6 raspistill command syntax

You can also download the full documentation for the Camera Board commands from http://is.gd/18PvNf.

To tell your Pi to snap a picture, enter the following command:

raspistill -o myimage.jpg

When you run raspistill, you see an LED light up on the Camera Board for approximately four seconds; the image is exposed just before the LED goes out.

Any photographs you capture are stored in your present working directory. Thus, if you execute the raspistill command in the context of your home directory, that’s where your files reside by default.

The -o switch enables you to name the images using whatever file name you input after the switch, myimage.jpg in this example.

From LXDE, you can double-click the image files to open them in your default web browser. Alternatively, you can right-click them and select ImageMagick (display) to open them in the ImageMagick open source image viewer.

Despite the lack of a flash or manual focus override, the picture quality is actually pretty good. Take a look at Figure 16.7 to see yours truly posing for your viewing (and laughing) pleasure:

Image

FIGURE 16.7 Despite the poor subject, you can see that the Raspberry Pi Camera Board takes a pretty good picture.

Let’s buzz through some more sample syntax to give you a better idea as to what raspistill can do for you. You can, for example, tell your Pi’s camera to take a picture at a set delay or image quality.

Take an image with a quality of 50% and a “shutter” delay of 10 seconds:

raspistill -o image3.jpg -q 50 -t 10000

The -q parameter goes from 0 (lowest quality) to 100 (highest quality). Quality, in this case, refers to the degree of JPEG compression that is applied to captured images. JPEG is a lossy compression algorithm, so even images taken at quality 100 will have some pixel loss due to the file format.

The timer value adds an exposure delay and works in thousands of seconds (milliseconds). Thus, a value of 5000 represents a 5 second delay.

Take an image with custom dimensions, verbose command output, a quality of 80%, and a one-second delay:

raspistill -v -w 1024  -h 768 -q 80 -o image4.jpg -t 1000

The verbose (-v) parameter is useful for educational and troubleshooting purposes. In fact, let me show you the output of the previous raspistill command example:

pi@raspberrypi ~ $  raspistill -v -w 1024 -h 768 -q 80 -o image4.jpg -t 1000 >
output.txt

raspistill Camera App v1.2

Width 1024, Height 768, quality 80, filename image4.jpg
Time delay 1000, Raw no
Thumbnail enabled Yes, width 64, height 48, quality 35
Full resolution preview No

Preview Yes, Full screen Yes
Preview window 0,0,1024,768
Opacity 255
Sharpness 0, Contrast 0, Brightness 50
Saturation 0, ISO 400, Video Stabilisation No, Exposure compensation 0
Exposure Mode 'auto', AWB Mode 'auto', Image Effect 'none'
Metering Mode 'average', Colour Effect Enabled No with U = 128, V = 128
Rotation 0, hflip No, vflip No
ROI x 0.000000, y 0.000000, w 1.000000 h 1.000000
Camera component done
Encoder component done
Starting component connection stage
Connecting camera stills port to encoder input port
Opening output file image4.jpg
Enabling encoder output port
Starting capture 1
Finished capture 1
Closing down
Close down completed, all components disconnected, disabled and destroyed

Although the previous verbose output looks like so much gobbledygook at first glance, given experience and practice you’ll come to appreciate the degree of detail that raspistill gives you. The verbose output leaves nothing to the imagination, so you can figure out the source of any unexpected behavior you see in taking still pictures with your Raspberry Pi.


Note: About Flash

Because neither the Raspberry Pi Camera Board nor any webcam I’ve ever used includes a built-in flash, you need to pay attention to ambient light when you capture still pictures or video. If you visit the Raspberry Pi forums (check out these threads: http://is.gd/LaKy6m; http://is.gd/d1SoH5) you’ll find some enthusiastic debate regarding the relative difficulty of accessing the Pi’s GPIO pins to attach and sync a flash bulb with the camera sensor.

The consensus among Pi developers at the time of this writing is that the Omnivision Camera Board firmware does include support for a flash, but digging into that firmware source code isn’t something that the Raspberry Pi Foundation has addressed yet.


Recording Video with the Camera Board

As I mentioned earlier, the Raspberry Pi Foundation gives you the raspivid command-line utility to capture full-motion video on your Pi.

Let’s get right into the thick of things by learning the raspivid command syntax:

raspivid | less

For those of you who want to see the raspivid syntax right now (nothing like instant gratification, right?), let me give you the results of raspivid --help.

pi@raspberrypi ~ $ raspivid --help
Display camera output to display, and optionally saves an H264 capture at requested bitrate
usage: raspivid [options]
Image parameter commands
-?, --help      : This help information
-w, --width     : Set image width <size>. Default 1920
-h, --height    : Set image height <size>. Default 1080
-b, --bitrate   : Set bitrate. Use bits per second (e.g. 10MBits/s would be -b
10000000)
-o, --output    : Output filename <filename> (to write to stdout, use '-o -')
-v, --verbose   : Output verbose information during run
-t, --timeout   : Time (in ms) to capture for. If not specified, set to 5s. Zero
to disable
-d, --demo      : Run a demo mode (cycle through range of camera options, no capture)
-fps, --framerate       : Specify the frames per second to record
-e, --penc      : Display preview image *after* encoding (shows compression
artifacts)
-g, --intra     : Specify the intra refresh period (key frame rate/GoP size)
Preview parameter commands
-p, --preview   : Preview window settings <'x,y,w,h'>
-f, --fullscreen        : Fullscreen preview mode
-op, --opacity  : Preview window opacity (0-255)
-n, --nopreview : Do not display a preview window
Image parameter commands
-sh, --sharpness        : Set image sharpness (-100 to 100)
-co, --contrast : Set image contrast (-100 to 100)
-br, --brightness       : Set image brightness (0 to 100)
-sa, --saturation       : Set image saturation (-100 to 100)
-ISO, --ISO     : Set capture ISO
-vs, --vstab    : Turn on video stablisation
-ev, --ev       : Set EV compensation
-ex, --exposure : Set exposure mode (see Notes)
-awb, --awb     : Set AWB mode (see Notes)
-ifx, --imxfx   : Set image effect (see Notes)
-cfx, --colfx   : Set colour effect (U:V)
-mm, --metering : Set metering mode (see Notes)
-rot, --rotation        : Set image rotation (0-359)
-hf, --hflip    : Set horizontal flip
-vf, --vflip    : Set vertical flip
-roi, --roi     : Set region of interest (x,y,w,d as normalised coordinates [0.0-1.0])
Notes
Exposure mode options :
off,auto,night,nightpreview,backlight,spotlight,sports,snow,beach,verylong,fixedfps,antishake,fireworks
AWB mode options :
off,auto,sun,cloud,shade,tungsten,fluorescent,incandescent,flash,horizon
Image Effect mode options :
none,negative,solarise,sketch,denoise,emboss,oilpaint,hatch,gpen,pastel,
watercolour,film,blur,saturation,colourswap,washedout,posterise,colourpoint,
colourbalance,cartoon
Metering Mode options :
average,spot,backlit,matrix

You probably noticed that the raspivid command syntax is almost identical to that of raspistill. This behavior, of course, is by design.

Now let’s record a quick five-second video:

raspivid -o fiveseconds.h264

By default, the camera’s video capture is full 1080p HD at 1920x1080 pixels. Of course, you can make a smaller capture; let’s make a 15-second clip:

raspivid -o smallvid.h264 -t 15000 -w 1024 -h 768


Note: Perhaps a Larger SD Card Is in Order...

Recording at 1080p equates to a disk storage footprint of 17 megabytes (MB) per second, or 115MB per minute of video. Thus if you plan on capturing a significant amount of video with your Pi, you might want to purchase a higher capacity SD card.


Of course, the Raspberry Pi Camera Board has no microphone, so your videos won’t have any audio. Actually, adding an audio feed to your video captures is a good Raspberry Pi project idea!

You can get full documentation on Omxplayer, including keyboard controls, by visiting the Embedded Linux Wiki at http://is.gd/PNC7Mf.

If you have trouble with Omxplayer, you can try my personal favorite media player on any platform—VLC (http://is.gd/kfwzk5):

sudo apt-get install -y vlc
vlc smallvid.mp4

From LXDE, you can also right-click your MP4 file and select VLC media player from the shortcut menu.

Using a Third-Party USB Webcam

If you already have a Raspberry Pi-compatible USB webcam, feel free to use that piece of hardware instead of purchasing the Camera Board. You can check your webcam against the list of verified peripherals here at http://is.gd/ZJA79A.

For my part, I have a nifty Logitech HD Webcam C615, shown in Figure 16.8, that works on my Pi like a champ even without a powered hub!

Image

FIGURE 16.8 I actually prefer using a third-party webcam to the Raspberry Pi Camera Board.

For this exercise we use an awesome piece of open source software called Motion.

The Pale Blue Dot blog (http://is.gd/XS6fY8) has good instructions for setting up Motion to run as a service and auto-start each time your Pi boots up. For security reasons, though, I advise you to be careful about this, especially if your Pi is publicly accessible.

You’re probably wondering if the raspistill and raspivid commands that were written to function with the official Raspberry Pi Camera Board work with a third-party webcam. I’ve tested this out for you, and the answer is no. If you try to run raspistill or raspivid against a third-party webcam, you will receive an error message that says in part:

Camera is not detected. Please check carefully the camera module is installed
correctly.

Setting Up Your Webcam

As you just saw, Motion provides you with a video stream and a web browser in one fell swoop. But what if you wanted your Raspberry Pi cam to monitor a particular area (be it your front door, your back yard, your fish tank, whatever) and snap a picture at regular intervals?

Our current Motion setup feeds a live stream to a tiny web server on port 8080 (configurable through montion.conf, naturally).

Adding a Rechargeable Battery Pack to Your Pi Camera

Attaching a rechargeable battery pack to your Raspberry Pi (especially when combined with a Wi-Fi dongle) makes your Pi eminently flexible. In this scenario, you could feasibly place your Pi webcam anywhere, either indoors or outdoors, and happily snap pictures, record video, or both. You can even program the Pi to send the capture files to a desktop computer by using FTP!

Raspberry Pi enthusiasts around the world have developed novel ways to provide 5-volt battery power to their Pis. Some of these ideas involve car batteries, 12V vehicle power sockets, and the like. However, we have much more basic and reliable methods available to us.

I suggest you look into a Lithium-Ion (Lion) battery pack that includes a Micro-B USB plug and regulated 5V output. This option means that (a) you can plug the Lion portable battery into your desktop computer or a powered USB hub to charge the battery; and (b) you can simply plug the Micro-B plug into your Pi’s power port to give it portable juice on the go!

As usual, the Embedded Linux wiki (http://is.gd/ETvw9J) has a list of external battery packs that have been verified to work with the Raspberry Pi.

Remember that the standard power draw of the Model B board is 500mA. The amount of energy in a battery pack is ordinarily given in milliampere hours (mAh). This means that a 1,000mAh battery pack can deliver 1000mA of power for one hour, or 500mA for two hours.

Adafruit sells a Lion battery pack (http://is.gd/7ID1JP) for $59.95 that contains 3300mAh worth of power. This equates to over six hours of continuous power to a normally operating Raspberry Pi unit. Pretty cool, eh?

Some Lion portable batteries, like my own Scosche IPDBAT2 shown in Figure 16.12, have two 5V USB ports with different current supplies: 1A and 2.1A. This difference is normally intended to allow the battery pack to supply power to either the iPhone (1A current) or iPad (2.1A current). For the Pi, I suggest you go with the 2.1A port.

Image

FIGURE 16.12 This is my Raspberry Pi battery pack: It gives me over six hours of power!

Python and Your Webcam

A good choice for blending the power of Python programming with your webcam is the SimpleCV vision library (http://is.gd/Db2osA). One aspect of SimpleCV that you might want to investigate is the motion and face detection capabilities built into the library.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset