Installing OpenCV 3.0 for both Python 2.7 and Python 3+ on your Raspberry Pi 2

rpi_py2_face_detection

Honestly, I love the Raspberry Pi for teaching computer vision — it is perhaps one of the best teaching tools to expose programmers, developers, and students to the world of computer vision.

It’s great for hobbyists and garage-room hackers, as you get to learn on a cheap, but super fun piece of hardware. It’s awesome for businesses and products as they can deploy computer vision algorithms on cost-affordable and reliable hardware. The Raspberry Pi is also certainly prevalent in research and academia. Given its lost-cost, we can now undertake large-scale, distributed computer vision research projects using a fleet of Raspberry Pis.

Given these benefits and applicability to a wide variety of domains, it’s perhaps comes as no surprise that my tutorial on installing OpenCV and Python on your Raspberry Pi 2 and B+ is still one of the most popular posts on the PyImageSearch blog.

But today, that’s going to change — because I think this blog post will over take its predecessor become the most popular article on the PyImageSearch blog.

You see, we’re going to take a step forward and learn how to install the (just released) OpenCV 3.0 library for both Python 2.7 and Python 3+ on your Raspberry Pi.

That’s right. By the end of this step-by-step guide you’ll have the brand new OpenCV 3.0 library installed on your Raspberry Pi 2, along with either Python 2.7+ or Python 3+ bindings. This is definitely an exciting tutorial — up until now only Python 2.7 was supported by OpenCV. But now given the OpenCV 3.0 release, we can finally utilize Python 3+ in our projects.

And as you have seen elsewhere on the PyImageSearch blog, being able to utilize OpenCV on the Raspberry Pi has lead to be really great projects, such as an automated home surveillance and security system using Python + OpenCV + Dropbox + a Raspberry Pi 2:

Figure 7: Examples of the Raspberry Pi home surveillance system detecting motion in video frames and uploading them to my personal Dropbox account.

So if you’re interested in building awesome computer vision based projects like this, then follow along with me and we’ll have OpenCV 3.0 with Python bindings installed on your Raspberry Pi 2 in no time.

Install OpenCV 3.0 for both Python 2.7+ and Python 3+ on your Raspberry Pi 2

UPDATE: The tutorial you are reading now covers how to install OpenCV 3 with Python 2.7 and Python 3 bindings on Raspbian Wheezy. Raspbian Jessie has now replaced Raspbian Wheezy and if this is the first time you are reading this tutorial then in all likelihood you are using Raspbian Jessie. Please use the following updated guides to help you install OpenCV + Python on your Raspberry Pi.

The rest of this blog post will detail how to install OpenCV 3.0 for both Python 2.7 and Python 3+ on your Raspberry Pi 2. These install instructions could also be used for the B+, but I highly recommend that you use the Pi 2 for running OpenCV applications — the added speed and memory makes the Pi 2 much more suitable for computer vision.

In order to keep this tutorial concise and organized, I have broken down the OpenCV 3.0 install process into four sections:

  • Section 1: Configuring your Raspberry Pi by installing the required packages and libraries. Regardless of whether you are using Python 2.7 or Python 3+, we need to take some steps in order to prepare our Raspberry Pi for OpenCV 3.0 — these steps are mainly calls to apt-get  , followed by installing the required packages and libraries.
  • Section 2: Compiling OpenCV 3.0 with Python 2.7+ support. If you want to install OpenCV 3.0 with Python 2.7+ bindings on your Raspberry Pi, then this is the section that you’ll want to go to. After you complete this section,  skip Section 3 and head right to Section 4.
  • Section 3: Compiling OpenCV 3.0 with Python 3+ support. Similarly, if you want to install OpenCV 3.0 with Python 3+ bindings on your Pi 2, then complete Section 1 and skip right to Section 3.
  • Section 4: Verifying your OpenCV 3.0 install. After you have installed OpenCV 3.0 with Python support on your Raspberry Pi 2, you’ll want to confirm that is is indeed installed correctly and working as expected. This section will show you how to verify your OpenCV 3.0 install and ensure it’s working correctly.

Python 2.7+ or Python 3+?

Before we get started, take a second and consider which version of Python you are going to use. Are you going to compile OpenCV 3.0 with Python 2.7 bindings? Or are you going to compile OpenCV 3.0 Python 3 bindings?

There are pros and cons of each, but the choice is honestly up to you. If you use Python 3 regularly and are comfortable with it, then go ahead and compile with Python 3 bindings. However, if you do a lot of scientific Python development, you might want to stick with Python 2.7 (for the time being at least). While packages such as NumPy, Scipy, and scikit-learn are certainly increasing the Python 3+ adoption rate in the scientific community, there are still many scientific packages that still require Python 2.7 — because of this, you can easily pigeonhole yourself if you go with Python 3 and then realize that many of the packages you use on a daily basis only support Python 2.7.

When in doubt, I normally suggest that scientific developers use Python 2.7 since it ensures capability with a larger set of scientific packages and allows you to run experiments with legacy code. However, that is quickly changing — so proceed with whichever Python version you are most comfortable with!

Section 1: Configuring your Raspberry Pi by installing required packages and libraries

Let’s kick off this OpenCV 3.0 install tutorial by updating our Raspberry Pi:

Timing: 9m 5s

Now we can install developer tools required to build OpenCV from source:

Timing: 43s

As well as install packages used to load various image formats from disk:

Timings: 27s

Let’s install some video I/O packages:

Timings: 26s

Install GTK, which handles OpenCV’s GUI operations:

Timings: 2m 20s

We can also optimize various functions (such as matrix operations) inside OpenCV by installing these packages:

Timings: 46s

At this point we have all our prerequisites installed, so let’s pull down the OpenCV repository from GitHub and checkout the 3.0.0  version:

Timings: 8m 34s

Update (3 January 2016): You can replace the 3.0.0  version with whatever the current release is (as of right now, it’s 3.1.0 ). Be sure to check OpenCV.org for information on the latest release.

For the full, complete install of OpenCV 3.0, grab the opencv_contrib repo as well:

Timings: 1m 7s

Again, make sure that you checkout the same version for opencv_contrib  that you did for opencv  above, otherwise you could run into compilation errors.

Now we’re at at a crossroads, a sort of Choose Your Own (OpenCV) Adventure!

You can either follow Section 2 and compile OpenCV 3.0 with Python 2.7+ bindings. Or you can head to Section 3 and install OpenCV 3.0 with Python 3+ bindings. The choice is up to you — but choose wisely! Once you make the choice it will be non-trivial to change your mind later.

Note: It’s certainly possible to install OpenCV 3.0 for both versions of Python (it’s actually not too hard), but it’s outside the scope of this tutorial; I’ll be sure to cover this technique in a future post.

Section 2: Compiling OpenCV 3.0 with Python 2.7+ support

Install the Python 2.7 header files so we can compile the OpenCV 3.0 bindings:

Timings: 1m 20s

Install pip , a Python package manager that is compatible with Python 2.7:

Timings: 33s

Just as we did in the original tutorial on installing OpenCV 2.4.X on your Raspberry Pi, we are going to utilize virtualenv and virtualenvwrapper which allow us to create separate Python environments for each of our Python projects. Installing virtualenv  and virtualenvwrapper  is certainly not a requirement when installing OpenCV and Python bindings; however, it’s a standard Python development practiceone that I highly recommend, and the rest of this tutorial will assume you are using them!

Installing virtualenv  and virtualenvwrapper  is as simple as using the pip  command:

Timings: 17s

Next up, we need to update our ~/.profile  file by opening it up in your favorite editor and adding the following lines to the bottom of the file.

And if your ~/.profile  file does not exist, create it.

Now that your ~/.profile  file has been updated, you need to reload it so the changes take affect. To force a reload of the . profile , you can: logout and log back in; close your terminal and open up a new one; or the most simple solution is to use the source  command:

Time to create the cv3  virtual environment where we’ll do our computer vision work:

Timings: 19s

If you ever need to access the cv3  virtual environment (such as after you logout or reboot your Pi), just source  your ~/.profile  file (to ensure it has been loaded) and use the workon  command:

And your shell will be updated to only use packages in the cv3  virtual environment.

Moving on, the only Python dependency we need is NumPy, so ensure that you are in the cv3  virtual environment and install NumPy:

Timings 13m 47s

While unlikely, I have seen instances where the .cache  directory gives a “Permission denied” error since we used the sudo  command to install pip . If that happens to you, just remove the .cache/pip  directory and re-install NumPy:

Awesome, we’re making progress! You should now have NumPy installed on your Raspberry Pi in the cv3  virtual environment, as shown below:

Figure 1: NumPy has been successfully installed into our virtual environment for Python 2.7+.

Figure 1: NumPy has been successfully installed into our virtual environment for Python 2.7+.

Note: Performing all these steps can be time consuming, so it’s perfectly normal to logout/reboot and come back later to finish the install. However, if you have logged out or rebooted your Pi then you will need to drop back into your cv3  virtual environment prior to moving on with this guide. If you do not, OpenCV 3.0 will not compile and install correctly and you’ll likely run into import errors.

So I’ll say this again, before you run any other command, you’ll want to ensure that you are in the cv3  virtual environment:

And once you are in cv3  virtual environment, you can use cmake  to setup the build:

Update (3 January 2016): In order to build OpenCV 3.1.0 , you need to set -D INSTALL_C_EXAMPLES=OFF  (rather than ON ) in the cmake  command. There is a bug in the OpenCV v3.1.0 CMake build script that can cause errors if you leave this switch on. Once you set this switch to off, CMake should run without a problem.

CMake will run for about 30 seconds, and after it has completed (assuming there are no errors), you’ll want to inspect the output, especially the Python 2 section:

Figure 2: The output of CMake looks good -- OpenCV 3.0 will compile with Python 2.7 bindings using the Python interpreter and NumPy package associated with our virtual environment.

Figure 2: The output of CMake looks good — OpenCV 3.0 will compile with Python 2.7 bindings using the Python interpreter and NumPy package associated with our virtual environment.

The key here is to ensure that CMake has picked up on the Python 2.7 interpreter and numpy  package associated with the cv3  virtual environment.

Secondly, be sure look at the packages path  configuration — this is the path to the directory where your OpenCV 3.0 bindings will be compiled and stored. From the output above, we can see that my OpenCV bindings will be stored in /usr/local/lib/python2.7/site-packages

All that’s left now is to compile OpenCV 3.0:

Where the 4 corresponds to the 4 cores on our Raspberry Pi 2.

Timings: 65m 33s

Assuming OpenCV has compiled without an error, you can now install it on your Raspberry Pi:

At this point, OpenCV 3.0 has been installed on your Raspberry Pi 2 — there is just one more step to take.

Remember how I mentioned the packages path  above?

Take a second to investigate the contents of this directory, in my case /usr/local/lib/python2.7/site-packages/ :

Figure 3: Our Python 2.7+ bindings for OpenCV 3.0 have been successfully installed on our system. The last step is to sym-link the cv2.so file into our virtual environment.

Figure 3: Our Python 2.7+ bindings for OpenCV 3.0 have been successfully installed on our system. The last step is to sym-link the cv2.so file into our virtual environment.

You should see a file named cv2.so , which is our actual Python bindings. The last step we need to take is sym-link the cv2.so  file into the site-packages  directory of our cv3  environment:

And there you have it! You have just compiled and installed OpenCV 3.0 with Python 2.7 bindings on your Raspberry Pi! 

Proceed to Section 4 to verify that your OpenCV 3.0 install is working correctly.

Section 3: Compiling OpenCV 3.0 with Python 3+ support

First up: Install the Python 3 header files so we can compile the OpenCV 3.0 bindings:

Timings: 54s

Install pip , ensuring that it is compatible with Python 3 (note that I am executing python3  rather than just python ):

Timings: 28s

Just like in the original tutorial on installing OpenCV 2.4.X on your Raspberry Pi 2, we are going to make use of virtualenv and virtualenvwrapper. Again, this is not a requirement to get OpenCV 3.0 installed on your system, but I highly recommend that you use these packages to manage your Python environments. Furthermore, the rest of this tutorial will assume you are using virtualenv  and virtualenvwrapper .

Use the pip3  command to install virtualenv  and virtualenvwrapper :

Timings: 17s

Now that virtualenv  and virtualenvwrapper  are installed on our system, we need to update our ~/.profile  file that is loaded each time we launch a terminal. Open up your ~/.profile  file in your favorite text editor (if it doesn’t exist create it) and add in the following lines:

In order to make the changes to our ~/.profile  file take affect, you can either (1) logout and log back in, (2) close your current terminal and open up a new one, or (3) simply use the source  command:

Let’s create our cv  virtual environment where OpenCV will be compiled and accessed from:

Timings: 19s

Note: I gathered the Python 2.7+ and Python 3+ install instructions on the same Raspberry Pi so I could not use the same virtual environment name for each installation. In this case, the cv3  virtual environment refers to my Python 2.7 environment and the cv  virtual environment refers to my Python 3+ environment. You can name these environments whatever you wish, I simply wanted to offer a clarification and hopefully remove any confusion.

This command will create your cv  virtual environment which is entirely independent of the system Python install. If you ever need to access this virtual environment, just use the workon  command:

And you’ll be dropped down into your cv  virtual environment.

Anyway, the only Python dependency we need is NumPy, so ensure that you are in the cv  virtual environment and install NumPy:

Timings 13m 47s

If for some reason your .cache  directory is giving you a Permission denied error, just remove it and re-install NumPy, otherwise you can skip this step:

At this point you should have a nice clean install of NumPy, like this:

Figure 3: NumPy has been successfully installed for Python 2.7 in the cv virtual environment.

Figure 4: NumPy has been successfully installed for Python 3+ in the cv virtual environment.

Alright, it’s taken awhile, but we are finally ready to compile OpenCV 3.0 with Python 3+ bindings on your Raspberry Pi.

It’s important to note that if you have logged out or rebooted, that you will need to drop back into your cv  virtual environment before compiling OpenCV 3.0. If you do not, OpenCV 3.0 will not compile and install correctly and you’ll be scratching your head in confusion when you try to import OpenCV and get the dreaded ImportError: No module named cv2  error.

So again, before you run any other command in this section, you’ll want to ensure that you are in the cv  virtual environment:

After you are in the cv  virtual environment, we can setup our build:

Update (3 January 2016): In order to build OpenCV 3.1.0 , you need to set -D INSTALL_C_EXAMPLES=OFF  (rather than ON ) in the cmake  command. There is a bug in the OpenCV v3.1.0 CMake build script that can cause errors if you leave this switch on. Once you set this switch to off, CMake should run without a problem.

After CMake has run, take a second to inspect the output of the make configuration, paying close attention to the Python 3 section:

Figure 4: Definitely take the time to ensure that CMake has found the correct Python 3+ interpreter before continuing on to compile OpenCV 3.0.

Figure 5: Definitely take the time to ensure that CMake has found the correct Python 3+ interpreter before continuing on to compile OpenCV 3.0.

Specifically, you’ll want to make sure that CMake has picked up your Python 3 interpreter!

Since we are compiling OpenCV 3.0 with Python 3 bindings, I’m going to examine the Python 3 section and ensure that my Interpreter  and numpy  paths point to my cv  virtual environment. And as you can see from above, they do.

Also, take special note of the packages path  configuration — this is the path to the directory where your OpenCV 3.0 bindings will be compiled and stored. After the running the make  command (detailed below), you’ll be checking in this directory for your OpenCV 3.0 bindings. In this case,  my packages path  is lib/python3.2/site-packages , so I’ll be checking /usr/local/lib/python3.2/site-packages  for my compiled output file.

All that’s left now is to compile OpenCV 3.0:

Where the 4 corresponds to the 4 cores on our Raspberry Pi 2. Using multiple cores will dramatically speedup the compile time and bring it down from 2.8 hours to just above 1 hour!

Timings: 65m 33s

Assuming OpenCV has compiled without an error, you can now install it on your Raspberry Pi:

Timings: 39s

At this point OpenCV 3.0 has been installed on our Raspberry Pi!

However, we’re not quite done yet.

Remember how I mentioned the packages path  above?

Well, let’s list the contents of that directory and see if our OpenCV bindings are in there:

Here we can see there is a file named cv2.cpython-32mu.so , which is our actual Python bindings.

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

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

Figure 5: A good validation step to take is to list the contents of the site-packages directory for the cv virtual environment. You should see your cv2.so file sym-linked into the directory.

Figure 6: A good validation step to take is to list the contents of the site-packages directory for the cv virtual environment. You should see your cv2.so file sym-linked into the directory.

And there you have it! OpenCV 3.0 with Python 3+ support is now successfully installed on your system!

Section 4: Verifying your OpenCV 3.0 install

Before we wrap this tutorial up, let’s ensure that our OpenCV bindings have installed correctly. Open up a terminal, enter the cv  virtual environment (or cv3 , if you followed the Python 2.7+ install steps), fire up your Python shell import OpenCV:

And sure enough, we can see OpenCV 3.0 with Python 3+ support has been installed on my Raspberry Pi:

Figure 6: Success! OpenCV 3.0 with Python bindings has been successfully installed on our Raspberry Pi 2!

Figure 7: Success! OpenCV 3.0 with Python bindings has been successfully installed on our Raspberry Pi 2!

So, what’s next?

Congrats! You have a brand new, fresh install of OpenCV on your Raspberry Pi — and I’m sure you’re just itching to leverage your Raspberry Pi to build some awesome computer vision apps.

But I’m also willing to bet that you’re just getting started learning computer vision and OpenCV, and you’re probably feeling a bit confused and overwhelmed on where exactly to start.

Personally, I’m a big fan of learning by example, so a good first step would be to read this blog post on accessing your Raspberry Pi Camera with the picamera module. This tutorial details the exact steps you need to take to (1) capture photos from the camera module and (2) access the raw video stream.

And if you’re really interested in leveling-up your computer vision skills, you should definitely check out my book, Practical Python and OpenCV + Case Studies. My book not only covers the basics of computer vision and image processing, but also teaches you how to solve real world computer vision problems including face detection in images and video streams, object tracking in video, and handwriting recognition.

raspberry_pi_in_post

All code examples covered in the book are guaranteed to run on the Raspberry Pi 2 and Pi 3 as well! Most programs will also run on the B+ and Zero models, but might be a bit slow due to the limited computing power of the B+ and Zero.

So let’s put your fresh install of OpenCV on your Raspberry Pi to good use — just click here to learn more about the real-world projects you can solve using your Raspberry Pi + Practical Python and OpenCV .

Summary

In this blog post, I have detailed how to install OpenCV 3.0 with Python 2.7+ and Python 3+ bindings on your Raspberry Pi 2. Timings for each installation step were also provided so you could plan your install accordingly. Just keep in mind that if you ever logout or reboot your Pi after setting up virtualenv  and virtualenvwrapper  that you’ll need to execute the workon  command to re-access your computer vision virtual environment prior to continuing the steps I have detailed. If you do not, you could easily find yourself in a situation with the dreaded ImportError: No module named cv2  error.

As the Raspberry Pi and the Raspbian/NOOBS operating system evolves, so will our installation instructions. If you run across any edge cases, please feel free to let me know so I can keep these install instructions updated.

And of course, in future blog posts we’ll be doing some really amazing projects using OpenCV 3.0 and the Raspberry Pi, so consider entering your email address in the form below to be notified when these posts go live!

, , , , , ,

225 Responses to Installing OpenCV 3.0 for both Python 2.7 and Python 3+ on your Raspberry Pi 2

  1. Jason July 27, 2015 at 11:22 pm #

    Adrian thank you for the tutorial. I was following your last tutorial with a pi b, so now that I have a pi 2 and I would like to do it with opencv 3. However I have a question, do you know how to install the smbus on a virtualenv? I want to use i2c with python and opencv.

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

      Hey Jason, I actually detail the answer to this question in the comments section over on the original Raspberry Pi + OpenCV post.

      The first thing to do would be read up a bit on Python virtual environments.

      The cv virtual environment is entirely independent of your system install and you can install whatever Python packages you want into it. I have never used smbus before, but if I wanted to, I would just enter the cv virtual environment and let pip install it:

      $ workon cv
      $ pip install cffi

    • John Tran March 1, 2016 at 1:58 am #

      Hi Jason,

      You can go here https://pypi.python.org/pypi/smbus-cffi for details of instructions.
      Basically, you only need to use two commands below (they worked for me)

      ~ $ sudo apt-get install build-essential libi2c-dev i2c-tools python-dev libffi-dev
      ~ $ pip install smbus-cffi

  2. Andrei July 28, 2015 at 3:03 pm #

    Tip:

    After 1 build failed because of “no space to write xxx file” i actually discovered that if you mount an external HDD partition and build there the process is actually way faster than on SDCard.

    I am at 35% in around 10 min. I do not know if the install will work but i can not see why it will fail.

    Failed…. vfat partition system does not support symlinks. Can be the same for fat32. I do not have a windows terminal to test it.

    Reformat to ext2 hope it works.

    Worked!!!!

    Now for the symlink ,if you choose to instal OpenCV for Python 2.7:

    workon cv3 (or whatever name you choose for the virtual environment)
    ln -s /usr/local/lib/python2.7/site-packages/cv2.so ~/.virtualenvs/cv3/lib/python2.7/site-packages

    also is working if you make symlink at ~/.virtualenvs/cv3/lib/python2.7 but it can throw an error down the way.

    Me happy

  3. Jayman July 29, 2015 at 1:19 am #

    Hey Adrian,
    Thank you for your great instruction!
    I’ve successfully installed all the things you listed in your detailed description.
    (Section 1 and 3 -> openCV for Python 3)
    And it works, in the python IDLE in a terminal window, but how can I use the virtualenv and virtualenvwrapper with eg. Geany… cause, I’ve to mange a larger project with my RasPi and when I run my *.py with import cv2 in geany, I still get “no module named cv2”
    Which settings do I have to make in Geany, to get this workin?
    thanks for your support!
    br, J

    • Adrian Rosebrock July 29, 2015 at 6:30 am #

      I have never used the Geany editor before, so I’m honestly not sure about that particular question. I would look at the project settings and see if there is a place you can set the Python Interpreter, and if so, point it to your correct virtual environment.

      • Jayman July 29, 2015 at 3:23 pm #

        thank you for your quick answer. I’ll try this.

        • Gilad March 28, 2016 at 11:31 am #

          Hi Jayman,

          Did you manage to solve this?

    • Chujiang May 11, 2016 at 9:59 am #

      Hi Jayman

      I meet this problem, too. Have you solved this problem? Could you please give me some advice?

      Thank you very much

  4. Jason July 29, 2015 at 9:24 pm #

    Hey Adrian, thanks for your great tutorials!! I really appreciate your effort!
    I encountered a problem after installing Opencv3 with python 2.7 that I failed to boot into graphical desktop.
    I have tried to reconfigure boot option using “sudo raspi-config” command, but my raspi still boot as text console…..any solution for this?

    Many thanks~

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

      Hey Jason, the OpenCV + Python install wouldn’t affect the graphical desktop, so it’s likely that something else caused the problem. Either way, you can just execute the command startx to launch the GUI after logging in. Otherwise, you might want to post on the Raspberry Pi forums and ask there.

  5. Talha August 2, 2015 at 10:20 pm #

    Wonderful tutorial once again

    Quick question for you. Installing numpy in the cv3 env and it seems to be taking a long time while running setup.py. On screen it says ‘Running setup.py bdist_wheel for numpy’. Theres no progress bar or anything and just seems stuck, I ctrl+C’d out of it and removed the cahce file and tried again but still seems like its stuck. its been ~40 mins or so and its till there. Could it be normal? I am running on the B+ model which is slower.

    thanks!

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

      For the B+ model that is completely normal. From my experience, NumPy takes about 45 minutes to install on the B+.

      • Xiyu Ouyang March 1, 2016 at 2:15 am #

        Hello, Adrain!

        I got the same problem here, but I am using B model. My progress bar is just …. completely dead….Do you know the approximate time for Numpy to install on B?

        • Adrian Rosebrock March 1, 2016 at 3:40 pm #

          The progress bar isn’t “dead” — it just takes at least 45 minutes to install on the Pi model B. In general, I really recommend that you use the Pi 2 for computer vision and image processing.

  6. Antonio August 3, 2015 at 1:09 am #

    Hi Adrian,
    Great installation guilde. Should I install any specific libraries in order to open a video stream with opencv?

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

      Hey Antonio — you should be able to open video files/access the webcam without an issue.

  7. alexander isz August 5, 2015 at 8:29 am #

    hello adrian…
    very very nice tutorial for beginner like me….

    i got ” bash: cmake :command not found ”

    can you help me ? thanks!

    • Adrian Rosebrock August 6, 2015 at 6:25 am #

      Please see Section 1 of this post. It sounds like you did not run the commands in order. You missed this one:

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

  8. Shubham Mittal August 7, 2015 at 12:14 pm #

    Tnx, followed all the mentioned steps to successfully install opencv3

    • Adrian Rosebrock August 8, 2015 at 6:33 am #

      Awesome, glad to hear it!

  9. Stridera August 8, 2015 at 11:33 pm #

    I wasn’t able to get it to work. It seems to want to only do the python 2.7 steps.

    (cv)stridera@glados ~/opencv/build $ python3 -V
    Python 3.2.3
    (cv)stridera@glados ~/opencv/build $ echo $VIRTUALENVWRAPPER_PYTHON
    /usr/bin/python3
    (cv)stridera@glados ~/opencv/build $ python3
    Python 3.2.3 (default, Mar 1 2013, 11:53:50)
    [GCC 4.6.3] on linux2
    Type “help”, “copyright”, “credits” or “license” for more information.
    >>> import numpy
    Traceback (most recent call last):
    File “”, line 1, in
    ImportError: No module named numpy
    >>> quit()
    (cv)stridera@glados ~/opencv/build $ pip-3.2 install numpy
    Requirement already satisfied (use –upgrade to upgrade): numpy in /usr/lib/python3/dist-packages
    Cleaning up…
    (cv)stridera@glados ~/opencv/build $ which python
    /home/stridera/.virtualenvs/cv/bin/python
    (cv)stridera@glados ~/opencv/build $ python -V
    Python 3.2.3

    Any ideas?

    • Adrian Rosebrock August 9, 2015 at 7:11 am #

      Once you are inside the virtual environment, you use python and pip as you normally would — no need to specify python3 and pip-3.2. When you performed pip3.2 install numpy it looks like you installed NumPy into the system Python install rather than your virtual environment.

      I would try doing something like this:

      $ mkvirtualenv cv3 -p python3
      $ pip install numpy
      ...

  10. Dustin August 10, 2015 at 7:28 am #

    In the printout produced after cmake runs, I receive several failures that look like missing dependencies. Is this expected?

    Also, after cmake runs, my Python 3 config shows the proper paths for the Interpreter and numpy, but shows Libraries for Python 2.7. Did I miss an installation step?

    • Adrian Rosebrock August 10, 2015 at 10:34 am #

      What dependencies are missing? Some are critical, most are not.

      As far as your config, it sounds like you might want to explicitly set your PYTHON3_LIBRARY option to be the path to your Python 3 library.

  11. Alex Sobrino August 11, 2015 at 12:23 am #

    Hi Adrian,

    Quick question, I need the cv2 library to be available via sudo (as I am also using the RPi.GPIO library in the same python program). Is there something I need to change in the installation process to make it available for sudo?

    By the way, with many of your great tutorials I am implementing an iris recognition project. Great work 🙂

    Thank you!

    • Adrian Rosebrock August 11, 2015 at 6:29 am #

      Hey Alex, great question. Head over to my first post on installing OpenCV on the Raspberry Pi and search the comments for “GPIO” — you’ll be able to find your solution 🙂

      The gist is that you need to launch a root shell, create the cv virtual environment as the root, and setup the environment as the root user.

  12. skip August 12, 2015 at 1:11 pm #

    Wow, an install instruction that actually works!! First attempt blew up at 85% (I suspect a download glitch) but the second went thru without a hitch. Thanks so much for the detailed hand-holding. I went the Py 2.7 route. I am sold on your abilities as an instructor, and will be ordering cv course materials from you shortly.

    • Adrian Rosebrock August 13, 2015 at 7:09 am #

      Nice! I’m so happy that the tutorial worked for you Skip! 😀

  13. Adam Geitgey August 18, 2015 at 5:34 pm #

    I bought a brand new Raspberry Pi 2 + camera kit off of Amazon and tried these directions. The kit comes with an 8GB SD card with NOOBS/raspbian.

    The OpenCV build failed at about 75% because I ran out of space on the 8GB card with an error like “no space to write file”.

    Following these directions, you clone the entire OpenCV repo from git:

    $ cd ~
    $ git clone https://github.com/Itseez/opencv.git
    $ cd opencv
    $ git checkout 3.0.0

    That wastes a lot of space because it includes the entire git history of every OpenCV change ever.

    If you change that step to just download OpenCV without the git history:

    $ wget https://github.com/Itseez/opencv/archive/3.0.0.zip
    $ unzip 3.0.0.zip
    $ cd 3.0.0

    Then you won’t use nearly as much space for then OpenCV download and then the build will succeed on a freshly installed 8GB card without running out of space.

    If that still fails for some reason, you could delete Mathematica (which takes up about 500mb on a fresh install):

    $ sudo apt-get remove –purge wolfram-engine

    With those tweaks, everything worked great and I had real-time face detection via Python working on my Raspberry Pi 2 in a couple of hours! Thanks!

    • Adrian Rosebrock August 19, 2015 at 6:43 am #

      Hey Adam, thanks for the great suggestions — and congrats on getting OpenCV installed on your Pi, despite the hiccups in the process.

      Although one quick question for you: when you ran out of space on the 8GB card, was it expanded to use the entire card? Most SD card that come with NOOBS/Raspbian pre-installed only use 4gb of the allotted space and you need to go into raspi-config to update it to use the entire card.

      • Jeff August 19, 2015 at 9:55 pm #

        Hello Adrian,

        I know you asked Adam but I’ll give you my feedback as well since the issues appear to be the same. My Pi 2 is a CanaKit Complete Starter Kit and came with the 8GB SD card. I ran raspi-config on startup and tried to expand the card by selecting 1) Expand Filesystem. However, I received the following dialog box:

        Your partition layout is not currently supported by this
        tool. You are probably using NOOBS, in which case your
        root filesystem is already expanded anyway.

        Which is correct; I am using NOOBS. Upon running “df” I get:

        Filesystem 1K-blocks Used Available Use% Mounted on
        /dev/root 6581636 6172868 51392 100% /
        devtmpfs 437276 0 437276 0% /dev
        tmpfs 88320 324 87996 1% /run
        tmpfs 5120 0 5120 0% /run/lock
        tmpfs 176620 0 176620 0% /run/shm
        /dev/mmcblk0p5 60479 19766 40714 33% /boot
        /dev/mmcblk0p3 27633 397 24943 2% /media/SETTINGS

        So that’s about 6.3GB on the SD card for the root file system. Notice it’s at 100% b/c I haven’t deleted the OpenCV and OpenCV_Contrib directories yet. I hope that helps.

        Thank you.

        • Adrian Rosebrock August 20, 2015 at 6:59 am #

          Hey Jeff, thanks for the extra information I appreciate it. I haven’t tried on a brand new NOOBS install in awhile which is probably why I never ran into this particular situation. If that’s the case, Adam’s method of pulling down the .zip file is a much better option.

    • Andrew November 7, 2015 at 10:54 pm #

      Instead of downloading a zip file you can ask git to only download the relevant files:

      git clone –depth 1 –branch 3.0.0 https://github.com/Itseez/opencv.git

      This only took 3m 25s on my Raspberry Pi 2.

      • Adrian Rosebrock November 8, 2015 at 7:05 am #

        Thanks for sharing Andrew!

    • Jacob February 14, 2016 at 9:24 am #

      Hi Adam,

      How do you then remove OpenCv from Pi, I want to download it your way to save space but I have already got the version from tutorial.

  14. Jeff August 19, 2015 at 2:55 am #

    Total noob question, but I need to ask/suggest… I purchased a new Pi 2 just to play with OpenCV and Python and work along with your tutorial which is great BTW. Thank you.

    I wanted to note an issue and it seems others have experienced it also – I ran out of disk space during the install. Thankfully “sudo apt-get clean” opened up enough space to complete the install but it seems odd that it’s not mentioned in your tutorial since even a new Pi w/8gig MicroSD ran out of space. I was shocked to discover that OpenCV is almost 3gigs alone! Maybe note in the tutorial that it might be an issue? And suggest fixes?

    And being a noob, I’m curious – now that it’s installed, can I delete the OpenCV and OpenCV_Contrib directories to free up that space? If so, I’d like to suggest that you add that cleanup to the tutorial to help other noobs.

    Thank you again! I look forward to more great tutorials.

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

      Hey Jeff, thanks for the suggestions. And yes, you can delete the opencv and opencv_contrib directories now that the compile and install has finished.

  15. Mesut August 21, 2015 at 8:01 pm #

    Hello Adrian, How can I use openCV binaries to avoid the compilation level? it there a way?

    • Adrian Rosebrock August 22, 2015 at 7:13 am #

      You could try installing OpenCV via apt-get but I really, really do not recommend doing this. In the future I hope I can offer a download of the Raspbian img that comes pre-installed with OpenCV.

  16. Thom August 22, 2015 at 10:11 am #

    Hi Adrian, thanks for this tutorial.

    Unfortunately when trying to configure with CMake, my Python 3 library and interpreter locations are not being picked up at all. Any ideas on what I should set PYTHON3_LIBRARY and/or PYTHON3_INCLUDE_DIR to (or if I need to)? I’m pretty sure I’ve followed all the rest of the instructions precisely.

    Thanks.

    • Adrian Rosebrock August 23, 2015 at 6:56 am #

      My first suggestion would be to check what version of Python 3 you are running. My Pi is running 3.2, but if you’re running a different version, the paths to your PYTHON3_LIBRARY and PYTHON3_INCLUDE_DIR will be different. Although if you are in the cv virtual environment you should (ideally) not have to explicitly supply these options.

      • Thom August 24, 2015 at 9:08 am #

        I realise the specific library locations could (will) be different depending on the installed version of Python, I’m just not sure what the general area for the libraries is. I tried /usr/local/lib/python3 (I think – can’t check right now) with no luck.

        So close…! Kind of annoying it didn’t work it out.

        • Adrian Rosebrock August 25, 2015 at 6:41 am #

          The “library file” should be a .so file, for example libpython2.7.so

  17. Krystian August 30, 2015 at 2:05 am #

    Hi Adrian,

    I get as far as building OpenCV but it stopped at 85% with the following error:

    http://tinypic.com/r/awduud/8

    Any suggestions?

    • Adrian Rosebrock August 30, 2015 at 7:53 am #

      It looks like the tests for the xfeatures2d module failed to build. You could try compiling without the tests (not recommended) or disabling the module via the CMake command. Delete your build, recreate it, add the following switch to your CMake command: -D WITH_xfeatures2d=OFF, re-run cmake, and then restart the compile.

  18. Fran September 7, 2015 at 3:14 pm #

    Thanks for the tutorial. I installed OpenCV in a RP2 without troubles.

    Have you considered a tutorial using conda? It has finally support for RPi and installing libraries with “conda instal …” is trivial.

    http://continuum.io/blog/new-arch

    Thanks!

    • Adrian Rosebrock September 8, 2015 at 7:27 am #

      Awesome, I’m glad to hear you got OpenCV installed on your Pi! 🙂

      Conda is a great tool, but you won’t see me use it on the PyImageSearch blog. Continuum, the parent company in charge of Anaconda and other tools and myself had a pretty big falling out. Like I said, conda is a great tool, feel free to use it, but I won’t be covering it here.

  19. gomelkiev September 14, 2015 at 12:50 am #

    Adrian,
    Hello, thanks for tutorial, have a problem:
    I am doing: “Section 2: Compiling OpenCV 3.0 with Python 2.7+ support” and I don’t have rapsberry pi camera yet ( just ordered) – is it matter? or camera must be connected?

    I am not getting after running cmake same results as yours
    here is what I get:
    — Python 2:
    — Interpreter: /home/pi/.virtualenvs/cv3/bin/python2.7 (ver 2.7.3)

    — Python 3:
    — Interpreter: NO

    — Python (for build): /home/pi/.virtualenvs/cv3/bin/python2.7

    thanks

    • Adrian Rosebrock September 14, 2015 at 6:13 am #

      You can compile without the Raspberry Pi camera attached, that does not matter. As far as your CMake output, it’s strange that your Libraries, NumPy version, and packages path aren’t being automatically detected. I would proceed with the build and see if OpenCV installs incorrectly. If not, then the CMake command will have to be manually adjusted to add in the extra parameters.

      • gomelkiev September 14, 2015 at 10:36 am #

        i finished build/ install but directory /usr/local/lib/python2.7/site-packages/ is empty
        thanks

        • Adrian Rosebrock September 15, 2015 at 6:10 am #

          I would check the lib directory of your build and see if your .so bindings are in there. If so, then the compile was successful and you can move them in to the site-packages directory. If not, then go back and investigate your output of CMAke — in that case, it would be likely that the bindings were not set to be compiled.

  20. Morgatte September 16, 2015 at 1:55 pm #

    Hello, (I’m French, sorry for my language)

    I have followed your tutorial (v2.7) and it works (in virtual environment).
    Really great tutorial,

    But it is not practical. I must open idle-python2.7 to write the code and save it, and after I must open a terminal to execute it. $ python source.py

    Moreover, all time I reboot, before that I must reinstall my CV3 virtual environment.

    Is it possible when I restart my Pi, to directly use idle-python under virtal env, or not ?

    thank’s

    • Adrian Rosebrock September 17, 2015 at 8:17 am #

      Hey Morgatte, you do not need to re-install your cv3 virtual environment, you just need to access it:

      And that will drop you into the cv3 virtual environment. It’s only two commands to run and your development environment will be all setup.

  21. Andrea September 18, 2015 at 6:14 am #

    Great tutorial, I successfully installed OpenCV. Anyway now I’m trying to import cv2 from a script in the Python IDLE but I cannot do it because it says “No module named cv2”. I placed my script NAME.py in /home/pi. I think I’m not in the virtual environment cv3 but I don’t know how to enter it from the script. Thank you!

    • Adrian Rosebrock September 18, 2015 at 7:30 am #

      Hey Andrea — you need to access the virtual environment before you execute your script:

      • RedDevil September 19, 2015 at 12:31 pm #

        Hey Adrian!
        Great tutorial. It worked fine but I rebooted my PI and I can’t use workon command…What did I wrong?

        • Adrian Rosebrock September 20, 2015 at 9:45 am #

          It sounds like for some reason your .profile file was changed during the reboot, which would be quite strange. Check your ~/.profile and ensure the edits made to both virtualenv and virtualenvwrapper are still there. If not, add them back in and reload it and everything should work fine.

  22. Piuser September 18, 2015 at 11:32 am #

    I have installed the OpenCV 2.4 on Raspbery Pi2 following your previous tutorial. Thanks a lot for the detailed decription made my life much easier. CV 2 and python works fine with my syste now. Could you please tell me how I can upgrade that to OpenCV3 (if possible). I reget not checking on this poste before installing OpenCV.

    • Adrian Rosebrock September 20, 2015 at 9:46 am #

      No problem, happy to help! As far as installing OpenCV 3 on your system, just follow this tutorial. I honestly haven’t tried installing OpenCV 3 over top of OpenCV 2.4, but it should theoretically work. Just create a separate Python virtual environment than the one you are using for OpenCV 2.4.

  23. Soren September 19, 2015 at 10:12 pm #

    hi Adrian, I just installed opencv but when I open the terminal and type : workon cv3 , then I got the line : bash: workon: command not found
    I dont know how to fix this. can u help me?

    • Adrian Rosebrock September 20, 2015 at 9:44 am #

      Hey Soren — I would suggest reading through the comments on my original tutorial on installing OpenCV on your Raspberry Pi. There are many reasons why the workon command isn’t found, most of them being either (1) virtualenv and virtualenvwrapper not being installed, or (2) not having your ~/.profile file updated and loaded. I discuss solutions to these inside the comments section of that post.

    • Brian October 5, 2015 at 6:47 pm #

      This might be late, but I had the same problem with workon not being found. Instead of adding the above modifications to .profile file, I added them to .bashrc and it worked fine after that.

      and was on ubuntu, not raspian.

  24. Izwan September 23, 2015 at 3:54 am #

    i try to use xfeatures2d…but appear no ‘module’…
    how i can solve this.

    Already follow exactly same procedure. Hurm…

    • Adrian Rosebrock September 23, 2015 at 6:41 am #

      If the xfeatures2d module was not found, then it looks like the opencv_contrib directory was not included when compiling OpenCV. Make sure your path to opencv_contrib is correct.

  25. CleoQc September 24, 2015 at 2:23 pm #

    Worked perfectly!!! Thanks for all the hard work!

    • Adrian Rosebrock September 25, 2015 at 6:39 am #

      Thanks Nicole! 🙂 I’m glad you have OpenCV 3 up and running on your Pi! I can’t wait to see the projects you come up with.

  26. Alex September 29, 2015 at 10:41 am #

    Hi Adrian,
    first of all great job! I really iked your tutorial. I followed it step by step and I finally got installed openCV with python 3+ correctly I have the 3.0.0 version of openCV installed. But when I’m trying to compile the code of my first program I get this error:
    ImportError: No module named cv2
    I am in the virtual environment by typing the command workon cv…. how can I solve this problem? I’ll appreciate your help, thanks a lot Adrian and again great job!

    • Adrian Rosebrock September 30, 2015 at 6:35 am #

      Hey Alex, this is a pretty common question. There are a number of reasons why the cv2 would fail to import so it’s hard to diagnose exactly which step might be tripping you up. That said, since you are now to virtual environments, I would start there and make sure (1) your cv virtual environment has been created using the mkvirtualenv command and (2) you can access it via the workon command.

      From there, the last step is to make sure your sym-link to the cv2.so file is valid.

  27. eric October 2, 2015 at 4:10 pm #

    Worked perfectly using the actual raspbian jessy distribution. The file is now located in /usr/local/lib/python3.4/site-packages/cv2.cpython-34m.so
    Thank you very much.

    • Adrian Rosebrock October 3, 2015 at 6:13 am #

      Hey Eric, I would run:

      $ mv cv2.cpython-34m.so cv2.so

      Just to finalize the installation. If you can import cv2 from the command line, then it’s not necessary, but it does tidy up the install.

  28. patrick poirier October 4, 2015 at 10:28 am #

    Hi Adrian,

    Just finished installing openCV & Python on my Pi2 = Great tutorial !!

    I have 2 question:
    – Concerning usage of the 4 cores, there where lots of talks about it, but can you tell us how we can tune the Pi2 to get the most out of openCV & Python ?

    – How can I get IDLE working within the virtual environement ?
    I call IDLE on a (CV) shell and try to import cv2; it gives: , Import error: No module named cv2

    Thanks for your help

    • Adrian Rosebrock October 5, 2015 at 6:37 am #

      Hey Patrick — great questions, thanks for asking.

      1. Some functions will use multiple cores by default, others will not. Some image processing functions are not designed to use multiple cores. To get the absolute most out of the performance with your Pi, try compiling with the --WITH_TBB=ON flag set. I’ve ran into a lot of compile errors, but it is possible.

      2. Are you talking about the GUI version of IDLE or the command line version of IDLE? I don’t know the GUI version, so I wouldn’t know about that error. If you are using the GUI version, you should instead switch to IPython Notebooks. Otherwise, the following should work:

  29. Dennis October 6, 2015 at 10:04 am #

    Hello Adrian,

    Thanks for this great tutorial!!
    i have only one question, i already develop many code in the “Python shell”.
    But the “Pyton shell” (IDLE 2.7.3) does not know about the Virtual Environment VC3.
    Is there any way to include the environment in the Python shell?
    Its okey to delete the “OpenCV” folder after completed your tutorial?

    Thanks!

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

      Hey Dennis: (1) Yes, after you have compiled and installed OpenCV it is safe to delete the opencv and opencv_contrib directories. 2. If you want to use the shell, just use the workon cv3 command:

  30. Grace October 6, 2015 at 10:47 pm #

    Hi Adrian,

    I am currently a college student and we are using an old Model B for image processing for a school project. Will this tutorial work for a Model B?

    Thanks

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

      While I have not personally tried to use this tutorial with the B, I do think it would work. That said, I don’t think it’s worth it. The B and the B+ are not fast enough for many (real-time) computer vision applications. I would absolutely try to get a Pi 2 if at all possible.

      That said, as long as you’re only doing basic image processing the B should be okay.

  31. Marco October 7, 2015 at 7:46 am #

    Hi Adrian

    Thanks for the great tutorial

    Unfortunately OpenCV does not compile (make -j4) without errors 🙁

    • Adrian Rosebrock October 7, 2015 at 8:11 am #

      If you’re getting an error with make -j4, then compile with just a single thread. I would also do a clean to make sure you’re building from scratch:

      • Jacob February 14, 2016 at 9:27 am #

        Hi Adrian,

        What do you mean by saying “compile with just a single thread”? I’m new to Pi so I’m not sure what that means.

        • Adrian Rosebrock February 14, 2016 at 9:46 am #

          The -j flag determines the number of threads that are used when compiling a project. If you have a multi-core system (such as the Pi 2), a compile can run faster. For example, make -j4 would compile with 4 threads. To compile with just a single thread, just leave the -j switch off: make

  32. Parviz October 13, 2015 at 4:31 pm #

    Hi adrian , Thanks for the great tutorial and very Helpful Course and Content on Computer vision and Image Serch engines…
    I am trying to install step by step As you’ve explained above…
    but in the Command Line : $ git clone https://github.com/Itseez/opencv.git
    My Pi ,from 11:20 local time (+3.5 GMT) until the time is now 23:51

    Carried out about 84% …
    Is this normal?
    My connection is ADSL with 2MB Speed ofcurse not realy but I think this is very Slowly…
    How do I solve this problem?

    Thanks For Help and Great Your generosity…

    • Adrian Rosebrock October 14, 2015 at 6:23 am #

      OpenCV is a huge library so it can take awhile to pull down the entire repository. It sounds like your connection may have hung up for some reason, but I’m not entirely sure. You can always abort the clone and try again. You can also grab a much smaller archive of the .zip here.

  33. @Cleo_qc October 14, 2015 at 4:47 pm #

    Tiny typo: ~./profile

    • Adrian Rosebrock October 15, 2015 at 6:34 am #

      Thanks! I’ve updated it now 🙂

  34. fBoesche October 17, 2015 at 2:21 pm #

    Hi Adrian, Thanks for a great job ! It is a tutorial that really takes you by the hand to success.

    A question, you have a version of your tutorial Home surveillance and motion detection With the Raspberry Pi, Python, OpenCV, and Dropbox for use with for OpenCV 3.0?

    • Adrian Rosebrock October 18, 2015 at 7:07 am #

      I have not coded up a version for OpenCV 3.0, although it would be very easy to switch it over from 2.4 to 3.0. Just update the calls to cv2.findContours and you’re well over half the way there.

  35. Michael October 18, 2015 at 6:13 am #

    Thanks Adrian. Great tutorial.

    • Adrian Rosebrock October 18, 2015 at 7:02 am #

      Thanks Michael! 🙂

  36. Gabriel October 20, 2015 at 12:21 am #

    Hi Adrian!
    Great tutorial!! Thanks for sharing!!
    I’ve discovered your blog and followed this tutorial in hopes to learn how to stream my home surveillance camera to a website. Will installing OpenCV help to do this?
    Where do I go from here?

    Great site! Thanks again!

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

      Hey Gabriel — thanks for the comment. I personally have not tried to stream the output of an OpenCV + Python script to a website, but I’m sure it’s possible. These tutorials are a bit dated, should should help get you started.

  37. Peter October 20, 2015 at 8:28 pm #

    Adrian,

    Great tutorial, clear and concise.
    I’ve previously followed your tutorial to successfully install OpenCv2.
    Three questions about RasPi 2 and multi cores.
    (from my low level of knowledge)
    I notice that openMP is not selected on CMake and I assume it is not selected for previous OpenCv2 build/install.
    Could this be included in a build for RasPi 2?
    Would it make any performance difference if it was?
    Any other speed-ups (specifically cv2.Canny()) available for RasPi 2’s?
    Kind Regards

    • Adrian Rosebrock October 21, 2015 at 5:48 am #

      I’m glad you enjoyed the tutorial Peter! As far as speeding up OpenCV itself, one of the best ways to do this is to use TBB. You would need to install the TBB dependency and then compile OpenCV using the -D WITH_TBB=ON flag. This will help speedup various functions with OpenCV.

  38. Tony October 25, 2015 at 6:34 am #

    Adrian, thanks for this; very helpful.
    Two minor points:
    in section 1: apt-get install libjpeg8-dev libtiff4-dev libjasper-dev libpng12-dev
    In the latest Raspbian Jessie, libjpeg8-dev is now generically named libjpeg-dev, and libtiff4-dev is now libtiff5-dev.

    At the end, the name of the virtual environment seems to have silently changed from cv3 to cv. Perhaps I’ve missed something!

    • Adrian Rosebrock October 26, 2015 at 11:08 am #

      Hey Tony — for OpenCV 3 install instructions, please see this post.

  39. Andrej October 29, 2015 at 4:31 pm #

    Hi Adrian,

    your tutorial is really nice. The proof? Even knowing almost nothing about Linux I managed to install the OpenCV on my RasPi2 🙂

    However, I can now only import OpenVC into python within the Terminal window, but not in the IDLE2 window.

    I.e, the following does work:

    But importing cv2 into IDLE2 window returns ImportError (No module named cv2).

    What am I doing wrong?

    Thanks.

    • Adrian Rosebrock November 3, 2015 at 10:36 am #

      Awesome, I am glad to hear the tutorial worked for you! As for IDLE2, I have personally not used it before. I would suggest dropping into the cv3 virtual environment before running IDLE2 and seeing if that fixes the problem.

      Otherwise, perhaps another PyImageSearch reader with more experience with IDLE2 can can help resolve this issue.

  40. Timco October 30, 2015 at 8:01 pm #

    Hi, first, Thank you for this, is ist very good and helpful!
    second, I have a problem with this – ImportError: No module named cv2.
    I have done all, as you said, but when I want – import cv2 – it always write that. Please, what can I do??? 🙁

  41. Keshav November 11, 2015 at 4:32 am #

    Hi Adrian,

    Great tutorial! Works out of the box and thanks for the putting it together!

    I’m a noob to python bindings and virtual environments. Could you tell me why the

    $ workon cv

    statement is needed and the cv3 virtual environment represents? Is there any way we can use opencv in python without this command? Seems like a couple of addition steps to remember!

    Thanks!

    Keshav

    • Adrian Rosebrock November 11, 2015 at 6:33 am #

      Hey Keshav — I’m glad the tutorial worked for you, that’s great. Python virtual environments allow you to create Python environments other than your system one. The reason we do this is for dependency reasons. Imagine you’re working on Project A which requires v1.1 of library X. Then, you start working on project B which requires v1.2 of library X. If you installed library X into your system install of Python, you’re pretty much at a stalemate. But if you use virtual environments, you just create two separate environments: one for Project A and another for Project B. You can install different versions of library X into each environment and you don’t have to worry about the dependency issues.

      The mkvirtualenv creates a new Python virtual environment. And the workon command allows you to access your virtual environments. It is an extra step to remember, but the benefits are well worth it.

  42. C++Guy November 14, 2015 at 12:44 pm #

    Great tutorial! I followed this and got OpenCV working. However, I’m on C++. I can do all functions, but I can’t access the Raspberry Cam, Any idea on this?

  43. Neto November 16, 2015 at 10:44 am #

    asking for rpi-update seems to be a bad idea since many rpi’s fail to update firmware and end up requiring a reinstall…

    The general concensus seems to be 99 % of users don’t need to try it.

    • Adrian Rosebrock November 16, 2015 at 1:53 pm #

      Hmmm. I’ve ran rpi-update many times and it’s never once failed for me. Perhaps I’ve just been lucky?

  44. Nichten November 16, 2015 at 6:53 pm #

    I have tried to install OpenCV with python3 support but with typing source ~/.profile I get information that support for python 3.0-3.2 will fall down. Any suggestion what to do? Can I just ignore it?

    • Adrian Rosebrock November 17, 2015 at 6:19 am #

      This should just be a warning, not an error message — you should be able to continue without a problem.

  45. RSS December 3, 2015 at 11:18 pm #

    Hello Adrian

    I had a little doubt regarding installation of opencv 3-3.0,python 3+ in NOOBs_v1_4_1
    (os from raspbian downloads ) as in the course of installation at the step

    apt-get install libjpeg8-dev libtiff4-dev libjasper-dev libpng12-dev

    as it alerting me to install libtiff5-dev instead of libtiff4-dev and also telling that libtiff5-dev supports with libjpeg-dev instead of libjpeg8-dev……

    by doing apt-get install libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev it was installed fine…

    pls tell me if any problem rises if so what it is….

    As the remaing installation done PERFECTLY……

    • Adrian Rosebrock December 4, 2015 at 6:28 am #

      It sounds like you might be installing OpenCV on Raspbian Jessie, not Raspbian Wheezy, which this tutorial is designed for. If so, you’ll want to head over to this post on installing OpenCV 3 on Raspbian Jessie.

      If switching the apt-get commands solved the issue (as it does in the post I linked to), then everything should work just fine.

  46. Hailey December 11, 2015 at 5:37 am #

    Hi Adrian,

    I’m really new to this whole Raspberry Pi stuff. I’m stuck at section 2, we need to update the ~.profile but I don’t think I have the file and you said if we don’t have the file we need to create it. How can I do so?
    Thanks.

    • Adrian Rosebrock December 11, 2015 at 6:28 am #

      You need to open up the file in a text editor. You could use a terminal editor such as vi, emacs, or nano, or go with a GUI based editor such as the one included with the Raspberry Pi. Give the following command a shot and see if you can navigate the terminal text editor:

      $ nano ~/.profile

  47. Simon December 31, 2015 at 4:04 am #

    Thank you so much! This is absolutely the best tutorial on the internet and all your tutorials are top notch.

    • Adrian Rosebrock December 31, 2015 at 6:54 am #

      Thanks Simon! 😀

  48. Nick January 1, 2016 at 4:22 pm #

    Hello, great tutorial! I’ll get to it, I don’t think I had python 3 on my device (I have a brand new Pi 2 though, it should be there?) and I followed the 3.0 choose your own installation. I forget which step, I believe one of the steps configuring the virtual environment, I started getting the message: Support for Python 3.0-3.2 has been dropped. Future versions will fail here.

    I have no idea what to do, and further, everything seemed smooth until I did cmake, and across from “libraries” it said python 2.7, much like the other commenter Dustin said. I didn’t want to do the step that takes awhile without clearing this up first. If it’s not obvious, I’m not only a noob, but in a hurry to type at the moment. Thanks so much for the tutorial and for reading this.


    Did… did you delete my comment? I understand moderation takes awhile, but you seemed more than willing to go above and beyond helping others 🙁


    nevermind, I’m an idiot. Having a rough day. brb changing my name to pebkac

    • Adrian Rosebrock January 2, 2016 at 7:07 am #

      To address your previous comments, yes, I do hand moderate every single comment on the PyImageSearch blog (I have consolidated your other comments into a single comment to keep them cohesive). Sometimes it does take me 24-36 hours to respond to every comment/moderate them. It has nothing to do with you personally, or any other reader on the blog, so please be respectful of my time and the free support I try to provide.

      As for your question, Python 3 should be installed on your Raspberry Pi; although, it would be helpful to know which version of Raspbian (i.e., Wheezy or Jessie) that you are running. You can see your Python 3 shell by typing:

      $ python3

      And that will open up a Python 3 shell and display important information such as version number.

      Regarding “Support for Python 3.0-3.2 has been dropped”, I don’t know what may have caused that error without knowing the command you executed. Since it was for the virtual environment, my suspicion is that you might be running an older version of Raspbian (perhaps you installed with an old NOOBS card?)

      • Nick January 2, 2016 at 12:44 pm #

        I was hoping you’d delete the other comments xD Again, I’m sorry, I was a little irritable. Thank you so much for replying. Any time I do anything with Python or boot the system I get the message “support dropped for 3.0-3.2” or something on that line. I got the message three times creating a new virtual environment when I went to refollow your tutorial. When I type $ python3 everything looks normal, no mention of that message, and things run in python3 (I believe) well (like the python3 pip command). I really can’t remember but I think it was the pip.py step that started the errors. I’d really like to get this working, but I can’t find anything online that talks about a fix to that error message. It *says* support dropped, future versions will fail, but it seems to work. I guess I’ll have to reinstall. Do you recommend Jessie? It’s the “newest” version, right? I remember seeing a few guides on how to retrograde parts of Jessie to Wheezy again, not sure what that means for installation.

        • Adrian Rosebrock January 2, 2016 at 3:21 pm #

          You can certainly use Jessie or Wheezy. In the context of computer vision, it doesn’t make much of a difference. Jessie is certainly the newest version and will be supported for longer, but again, that shouldn’t be the make or break decision. I would be curious to know which version of Python 3 you are running. But if everything else is working, I wouldn’t worry about the error message and go about your coding 🙂

          • nick January 2, 2016 at 4:33 pm #

            Python 3.2.0 if I recall correctly. I’m going to reinstall raspbian and try again. I’m not sure what happened, but I couldn’t compile anything, and now I can’t load the graphical environment. I almost ran out of space anyway, going nuts and installing everything. I think bastardizing multiple different tutorials messed something up. I’m a “fire hot” learner, I usually have to mess something up the first time I try it, I’m glad that didn’t mean frying my pi in this case. Sincerely, thank you for your tutorials, responsiveness, and willingness to deal with frustrated users!

          • Nick January 3, 2016 at 8:51 pm #

            Ok, I reinstalled wheezy and started from the top of your tutorial on my pi 2, going the python3 route, and got an error after $ mkvirtualenv cv

            [ output removed by admin ]

          • Adrian Rosebrock January 4, 2016 at 6:52 am #

            Hey Nick, as I mentioned in an email I sent to you last night, I can’t approve comments that include large code blocks or output. Please use GitHub Gists or PasteBin and link to it from it your comment. Including large output blocks destroys the formatting and makes it impossible to read.

            Secondly, when I did look through your output, you did not get an error message. It was only a “warning”. It looks like the virtual environment was setup just fine.

  49. BOBAN RAPHAEL January 7, 2016 at 8:31 am #

    Can u tell me how to uninstall OpenCV and python? i had followed this page for installation instructions

    • Adrian Rosebrock January 7, 2016 at 12:36 pm #

      You can uninstall OpenCV by removing all libopencv* files from /usr/local/lib. You shouldn’t uninstall the system version of Python since there are other programs that rely on Python.

  50. Mootaz January 15, 2016 at 5:01 pm #

    Hello Adrian Rosebrock.
    First, Thank you for your tutorials.
    Using my RPI 2, I followed your steps carefully, I completed the “Section1” with successful results, then jumped to “Section3” as I like to install the OpenCV to use with python 3.
    The “cv” environment was successfully created, but the when I executed the
    “pip install numpy” command, numpy was downloaded but not build, the system returned red lines finished with “MemoryError”.
    The putty software outputs after a long break, session out, and After restart the RPI becames N times solower to respond to simple commands.
    Maybe the memory cache dedicated to the numpy install process isn’t big enough to support that, or maybe inexistant, any suggestion ?
    Thanks

    • Adrian Rosebrock January 16, 2016 at 9:33 am #

      That’s pretty strange to receive a MemoryError on the Pi 2, given that it has 1GB of RAM. Can you confirm:

      1. That you are indeed using a Raspberry Pi 2.
      2. That you are receiving a MemoryError or an “out of space error” (meaning that the disk/card is full).

      Otherwise, you can try using the --no-cache-dir switch which can help with the memory usage:

      $ pip install numpy --no-cache-dir

      • Mootaz February 20, 2016 at 4:26 pm #

        Hi Adrian.
        Sorry for late response.
        The problem there was that tightvncserver also was running in parallel while installing numpy. I added the “$ vncserver” command to the ~/.profile file to launch it at login.
        The experience revealed that the vnc server is a memory hungry process that affects the numpy installation.
        Now I use Xming, a much better alternative.

        • Adrian Rosebrock February 22, 2016 at 4:29 pm #

          Thanks for sharing. And yes, whenever possible, try to use X11 if you are working via a network. It’s much less resource intensive.

  51. Anuj January 20, 2016 at 5:35 pm #

    I installed OpenCV. However, I am getting the ‘No module named cv2’ error.
    I tried to sym-link the cv2.so again. However its failing and I am just getting a message that the file exists. What do I do now?

    • Adrian Rosebrock January 21, 2016 at 5:02 pm #

      You can remove the sym-link by using the rm command:

      rm cv2.so

      As for the error message, please be sure to consult the “Troubleshooting” section of this post.

  52. mai January 22, 2016 at 10:07 am #

    I think you need to update this page as libtiff4-dev no longer exists and the rest doesn’t go as smooth either. I tried to install on a fresh RPI2 running Jessie, and no luck.

    • Adrian Rosebrock January 22, 2016 at 4:45 pm #

      You should be using this post to install OpenCV on Rasbpian Jessie.

  53. pritam raskar January 24, 2016 at 5:34 am #

    Workon cv
    This command is not working in Debian Jessie
    Help me out….
    On writing on terminal
    Workon cv
    It is showing ‘command not found’…

    • Adrian Rosebrock January 25, 2016 at 4:11 pm #

      Please see the “Troubleshooting” section of this post. It’s likely that your .profile file has not been properly updated.

  54. Andrea January 25, 2016 at 2:44 pm #

    Sorry,
    I made the compilation on a USB external , since my first attempt ran out of disk space ( in need of more than 1 GB for the compilation , apparently ) .
    found the answer:
    http://askubuntu.com/questions/235700/linking-error-when-trying-to-compile-on-a-shared-drive

    • Adrian Rosebrock January 25, 2016 at 4:01 pm #

      Congrats on resolving the issue Andrea!

  55. Andrea January 27, 2016 at 8:20 am #

    hi
    i have installed and apparently work fine…
    but i’m connecting via ssh from ubuntu and when launch a py script (es. face.py) obtain following error:
    (Video:15600): Gtk-WARNING **: cannot open display:

    • Adrian Rosebrock January 27, 2016 at 3:03 pm #

      If you’re SSH’ing into your Pi, you need to enable X11 forwarding:

      $ ssh -X pi@your_ip_address

  56. Rishabh February 1, 2016 at 12:22 pm #

    Hi Adrian, i forgot to download the opencv_contrib folder. And now i dont have the extra modules of opencv which i desperately need for my project. Somehow without that folder also the rest of the opencv got downloaded and installed and it works perfectly but i cant use the face recognition and other functions which are present in the extra modules. Can you tell me how i can add those extra modules without having to remove the whole opencv-3 altogether. And if there is no other way to go ahead with this then can you tell me how i can uninstall the whole opencv package from my raspberry pi 2?

    • Adrian Rosebrock February 2, 2016 at 10:34 am #

      All you need to do is re-download the original opencv and opencv_contrib repos. Then, re-run CMake and make sure that the extra modules were properly recognized. From there, just run make and make install — everything will be properly installed from there. No need to uninstall OpenCV!

      • Rishabh Shukla February 3, 2016 at 8:52 am #

        Thanks a lot Adrian
        I will try doing that and let you know

        • Adrian Rosebrock February 4, 2016 at 9:18 am #

          Best of luck Rishabh!

  57. Andrea February 2, 2016 at 6:48 pm #

    Hi
    i have recompiled and reinstalled many times from user pi and root and from cv3 virtualenv and not…
    Ok now it works fine with complete features of contribute modules and installed in CV3 virtualenv…
    i have worked and recompiled on usbkey for leave free space on rasperry SD

    but after reinstallation in my raspberry i not have free disk space…
    after the first time i’ve tried to install i had 1.2 GB free and now i have only 22MB
    how i can purge old installation file? or temp files? why the reinstallation increase too much disk space? the files is not overwritten?
    thanks very much for your answers

    • Adrian Rosebrock February 4, 2016 at 9:27 am #

      If you already have OpenCV setup and installed on your system, you can delete the opencv and opencv_contrib directories — they are only required for the build.

  58. Hermawan February 8, 2016 at 10:09 am #

    Dear Adrian,
    I have followed your step by step instruction, however after come to step 4 I wonder that the version I got is 2.4.1 !! not 3.0.0 nor 3.1.0 as expected. Could you give me why this could be?
    Thanks.

    • Adrian Rosebrock February 8, 2016 at 3:45 pm #

      Did you already have OpenCV 2.4.1 installed on your system before you compiled and install OpenCV 3? If so, that is likely why.

  59. Ugur February 11, 2016 at 4:13 am #

    Problenm for libtiff4 (after upgrade jessie ) i found solution from ubuntu pages:
    sudo apt-get install libtiff5-dev libtiff5 libjbig-dev

    • Adrian Rosebrock February 11, 2016 at 9:21 am #

      Thanks for sharing Ugur! I actually have an entire tutorial dedicated to installing OpenCV on Raspian Jessie here.

  60. Abdullah February 15, 2016 at 2:56 pm #

    Hi Adrian, thanks for all. But I want to ask some question. I did this operation with ssh session. If I connect to my Pi with ssh it works but When I connect to my TV with HDMI interface , I open console terminal and I typed workon cv . Terminal returns me “workon: command not found”. How can I fix this? Can you help me about it.

    • Adrian Rosebrock February 15, 2016 at 3:02 pm #

      You need to source ~/.profile each time you open up a new terminal/reboot. From there, the workon command will work.

      • Abdullah February 15, 2016 at 3:46 pm #

        Thank you very much Adrian. Have a good days.

  61. Nasaa February 16, 2016 at 4:29 am #

    Hello Adrian, thanks for all. But I want to ask some question. I open console terminal and I typed workon cv . Terminal returns me “workon: command not found”. How can I fix this? and i typed source ~/.profile but its doesn’t work. can you help me

    • Adrian Rosebrock February 16, 2016 at 3:39 pm #

      Please see the “Troubleshooting” section of this post. I details reasons why the workon command may not work. Even if you have ran source ~/.profile, you may not have updated your ~/.profile correctly.

  62. Neil De Guzman February 19, 2016 at 3:41 am #

    hello. i want to use pyside along the opencv in raspberry pi but when i install pyside and opencv(using your method) and go to cv3 it said that there is no pyside module and when i exit from cv3 it said there is no cv2 module. how can i use both?

    • Adrian Rosebrock February 19, 2016 at 6:52 am #

      I’ve never used pyside before, but from what I understand, it’s pip-installable? If so, just enter the cv3 virtual environment and install it:

      From there, you’ll have access to both OpenCV and pyside. Again, keep in mind that the virtual environment by definition is separate and independent from your main Python install.

  63. David March 3, 2016 at 1:24 pm #

    Hello Adrian,
    Is it possible to use a 2.7 build for a 3.4 ?
    I followed your excellent tuto to install opencv3 + python3, but cv2 is only available on python 2.7 …. I think I loosed my way somewhere ..

    So I need to save 1h of my time with the build operation. I don’t need virtualenv for my project.

    Another question : Is it possible to save (for ex in a zip file), the result of the build and use it on an another raspberry? This is again for save time.

    David

    • Adrian Rosebrock March 6, 2016 at 9:31 am #

      Provided that the (1) architectures of the Pi are the same and (2) you are using the exact same Python + NumPy versions, then yes, you should be able to make an archive of the build and move it to another Raspberry Pi. However, if you want to migrate the install, I would suggest creating a .img from your SD card and flashing it on to another card, that way you can ensure everything is using the same version.

      As for your Python 2.7 versus Python 3.4 issue, it’s hard to tell what the exact problem is, but the issue almost certainly lies with CMake. I would double check your output of CMake and ensure that you are compiling OpenCV support for Python 3 rather than Python 2.7.

  64. David March 4, 2016 at 4:23 am #

    Hi Adrian,

    I found a little error in the tuto. When you link the build file to the virtualenv, you tape the command line :

    ln -s /usr/local/lib/python3.4/site-packages/cv2.cpython-34mu.so cv2.so

    But in my raspberry, the file name is : cv2.cpython-34m.so (without u)

    Regards.

    • Adrian Rosebrock March 6, 2016 at 9:23 am #

      Hey David, thanks for sharing. It’s important to note that the exact output of the filename can vary depending on your system and which version of OpenCV you are compiling.

  65. Nickolas March 21, 2016 at 1:49 pm #

    What if I don’t want virualenv ?

    • Adrian Rosebrock March 21, 2016 at 6:31 pm #

      You don’t have to use virtualenv and virtualenvwrapper if you don’t want to. Simply skip those steps and any commands that use “workon”, “mkvirtualenv” or “source”.

  66. darshan March 29, 2016 at 9:15 am #

    hello..
    can we install opencv 3.0+ above given procedure for raspbian wheezy ?

    • Adrian Rosebrock March 29, 2016 at 3:43 pm #

      Yes, the tutorial you are reading now covers how to install OpenCV 3 with Python 2.7 and Python 3 bindings on Raspbian Wheezy.

  67. Emilio Cremades April 5, 2016 at 6:59 am #

    Hello Adrian, your manual is fantastic.

    I follow it and install two differents workon environment on my raspberry pi 2. I install them through a ssh conection.They works perfectly when i am connected by a ssh connection.

    workon cv2 has python 2.7 and OpenCV 3.0.0
    workon cv3 has python 3.4 and OpenCV 3.0.0

    But I have a problem when I work directly on the raspi, i do not have access to workon. In fact as I type: workon cv2

    pi@raspberrypi:~$ workon cv2,
    bash: workon: commnd not found

    Of course if I connect via ssh I find no problem.

    I don’t understand what is happend, can you help me?

    • Adrian Rosebrock April 6, 2016 at 9:13 am #

      Hey Emilio — make sure you source your ~/.profile before trying to execute the workon command. This should take care of the error:

  68. Simon P April 6, 2016 at 2:58 pm #

    I’m trying to build opencv on a Rasbperry Pi 3, I get as far as:

    All that’s left now is to compile OpenCV 3.0:
    make -j4

    However all I get is:
    make: *** No targets specified and no makefile found. Stop.

    I’ve been through the entire instructions twice and both times I get as far as the above, any suggestions?

    • Adrian Rosebrock April 7, 2016 at 12:43 pm #

      It sounds like either (1) you are not in the build directory, (2) CMake has not been executed, or the most likely, (3) CMake finished with errors. Double check your output to CMake and ensure your build files were generated without error.

  69. Simon P April 7, 2016 at 4:04 pm #

    Thank you, install complete and working 🙂

    • Adrian Rosebrock April 8, 2016 at 12:56 pm #

      Congrats on getting OpenCV installed Simon!

  70. edroor April 8, 2016 at 3:08 am #

    Hi, i did all steps, and my opencv works on python 2.7 perfect… but dont works on python 3.4, i did step by step and i had the virtualenv CV but i cant import cv2, and i can se the file cv2 in the virtualenv CV, but when y did make -j in the CV dont take the 60 min or more for the install but say installed i realy need some help here.

    • Adrian Rosebrock April 8, 2016 at 12:52 pm #

      I’m not sure I 100% understand your question, but it sounds like you have OpenCV working for Python 2.7, but it doesn’t work for Python 3.4? If so, you’ll need to make sure you create a Python virtual environment explicitly for Python 3 and then make sure the output of CMake is picking up your Python 3.4 version.

  71. Arasch April 16, 2016 at 12:54 am #

    Hi Adrian, Thank you so much for the great tutorial. I applied your steps to set up to install openCV3.1 with python 3.4 a couple times on other Pi’s with success.
    But this time I am trying to install on ubuntu 14.04 (Trusty). Everything seems to run right up-to where I look for
    ls -l /usr/local/lib/python3.2/site-packages
    and only find “dist-packages” there — no site-packages and no cv package.
    I noticed that after cmake -D … in the Python 3 section it says Libraries: NO.
    What am I missing?

    • Adrian Rosebrock April 17, 2016 at 3:35 pm #

      This blog post assumes you’re using Python 3.2. I might be wrong here, but doesn’t Ubuntu 14.04 use Python 3.4? If so, then the paths to CMake need to be modified to use the correct version of Python.

  72. raju kunde April 23, 2016 at 3:03 pm #

    Great tutorial.

    Ran out of disk space while running make. So, I removed build folder and also uninstalled mathematica.

    • Adrian Rosebrock April 25, 2016 at 2:06 pm #

      Thanks for sharing Raju!

  73. Zack May 26, 2016 at 4:09 am #

    Hi Adrian.How to install opencv2.4 and opencv3.0 on the same device?

    • Adrian Rosebrock May 26, 2016 at 6:16 am #

      This is absolutely possible, I’ve done it many times myself, but I do not recommend it unless you’re very comfortable with Unix environments, understanding how they work, and debugging paths.

      Basically, what you do is download the source to OpenCV 2.4 and OpenCV 3. Create a virtual environment for each. Compile them both. But don’t run sudo make install. Keep everything but the build directories for each OpenCV version. Then sym-link in your cv2.so file for each virtual environment.

  74. Rob June 1, 2016 at 10:03 pm #

    Hi Adrian!

    Your tutorial has been absolutely fantastic. Clear, accurate, and the perfect amount of information. I am very new to using the Pi and coding, so your tutorial has helped me get a virtual environment and opencv installed, which would have been impossible otherwise.

    The program I am starting to work on is using GPIO, camera, and openCV. I have been developing in the startx gui since I am going back and forth between my emails, and I generally find it easier to have a couple windows open on the Pi in order to troubleshoot and easily move files around. I have not been successful getting openCV to work in the gui desktop since it never finds the module, but things seem to work if I run them from the command line from a virtual environment. I have installed opencv3.1.0 and python 3.4.2.

    So, my question is: Within the “sudo startx” gui, is it possible to use a virtual environment and incorporate opencv for my project? Or am I doomed to the command line and nano editing environment??

    • Adrian Rosebrock June 3, 2016 at 3:11 pm #

      Once you’ve started the GUI, can you not open a terminal and execute the workon cv command? This will resolve the issue. Or perhaps i’m not understanding the question correctly.

  75. Anje June 3, 2016 at 2:29 am #

    Hi Adrian,

    I’m having some small doubts. How much memory it will take?? And from where it will use the memory?? From sd card or raspi??

    Thank you. Sorry if it is a stupid question.

    • Adrian Rosebrock June 3, 2016 at 2:59 pm #

      To compile OpenCV, I would suggest having at least 1-2GB of space free on your SD card.

      • Anje June 8, 2016 at 1:27 am #

        Thank you. I have installed it successfully. But when I run the program it shows “ImportError: No module named cv2”. What should be done to clear this error. Please help me. I’m all new to this area.

        • Adrian Rosebrock June 9, 2016 at 5:30 pm #

          Please see the “Troubleshooting” section of this post.

  76. Kristen June 24, 2016 at 10:18 am #

    This tutorial helped vastly yesterday, I checked my version since I had uninstalled (or attempted to at least) openCV 2.4.10, and all was well. Today, I got to work, booted up my machine and checked versions after my code wouldn’t work and found that today the version is 2.4.10. Any thoughts on how to either fully uninstall the old version without hurting the current one or even just switching to 3.1?

    • Adrian Rosebrock June 25, 2016 at 1:29 pm #

      If you code didn’t execute, make sure you are in the cv virtual environment prior to executing your code:

      As for installing both OpenCV 2.4 and OpenCV 3 on the same machine, that’s absolutely possible, but challenging if you’re new to Unix. Basically, you need to create a new virtual environment for OpenCV 3, compile the OpenCV sources against this virtual environment, delete everything but the build directory, and then sym-link in the cv2.so file into your site-packages directory for the virtual environment.

  77. Kenneth July 8, 2016 at 9:13 pm #

    Hi!
    I’ve done through section 1 & 3 but when I verify my CV in section 4 I got this error message says:

    ImportError: dynamic module does not define init function (PyInit_cv2)

    I’ve also read the comments and there’s no related to my case. By the way, your tutorials about openCV on raspi are great, thanks for sharing!

    PS. Im using Python3.4 and CV3.0.0

    • Adrian Rosebrock July 9, 2016 at 7:24 am #

      Based on the error message, it sounds like CMake isn’t sure which Python version to use. Double-check your output of CMake and ensure your Python 3 version is being properly picked up. From what I see here, I think CMake is picking up your Python 2.7 version instead.

  78. Sasikumar M August 1, 2016 at 8:09 am #

    Pretty good tutorial for beginners.

    I successfully installed opencv in my RPI3 without error.

    But i could not able to import cv2 in python IDE

    pls. help me

    thank you in advance

    • Adrian Rosebrock August 1, 2016 at 1:57 pm #

      Which IDE are you using? Make sure you set the correct Python interpreter for the project (i.e., the Python virtual environment).

  79. Rick August 24, 2016 at 1:31 pm #

    Adrian

    WOW WOW WOW!

    I am glad I found You, In your awesome instructions you only mention the “Pi” Camera.
    I am an old dog learning new things and Have built a CnC controller with the Pi 2 and have learned many things from my mistakes! The most important one is If you don’t Know ask.
    so here it goes: Following is an edited text from BCNC Wiki (all credit goes to them) they say any web based camera recognized by OpenCv will work? As long as you have the two files/programs listed installed. Do You think this work?

    “Probe Camera Alignment

    The Camera alignment system permits the user to align in X/Y with accuracies of the order of tens of micrometer using a web camera mounted on the Z-axis next to the spindle.
    Configuration

    The camera alignment system requires the presence of python-opencv module
    Select the camera from “Tools -> Config -> Camera”.
    Select the appropriate rotation.
    Note: for the moment v0.9.2 the configuration is disabled, you have to manually edit the .bCNC file

    note: Be sure you have also installed PIL, OpenCv, ImageTk for your Python.
    Hardware

    Any USB camera that can be recognized by opencv will work with bCNC. In my machine

    I’ve bought a cheap web camera, with a clip that mounts on top of the laptop screen. The resolution is 352×288, despite that it gives very nice results.”

    Thank You

    • Adrian Rosebrock August 25, 2016 at 7:21 am #

      Thanks for the kind words, Rick 🙂 As for your question, I personally haven’t used the CNC tools you’re referring to, but I would likely trust their documentation — if a USB camera can be recognized by OpenCV, then it should work with their software. I prefer using the Logitech C920 since it’s relatively cheap and a nice camera. Otherwise, you can find a list of Raspberry Pi compatible cameras here. I hope that helps!

  80. sharath December 31, 2016 at 1:22 pm #

    while compiling the opencv file the core heats up really bad, is it ok or do i need to worry about it?

    • Adrian Rosebrock December 31, 2016 at 1:54 pm #

      It’s totally fine. Keep the Pi in a well ventilated area.

  81. Lidya January 18, 2017 at 5:40 am #

    Hi Adrian,

    I followed the tutorial until the end and everything worked just fine. A few days later, when I type workon cv3 on the command line, it says “bash: command not found”. Any ideas on what to do? Also, is there a way to use normal USB camera instead of a Raspberry Pi camera to stream video?

    Thank you.

    • Adrian Rosebrock January 18, 2017 at 7:04 am #

      Hey Lidya — it sounds like your ~/.profile file was not updated correctly. Go back to Section 2 and ensure that your .profile file matches mine.

      • Lidya January 22, 2017 at 12:32 am #

        Hi Adrian,

        You were right, I didn’t update the ~/.profile file correctly. Thank you!

        • Adrian Rosebrock January 22, 2017 at 10:13 am #

          Happy I could help Lidya — and congrats on resolving the issue 🙂

  82. Mona Jalal January 18, 2017 at 11:37 pm #

    Hi Adrian, Any idea why after installing OpenCV3.2.0 successfully in Virtual Env I get this error http://stackoverflow.com/questions/41733557/opencv-installed-successfully-but-importerror-no-module-named-cv2 Thanks

  83. prashanth January 19, 2017 at 5:08 am #

    hello, while compiling the opencv using “make -j4” raspberry pi keeps on restarting. Please tell how to resolve it….

    • Adrian Rosebrock January 20, 2017 at 11:04 am #

      That sounds like an issue with your Raspberry Pi. I would suggest re-installing Raspbian and trying to keep the Pi cool during the compilation step. The Pi might be overheating in which case just run make.

  84. numreen February 7, 2017 at 10:52 am #

    can i Know where exactly to write export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python2.7

  85. safa February 24, 2017 at 9:03 am #

    ln -s /usr/local/lib/python3.4/site-packages/cv2.so cv2.so
    ln: failed to create symbolic link ‘cv2.so’: File exists
    please guide me for this error

    • Adrian Rosebrock February 24, 2017 at 11:23 am #

      Please see my reply to “Anuj” above.

  86. rohit February 24, 2017 at 3:11 pm #

    Hey my raspberry pi 3 is stuck at 87% while building the following file
    for installing open cv 3.2.0 on python 2.7

    Building CXX object cpp/CMakeFiles/tutorial_camera_calibration.dir/tutorial_code/calib3d/camera_calibration/camera_calibration.cpp.o

    it isnt showing any error
    how do i go past this

    • Adrian Rosebrock February 27, 2017 at 11:21 am #

      It sounds like your Raspberry Pi may be stuck in a race condition. Clean the build and then re-compile using only a single core:

  87. joseph adamu February 27, 2017 at 3:40 pm #

    I just finished the following process
    $ sudo rm -rf ~/.cache/pip/
    $ pip install numpy

    and I rebooted the raspberry pi as soon as the installation was done but it just logged me out and when I tried to login with raspberry as the password it will just show a black screen and then come back again with the login window please what do I do help me out

    • Adrian Rosebrock February 27, 2017 at 5:02 pm #

      That’s very strange Joseph, I’m not sure what the error might be. I think it’s likely an issue with your Raspberry Pi, specifically with the firmware. I would suggest re-installing Raspbian if you can. The install process itself should not have any impact on the usability of your Raspberry Pi.

      • Joseph adamu March 1, 2017 at 5:22 pm #

        thank you very much i really appreciate the advice i don’t know what was wrong with it but i re-installed Raspbian and it worked.
        but i have encountered another problem which am not suppose to but i inserted my raspberry pi camera in the the camera slot on the raspberry and powered the raspberry pi but didn’t get the window option that shows where to enable camera so i did the “sudo apt-get update”, and “sudo apt-get upgrade” , and then “sudo raspi-config” in the terminal but still couldn’t see enable the camera option i even went to the preference and selected raspberry pi configuration and under interface i enabled the camera and enaled SSH but still couldnt get the camera enabled but the camera shows it is powered. please so help me out cheers.

        • Adrian Rosebrock March 2, 2017 at 6:44 am #

          Hey Joseph — that is very strange, I have not encountered that error before. You might want to install a fresh version of Raspbian or even post on the Raspberry Pi forums as this is isn’t a computer vision error, but likely a problem with your Pi or Raspbian install.

  88. Stein March 19, 2017 at 5:32 am #

    Adrian,

    I just wanted to let you know that I followed the tutorial and successfully installed OpenCV 3.2.0 with binding for python 3 on the new Raspberry Pi Zero W! The process takes a while (as you would expect with only 1 core) but it worked like a charm.

    I will be testing the surveillance camera project I initiated using your tutorial to slim it down to work with the Pi Zero W.

    • Adrian Rosebrock March 21, 2017 at 7:34 am #

      Congrats on getting OpenCV installed Stein, nice job!

  89. wilson April 14, 2017 at 1:59 am #

    Hi Adrian,could you please send me a link about installing opencv on python2.7 and python3 at one raspi? i ‘ve already installed it on python2.7. but i still want to execute the program on python3, i red your article metioned above, you were gonna write a report about how to install opencv for both python version..

    Thanks mate.

    • Adrian Rosebrock April 16, 2017 at 9:00 am #

      Hey Wilson — I’m not sure what you mean. This blog post covers how to install OpenCV bindings for both Python 2.7 and Python 3.

      If you are interested in a pre-configured Raspbian .img file with OpenCV 3 + Python 2.7 and OpenCV 3 + Python 3 pre-configured and pre-installed, then be sure to take a look at the Quickstart Bundle and Hardcopy Bundle of Practical Python and OpenCV.

  90. JOSEPH May 5, 2017 at 12:05 pm #

    Hello Adrain am back again thank you for your support am trying to run the facial detection file and i am getting this error please how do i fix it

    import cv2
    ImportError: No module named ‘cv2’

  91. Parker June 14, 2017 at 8:52 pm #

    Is it possible to have opencv built for both python2.7 and python3? Would I just make build1 and build2 folders in the opencv directory and cmake each build with a different python specified?

    I would like to have the ability to write opencv in 2.7 or python3.

    Could you confirm whether or not this is an okay approach?

    Thanks!
    Parker R

    • Adrian Rosebrock June 16, 2017 at 11:24 am #

      Correct, you would create two separate build directories, one for Python 2.7 and another for Python 3. Then compile OpenCV independently for each.

  92. Rahul August 22, 2017 at 8:35 am #

    I have the model B+ version running a fresh NOOBS insallation. The build is nearly 20 hours in and is still at just 83%.

    What might be the reason for it being this slow?

    • Adrian Rosebrock August 22, 2017 at 10:41 am #

      It sounds like there might be a race condition issue. Try running:

      $ make clean
      $ make

      This will restart the compile using one core. It will take longer, but will ideally alleviate the race condition issue.

      • Rahul August 23, 2017 at 10:03 am #

        Hey Adrian,

        I did use only make the first time as well, and went ahead and executed both:

        $ make clean
        $ make,

        and yet again after around 22 hours, it gets stuck at exactly the same point without showing any type of error at all.

        [83%] Building CXX object modules/python2/CmakeFiles/opencv_python2.dir/__/cv2.cpp.o

        The above point is exactly where the problem seems to be persisting, where it keeps attempting to build it for nearly 2-3 hours now. Shall i give it more time perhaps?

        Also it might be helpful to know that the OpenCV version I’m trying to build is 3.3.0, the latest one. So might that be the problem?

        Thank you for your time,
        Rahul

        • Adrian Rosebrock August 23, 2017 at 11:17 am #

          Hi Rahul — it’s hard to tell what the exact issue is without physical access to your machine. Take a look at the output of “top” to see if OpenCV is still compiling. I would also try another version of OpenCV (such as 3.1 or 3.2) and see if that resolves the issue.

  93. James September 24, 2017 at 12:56 pm #

    Hey, i’m up to the bit where I have to install pip for python 3 but I keep getting a syntax error saying invalid syntax. My raspberry pi will let me the thing before (line starting with wget) but it wont let me run the next line. Help please need this software for a project thank you

    • James September 24, 2017 at 12:59 pm #

      I’m also trying to download the 3.3.0 version of open cv

    • Adrian Rosebrock September 26, 2017 at 8:39 am #

      What is the exact error you are getting? Without knowing myself and others cannot point you in the right direction.

Trackbacks/Pingbacks

  1. Install OpenCV and Python on your Raspberry Pi 2 and B+ - PyImageSearch - July 27, 2015

    […] have just released a brand new tutorial that covers installing OpenCV 3 with Python 2.7/Python3+ bindings on the Raspberry Pi 2. The tutorial you are reading now still works perfectly, but if you’re interested in […]

  2. How to install OpenCV 3 on Raspbian Jessie - PyImageSearch - November 15, 2015

    […] Install OpenCV 3.0 with Python 2.7/Python 3+ bindings on Raspbian Wheezy. […]

Leave a Reply