Install OpenCV and Python__python on the raspberry Pi 2/b+

Source: Internet
Author: User
Tags virtual environment virtualenv

This assumes that you already have the Raspberry Pi 2/b+, and that it 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 buying from Canakit. Their logistics quickly and reliably, 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.
?

1 2 3 $ sudo apt-get update $ sudo apt-get upgrade $ sudo rpi-update


Step 1:

Install the required installation tools and packages:

?

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

Build-essential and Pkg-config may have been installed. Just in case, we'll type them in 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 the images in these formats such as Jpeg,png,tiff.

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

Take:

Raspberry Pi B +: less than 5 clock

Raspberry Pi 2: Less than 30 seconds
Step 3:

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

?

1 $ 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.

?

1 $ 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 that is required to install the OPENCV optimization operation.

?

1 $ 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:

?

1 2 $ 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:

Install Virtualenv and Virtualenvwrapper

?

1 $ sudo pip install virtualenv virtualenvwrapper

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

?

1 2 Export Workon_home = $HOME/. Virtualenvs source/usr/local/bin/virtualenvwrapper.sh

Reload the. Profile file:

?

1 $ source ~/. Profile

Create your computer vision virtual environment

?

1 $ 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:

?

1 $ 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 not clear when OpenCV 3.0 's Python interface will be perfected. So I suggest that it is better 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.

?

1 $ pip Install NumPy

Take:

Raspberry Pi B +: less than 45 minutes

Raspberry Pi 2: Less than 15 minutes
Step 9:

Download and decompression OpenCV:
?

1 2 3 $ 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:

?

1 2 3 $ mkdir Build $ cd Build $ cmake-d cmake_build_type = release-d Cmake_install_prefix =/usr/local-d Build_new_pyt Hon_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:

?

1 $ make

Important: Make sure you are in the CV virtual environment, OPENCV also to be compiled according to the Python and numpy in the environment. Otherwise, the 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:

?

1 2 $ sudo make install $ sudo ldconfig

Take:

Raspberry Pi B +: less than 3 minutes

Raspberry Pi 2: Less than 1 minutes
Step:

At this stage, OPENCV should already be installed in/usr/local/lib/python2.7/site-packages.

But in order to use OPENCV in the CV virtual environment, we first need to meet the link to our Site-packages directory:

?

1 2 3 $ 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

steps:

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

?

1 2 3 4 5 $ workon CV $ python >>> import cv2 >>> cv2.__version__ ' 2.4.10 '

OpenCV and Python have been successfully installed into your Raspberry Pi. This is an example of running on my Raspberry Pi, I ssh login to Raspberry Pi, and then read and display an image.

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.