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:
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.
- How to install OpenCV 3.0 on Raspbian Jessie.
- Install guide: Raspberry Pi 3 + Raspbian Jessie + OpenCV 3.
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:
$ sudo apt-get update $ sudo apt-get upgrade $ sudo rpi-update
Timing: 9m 5s
Now we can install developer tools required to build OpenCV from source:
$ sudo apt-get install build-essential git cmake pkg-config
Timing: 43s
As well as install packages used to load various image formats from disk:
$ sudo apt-get install libjpeg8-dev libtiff4-dev libjasper-dev libpng12-dev
Timings: 27s
Let’s install some video I/O packages:
$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
Timings: 26s
Install GTK, which handles OpenCV’s GUI operations:
$ sudo apt-get install libgtk2.0-dev
Timings: 2m 20s
We can also optimize various functions (such as matrix operations) inside OpenCV by installing these packages:
$ sudo apt-get install libatlas-base-dev gfortran
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:
$ cd ~ $ git clone https://github.com/Itseez/opencv.git $ cd opencv $ git checkout 3.0.0
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:
$ cd ~ $ git clone https://github.com/Itseez/opencv_contrib.git $ cd opencv_contrib $ git checkout 3.0.0
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:
$ sudo apt-get install python2.7-dev
Timings: 1m 20s
Install pip
, a Python package manager that is compatible with Python 2.7:
$ wget https://bootstrap.pypa.io/get-pip.py $ sudo python get-pip.py
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 practice, one 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:
$ sudo pip install virtualenv virtualenvwrapper $ sudo rm -rf ~/.cache/pip
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.
# virtualenv and virtualenvwrapper export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python2.7 export WORKON_HOME=$HOME/.virtualenvs source /usr/local/bin/virtualenvwrapper.sh
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:
$ source ~/.profile
Time to create the cv3
virtual environment where we’ll do our computer vision work:
$ mkvirtualenv cv3
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:
$ workon cv3
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:
$ pip 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:
$ sudo rm -rf ~/.cache/pip/ $ pip 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:
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:
$ workon cv3
And once you are in cv3
virtual environment, you can use cmake
to setup the build:
$ cd ~/opencv $ mkdir build $ cd build $ cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_INSTALL_PREFIX=/usr/local \ -D INSTALL_C_EXAMPLES=ON \ -D INSTALL_PYTHON_EXAMPLES=ON \ -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \ -D BUILD_EXAMPLES=ON ..
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:
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:
$ make -j4
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:
$ sudo make install $ sudo ldconfig
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/
:
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:
$ cd ~/.virtualenvs/cv3/lib/python2.7/site-packages/ $ ln -s /usr/local/lib/python2.7/site-packages/cv2.so cv2.so
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:
$ sudo apt-get install python3-dev
Timings: 54s
Install pip
, ensuring that it is compatible with Python 3 (note that I am executing python3
rather than just python
):
$ wget https://bootstrap.pypa.io/get-pip.py $ sudo python3 get-pip.py
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
:
$ sudo pip3 install virtualenv 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:
# virtualenv and virtualenvwrapper export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3 export WORKON_HOME=$HOME/.virtualenvs source /usr/local/bin/virtualenvwrapper.sh
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:
$ source ~/.profile
Let’s create our cv
virtual environment where OpenCV will be compiled and accessed from:
$ mkvirtualenv cv
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:
$ workon cv
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:
$ pip 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:
$ sudo rm -rf ~/.cache/pip/ $ pip install numpy
At this point you should have a nice clean install of NumPy, like this:
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:
$ workon cv
After you are in the cv
virtual environment, we can setup our build:
$ cd ~/opencv $ mkdir build $ cd build $ cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_INSTALL_PREFIX=/usr/local \ -D INSTALL_C_EXAMPLES=ON \ -D INSTALL_PYTHON_EXAMPLES=ON \ -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \ -D BUILD_EXAMPLES=ON ..
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:
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:
$ make -j4
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:
$ sudo make install $ sudo ldconfig
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:
$ ls -l /usr/local/lib/python3.2/site-packages total 1416 -rw-r--r-- 1 root staff 1447637 Jun 22 18:26 cv2.cpython-32mu.so
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:
$ cd ~/.virtualenvs/cv/lib/python3.2/site-packages/ $ ln -s /usr/local/lib/python3.2/site-packages/cv2.cpython-32mu.so cv2.so
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):
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:
$ workon cv $ python >>> import cv2 >>> cv2.__version__ '3.0.0'
And sure enough, we can see OpenCV 3.0 with Python 3+ support has been installed on my Raspberry Pi:
What's next? We recommend PyImageSearch University.
86 total classes • 115+ hours of on-demand code walkthrough videos • Last updated: October 2024
★★★★★ 4.84 (128 Ratings) • 16,000+ Students Enrolled
I strongly believe that if you had the right teacher you could master computer vision and deep learning.
Do you think learning computer vision and deep learning has to be time-consuming, overwhelming, and complicated? Or has to involve complex mathematics and equations? Or requires a degree in computer science?
That’s not the case.
All you need to master computer vision and deep learning is for someone to explain things to you in simple, intuitive terms. And that’s exactly what I do. My mission is to change education and how complex Artificial Intelligence topics are taught.
If you're serious about learning computer vision, your next stop should be PyImageSearch University, the most comprehensive computer vision, deep learning, and OpenCV course online today. Here you’ll learn how to successfully and confidently apply computer vision to your work, research, and projects. Join me in computer vision mastery.
Inside PyImageSearch University you'll find:
- ✓ 86 courses on essential computer vision, deep learning, and OpenCV topics
- ✓ 86 Certificates of Completion
- ✓ 115+ hours of on-demand video
- ✓ Brand new courses released regularly, ensuring you can keep up with state-of-the-art techniques
- ✓ Pre-configured Jupyter Notebooks in Google Colab
- ✓ Run all code examples in your web browser — works on Windows, macOS, and Linux (no dev environment configuration required!)
- ✓ Access to centralized code repos for all 540+ tutorials on PyImageSearch
- ✓ Easy one-click downloads for code, datasets, pre-trained models, etc.
- ✓ Access on mobile, laptop, desktop, etc.
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!
Join the PyImageSearch Newsletter and Grab My FREE 17-page Resource Guide PDF
Enter your email address below to join the PyImageSearch Newsletter and download my FREE 17-page Resource Guide PDF on Computer Vision, OpenCV, and Deep Learning.
Jason
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
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 usedsmbus
before, but if I wanted to, I would just enter thecv
virtual environment and let pip install it:$ workon cv
$ pip install cffi
John Tran
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
Andrei
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
Jayman
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
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
thank you for your quick answer. I’ll try this.
Gilad
Hi Jayman,
Did you manage to solve this?
Chujiang
Hi Jayman
I meet this problem, too. Have you solved this problem? Could you please give me some advice?
Thank you very much
Jason
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
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.Talha
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
For the B+ model that is completely normal. From my experience, NumPy takes about 45 minutes to install on the B+.
Xiyu Ouyang
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
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.
Antonio
Hi Adrian,
Great installation guilde. Should I install any specific libraries in order to open a video stream with opencv?
Adrian Rosebrock
Hey Antonio — you should be able to open video files/access the webcam without an issue.
alexander isz
hello adrian…
very very nice tutorial for beginner like me….
i got ” bash: cmake :command not found ”
can you help me ? thanks!
Adrian Rosebrock
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
Shubham Mittal
Tnx, followed all the mentioned steps to successfully install opencv3
Adrian Rosebrock
Awesome, glad to hear it!
Stridera
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
Once you are inside the virtual environment, you use
python
andpip
as you normally would — no need to specifypython3
andpip-3.2
. When you performedpip3.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
...
Dustin
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
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.Alex Sobrino
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
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.skip
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
Nice! I’m so happy that the tutorial worked for you Skip! 😀
Adam Geitgey
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
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
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
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
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
Thanks for sharing Andrew!
Jacob
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.
Jeff
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
Hey Jeff, thanks for the suggestions. And yes, you can delete the
opencv
andopencv_contrib
directories now that the compile and install has finished.Mesut
Hello Adrian, How can I use openCV binaries to avoid the compilation level? it there a way?
Adrian Rosebrock
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.marco
hello, thanks for guides on how to install opencv on Raspberry, unfortunately, however, problems with the installation, especially because they are at the beginning and I do not know much, I arrived quite well to the “make-j4” then I could no longer continue because when I execute the MAKE command in response it tells me “no specified target and no found makefile, stop. can you help me? ache point six with disco image with opencv I would be grateful if I could have it.
Adrian Rosebrock
If your “make” command told you that no build files were found then your error happened in the “cmake” command. Go back to the “cmake” command and look for the error.
Thom
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
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
andPYTHON3_INCLUDE_DIR
will be different. Although if you are in thecv
virtual environment you should (ideally) not have to explicitly supply these options.Thom
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
The “library file” should be a
.so
file, for examplelibpython2.7.so
Krystian
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
It looks like the tests for the
xfeatures2d
module failed to build. You could try compiling without the tests (not recommended) or disabling thebuild, recreate it, add the following switch to your CMake command:
-D WITH_xfeatures2d=OFF
, re-run cmake, and then restart the compile.Fran
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
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.
gomelkiev
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
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
i finished build/ install but directory /usr/local/lib/python2.7/site-packages/ is empty
thanks
Adrian Rosebrock
I would check the
lib
directory of yourbuild
and see if your.so
bindings are in there. If so, then the compile was successful and you can move them in to thesite-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.Morgatte
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
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.Andrea
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
Hey Andrea — you need to access the virtual environment before you execute your script:
RedDevil
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
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 bothvirtualenv
andvirtualenvwrapper
are still there. If not, add them back in and reload it and everything should work fine.Piuser
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
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.
Soren
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
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
andvirtualenvwrapper
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
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.
Izwan
i try to use xfeatures2d…but appear no ‘module’…
how i can solve this.
Already follow exactly same procedure. Hurm…
Adrian Rosebrock
If the
xfeatures2d
module was not found, then it looks like theopencv_contrib
directory was not included when compiling OpenCV. Make sure your path toopencv_contrib
is correct.CleoQc
Worked perfectly!!! Thanks for all the hard work!
Adrian Rosebrock
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.
Alex
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
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) yourcv
virtual environment has been created using themkvirtualenv
command and (2) you can access it via theworkon
command.From there, the last step is to make sure your sym-link to the
cv2.so
file is valid.eric
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
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.patrick poirier
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
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:
Dennis
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
Hey Dennis: (1) Yes, after you have compiled and installed OpenCV it is safe to delete the
opencv
andopencv_contrib
directories. 2. If you want to use the shell, just use theworkon cv3
command:Grace
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
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.
Marco
Hi Adrian
Thanks for the great tutorial
Unfortunately OpenCV does not compile (make -j4) without errors 🙁
Adrian Rosebrock
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
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
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
Parviz
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
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.
@Cleo_qc
Tiny typo: ~./profile
Adrian Rosebrock
Thanks! I’ve updated it now 🙂
fBoesche
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
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.Michael
Thanks Adrian. Great tutorial.
Adrian Rosebrock
Thanks Michael! 🙂
Gabriel
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
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.
Peter
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
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.Tony
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
Hey Tony — for OpenCV 3 install instructions, please see this post.
Andrej
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
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.
Timco
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??? 🙁
Adrian Rosebrock
Hey Timco — please see my latest Raspberry Pi install tutorial where I discuss how to fix common errors such as the one you are having.
Keshav
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
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 theworkon
command allows you to access your virtual environments. It is an extra step to remember, but the benefits are well worth it.C++Guy
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?
Neto
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
Hmmm. I’ve ran
rpi-update
many times and it’s never once failed for me. Perhaps I’ve just been lucky?Nichten
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
This should just be a warning, not an error message — you should be able to continue without a problem.
RSS
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
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.Hailey
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
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
Simon
Thank you so much! This is absolutely the best tutorial on the internet and all your tutorials are top notch.
Adrian Rosebrock
Thanks Simon! 😀
Nick
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
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
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
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
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
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
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.
BOBAN RAPHAEL
Can u tell me how to uninstall OpenCV and python? i had followed this page for installation instructions
Adrian Rosebrock
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.Mootaz
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
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
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
Thanks for sharing. And yes, whenever possible, try to use X11 if you are working via a network. It’s much less resource intensive.
Anuj
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
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.
mai
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
You should be using this post to install OpenCV on Rasbpian Jessie.
pritam raskar
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
Please see the “Troubleshooting” section of this post. It’s likely that your
.profile
file has not been properly updated.Andrea
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
Congrats on resolving the issue Andrea!
Andrea
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
If you’re SSH’ing into your Pi, you need to enable X11 forwarding:
$ ssh -X pi@your_ip_address
Rishabh
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
All you need to do is re-download the original
opencv
andopencv_contrib
repos. Then, re-run CMake and make sure that the extra modules were properly recognized. From there, just runmake
andmake install
— everything will be properly installed from there. No need to uninstall OpenCV!Rishabh Shukla
Thanks a lot Adrian
I will try doing that and let you know
Adrian Rosebrock
Best of luck Rishabh!
Andrea
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
If you already have OpenCV setup and installed on your system, you can delete the
opencv
andopencv_contrib
directories — they are only required for the build.Hermawan
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
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.
Ugur
Problenm for libtiff4 (after upgrade jessie ) i found solution from ubuntu pages:
sudo apt-get install libtiff5-dev libtiff5 libjbig-dev
Adrian Rosebrock
Thanks for sharing Ugur! I actually have an entire tutorial dedicated to installing OpenCV on Raspian Jessie here.
Abdullah
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
You need to
source ~/.profile
each time you open up a new terminal/reboot. From there, theworkon
command will work.Abdullah
Thank you very much Adrian. Have a good days.
Nasaa
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
Please see the “Troubleshooting” section of this post. I details reasons why the
workon
command may not work. Even if you have ransource ~/.profile
, you may not have updated your~/.profile
correctly.Neil De Guzman
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
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.
David
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
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.
David
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
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.
Nickolas
What if I don’t want virualenv ?
Adrian Rosebrock
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”.
darshan
hello..
can we install opencv 3.0+ above given procedure for raspbian wheezy ?
Adrian Rosebrock
Yes, the tutorial you are reading now covers how to install OpenCV 3 with Python 2.7 and Python 3 bindings on Raspbian Wheezy.
Emilio Cremades
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
Hey Emilio — make sure you
source
your~/.profile
before trying to execute theworkon
command. This should take care of the error:Simon P
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
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.Nano
What do you mean by “CMake finished with errors means??”
Adrian Rosebrock
I quite literally mean that if you check the output of the “cmake” command, in particular the last few lines of the output of “cmake”, the output will mention that the command could not be fully executed, your build files could not be generated, and there was a problem with the configuration. Cmake will then provide you with the location of the log files you can check for the errors.
Simon P
Thank you, install complete and working 🙂
Adrian Rosebrock
Congrats on getting OpenCV installed Simon!
edroor
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
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.
Arasch
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
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.
Arasch
Thanks Adrian. I was following the wrong post from you. This one solved all issues:
https://pyimagesearch.com/2015/07/20/install-opencv-3-0-and-python-3-4-on-ubuntu/
Adrian Rosebrock
Congrats on resolving the issue Arasch!
raju kunde
Great tutorial.
Ran out of disk space while running make. So, I removed build folder and also uninstalled mathematica.
Adrian Rosebrock
Thanks for sharing Raju!
Zack
Hi Adrian.How to install opencv2.4 and opencv3.0 on the same device?
Adrian Rosebrock
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 thebuild
directories for each OpenCV version. Then sym-link in yourcv2.so
file for each virtual environment.Rob
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
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.Anje
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
To compile OpenCV, I would suggest having at least 1-2GB of space free on your SD card.
Anje
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
Please see the “Troubleshooting” section of this post.
Kristen
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
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 thecv2.so
file into yoursite-packages
directory for the virtual environment.Kenneth
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
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.
Sasikumar M
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
Which IDE are you using? Make sure you set the correct Python interpreter for the project (i.e., the Python virtual environment).
Rick
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
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!
sharath
while compiling the opencv file the core heats up really bad, is it ok or do i need to worry about it?
Adrian Rosebrock
It’s totally fine. Keep the Pi in a well ventilated area.
Lidya
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
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
Hi Adrian,
You were right, I didn’t update the ~/.profile file correctly. Thank you!
Adrian Rosebrock
Happy I could help Lidya — and congrats on resolving the issue 🙂
Mona Jalal
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
prashanth
hello, while compiling the opencv using “make -j4” raspberry pi keeps on restarting. Please tell how to resolve it….
Adrian Rosebrock
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
.numreen
can i Know where exactly to write export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python2.7
Adrian Rosebrock
Please see my reply to “Anuj” above.
rohit
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
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:
joseph adamu
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
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
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
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.
Stein
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
Congrats on getting OpenCV installed Stein, nice job!
wilson
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
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.
JOSEPH
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’
Adrian Rosebrock
Please see the “Troubleshooting” section of this guide for reasons why the OpenCV bindings may fail to import.
Parker
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
Correct, you would create two separate build directories, one for Python 2.7 and another for Python 3. Then compile OpenCV independently for each.
Rahul
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
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
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
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.
James
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
I’m also trying to download the 3.3.0 version of open cv
Adrian Rosebrock
What is the exact error you are getting? Without knowing myself and others cannot point you in the right direction.
bill lewey
Hi Adrian, I just attempted install with all new today noobs, python, opencv with 2.7 bindings. it took me about 3 days because other stuff. I failed to catch the INSTALL_C_EXAMPLES=OFF and then the make -j4 failed about about 13% through, unfortunately I can see the lines of text since i tried several restarts in the middle of your instructions and I dont know how to capture a log of errors for bug reporting. So, finally my question. Did you manage to make an install package that is more turn key for mechanical engineers? if you didnt then I will just start from the very beginning again. cheers! I appreciate these instructions, they are great! – bill
Adrian Rosebrock
Hey Bill — I’m sorry to hear about the issues getting OpenCV installed on the Pi. It can be a bit of a pain.
To address your question:
Yes, I have a turn key solution that has OpenCV pre-configured and pre-installed inside the Quickstart Bundle and Hardcopy Bundle of my book, Practical Python and OpenCV. These bundles include a pre-configured Raspbian .img file. Just download the .img file, flash it to your Pi, and boot. Be sure to give it a look, it would save you a bunch of time and hassle.
mohini nayak
Can we follow above tutorial to install Opencv in Raspberry pi 1 model B+ with Raspbian stretch os?
Since,while installing i’m getting errors.
Python 3 is not configured yet.
If I check the python3 version in command line I get version as Python3.5.2.
Adrian Rosebrock
Sorry, I have not tried with the Pi v1. Good luck with it!
Karl
I am new to the Raspberry Pi and I did buy the pre-installed bundle from you. I wasn’t finding open cv on the image so I downloaded and compiled it. In order to compile it I had to increase the swap space on the Pi. I think I have the Pi 3, B model. I thought this would be useful knowledge for others. You can search for the dphys-swapfile command to find out more.
Adrian Rosebrock
Hey Karl — OpenCV is pre-installed on the Raspbian .img file.. I think you’re forgetting the “workon” command. Make sure you refer to the .img file documentation.