Install the Python and OpenCV tutorials on the Raspberry Pie 2 or the raspberry Pie B + _python

Source: Internet
Author: User
Tags virtual environment virtualenv

My Raspberry Pi 2 just arrived yesterday, this guy looks very small and cute.

This little guy has 4 cores 900MHZ of processor, 1G RAM. You know, Raspberry Pi 2 is much faster than most computers in my high school computer lab.

Anyway, since Raspberry Pi 2 was released, I've received a lot of requests that I can write a detailed description of how to install OpenCV and Python on it.

So if you want to start running in Raspberry Pi OpenCV and python, just look down!

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

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

Finally, we will use 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 and walking back and forth in my apartment.
install OpenCV and python on the raspberry Pi 2/b+

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.

$ 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

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.

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

$ 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 that is 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:

Install Virtualenv and Virtualenvwrapper

$ sudo pip install virtualenv virtualenvwrapper

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

Export workon_home= $HOME/.virtualenvs
source/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 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.

$ 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_pyth on_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 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:

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

$ 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 an example of running on my Raspberry Pi, I ssh login to Raspberry Pi, and then read and display an image.
Summary

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

The installation instructions may change as Raspberry Pi is upgraded. If you are experiencing any extreme situations or changes to the installation instructions, please feel free to contact me. Of course I can't guarantee that every email will reply, so it's a good idea to summarize how to install OpenCV and Python on Raspberry Pi as a list.

Shortly thereafter, I will talk about how to operate its additional camera in Raspberry Pi.

Before that, 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 within 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.