Setting up Pi for Computer Vision

Make sure that you have a working, wired Internet connection with reasonable speed for this activity. Now, let's prepare our Pi for computer vision:

  1. Connect your Pi to the Internet through Ethernet or a Wi-Fi USB dongle.
  2. Run the following command to restart the networking service:
    sudo service networking restart
    
  3. Make sure that Raspberry Pi is connected to the Internet by typing in the following command:
    ping –c4 www.google.com
    

    If the command fails, then check the Internet connection with some other device and resolve the issue. After that, repeat the preceding steps again.

  4. Run the following commands in a sequence:
    sudo apt-get update
    sudo apt-get upgrade
    sudo rpi-update
    sudo reboot –h now
    
  5. After this, we will need to install a few necessary packages and dependencies for OpenCV. The following is the list of packages we need to install. You just need to connect your Pi to the Internet and type this in:
    sudo apt-get install <package-name> -y
    

    Here, <package-name> is one of the following packages:

    libopencv-dev

    libpng3

    libdc1394-22-dev

    build-essential

    libpnglite-dev

    libdc1394-22

    libavformat-dev

    zlib1g-dbg

    libdc1394-utils

    x264

    zlib1g

    libv4l-0

    v4l-utils

    zlib1g-dev

    libv4l-dev

    ffmpeg

    pngtools

    libpython2.6

    libcv2.3

    libtiff4-dev

    python-dev

    libcvaux2.3

    libtiff4

    python2.6-dev

    libhighgui2.3

    libtiffxx0c2

    libgtk2.0-dev

    libpng++-dev

    libtiff-tools

    libunicap2-dev

    opencv-doc

    libjpeg8

    libeigen3-deva

    libcv-dev

    libjpeg8-dev

    libswscale-dev

    libcvaux-dev

    libjpeg8-dbg

    libjpeg-dev

    libhighgui-dev

    libavcodec-dev

    libwebp-dev

    python-numpy

    libavcodec53

    libpng-dev

    python-scipy

    libavformat53

    libtiff5-dev

    python-matplotlib

    libgstreamer0.10-0-dbg

    libjasper-dev

    python-pandas

    libgstreamer0.10-0

    libopenexr-dev

    python-nose

    libgstreamer0.10-dev

    libgdal-dev

    v4l-utils

    libxine1-ffmpeg

    python-tk

    libgtkglext1-dev

    libxine-dev

    python3-dev

    libpng12-0

    libxine1-bin

    python3-tk

    libpng12-dev

    libunicap2

    python3-numpy

    For example, you have to install x264, then you will need to to type the following:

    sudo apt-get install x264 -y
    

    This will install the necessary package. Similarly, install all the previously mentioned packages. If a package is already installed on your Pi, then it will show the following message:

    Reading package lists... Done
    Building dependency tree       
    Reading state information... Done
    x264 is already the newest version.
    0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
    

    In this case, don't worry. This package is already installed and comes with its newest version. Just proceed with installing all the other packages in the list one by one.

  6. Finally, install OpenCV for Python with this:
    sudo apt-get install python-opencv -y
    

    This is the easiest way to install OpenCV for Python; however, there is a problem with this. Raspbian repositories may not always contain the latest version of OpenCV. For example, at the time of writing this, Raspbian repository contains 2.4.1, while the latest OpenCV version is 2.4.10. With respect to the Python API, the latest version will always contain much better support and more functionality.

    For the convenience of the readers, all these commands are included in an executable shell script, chapter07.sh, in the code bundle. Just run the script with the following command:

    ./chapter07.sh 
    

    This will install all the required packages and dependencies to get started with OpenCV on Pi.

    Note

    Another method to do the same is to compile OpenCV from the source, which I will not recommend for beginners as it's a bit complex and will take a lot of time.

Testing the OpenCV installation with Python

In Python, it's very easy to code for OpenCV. It requires very few lines of code compared to C/C++, and powerful libraries such as NumPy can be exploited for multidimensional data structures required for image processing.

Open a terminal and type python, and then type the following lines:

>>> import cv2
>>> print cv2.__version__

This will show us the version of OpenCV installed on the Pi, which is 2.4.1 in our case.

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

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