tutorial on installing Python and OpenCV on Raspberry Pi 2 or Raspberry Pi B +

Source: Internet
Author: User
Tags virtual environment virtualenv
My Raspberry Pi 2 was just mailed yesterday, and this guy looks very small and cute.

This little guy has a 4-core 900MHZ processor, 1G of memory. You know, Raspberry Pi 2 is much faster than most computers in my high school computer labs.

In other words, since the release of Raspberry Pi 2, I have received many requests to write a detailed description of the installation of OpenCV and Python on top of it.

So if you want to start running OpenCV and Python on the Raspberry Pi, look down!

In the remainder of the blog post, I will provide detailed installation instructions on the Raspberry Pi 2 and Raspberry Pi B +.

I will also explain the time-consuming installation of each step. Some of these steps require more processing time. For example, the Raspberry Pi 2 compilation OpenCV takes about 2.8 hours and is 9.5 hours on the Raspberry Pi B +. So please arrange your installation accordingly.

Finally, remind us that we will use the Raspberry Pi in the Pyimagesearch Gurus Computer Vision course. Our project will include home monitoring applications such as motion detection and personnel tracking in the room.

Here is a simple example of motion detection and tracking, when I was making a phone call around the apartment.
Installing OpenCV and Python on the Raspberry Pi 2/b+

This assumes that you already have the Raspberry Pi 2/b+ and is already installed. If not, I suggest you buy one, they are both cheap and fun.

Personally, I'd rather spend a little more money on buying from Canakit. Their logistics are quick and reliable, plus their ready-to-go service is also very good.

Okay, let's start OpenCV and Python installation.
Step 0:

We assume that you have opened Raspberry Pi 2/b+. To open the terminal, we first update and upgrade the installed packages, and then update the Raspberry Pi firmware.

$ sudo apt-get update$ sudo apt-get upgrade$ sudo rpi-update


Step 1:

Install the required installation tools and packages:

$ sudo apt-get install build-essential cmake pkg-config

Both Build-essential and Pkg-config may have been installed. Just in case, we'll still type them into the Apt-get command.

Take:

Raspberry Pi B +: less than 2 minutes

Raspberry Pi 2: Less than 40 seconds
Step 2:

Install the necessary image I/O packages so that you can read images in these formats such as Jpeg,png,tiff.

$ sudo apt-get install Libjpeg8-dev libtiff4-dev libjasper-dev Libpng12-dev

Take:

Raspberry Pi B +: less than 5 minutes

Raspberry Pi 2: Less than 30 seconds
Step 3:

Install the GTK Dev Library, which is used to build the GUI. The Highgui library in OpenCV also needs it to display the image on the screen.

$ sudo apt-get install Libgtk2.0-dev

Take:

Raspberry Pi B +: less than 10 minutes

Raspberry Pi 2: Less than 3 minutes
Step 4:

Install the necessary video I/O packages, OpenCV need them to read into the video file.

$ sudo apt-get install Libavcodec-dev libavformat-dev libswscale-dev Libv4l-dev

Take:

Raspberry Pi B +: less than 5 minutes

Raspberry Pi 2: Less than 30 seconds
Step 5:

The library required to install the OPENCV optimization operation.

$ sudo apt-get install Libatlas-base-dev Gfortran

Take:

Raspberry Pi B +: less than 2 minutes

Raspberry Pi 2: Less than 30 seconds
Step 6:

Install PIP:

$ wget https://bootstrap.pypa.io/get-pip.py$ sudo python get-pip.py

Take:

Raspberry Pi B +: less than 2 minutes

Raspberry Pi 2: Less than 30 seconds
Step 7:

Installing Virtualenv and Virtualenvwrapper

$ sudo pip install virtualenv virtualenvwrapper

Then, update the ~/.profile file as follows:

Export Workon_home= $HOME/.virtualenvssource/usr/local/bin/virtualenvwrapper.sh

Reload the. Profile file:

$ source ~/.profile

Create your computer vision virtual environment

$ mkvirtualenv CV

Take:

Raspberry Pi B +: less than 2 minutes

Raspberry Pi 2: Less than 2 minutes
Step 8:

Now we install the Python 2.7 development tool:

$ sudo apt-get install Python2.7-dev

Note: We will use Python2.7. Because OpenCV 2.4.X does not support Python 3, it is unclear when OpenCV 3.0 's Python interface will be perfect. So I suggest it's good to use opencv2.4.x now.

We also need to install numpy because the OpenCV python interface represents the image through a multidimensional array of numpy.

$ pip Install NumPy

Take:

Raspberry Pi B +: less than 45 minutes

Raspberry Pi 2: Less than 15 minutes
Step 9:

Download and decompression OpenCV:

$ wget-o Opencv-2.4.10.zip http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.4.10/ opencv-2.4.10.zip/download$ Unzip opencv-2.4.10.zip$ CD opencv-2.4.10

Installation:

$ mkdir build$ cd build$ cmake-d cmake_build_type=release-d cmake_install_prefix=/usr/local-d BUILD_NEW_PYTHON_SUPPORT =on-d install_c_examples=on-d install_python_examples=on-d Build_examples=on.

Take:

Raspberry Pi B +: less than 3 minutes

Raspberry Pi 2: less than 1.5 minutes

Compile OpenCV:

$ make

Important: Make sure you are in a CV virtual environment, OPENCV is also compiled according to the Python and numpy in that environment. Otherwise, OPENCV will be compiled with Python and NumPy in the system, creating a variety of problems.

Take:

Raspberry Pi B +: less than 9.5 hours

Raspberry Pi 2: Less than 2.8 hours

Finally, we install OPENCV:

$ sudo make install$ sudo ldconfig

Take:

Raspberry Pi B +: less than 3 minutes

Raspberry Pi 2: Less than 1 minutes
Step 10:

At this point, OpenCV should already be installed in the/usr/local/lib/python2.7/site-packages.

But in order to use OPENCV in a CV virtual environment, we first need to conform to the Site-packages directory that is linked to us:

$ cd ~/.virtualenvs/cv/lib/python2.7/site-packages/$ ln-s/usr/local/lib/python2.7/site-packages/cv2.so cv2.so$ ln-s /usr/local/lib/python2.7/site-packages/cv.py cv.py

Step 11:

Finally, let's test the installation of OpenCV and Python:

$ workon cv$ python>>> import cv2>>> cv2.__version__ ' 2.4.10 '

OpenCV and Python have been successfully installed into your Raspberry Pi.

This is a running example on my Raspberry pi, I ssh login to Raspberry pi and then read and display an image.
Summarize

In this blog post, I detail how to install Python and OpenCV on the Raspberry pi 2/raspberry Pi B +. also provides their installation time-consuming, please consider your installation as appropriate.

As the Raspberry Pi is upgraded, the installation instructions may vary. Please feel free to contact me if you encounter some extreme situations or changes to the installation instructions. Of course I can't guarantee that every email will be answered, so it's best to summarize how to install OpenCV and Python on Raspberry pi into a list.

Soon after, I'll talk about how to operate its attached camera in the Raspberry Pi.

Before that, take a look at the Pyimagesearch Gurus computer Vision course. We will use Raspberry Pi in some of these projects, such as building a monitoring application for motion detection and personnel tracking inside the room.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.