Setting up Raspberry Pi
Raspberry Pi is a single-board computer (SBC) with a low cost and low power consumption. It can be used as a desktop, a server, or an embedded system that controls other electronics. The Pi comes in two versions, Model A and Model B. We want Model B because it has more memory (an important consideration for image processing) and it has an Ethernet port, whereas Model A does not. Model B costs about $35.
Several operating systems are available for Raspberry Pi. We will use Raspbian, which is a port of Debian Wheezy (a major Linux distribution) to ARM.
Download the latest Raspbian disk image from http://downloads.raspberrypi.org/raspbian_latest. Unzip the downloaded file. At the time of writing, the ZIP file is called 2014-09-09-wheezy-raspbian.zip
and the unzipped file is called 2014-09-09-wheezy-raspbian.img
. Since your filenames can differ, we will refer to the unzipped file as <raspbian_image>
.
We need to burn <raspbian_image>
to an SD card of size 4 GB or larger. Any existing data on the card will be lost in the process. The steps to burn the disk image are platform-specific, as follows:
- On Windows, download Win32 Disk Imager from http://sourceforge.net/projects/win32diskimager/ and unzip it to any destination. With the SD card inserted, run
Win32DiskImage.exe
, which should be inside the unzipped folder. Click the folder icon next to the Image File field and open<raspbian_image>
. Select the SD card in the Device drop-down menu. Click the Write button. - On Mac, first we must check which device path is assigned to the SD card. Before inserting the card, run the following command in Terminal:
$ diskutil list
You should see information on one or more devices such as
/dev/disk0
. Now, insert the SD card and rerun the same command. A new device, such as/dev/disk1
, should be in the list this time. This device is the SD card and we will refer to it as<sd_card_device>
.Run the following commands in Terminal to format the card and burn the image to it:
$ sudo diskutil eraseDisk FAT32 UNTITLED <sd_card_device> $ diskutil unmountDisk <sd_card_device> $ sudo dd if=<raspbian_image> of=<sd_card_device> bs=1m $ diskutil eject <sd_card_device>
The
dd
command might run for several minutes without producing any command-line output. Do not assume it is frozen. - On Linux, first we must check which device path is assigned to the SD card. Before inserting the card, run the following command in Terminal:
$ df -h
You should see information on one or more devices such as
/dev/sda1
. Now, insert the SD card and rerun the same command. One or more new devices, such as/dev/sdb1
,/dev/mmcblk0p1
, or/dev/ssd1
, should be in the list this time. These devices are partitions of the SD card. Their common suffix (before the last number or the lastp
and number), such as/dev/sdb
,/dev/mmcblk0
, or/dev/sdd
, is the SD card itself and we will refer to it as<sd_card_device>
.Run the following commands in Terminal to format the card and burn the image to it:
$ sudo mkdosfs -F 32 <sd_card_device> $ for n in <sd_card_device>* ; do umount $n ; done $ sudo dd if=<raspbian_image> of=<sd_card_device> bs=1m $ for n in <sd_card_device>* ; do umount $n ; done
Again, the
dd
command might run for several minutes without producing any command-line output. Do not assume it is frozen.
Ensure that the Raspberry Pi's Micro USB power cable is disconnected. Connect an HDMI monitor or TV, USB keyboard, USB mouse, and Ethernet cable. Then, insert the SD card firmly into the slot on the bottom on the Pi. Connect the Pi's power cable. The Pi should start booting from the SD card as shown in the following screenshot:
Soon, we should see a configuration menu like the one in the preceding screenshot. With option 1 Expand Filesystem selected, hit Enter. This option ensures that the entire space on the SD card is available to Raspbian for storage.
Use the up and down arrow keys to select other options. For option 3 Enable Boot to Desktop, hit Enter, select Yes, and hit Enter again. For option 4 Internationalisation Options, select an appropriate locale, time zone, and keyboard. Raspbian defaults to a UK keyboard layout, which will cause problems if you have a US or other keyboard. If you are using the Raspberry Pi Camera Module or Pi NoIR (as described in the next section of this chapter), select option 5 Enable Camera, hit Enter, Select Enable, and hit Enter again. Configure all other options as you wish. Once you are satisfied with the configuration, use the right arrow key to select Finish. Hit Enter.
A login screen should appear. Enter the username pi and the password raspberry. Prepare to witness the wonders of the Raspbian desktop!
The grand raspberry stands before us in all its flavorful glory!
At heart (or in its seeds), Raspbian is "just" Debian Linux with an LXDE desktop and some special developer tools. If you are familiar with Debian or derivatives such as Ubuntu, you should feel right at home. Otherwise, you might want to explore the tutorials for beginners that are posted on the Raspberry Pi site at http://www.raspberrypi.org/archives/tag/tutorials.
Now, as an exercise, let's share our Raspbian desktop via Virtual Network Computing (VNC) so that we can control it from a Windows, Mac, or Linux machine.
On the Pi, we first need to determine our local network address, which we will refer to as <pi_ip_address>
. Open LXTerminal and run the following command:
$ ifconfig
The output should include a line beginning with something like inet addr:192.168.1.93
, though the numbers will probably differ. In this example, <pi_ip_address>
is 192.168.1.93
.
Now, we need to install a VNC server on the Pi by running the following command:
$ sudo apt-get install tightvncserver
To start the server, run this command:
$ tightvncserver
When prompted, enter a password that other users must enter while connecting to this VNC server. Later, if you want to change the password, run this command:
$ vncpasswd
Tip
Unless the Pi (or the Ethernet socket to which it is connected) has a static IP address, the address can change whenever we reboot. Thus, on reboot, we would need to run ifconfig
again to determine the new address. Also, after rebooting, we need to run tightvncserver
to relaunch the VNC server. For instructions on making the Pi's IP address static and automatically running tightvncserver
on boot, refer to Neil Black's online Raspberry Pi Beginner Guide: http://www.neil-black.co.uk/raspberry-pi-beginners-guide#.UsuIVPYnXnY.
Now, on another machine on the same local network, we can access the Pi's desktop via a VNC client. The steps are platform-dependent, as follows:
- On Windows, download VNC Viewer from https://www.realvnc.com/download/. Unzip it to any destination and run the executable file (such as
VNC-Viewer-5.1.0-Windows-64bit.exe
), which is inside the unzipped folder. Entervnc://<pi_ip_address>:5901
in the VNC Server field and click the Connect button. When prompted, enter the VNC password that you created earlier. - On Mac, open Safari and enter
vnc://<pi_ip_address>:5901
in the address bar. A window, Connect to Shared Computer, should appear. Click the Connect button. When prompted, enter the VNC password that you created earlier. - Ubuntu normally comes with a VNC client called Vinagre. However, if we do not already have Vinagre, we can install it on Ubuntu or any Debian-based system by running the following command in Terminal:
$ sudo apt-get install vinagre
Open Vinagre. (It might be listed as Remote Desktop Viewer in our Applications menu or launcher.) Click the Connect button in the toolbar. Enter
vnc://<pi_ip_address>:5901
in the Host: field. Click the Connect button in the lower-right corner.
Now you know how to prepare and serve Pi.
Setting up the Raspberry Pi Camera Module
Raspbian supports most USB webcams out of the box. Also, it supports the following Camera Serial Interface (CSI) cameras, which offer faster transfer speeds:
- Raspberry Pi Camera Module: A $25 RGB camera
- Pi NoIR: A $30 variant of the same camera, with the infrared (IR) block filter removed so that it is sensitive to not only the visible light, but also the adjacent part of the infrared spectrum (near infrared or NIR)
Refer to the official tutorial for details on setting up either the Camera Module or the NoIR at http://www.raspberrypi.org/help/camera-module-setup/.
Compared to a USB webcam, the Camera Module or NoIR improves our chances of achieving high enough frame rates for interactive computer vision on the Pi. For this reason, I recommend these Pi-specific CSI cameras. However, commensurate with the low price, they have poor color rendition, mediocre auto-exposure, and fixed focus.
If in doubt, choose the Camera Module over the NoIR because, depending on the subject and lighting, NIR may interfere with vision rather than aid it.
At the time of writing, the Camera Module and NoIR do not work out of the box with OpenCV. We need to load a kernel module that adds support for the cameras via the Video for Linux 2 (V4L2) drivers. To do this for a single session, run the following command in Terminal:
$ sudo modprobe bcm2835-v4l2
Alternatively, to always load the kernel module on boot up, run the following command that appends the module to the /etc/modules
file:
$ echo "bcm2835-v4l2" | sudo tee -a /etc/modules
Now we can use the Camera Module or the NoIR with any camera software that supports V4L2 drivers, including OpenCV.