...

代码交流,不作过多解释,看代码即可. 不懂勿问

How to Install OpenCV 3.0 and Python 3.4+ on Ubuntu

  



http://www.pyimagesearch.com/2015/07/20/install-opencv-3-0-and-python-3-4-on-ubuntu/



ubuntu_cv3py3_facedetection

A couple weeks ago I provided step-by-step install instructions to setup OpenCV 3.0 and Python 2.7+ on your Ubuntu machine.

However, one of the huge benefits of migrating to OpenCV 3.0 is the new Python 3.4+ support. In the previous 2.4.X releases of OpenCV, only Python 2.7+ was supported. But now, we can finally leverage Python 3.4+ in our new projects.

In the remainder of this blog post, I’ll detail how to install OpenCV 3.0 with Python 3.4+ bindings on your Ubuntu 14.04+ system. If you have followed along from the previous tutorial, you’ll notice that many of the steps are the same (or at least very similar), so I have condensed this article a bit. That said, be sure to pay special attention when we start working with CMake later in this tutorial to ensure you are compiling OpenCV 3.0 with Python 3.4+ support!

How to Install OpenCV 3.0 and Python 3.4+ on Ubuntu

A few weeks ago I covered how to install OpenCV 3.0 and Python 2.7+ on Ubuntu, and while this was a great tutorial (since many of us are still using Python 2.7), I think it’s really missing out on one of the major aspects of OpenCV 3.0 — Python 3.4+ support!

That’s right, up until the v3.0 release, OpenCV only provided bindings to the Python 2.7 programming language.

And for many of us, that was okay. As scientific developers and researchers, it’s a pretty standard assumption that we’ll be sequestered to Python 2.7.

However, that’s starting to change. Important scientific libraries such as NumPy, SciPy, and scikit-learn are now providing Python 3 support. And now OpenCV 3.0 joins the ranks!

In general, you’ll find this tutorial very similar to the previous one on installing OpenCV 3.0 and Python2.7 on Ubuntu, so I’m going to condense my explanations of each of the steps as necessary. If you would like to full explanation of each step, please refer to the previous OpenCV 3.0 article. Otherwise, simply follow along with this tutorial and you’ll have OpenCV 3.0 and Python 3.4+ installed on your Ubuntu system in less than 10 minutes.

Step 1: Install prerequisites

Upgrade any pre-installed packages:

Install developer tools used to compile OpenCV 3.0:

Install libraries and packages used to read various image formats from disk:

Install a few libraries used to read video formats from disk:

Install GTK so we can use OpenCV’s GUI features:

Install packages that are used to optimize various functions inside OpenCV, such as matrix operations:

Step 2: Setup Python (Part 1)

Let’s get pip , a Python package manager, installed for Python 3:

Note that I have specifically indicated python3  when installing pip . If you do not supplypython3 , then Ubuntu will attempt to install pip  on your Python 2.7 distribution, which is not our desired intention.

Alright, so I’ve said it before on the PyImageSearch blog, and I’ll see it again. You should really be using virtual environments for Python development!

We’ll be using virtualenv and virtualenvwrapper in this tutorial. These packages allow us to create entirely separate and independent Python environments, ensuring that we don’t junk up our system Python install (and more importantly, so we can have a separate Python environment for each of our projects).

Let’s use our fresh pip3  install to setup  virtualenv  and virtualenvwrapper :

Again, notice how I am specifying pip3  instead of just pip  — I’m just making it explicitly obvious that these packages should be installed for Python 3.4.

Now we can update our ~/.bashrc  file (place at the bottom of the file):

Notice how I am pointing VIRTUALENVWRAPPER_PYTHON  to where our Python 3 binary lives on our Ubuntu system.

To make these changes take affect, you can either open up a new terminal or reload your ~/.bashrc  file:

Finally, let’s create our cv  virtual environment where we’ll be doing our computer vision development using OpenCV 3.0 and Python 3.4:

Step 2: Setup Python (Part 2)

We’re halfway done setting up Python. But in order to compile OpenCV 3.0 with Python 3.4+ bindings, we’ll need to install the Python 3.4+ headers and development files:

OpenCV represents images as NumPy arrays, so we need to install NumPy into our cv  virtual environment:

If you end up getting a Permission denied error related to pip’s .cache  directory, like this:

Figure 1: If you get a "Permission Denied" error, no worries -- that's an easy fix!

Figure 1: If you get a “Permission Denied” error, no worries — that’s an easy fix!

Then simply delete the cache directory and re-run the NumPy install command:

And you should now have a nice clean install of NumPy:

Figure 2: Deleting the .cache/pip directory and re-running pip install numpy will take care of the problem.

Figure 2: Deleting the .cache/pip directory and re-running pip install numpy will take care of the problem.

Step 3: Build and install OpenCV 3.0 with Python 3.4+ bindings

Alright, our system is all setup now! Let’s pull down OpenCV from GitHub and checkout the3.0.0  version:

We’ll also need to grab the opencv_contrib repo as well (for more information as to why we need opencv_contrib , take a look at my previous OpenCV 3.0 Ubuntu install post):

Time to setup the build:

Let’s take a second to look at my CMake output:

Figure 3: It's a good idea to inspect the output of CMake to ensure the proper Python 3 interpreter, libraries, etc. have been picked up.

Figure 3: It’s a good idea to inspect the output of CMake to ensure the proper Python 3 interpreter, libraries, etc. have been picked up.

Notice how CMake has been able to pick up our Python 3 interpreter! This indicates that OpenCV 3.0 will be compiled with our Python 3.4+ bindings.

Speaking of compiling, let’s go ahead and kickoff the OpenCV compile process:

Where the 4 can be replaced with the number of available cores on your processor to speedup the compilation time.

Assuming OpenCV 3.0 compiled without error, you can now install it on your system:

Step 4: Sym-link OpenCV 3.0

If you’ve reached this step, OpenCV 3.0 should now be installed in/usr/local/lib/python3.4/site-packages/

Figure 4: The Python 3.4+ OpenCV 3.0 bindings are now installed in /usr/local/lib/python3.4/site-packages/

Figure 4: The Python 3.4+ OpenCV 3.0 bindings are now installed in /usr/local/lib/python3.4/site-packages/

Here, our OpenCV bindings are stored under the name cv2.cpython-34m.so

Be sure to take note of this filename, you’ll need it in just a few seconds!

However, in order to use OpenCV 3.0 within our cv  virtual environment, we first need to sym-link OpenCV into the site-packages  directory of the cv  environment, like this:

Notice how I am changing the name from cv2.cpython-34m.so  to cv2.so  — this is so Python can import our OpenCV bindings using the name cv2 .

So now when you list the contents of the cv  virtual environment’s site-packages  directory, you’ll see our OpenCV 3.0 bindings (the cv2.so  file):

Figure 5: In order to access the OpenCV 3.0 bindings from our Python 3.4+ interpreter, we need to sym-link the cv2.so file into our site-packages directory.

Figure 5: In order to access the OpenCV 3.0 bindings from our Python 3.4+ interpreter, we need to sym-link the cv2.so file into our site-packages directory.

Again, this is a very important step, so be sure that you have the cv2.so  file in your virtual environment, otherwise you will not be able to import OpenCV in your Python scripts!

Step 5: Test out the OpenCV 3.0 and Python 3.4+ install

Nice work! You have successfully installed OpenCV 3.0 with Python 3.4+ bindings (and virtual environment support) on your Ubuntu system!

But before we break out the champagne and beers, let’s confirm the installation has worked. First, ensure you are in the cv  virtual environment, then fire up Python 3 and try to importcv2 :

Here’s an example of me importing OpenCV 3.0 using Python 3.4+ on my own Ubuntu system:

Figure 6: OpenCV 3.0 with Python 3.4+ bindings has been successfully installed on the Ubuntu system!

Figure 6: OpenCV 3.0 with Python 3.4+ bindings has been successfully installed on the Ubuntu system!

As you can see, OpenCV 3.0 with Python 3.4+ bindings has been successfully installed on my Ubuntu system!

Summary

In this tutorial I have demonstrated how to install OpenCV 3.0 with Python 3.4+ bindings on your Ubuntu system. This article is very similar to our previous tutorial on installing OpenCV 3.0 and Python 2.7 on Ubuntu, but takes advantage of OpenCV 3.0’s new Python 3+ support, ensuring that we can use the Python 3 interpreter in our work.

While having Python 3.4+ support is really awesome and is certainly the future of the Python programming language, I would also advise you to take special care when considering migrating from Python 2.7 to Python 3.4. For many scientific developers, the move from Python 2.7 to 3.4 has been a slow, arduous one. While the big Python packages such as NumPy, SciPy, and scikit-learn have made the switch, there are still other smaller libraries that are dependent on Python 2.7. That said, if you’re a scientific developer working in computer vision, machine learning, or data science, you’ll want to be careful when moving to Python 3.4 as you could easily pigeonhole your research.

Over the coming weeks the OpenCV 3.0 install-fest will continue, so if you would like to receive email updates when new install tutorials are released (such as installing OpenCV 3.0 with Homebrew, installing OpenCV 3.0 on the Raspberry Pi, and more), please enter your email address in the form below.

 

31 Responses to Install OpenCV 3.0 and Python 3.4+ on Ubuntu

  1. Sébastien Vincent July 21, 2015 at 3:58 pm # 

    Hi Adrien,
    Thanks a lot! I followed your tutorial, and at last, I’ve been able to install OpenCV3 with Python3.4 on my Ubuntu VM.

    I have a related question : your instruction is to update the .bashrc with
    “export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3”
    so python3.4 will become the default interpreter in every future virtualenv.

    Is it still possible to create virtual environments with python2.7?

    • Adrian Rosebrock July 21, 2015 at 6:58 pm # 

      Hey Sébastien, you can still create virtual environments using Python 2.7. Using a command like this should work:

      $ mkvirtualenv foo --python python2.7

      • Sébastien Vincent July 27, 2015 at 3:14 pm # 

        Thanks!
        Last question: is it safe to delete the opencv/build directory after install? Or must we keep it forever. Its size is 2.9Gb…

        • Adrian Rosebrock July 28, 2015 at 6:38 am # 

          As long as you have ran sudo make install, you can safely delete the build directory.

          • Sébastien Vincent July 28, 2015 at 2:10 pm # 

            ok, thanks.

  2. Ferdi Güler July 26, 2015 at 3:41 pm # 

    Hi Adrian,
    Such a good tutorial, would you mind explaining the installation steps if I choose to use standard virtual environment coming with Python3.4 (Python3 -m venv foo) instead of virtualenv?

    Thanks

    • Adrian Rosebrock July 26, 2015 at 4:48 pm # 

      Hey Ferdi, I’m actually unfamiliar with the virtual environment that comes with Python 3.4 (I’m just migrating to Python 3 myself — previously all I could use was Python 2.7). Do you have a link where I can read more about it?

      • Ferdi Güler July 27, 2015 at 4:41 am # 

        Hi Again, thanks for your reply. More information about Python’s native virtual environment can be found in the following link. It would be nice if can use it since it comes with Python 3.4 by default and there is no need to install any other 3rd party tools

        https://docs.python.org/3/library/venv.html

        • Adrian Rosebrock July 27, 2015 at 6:05 am # 

          Awesome, thanks for passing along the link I’ll be sure to read up on it.

  3. chetan July 31, 2015 at 2:59 am # 

    Sir i am getting error as shown below. I have installed as per your instructions but still unable to install correctly. pls help me.

    root@chetan-VirtualBox:~# workon cv
    workon: command not found
    root@chetan-VirtualBox:~# python
    Python 2.7.6 (default, Jun 22 2015, 18:00:18)
    [GCC 4.8.2] on linux2
    Type “help”, “copyright”, “credits” or “license” for more information.
    >>> import cv2
    Traceback (most recent call last):
    File “”, line 1, in
    ImportError: No module named cv2

    • Adrian Rosebrock July 31, 2015 at 7:06 am # 

      Hey Chetan — if you are getting an error related to the workon command, then it’s most likely that your ~/.bash_profile file was not updated corrected or was not reloaded via the source command. Go back to the “Step 2: Setup Python (Part 1)” section and ensure you have updated your ~/.bash_profile file to reflect the changes I suggested.

  4. Meareg August 3, 2015 at 4:38 am # 

    Hello adrian, very nice step-by-step tutorial. Thanks man!! :)
    I had to spend some time before figuring out I had to comment out export ‘PYTHONPATH=$PYTHONPATH:/usr/lib/local/python2.7/dist-packages’ (if such line exists in your ~/.bashrc file) and replace it with
    ‘PYTHONPATH=$PYTHONPATH:/usr/lib/local/python3.4/dist-packages’ or simply replace 2.7 with 3.4
    This will allow our openCV3.0 compilation to choose the python3.4 interpreter specifically and to include the opencv3.0’s bindings for python3.4 as shown above in step 3’s cmake output.

    • Adrian Rosebrock August 3, 2015 at 6:42 am # 

      Nice catch!

      • Mike Ream August 20, 2015 at 9:41 pm # 

        OK…here is what I did wrong….I had to exit in the middle of this process and when I came back into my terminal I was not on the cv environment when I did the cmake command. I tried running it again with no luck. I then removed the entire build directory and started again making sure I entered “workon cv” to be sure I was in the virtual environment. I followed the steps and all seems OK now.

        • Adrian Rosebrock August 21, 2015 at 7:17 am # 

          Nice, I’m glad it’s working for you Mike!

          And yes, to all other readers: if you leave your computer, restart it, open up a new terminal, etc. be sure to use the workon command to re-enter your virtual environment.

  5. Tony August 20, 2015 at 1:52 pm # 

    thanks.

  6. Mike Ream August 20, 2015 at 10:05 pm # 

    Sorry for all the comments. I also get the following output when I type cv2.__version__: ‘3.0.0-dev’ . Are other people getting the dev version? I was surprised to get a different output.

    • Adrian Rosebrock August 21, 2015 at 7:18 am # 

      After pulling down the repository from GitHub did you run git checkout 3.0.0? It seems like you might have forgotten the checkout command.

      • Mike Ream August 24, 2015 at 10:18 am # 

        Your suspicions were correct. I could have sworn that I ran the “git checkout 3.0.0″command, but I must have forgot it. After I followed the steps again, being more careful I do get the correct version installed.

        • Adrian Rosebrock August 24, 2015 at 11:11 am # 

          Fantastic, I’m glad it was just a git checkout issue :-)

  7. Tafadzwa August 25, 2015 at 4:56 am # 

    i’ve encountered an error whilst running “sudo pip3 install virtualenv virtualenvwrapper”…
    The response,”Cannot fetch base index………”

  8. Tafadzwa September 3, 2015 at 9:46 am # 

    Thanks a lot…..After running the command “cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D INSTALL_C_EXAMPLES=ON \
    -D INSTALL_PYTHON_EXAMPLES=ON \
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
    -D BUILD_EXAMPLES=ON ..” i have encountered this error,”CMake Error at 3rdparty/ippicv/downloader.cmake:75 (message):
    ICV: Failed to download ICV package: ippicv_linux_20141027.tgz.
    Status=22;”HTTP response code said error”

    How can that be resolved??

    • Adrian Rosebrock September 3, 2015 at 12:21 pm # 

      That sounds like an error related to your internet connection and not OpenCV. The ICV package needs to be downloaded and for whatever reason, the the download is failing — likely due to a connectivity issue (either on your end or OpenCV’s).

  9. Nitin September 10, 2015 at 11:10 am # 

    If anyone found dist-package instead of site-package, then use these commands and i guess it will work…

    • Adrian Rosebrock September 11, 2015 at 6:34 am # 

      Thanks for sharing Nitin!

  10. Jonathan October 7, 2015 at 3:47 am # 

    Hello,

    I’ve run through the compile and make processes with no warnings or errors. When I run sudo ldconfig from the build directory, there is no output. And when I try to look for the opencv *.so files, I cannot find them (neither in /usr/local/lib/python3.4/dist-packages or site-packages directories. Do you know what might be causing the problem ? What information can I provide to help you understand the issue ? Thanks in advance – and for the great tutorial !

    • Adrian Rosebrock October 7, 2015 at 6:28 am # 

      Hey Jonathan — be sure to check for .so files in the build/lib directory. This has happened to me a few times before and then I just manually moved it to the site-packages directory.

  11. Milos October 21, 2015 at 11:11 am # 

    Nice, works out of the box. Although, I installed virtual environment with just “virtualenv env –python python3.4”.

    • Adrian Rosebrock October 22, 2015 at 6:20 am # 

      Awesome, I’m glad the tutorial worked for you Milos! :-)

  12. Shiva September 30, 2015 at 7:59 am # 

    Oops, looks like I’d forgotten to update the .bashrc file! It works fine now.

    My other query still stands!


阅读更多
文章标签: OpenCV Linux
个人分类: OpenCV
想对作者说点什么? 我来说一句

OpenCV3 for Python3的安装方法

官方的OpenCV版本只支持到了Python2.X,如果要使用Python3就需要自己编译。本文将阐述如何在Windows和Ubuntu系统下安装OpenCV3 for Python3 。...

tinyzhao tinyzhao

2016-10-05 14:14:52

阅读数:3169

python3下安装OpenCV

本文适用于想在window下使用python 3 的童鞋,安装openCV 有问题的参考一、你要确定自己的python版本是3.x,在命令行窗口输入python本人使用的是python 3.6二、需要...

qq_41352018 qq_41352018

2018-04-11 21:58:58

阅读数:104

ubuntupython+opencv——读取视频

首先,给出一段我们要实现的代码import cv2 import numpy as npcap = cv2.VideoCapture("images/test.mp4") while(1): #get...

lxy_2011 lxy_2011

2017-12-21 18:51:52

阅读数:892

Ubuntu14.04 64位+Python3.4环境下安装opencv3.0的方法

折腾了估计得有1个星期,终于把opencv安装好了。     安装的过程主要是参考了这篇文章:https://github.com/rainyear/lolita/issues/18    但是实际安...

qingyuanluofeng qingyuanluofeng

2016-06-01 19:09:43

阅读数:11899

python3之opencv安装

一、安装pip3 install numpy pip3 install opencv-python 以上在ubunt上安装成功,但是在树莓派安装失败,在树莓派上Python2可以使用apt insta...

qq_25005909 qq_25005909

2017-11-16 19:13:58

阅读数:4822

python opencv 3.2 的pip安装文件

2017年02月11日 37.71MB 下载

python3 安装opencv 记录

1.下载了最新的opencv_python-3.2.0.7-cp35-cp35m-win_amd64.whl,使用pip安装成功 2. import cv2 失败,ImportError: DLL ...

mjcheng8879 mjcheng8879

2017-06-27 11:40:59

阅读数:246

Python3 安装Opencv方法

到http://www.lfd.uci.edu/~gohlke/pythonlibs/ 下载所需的版本,注意版本号的对应。 opencv_python-3.1.0-cp35-cp35m-win_a...

HuangZhang_123 HuangZhang_123

2017-03-19 22:26:47

阅读数:5726

Windows7 64位+python3.4环境下安装opencv3.0的方法

在win7 64位系统下+python3.4环境下安装opencv3.0主要过程如下: 首先在windows的python扩展包网址:http://www.lfd.uci.edu/~gohlke/p...

qingyuanluofeng qingyuanluofeng

2016-06-01 21:12:11

阅读数:9796

Ubuntu14.04 安装配置Opencv3.0Python2.7

声明:文章主要内容翻译自pyimagesearch上一位博主的文章,文章写的非常仔细,本人膜拜的五体投地,决定当个搬运工 原文链接地址Step 1打开终端,输入以下命令更新apt-get包管理器,...

u010381648 u010381648

2015-10-27 18:11:20

阅读数:4265

没有更多推荐了,返回首页

不良信息举报

How to Install OpenCV 3.0 and Python 3.4+ on Ubuntu

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭