A few weeks ago Raspbian Jessie was released, bringing in a ton of new, great features.
However, the update to Jessie also broke the previous OpenCV + Python install instructions for Raspbian Wheezy:
- Install OpenCV 2.4 with Python 2.7 bindings on Raspbian Wheezy.
- Install OpenCV 3.0 with Python 2.7/Python 3+ bindings on Raspbian Wheezy.
Since PyImageSearch has become the online destination for learning computer vision + OpenCV on the Raspberry Pi, I decided to write a new tutorial on installing OpenCV 3 with Python bindings on Raspbian Jessie.
As an additional bonus, I’ve also included a video tutorial that you can use to follow along with me as I install OpenCV 3 on my own Raspberry Pi 2 running Raspbian Jessie.
This video tutorial should help address the most common questions, doubts, and pitfalls that arise when installing OpenCV + Python bindings on the Raspberry Pi for the first time.
Assumptions
For this tutorial I am going to assume that you already own a Raspberry Pi 2 with Raspbian Jessie installed. Other than that, you should either have (1) physical access to your Pi 2 and can open up a terminal or (2) remote access where you can SSH in. I’ll be doing this tutorial via SSH, but as long as you have access to a terminal, it really doesn’t matter.
The quick start video tutorial
Before we get this tutorial underway, let me ask you two quick questions:
- Is this your first time installing OpenCV?
- Are you just getting started learning Linux and how to use the command line?
If you answered yes to either of these questions, I highly suggest that you watch the video below and follow along with me as a guide you step-by-step on how to install OpenCV 3 with Python bindings on your Raspberry Pi 2 running Raspbian Jessie:
Otherwise, if you feel comfortable using the command line or if you have previous experience using the command line, feel free to follow the tutorial below.
Installing OpenCV 3 on Raspbian Jessie
Installing OpenCV 3 is a multi-step (and even time consuming) process requiring you to install many dependencies and pre-requisites. The remainder of this tutorial will guide you step-by-step through the process.
To make the installation process easier, I’ve included timings for each step (when appropriate) so you know when to stick by your terminal, grab a cup of coffee, or go for a nice long walk.
If you’re an experienced Linux user or have already installed OpenCV on a Raspberry Pi (or another other system) before, you can likely just follow the steps outlined below.
However, if this is your first time installing OpenCV (or you don’t have much prior exposure to the Linux operating systems and the command line), I highly recommend that you watch the video above and follow along with me as I show you how to install OpenCV 3 on your Rasberry Pi running Raspbian Jessie.
That said, let’s get started installing OpenCV 3.
Step #1: Install dependencies
The first thing we should do is update and upgrade any existing packages, followed by updating the Raspberry Pi firmware.
$ sudo apt-get update $ sudo apt-get upgrade $ sudo rpi-update
Timing: 3m 33s
You’ll need to reboot your Raspberry Pi after the firmware update:
$ sudo reboot
Now we need to install a few developer tools:
$ sudo apt-get install build-essential git cmake pkg-config
Timing: 51s
Now we can move on to installing image I/O packages which allow us to load image file formats such as JPEG, PNG, TIFF, etc.:
$ sudo apt-get install libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev
Timing: 42s
Just like we need image I/O packages, we also need video I/O packages. These packages allow us to load various video file formats as well as work with video streams:
$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev $ sudo apt-get install libxvidcore-dev libx264-dev
Timing: 58s
We need to install the GTK development library so we can compile the highgui
sub-module of OpenCV, which allows us to display images to our screen and build simple GUI interfaces:
$ sudo apt-get install libgtk2.0-dev
Timing: 2m 48s
Various operations inside of OpenCV (such as matrix operations) can be optimized using added dependencies:
$ sudo apt-get install libatlas-base-dev gfortran
Timing: 50s
Lastly, we’ll need to install the Python 2.7 and Python 3 header files so we can compile our OpenCV + Python bindings:
$ sudo apt-get install python2.7-dev python3-dev
Step #2: Grab the OpenCV source code
At this point we have all of our prerequisites installed, so let’s grab the 3.0.0
version of OpenCV from the OpenCV repository. (Note: As future versions of OpenCV are released just replace the 3.0.0
with the most recent version number):
$ cd ~ $ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.0.0.zip $ unzip opencv.zip
Timing: 2m 29s
For the full install of OpenCV 3 (which includes features such as SIFT and SURF), be sure to grab the opencv_contrib repo as well. (Note: Make sure your opencv
and opencv_contrib
versions match up, otherwise you will run into errors during compilation. For example, if I download v3.0.0 of opencv
, then I’ll want to download v3.0.0 of opencv_contrib
as well):
$ wget -O opencv_contrib.zip https://github.com/Itseez/opencv_contrib/archive/3.0.0.zip $ unzip opencv_contrib.zip
Timing: 1m 54s
Step #3: Setup Python
The first step in setting up Python for our OpenCV compile is to install pip
, a Python package manager:
$ wget https://bootstrap.pypa.io/get-pip.py $ sudo python get-pip.py
Timing: 26s
I’ve discussed both virtualenv and virtualenvwrapper many times on the PyImageSearch blog before, especially within these installation tutorials. Installing these packages is certainly not a requirement to get OpenCV and Python up and running on your Raspberry Pi, but I highly recommend that you install them!
Using virtualenv
and virtualenvwrapper
allows you to create isolated Python environments, separate from your system install of Python. This means that you can run multiple versions of Python, with different versions of packages installed into each virtual environment — this solves the “Project A depends on version 1.x, but Project B needs 4.x” problem that often arises in software engineering.
Again, it’s standard practice in the Python community to use virtual environments, so I highly suggest that you start using them if you are not already:
$ sudo pip install virtualenv virtualenvwrapper $ sudo rm -rf ~/.cache/pip
Timing: 17s
After virtualenv
and virtualenvwrapper
have been installed, we need to update our ~/.profile
file and insert the following lines at the bottom of the file:
# virtualenv and virtualenvwrapper export WORKON_HOME=$HOME/.virtualenvs source /usr/local/bin/virtualenvwrapper.sh
You can use your favorite editor to edit this file, such as vim
, emacs
, nano
, or any other graphical editor included in the Raspbian Jessie distribution. Again, all you need to do is open the file located at /home/pi/.profile
and insert the lines above at the bottom of the file.
Now that your ~/.profile
has been updated, you need to reload it so the changes can take affect. To force a reload of the ~/.profile
file you can (1) logout and log back in, (2) close your terminal and open up a new one, or (3) just use the source
command:
$ source ~/.profile
Note: You’ll likely need to run the source ~/.profile
command each time you open up a new terminal to ensure your environment has been setup correctly.
The next step is to create our Python virtual environment where we’ll be doing our computer vision work:
$ mkvirtualenv cv
The above command will create a virtual environment named cv
using Python 2.7.
If you want Python 3, run this command instead:
$ mkvirtualenv cv -p python3
Again, it’s important to note that the cv
Python environment is entirely independent from the default version of Python included in the download of Raspbian Jesse.
If you ever reboot your system, logout and log back in, or open up a new terminal, you’ll need to use the workon
command to re-access the cv
virtual environment, otherwise you’ll be using the system version of Python instead:
$ source ~/.profile $ workon cv
You can ensure you are in the cv
virtual environment by examining your command line. If you see the text “(cv)” preceding your prompt, then you are in the cv
virtual environment:
Otherwise, you are not in the cv
virtual environment:
If this is the case, you need to run the source
and workon
commands above.
Assuming that you are in the cv
virtual environment, we can install NumPy, an important dependency when compiling the Python bindings for OpenCV. You might want to grab a cup of coffee or go for a walk while NumPy downloads and installs:
$ pip install numpy
Timing: 16m 10s
Step #4: Compile and install OpenCV
At this point, we are ready to compile OpenCV.
First, make sure you are in the cv
virtual environment:
$ workon cv
Followed by setting up the build:
$ cd ~/opencv-3.0.0/ $ 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-3.0.0/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.
Before you move on to the compilation step, make sure you examine the output of CMake!
Scroll down the section titled Python 2
and Python 3
.
If you’re compiling OpenCV 3 for Python 2.7, then you’ll want to make sure the Python 2
section looks like this (highlighted) in red:
Notice how both the Interpreter
and numpy
variables point to the cv
virtual environment.
Similarly, if you’re compiling OpenCV for Python 3, then make sure the Python 3
section looks like this:
Again, both the Interpreter
and numpy
variables are pointing to our cv
virtual environment.
In either case, if you do not see the cv
virtual environment for these variables MAKE SURE YOU ARE IN THE cv
VIRTUAL ENVIRONMENT PRIOR TO RUNNING CMAKE!
Now that our build is all setup, we can compile OpenCV:
$ make -j4
Timing: 1h 35m
The -j4
switch stands for the number of cores to use when compiling OpenCV. Since we are using a Raspberry Pi 2, we’ll leverage all four cores of the processor for a faster compilation.
However, if your make
command errors out, I would suggest starting the compilation over again and only using one core:
$ make clean $ make
Using only one core will take much longer to compile, but can help reduce any type of strange race dependency condition errors when compiling.
Assuming OpenCV compiled without error, all we need to do is install it on our system:
$ sudo make install $ sudo ldconfig
Step #5: Finishing the install
We’re almost there! Just a few more things and we’ll be 100% done.
For Python 2.7:
Provided you finished Step #4 without error, OpenCV should now be installed in /usr/local/lib/python2.7/site-packages
:
$ ls -l /usr/local/lib/python2.7/site-packages/ total 1636 -rw-r--r-- 1 root staff 1675144 Oct 17 15:25 cv2.so
Note: In some instances OpenCV can be installed in /usr/local/lib/python2.7/dist-packages
(note the dist-packages
rather than site-packages
). If you do not find the cv2.so
bindings in site-packages
, be sure to check dist-packages
as well.
The last step here is to sym-link the OpenCV bindings into the cv
virtual environment:
$ cd ~/.virtualenvs/cv/lib/python2.7/site-packages/ $ ln -s /usr/local/lib/python2.7/site-packages/cv2.so cv2.so
For Python 3:
OpenCV should now be installed in /usr/local/lib/python3.4/site-packages
:
$ ls /usr/local/lib/python3.4/site-packages/ cv2.cpython-34m.so
For some reason, unbeknownst to me, when compiling the Python 3 bindings the output .so
file is named cv2.cpython-34m.so
rather than cv2.so
.
Luckily, this is an easy fix. All we need to do is rename the file:
$ cd /usr/local/lib/python3.4/site-packages/ $ sudo mv cv2.cpython-34m.so cv2.so
Followed by sym-linking OpenCV into our cv
virtual environment:
$ cd ~/.virtualenvs/cv/lib/python3.4/site-packages/ $ ln -s /usr/local/lib/python3.4/site-packages/cv2.so cv2.so
Step #6: Verifying your OpenCV 3 install
At this point, OpenCV 3 should be installed on your Raspberry Pi running Raspbian Jessie!
But before we wrap this tutorial up, let’s verify that your OpenCV installation is working by accessing the cv
virtual environment and importing cv2
, the OpenCV + Python bindings:
$ workon cv $ python >>> import cv2 >>> cv2.__version__ '3.0.0'
You can see a screenshot of my terminal below, indicating that OpenCV 3 has been successfully installed:
Troubleshooting
Q. When I try to use the mkvirtualenv
or workon
commands, I get an error saying “command not found”.
A. Go back to Step #3 and ensure your ~/.profile
file has been updated properly. Once you have updated it, be sure to run source ~/.profile
to reload it.
Q. After I reboot/logout/open up a new terminal, I cannot run the mkvirtualenv
or workon
commands.
A. Anytime you reboot your system, logout and log back in, or open up a new terminal, you should run source ~/.profile
to make sure you have access to your Python virtual environments.
Q. When I open up a Python shell and type import cv2
, I get the dreaded ImportError: No module named cv2
error.
A. The reason for this error is hard to diagnose, mainly because there are multiple issues that could be causing this problem. For starters, make sure you are in the cv
virtual environment using workon cv
. If the workon
command is giving you problems, then see the previous questions in this section. From there, you’ll want to investigate the site-packages
directory of your cv
virtual environment located in ~/.virtualenvs/cv/lib/python2.7/site-packages/
or ~/.virtualenvs/cv/lib/python3.4/site-packages/
, respectively. Make sure that the sym-link path to the cv2.so
file is valid. If you do not know how to do this, please consult the video tutorial at the top of this post.
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 lesson we learned how to install OpenCV 3 with Python 2.7 and Python 3 bindings on your Raspberry Pi 2 running Raspbian Jessie. I provided timings for each step so ensure you can plan your install accordingly.
It’s also worth mentioning that I provide OpenCV v2.4 and v3 install instructions for Raspbian Wheezy in the following posts:
- Install OpenCV 2.4 with Python 2.7 bindings on Raspbian Wheezy.
- Install OpenCV 3.0 with Python 2.7/Python 3+ bindings on Raspbian Wheezy.
If you run into any issues during the installation process, please see the Troubleshooting section above. Additionally, I would suggest watching the video tutorial at the top of this post to aid you in the setup process.
Before you go…
I tend to cover a lot of great computer vision projects using OpenCV 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.
Anastasios Selalmazidis
Great guide Adrian. I was looking forward for this. Worked like a charm for python2.7.
I am going to install it now for python3.
Thank you !!!
Adrian Rosebrock
Awesome, I’m glad it worked for you Anastasios! 🙂
Aldi
Hello Adrian thanks for your tutorial it was great. I’ve just followed trough your guide and I am able to do the example from your site but How can I run the example from the opencv ?
Thank you for your attention
Adrian Rosebrock
Hey Aldi, I’m not sure what you mean by “run the example from OpenCV”? Most tutorials on this blog post assume you are executing a Python script via command line argument. I would suggest going through a more recent post, like this on on pedestrian detection to see how Python files are created and executed. Alternatively, if you’re just getting started using Python and OpenCV, Practical Python and OpenCV would be a good start.
Supra
It worked for Model B+ w/out using virtualenvwrapper by using python 3 merely.
Adrian Rosebrock
As I mentioned in the post, using virtual environments is entirely optional. But I do highly recommend it for Python development.
nathan
Im rather new to python so bear with me
I can get open cv to run directly from the terminal but I can’t get it to work with the python shell. I’m assuming this is because the python shell has no idea that open cv is in a different “virtual environment”
if I type in “import cv2” into the python shell it says
Traceback (most recent call last):
File “”, line 1, in
import cv2
ImportError: No module named ‘cv2’
Adrian Rosebrock
By Python shell do you mean the GUI version of IDLE? If so, then you are correct — the GUI version of IDLE does not work with virtual environments. I would suggest using Jupyter Notebooks instead.
Colin Rutter
Adrian, i followed this tutorial to a successful install also only to realise when readibg this question that i would not be able to access openCV using idle ! (perhaps a warning in the guide itself would be handy) How can i ‘re-install’ without virtualenvs to allow opencv use with idle ?
regards
Adrian Rosebrock
In general I wouldn’t recommend using IDLE or your system Python install. Use Jupyter Notebooks instead.
Brian
When I try to unzip OpenCV it gets about halfway through and then I get a write error, asking if my disk is full. This is a brand new 16gig class 10 microSD with Jessie installed. It’ can’t possibly be full already. I tried the wget again thinking the download was corrupt but it still gives me a write error. Any ideas?
Ah-ha! I was able to successfully unzip opencv.
A critical step that you might want to mention before running the “unzip opencv.zip” command:
Navigate to the Raspberry Pi Configuration Tool and select EXPAND FILESYSTEM
Hope that helps someone!
Adrian Rosebrock
Thanks for the tip Brian! I (incorrectly) made the assumption that everyone had already expanded their filesystems fully.
Matt
Hi,
My Raspberry Pi 2 won’t allow me to expand the filesystem like Brian mentioned. I used NOOBS to install Raspbian Jessie and am using an 8GB micro SD but only 1GB is available once the operating system is installed.
Any help would be much appreciated
Matt
Adrian Rosebrock
Normally to expand the file system all you need to do is run
raspi-config
, select the first option to “Expand filesystem”, and reboot. That should expand your 8GB card. Make sure you reboot after running the command. If you’re still not getting your file system expanded, either (1) your file system has already been expanded or (2) something very odd is going on and you should consult the Raspberry Pi documentation.Nathan
Hi Adrian,
I have also run into this.
The compiled code seems to up take more than 4GB disk space (that’s why people are having problems), but the installed libs take <100M. If we don't have enough space on the SD card, would using a USB stick or an external hard disk for the build directory work. Then after installing OpenCV, delete the build directory?
Adrian Rosebrock
Yep, that would absolutely work!
Samuel
I also have a problem for not having not enough space on the SD card, how can i use a USB stick to accomplish compiling? i guess it’s not as easy as simply creating the build folder in the USB stick right…?
Please help me Q.
Adrian Rosebrock
Actually, it is 🙂 Just download the OpenCV .zip files to the USB stick, create your
build
directory there, and let it compile.Roger
Howdy Matt
I ran into the same issue you expereined; not being able to expand the memory on SD card running NOOBS version for rsapi.
So, I decided to directly dd the rsapian os without all the oother os’es onto SD Card. I suggest this to you, too.
Just download the raspian os and copy via dd command in Linux CLI or with an additional software under windows; sorry I am not aware of a good one since I am not working with windows.
Google may help here.
Regards,
Roger
Matt
Hi Roger,
Yes I have done the same and it works now.
Thanks
Matt
Kevin
Are you sure you’re using the entire 16GB? Have you expanded the root partition, or are you running Jessie from the installer image? See https://coderwall.com/p/mhj8jw/raspbian-how-to-resize-the-root-partition-to-fill-sd-card or others on how to get the full 16GB
Sigi
Hi Adrian,
thank you for this great tutorial – it worked like a charm rpi2 – jessi – python3 – opencv3
I have a script for circle detection that runs well on rpi2 – wheezy – python2 – opencv2.
but with opencv3 it crashes at the line:
” wp = cv2.HoughCircles(blurred,cv2.cv.CV_HOUGH_GRADIENT,1,75,param1=50,param2=12,minRadius=7,maxRadius=9)) ”
with this error message:
” AttributeError: ‘module’ object has no attribute ‘cv’ ”
What can I do?
Thanks in advance
Sigi
Adrian Rosebrock
For OpenCV 3, you’ll need to change
cv2.cv.CV_HOUGH_GRADIENT
to:cv2.HOUGH_GRADIENT
.Sigi Weichenrieder
Hi Adrian,
that worked :-)) Thank you!!!!!!!!!!!!!!!!!!!!
Sigi
Adrian Rosebrock
Awesome, I’m glad it worked Sigi!
kk
Hi, I tried https://pyimagesearch.com/2014/07/14/3-ways-compare-histograms-using-opencv-python/ with rpi2 – jessi – python2.7 – opencv3.0 installation as guided in this post.
(“Correlation”, cv2.cv.CV_COMP_CORREL)
throws error message:
AttributeError: ‘module’ object has no attribute ‘cv’
Is similar change required? But,
(“Correlation”, cv2.CV_COMP_CORREL),
throws error either:
AttributeError: ‘module’ object has no attribute ‘CV_COMP_CORREL’
Adrian Rosebrock
Try using
cv2.HISTCMP_CORREL
.Nichten
Hello Adrian,
I have successfully installed OpenCV 3 with Python 3.4 on Raspbian Jessie. thank, you for your great work :).
But I have some problem with package python-serial. How to install it in virtualenv with python 3? I type command workon cv and I’m trying to install it, but the OS return information that it is already installed. But I can’t import serial in python3. I can do it with python2. What I’m doing wrong?
~Nichten
Adrian Rosebrock
Congrats on getting OpenCV 3 installed, that’s great! I personally have not used python-serial before so I am unfamiliar with it. I believe in Wheezy that you had to install and run it as root, but with Jessie you can install it and run it as as normal user — is that correct?
It sounds like you may have installed python-serial via
apt-get
which is why it’s not working for you. If you do:It should work for you.
Deepak
Hello Adrian,
I am also facing the same issue with serial module. I have tried pip install pyserial. Even then I am not able to import the serial module to cv virtual env. Outside the virtual env, serial module is working fine. Please advice.
Adrian Rosebrock
Since I am not a pyserial/GPIO user, I’m honestly not sure about the problem. I’ve added this to my queue as a potential new blog post to help resolve the issue.
Yvder
Please, have someone got the issue fixed? I am facing the same issue with pyserial in CV virtual env.
Thanks.
Eddie Mitchell
Beautiful walk through. worked like a charm and im super excited to dive into computer vision. getting your book[s] for christmas! keep up the awesome work Adrian!
Adrian Rosebrock
Thanks for the kind words Eddie! 🙂 I’m glad the tutorial worked for you.
kk
Hello Adrian,
Thanks for the great work. With my installation of OpenCV 3.0 with Python 2.7 on Raspbian Jessie, I’m running your blog codes one by one. I’m really enjoying it.
But I have a problem with installing “scipy”. “pip install numpy” works, but “pip install scipy” is failed. I wonder if I’m the only one who is suffering from the problem.
It would be greatly appreciated if you could help me. Thanks in advance.
Adrian Rosebrock
Did you get an error message when installing Scipy? And which Raspberry Pi model are you using?
armin
Hello
Thanks for your tutorials, according to these instructions I’m trying to build and compile opencv3 on RPI2 running ubuntu mate 15.10, but i’m facing this error in compilation step:
make file:136 recipe for target ‘all’ failed
error 2
Adrian Rosebrock
Hey Armin — this tutorial is dedicated to installing OpenCV on Raspbian Jessie. Theoretically the same steps can be applied to Ubuntu Mate, but I have not personally tried them on Ubuntu Mate before. If you’re getting an error when running the
make
command, more times than not it’s due to a missing dependency. Make sure you have ran all of the previousapt-get
commands to pull in the required prerequisites.Marijn
I ran into a similar error when ‘making’ with make -j4 on an Pi B+. I then did ‘make clean’ followed by the normal ‘make’ command. I took another 10+ hours, but finished without errors after that. And was able to finish the guide.
Adrian Rosebrock
If you’re using a B+, then definitely only use
make
instead ofmake -j4
. The B+ does not have four cores like the Pi 2 does so it makes running into dependency issues during compile a lot more likely.Nico
Hello Adrian
Thanks for your great walk throughs and tutorials. I successfully installed OpenCV3 and Python3 on my Pi2 with camera module and step 6 of this post returns the expected results.
When I tried to run the basic motion detection script from your article (https://pyimagesearch.com/2015/05/25/basic-motion-detection-and-tracking-with-python-and-opencv/) nothing happend. I created the script, opened command line, changed to the virtualenv cv and tried to run the script with this command: python basic_motion.py After hitting execute nothing happens for 2 seconds and then a new command line shows up waiting for input. No windows appear to show me the live camera view.
The 2 seconds gap is probably the time it takes to import OpenCV. I tried to run the command as sudo, I have given the script different permissions and I have changed some code. Sadly nothing helped to solve my problem. Commands as raspistill work perfectly nice so there can_t be any camera issues.
FYI I am working with the Pi’s GUI and not via SSH.
I would greatly appreciate your help. Thanks in advance.
Adrian Rosebrock
The basic motion detection script uses the
cv2.VideoCapture
function which can require additional drivers for the Pi. I recommend using this post for motion detection since it is 100% out-of-the-box workable with the Pi and the Pi camera module.Nico
Thanks Adrian the home surveillance tutorial works like a charm!
Adrian Rosebrock
Awesome, I’m glad to hear it Nico! 🙂
Gary
Help! I just upgraded to Jessie, and something broke on the previously stable virtual environment I setup using your instructions for Python 2.7, Open CV 2.X and Wheezy.
The upgrade to Jessie seemed to go well, but I am now seeing something broken with the virtual environment. Symptom:
1. If I try and execute any python in the virtual environment I had previously, python import fails. For instance, trying to import diatomite is failing.
2. If I go into Python, and manually import datetime, it fails.
3. If I am NOT in the virtual environment, and go into Python, I can import datetime just fine.
How can I repair the virtual environment here? Do I have to go through all of the steps above, or are their shortcuts that are faster and simpler?
Thanks Adrian!!
Adrian Rosebrock
Hey Gary — just to clarify, did you upgrade from Wheezy to Jessie? Or did you do a fresh install of Jessie? I’m assuming the former. Unfortunately, I have never tried upgrading before, only installing on a fresh OS. Hopefully another PyImageSearcher reader can help you out with this one!
Gary
I upgraded. I followed all steps here https://www.raspberrypi.org/forums/viewtopic.php?f=66&t=121880&sid=e513ef7fd4458647baf1abc6ac1ae30e which seems to be what most folks are pointing to for the best upgrade path. The upgrade seemed to go well.
One other clue for any reader….. if I load the virtual environment, and then perform step 6 above to verify all is well, I get an error from Python on the import cv2 statement:
“ImportError: numpy.core.multiarray failed to import”
Odd. If I go to ~/.virtualenvs/cv/lib/python2.7/site-packages, numpy is still there.
Makes me wonder if the bindings / sym-link is somehow trashed?
hoping to avoid having to reinstall everything above. If anyone has any experience on upgrading a previously stable Wheezy virtual environment to Jessie, please let me know.
Thanks
One more thing, if I go to check the links setup in Step 5 they are still valid and showing appropriately in the virtualenvs directory as well as when using the readlink command. Hmmmmmmm…………
Adrian Rosebrock
As you suggested, it sounds like your sym-links have been destroyed. I can’t speak for all the other packages on your system, but for OpenCV you should be able to re-create them using the
ln
command like we did in this tutorial. At that point you should have OpenCV back.I hate to say it, but you’ll likely need to do a re-install. I’ve ran into issues where my sym-links where destroyed (on OSX, not Raspbian) and every since then I never an “upgrade” to the latest OS. If I want to upgrade, I have to commit fully and re-install from scratch. It takes awhile and it’s a lot of effort, but it’s the best guarantee that you’re development environment will be 100% setup correctly.
Gary Lee
A hint to others…. I reinstalled numpy and it seemed to get everything back in order. Weird. I also (just to be safe) decided to start fresh with a new install of everything. Thanks for a great tutorial for getting this done. If others decide to follow the upgrade path, reinstalling numpy may be the solution.
Rob Jones
Adrian – This all worked *great* and I installed picamera[array] in the cv virtual env – I am able to read image frames from the camera, process them and output them
I’m using python 2.7
Now I want to bring RPi.GPIO into the mix … that works fine by itself outside the cv virtualenv as Jessie includes RPi.GPIO v0.6.0a3 by default – this gives you access to the GPIO pins without having to be root.
But the cv virtualenv does not see this… and the latest version of RPi.GPIO available by pip is 0.5.11 which won’t cut it…
How can I get v0.6.0a3 installed in the virtualenv ?
Compile from source inside that env ?
I’m not totally on top of pip / virtualenvs so I’m reluctant to just try it
I’m hoping that you have already dealt with this
thanks !
–Rob
I figured out the answer to my own question :^)
You just manually copy over the system RPi package files into the virtual environment like this:
$ cp -r /usr/lib/python2.7/dist-packages/RPi ~/.virtualenvs/cv/lib/python2.7/site-packages/
Adrian Rosebrock
Hey Rob — for future reference you can use
pip
and install it into your virtual environment. This is the preferred method rather than copying files directly:$ pip install rpi.gpio
Philip
Hi Adrian,
Thank you for your excellent tutorial. Everything worked perfectly except for a few errors installing numpy. Uninstalling and reinstalling fixed that. I also ran out of space building OpenCV as I was using Noobs. Formatting the SD card and just using the Jessie image file freed up enough space for the install.
I’m just wondering if you have had any experience with drivers for webcams on the Pi? When I use any of my webcams and try to get/set attributes such as CV2.CAP_PROP_FPS, I get the following error:
VIDEOIO ERROR: V4L: Property (16) not supported by device
I’ve got the libv4l library installed and the WITH_LIBV4L boolean is set to ON in the opencv build configuration.
I’ve googled but I haven’t come up with anything so far. Weird! :-/
Adrian Rosebrock
Hey Philip — I’m sorry to say that I don’t have much experience using webcam drivers for the Raspberry Pi. I either use the actual Pi camera board or a Logitech 920C which works out of the box. Be sure to check this page of compatible USB webcams for the Raspberry Pi to see if there are any known issues with it.
SK
Hi Adrian,
There is excellent content on your website – thanks you so much!
I have a request – will you be able to prepare a tutorial to install OpenCV with QT, VTK, and these additional performance libraries (CUDA, etc) for Raspberry Pi (all that it can support) and linux computers? Thank you for all you do, I am considering buying one of your packages.
SK
Adrian Rosebrock
It’s something that I can certainly look into.
rap
Thanks for the instructions! However, could you please tell if this will work for C++ as well as I wonder about the virtual environment part? If I install for Python 3 according to these instructions is the opencv library accessible for C++ from the default environment too or what should be done otherwise. Thanks!
Adrian Rosebrock
Once you run
make install
, you have OpenCV fully installed (as far as C++ is considered). The Python virtual environments have no impact on the C++ installation.rap
Thanks!
Mau
Hi Adrian, might I be able to compiling with raspberry b+ 512?
Thanks for your tutorials.
Mau
Adrian Rosebrock
This tutorial should work with the Raspberry Pi B+; however, I highly encourage you to use a Pi 2 for computer vision, especially if you want to do any type of video processing.
Bob
Is it bad if I accidentally ran “make clean” again after the long “make” process but canceled it almost immediately?
Adrian Rosebrock
Technically no, you can just run
make clean
again and let it finish cleaning up. However, you will need to runmake
again. Just to be safe, I would suggest deleting yourbuild
directory, re-creating it, and then runningcmake
just to make sure you are starting fresh.Birkan
can we delete opencv-3.0.0 and opencv_contrib-3.0.0 folders after succesfull installation?
Adrian Rosebrock
Yes, once you have OpenCV installed you can safely delete the
opencv-3.0.0
andopencv_contrib-3.0.0
directories.Birkan
Thanks..
mohamed abo amer
it says no enough space when downloading open cv
(wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.0.0.zip)
Adrian Rosebrock
If you’re getting an error that there is not enough space to download the .zip file, then your micro-SD card is filled. Either delete unneeded files to make room for OpenCV or make sure you have already ran
raspi-config
to ensure the entire micro-SD card is available for usage.Brian
Just to clarify, does this installation (OpenCV 3.0.0, Jessie, RPI2) run the OpenCV scripts in multi-core mode?
Adrian Rosebrock
No, it does not directly leverage multiple cores. You’ll need to write your own scripts to distribute processes to multiple cores/threads if.
Florian
Hello Adrian,
thanks for the great tutorial, but i have problems building with GTK+ support. I installed all the librarys and dependencies, but everytime i run the cmake command it remports back with “GTK+ No”.
Do you have any idea?
Thanks Florian
Adrian Rosebrock
Hey Florian — I have never ran into this issue, but it looks like this question on the OpenCV forums might be able to help.
deepak
hello Adrian thanks for your great tutorial i am compiling open cv on my pi2 but i have got a question how to compile opencv-contrib can to tell me the steps please and how to install PIL once again thankyou
Adrian Rosebrock
Hey Deepak, this tutorial already details how to compile and install opencv-contrib. Please see Step 2 where the OpenCV source code is downloaded and Step 4 where OpenCV is actually compiled.
You can also install OpenCV into your virtual environment by using
pip
:Mahmoud Tolba
Thanks for your great effort.
May I ask you a question please?
In case of using Raspberry pi type b, Should I change this command (make -j4) to be (make -j1) ?
Adrian Rosebrock
If you’re using the B, then just use
make
and leave off the-j
switch.David Kadouch
I’m so grateful to the deep level of detail of your tutorials. It works like a charm.
For the sport I tried first to install opencv 3.1 that was just released a few days ago. Alas it fails at the make stage with a ‘no targets’ error message. In fact no Makefile was created after ‘cmake’. I must say that it’s a bit frustrating that they break the install process for minor versions. It makes upgrading a real pain. Any insights here?
Adrian Rosebrock
Hey David, please see my reply to Nelson Chen above.
Nelson Chen
Thanks for great effort.
I have a question for you. My opencv is v3.1.0, and when I try to run cmake, I got an error. I have tried several times and found that I had to remove the INSTALL_C_EXAMPLES thing from the command line, or it led to an error. What do you think of it?
Adrian Rosebrock
I actually ran into this error myself when trying to compile OpenCV 3.1.0. Try removing
-D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON
from thecmake
command and it should generate a working Makefile.Tom
It seems that the issue is in the c examples, I had to specifically add -D INSTALL_C_EXAMPLES=OFF before I got it to work, the rest of what you mentioned can stay in the cmake command without issues.
Thanks for the great tutorial
Adrian Rosebrock
Thanks for the clarification regarding the cmake options!
Mario
Thanks, I ran into the same problem.
Adrian Rosebrock
You can fix the issue by removing the
-D INSTALL_C_EXAMPLES=ON
from the CMake command.Syed Tariq
Excellent Tutorial. I followed all the steps and tested it by using a simple program to display video from a Sony PS Eye. The only issue I am facing is that the ‘import cv2’ is only recognized when my python program is in the site-packages subdirectory. I used the instructions in ‘Installing OpenCV 3.0 for both Python 2.7 and Python 3+ on your Raspberry Pi 2’ but I am using Raspbian Jessie OS. I did compare it to the instructions on the ‘How to install OpenCV 3 on Raspbian Jessie’ and did not see anything that would affect the installation. Any help would be appreciated.
Adrian Rosebrock
Are you using the
workon cv
command to drop into the Python virtual environment before executing your script?Syed Tariq
I am using ‘workon cv’. I can see ‘(cv) in the command line. I don’t totally understand virtual environment. I will read up on it and try debugging. It appears that the link cv2.so is not global even though it has attributes lrwxrwxrwx. If I create a symbolic link cv2.so in the directory from which I run the python script it seems to work.
Thank you for your help.
Adrian Rosebrock
If it works within your current working directory, then the sym-link inside the
site-packages
of thecv
virtual environment is likely not right. I would delete the sym-link and then re-create it.Syed Tariq
Adrian – Thank you. It is all working right now. It was a great tutorial and it helped me immensely.
Adrian Rosebrock
Awesome, I’m glad you were able to resolve the issue 🙂
Nanne Kuperus
I seem to have made a hash of things and have accidently already installed an incorrect cv2 symbolic link; how do I remove it?
Entering ‘sudo rm -r /usr/local/lib/python3.4/site-packages/cv2.so cv2.so’ gives me the ‘no such file or directory’ error.
Awesome tutorial, really happy with the easiness of steps. Just wish I had paid a bit better attention 🙁
Adrian Rosebrock
You can remove the sym-link using the
rm
command. Regarding your error, give your command another look — you’re trying to remove acv2.so
file in your local directory (which doesn’t exist), hence the error.Fra
Hi Syed/Adrian, I have the same issue cv2.so works within my current working directory, so even for me It appears that the link cv2.so is not global.
How did you fix it?
I followed video and tutorial in any details…
When I run my test.py in ~ I get “ImportError: No module named cv2” otherwise in site-packages it is fine.
Bill Kearson
Great job,
With your detailed instructions and the comments I have OpenCV3.1 and Python 3 working, I had to remove the C examples, INSTALL_C_EXAMPLES=OFF, from the cmake command and ended up having to use one core to avoid errors during the make.
In the end everything is working.
Using a 8G card I was almost out of space but I saved an image before and after removing the downloaded files and folders. Left me with about %55 free space. Not bad.
A good time was had by all over two days. I did start over three times making noob mistakes: Expand the file system running raspi-config on first run.
Now I have my pi running headless, wifi connection, ssh primary connection and vnc available with manual startup, if needed.
Thank you
Adrian Rosebrock
Congrats on getting your setup working Bill! And I’m in the process of updating the OpenCV 3 install tutorials to mention removing the
INSTALL_C_EXAMPLES=OFF
flag. It looks like you beat me to it! 🙂Sebastian
Hi Adrian
First I wanna thank you for the tutorials, I tried for a long time install OpenCV in a Raspberry, but I can’t do it. I followed your instructions, and I don’t have any error during the process, but the problem is when I tried to import the library in Python, I have the next error: “ImportError: dynamic module does not define init function (initcv2)”, do you have any idea to solve that?. Thanks.
Adrian Rosebrock
I think one of two things happened. Either you compiled OpenCV with Python 2.7 support and then tried to import OpenCV with Python 3, or vice versa. Make sure you are in the
cv
virtual environment prior to importing OpenCV. If that still doesn’t work, go back to the CMake command and make sure you are compiling OpenCV for your intended version of Python.Sebastian
Hi Adrian
Yes, it was my problem, thank you so much. as we say in my country: “you deserve the sky and more”, thanks!!!
Adrian Rosebrock
Nice, congrats on resolving the issue 🙂
Tim Farnworth
Hi Adrian,
Thank you for these tutorials man. They are great!
I installed OpenCV last night and paused the installation just after the make -j4 command. I resumed this morning. Other then adding
-D WITH_OPENMP=ON
and set-D INSTALL_C_EXAMPLES=OFF
to the cmake install, I have followed the tutorial closely. But for some reason I am having issues.cv2.imshow()
and cv.ShowImage() dont open any image windows. I’m getting frustrated because I cant see where my code is wrong.Here is a bare bones version of my code:
The code terminates in the cv2.imshow() function and does not reach the print “Image shown” line.
Do you have any ideas what could be wrong? Cheers
Adrian Rosebrock
It’s very strange that the “Image shown” text is never printed to your screen. I would point that unless you are using a video stream, you likely don’t want to use
cv2.waitKey(1)
. This will only wait 1ms before automatically closing the window. You should usecv2.waitKey(0)
to wait indefinitely for a keypress.Since you deviated slightly from the tutorial, I would suggest doing two things as a sanity check:
1. Blow away the install and start from scratch. Specifically, pausing during
make -j4
might have introduced a hard to track down dependency issue. Allow OpenCV to compile without pausing the process.2. Secondly, re-compile with
-D WITH_OPENMP=OFF
. I’ve never encountered an issue with OpenMP before, but since it deviates from the tutorial, I think it’s good to perform a sanity check.Nicolas Roux
It’s a great guide, I think I already used one of your tutos in the past so a thank you was long overdue.
Here it is:
Thank you, keep up the good work.
Adrian Rosebrock
Thanks for the kind words Nicolas 🙂
Malef
i try to compile opencv 3.1.0
but it stalls after 5 mins [21%] on opencl/perf_arithm.cpp
6%CPU and 59% MEM
Using Swap Total 2097148 6800 used
what can i do waiting longer or is there something wrong?
is compliling with OpenCL makes sence ? is the GPU is used?
Thanks for help
Adrian Rosebrock
That’s strange that the compilation has stalled. Are you trying to compile with OpenCL support? If so, I would suggest disabling and trying again.
Also, try using compiling using only a single core with:
make
This will avoid any potential dependency/race conditions with the compile.
Malef
i switched of the opencl now the same thing at 82% building python module cv2
kswapd uses most time the cpu() and the cc1plus process
and then it stalls
maybe a swapping problem and then a crash?
Adrian Rosebrock
How long into the compile? Also, which Pi hardware are you using?
Malef
Raspberry B first version 256MB RAM 128MB Split
Swap on SD with 3 times Ram
I try it now with 16MB memory split for GPU
Adrian Rosebrock
The RAM definitely sounds like the issue. These OpenCV + Raspberry Pi tutorials are intended for either the B+ or the Pi 2. I have never tried with the B, simply because it has too little RAM and isn’t the most suitable for computer vision and image processing. If at all possible, you should try to get a Pi 2.
Paul Maher
Thanks for posting the detailed instructions which have been invaluable to me. I have a question though about the virtual environments. I’ve not used them before so I managed to tie myself in a knot.
If I switch to the CV virtual env as in your instructions everything works fine… but if I try to import the GPIO library it says its not found.I also cant use ‘sudo’ to run my OpenCV python programs (which I will need to do as I want to access use the GPIO ports as well as the camera). So at the moment I can *either* use the CV virtual env and get OpenCV OR not use the virtual env and get access to sudo and GPIO. Not sure how to dig myself out!
Any advice gratefully received!
Adrian Rosebrock
I’ve actually addressed this problem a few times on the PyImageSearch blog. First, you should read my reply to Rob James above. Secondly, do a ctrl+f and search this page for GPIO (especially in the comments section). You’ll notice that the solution is to launch a root shell, create a virtual environment for the root user, install OpenCV into it, and execute your script.
The important point is that you need to actually launch a root shell rather than just using sudo to call your script.
Finally, you could try ignoring the virtual environment entirely and ensure both OpenCV and NumPy are installed into your normal Python
site-packages
directory. This will also solve the problem.Paul Maher
Adrian, thanks for the prompt reply. I will try omitting the virtual env steps and see how I get on. Thanks again for the reply.
manish
Sir,
how to import cv2 on python shell …
I followed your all steps and it goes correct..
but I didn’t get on shell as it shows
no module name cv2
I m using
Raspi-2,model B
with NOOBS
Adrian Rosebrock
Please see the “Troubleshooting” section at the bottom of this post. I would also encourage you to watch the installation video at the top of this post and follow along with the exact commands that I perform. This will ensure that OpenCV is properly configured and running on your system.
manish
Sir I followed your video and at the terminal it is going right..
but it didn’t work on python shell….
I m a beginner in Raspberry pi …
Can you take me out of this??
it was all right but not no shell
Opencv is installed on my pi
I checked it through terminal as you mentioned in last step..
Adrian Rosebrock
Hey Manish, so just to clarify OpenCV is indeed installed on your Pi?
If it’s not, then I would suggest going through the “Troubleshooting” section of this post. I’m also not sure what you mean by it’s working in the terminal, but not in the Python shell.
Carlos Alberto Giron
Hello Adrian and thanks for the tutorial.
I have the same problem, if I open the terminal and follow the steps source ~/.profile –> workon cv –> python –> import cv2, it does it without any problems. But if i open a script directly from python and start with import cv2 then i get the error: ImportError: No module named cv2.
I believe it must have something to do with my python script not running on the profile and the cv environment but i don’t know how to make it run from there.
If you can help me i would really appreciate it.
Adrian Rosebrock
Hey Carlos — this actually is the intended behavior of using a virtual environment. Your
cv
environment is meant to be sequestered and independent from the system install of Python. I detail how virtual environments work in the first half of this blog post.Arash
Thank you for the tutorial.
As mentioned in the update I got the make error after (C_EXAMPLES=ON) so I removed the build folder and redid the cmake with C_EXAMPLES=OFF however I am still getting “No targets specified and no makefile found. Stop.”
Adrian Rosebrock
After you removed the
build
directory, you need to re-create it and enter it before running CMake again:From there you should run the CMake command.
manish
sorry to have a late response sir.
I got it .and solved the problem .
thanks for your support and suggestions..
and thanks for this tutorial.it is really helpful.
urby
Hello manish.
What did you do to solve the problem? Because I went trough troubleshooting and I still get the error.
Kewin
I have 8 gb sd card with raspian jessie. When I install raspian on it, the folder manager of rasperry pi shows the free space about 700mb. Is that normal. If 8gb is not enough I will buy a 32gb sd card. Because I want to install opencv and python and the disk space is not enough. Please help me
thanks
Adrian Rosebrock
700MB is very much borderline. Make sure you have ran
raspi-config
and expanded your Raspberry Pi filesystem to use the entire micro-SD card. Otherwise, if you have already expanded your filesystem, I would suggest either (1) deleting files to make extra room or (2) getting a large micro-SD card.Hilman
Hey Adrian.
I want to ask something. Is a 4GB microSD card big enough to install Raspbian Jessie, OpenCV3, and all the things you’ve wrote above? What if I want to make project on the Raspberry Pi (by using the Raspberry Pi camera and your tutorial of course), will it still be big enough?
Adrian Rosebrock
Realistically, a 4GB card is not large enough — I would suggest using an 8GB card.
Joseph
I followed every step (doing Python3) but when I got to Step #5: Finishing the install, my python3.4 directory is empty. python2.7 has a bunch of files, but not cv2.so. I looked back at all the commands I entered and everything looks right. Not sure what went wrong or where I could backtrack to.
Adrian Rosebrock
Be sure to take a look at the “Troubleshooting” section of this post. Be sure to take a look at the
build/lib
directory as well.Manu
Hi Adrian,
Thanks so much for your detailed blog on how to install OpenCV, it’s very good.
I have had a similar issue to Joseph, and I found the cv2.so file under build/lib folder as you explained.
I copied the file to /usr/local/lib/python2.7/site-packages and proceeded with Step #5 as you explained.
Everything seems to work just fine. Am I right to think the system is installed correctly now?
Thanks
Adrian Rosebrock
As long as you ran
make install
and then manually copied over thecv2.so
file, then yes, everything is installed.mai
went though make with no error.
However, make install gives following errors:
How do I post a screen shot?
Adrian Rosebrock
I would suggest uploading an image to imgur and then posting the link.
Vishwa
Followed the guide till the end, I can import cv2 in terminal but not in IDLE, none of the comments helped. There is a cv2.so file in site-packages. I don’t understand what you mean by running it in the cv virtual environment. Help
Adrian Rosebrock
To access the Python virtual environment, you need to use:
$ workon cv
And this will drop you down into the
cv
virtual environment. You can then use the terminal version of IDLE. The GUI version doesn’t properly work with the Python virtual environment.zevero
If you end up not having enough space like me on raspian (on 8GB) try to free some space:
500MB: sudo apt-get remove –purge wolfram-engine
100MB: sudo apt-get remove –purge scratch
xxxMB sudo apt-get clean autoclean
Adrian Rosebrock
Thanks for sharing the tip!
mikel
thank you so much!
Adrian Rosebrock
No problem Mikel, I’m happy the tutorial worked for you.
Fra
Hi Adrian,
I followed your instructions until step 4. Once I run CMake I checked Python 2 and 3 section.
Unfortunately for me both have “packages path” set to “lib/python3.4/site-packages”
What can be wrong?
Adrian Rosebrock
To start, make sure you are in the
cv
virtual environment prior executing CMake:$ workon cv
Otherwise, assuming you are trying to build Python 3 bindings, it should still be okay. You can always copy/sym-link into the
site-packages
of the virtual environment after compiling.PetriK
excellent instructions, suggest also adding
sudo apt-get purge wolfram-engine
to release some space on the 8Gb sd car, without removing there is not enough space to install opencv.
PetriK
Another suggestion for installation – just learned that when mounting usb disc as /build directory the filesystem must be ext4. If filesystem is FAT then libraries can not be build on due to user rights error. Needed to have an external disc as running short of memory during make even after removing wolfram-engine.
Looks like the /build directory needs around 1.5gigs free space for the make being successful. So altogether 3gigs free space on sdcard may not be enough for the full opencv installation, so needed an external usb drive for /build.
Hilman
Hey Adrian, I have a question.
When I entered this command:
“wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.0.0.zip” (I changed the 3.0.0 to 3.1.0, that is the latest one isn’t?)
I get the “ERROR 404: Not Found”. I can’t even go to the link in the command manually using my laptop’s browser. How can I fix this?
Thanks in advance.
Adrian Rosebrock
That’s very strange that the .zip is 404’ing. It’s downloading perfect on my machines. Try cloning down the entire OpenCV repository and then checking out the 3.0.0 version:
Hilman
Already figured it out. It was DNS issue…
greg
Uhh I’ve been using linux for about 15 years and I can’t get this to compile, I guess I’m retarded. Gave up after 3-4 hours of messing around.
http://pastebin.com/nSNjCggH
Adrian Rosebrock
Which version were you trying to compile for? Python 2.7 or Python 3? Also, is that your full output from CMake? Normally there is a longer trace.
GINO
i dont have “.profile”in my directory how to fix it..help
Adrian Rosebrock
If you do not have a
.profile
file in your home directory, create it using an editor such as nano:$ nano ~/.profile
ChicagoChuck
You can also navigate with the GUI editor (the file cabinet at the top of rasbarian) and go through the Directory Tree to the home/pi folder. Once there, right click and check on “show hidden” which will show the file.
So, the .profile is there, you just cant see it to edit it. If you create another one, you are going to overwrite your original one. Once you uncover it through this method, then you can click on the file and edit .profile
Also, the path that you have as source ~/.profile can also be accessed through:
source /home/pi/.profile if the source ~./profile isn’t working.
Adrian Rosebrock
Thanks for sharing the GUI editor tip! As for the
~/.profile
, that is simply a shortcut for the full path:/home/pi/.profile
. As long as you are logged in as thepi
user, thensource ~/.profile
should work.Girish Kumar
Thanks Adrian, Open CV 3.0.0 installed like a breeze, great write – Keep up the good work
Regards
Girish
‘
Adrian Rosebrock
Fantastic, I’m glad the tutorial worked for you Girish! 🙂
liudr
Thank you Adrian! It took me a few tries on RPI 2B jessie before I eventually got to the finishing line with python 2.7
As of 2/14/16, if one updates and upgrades jessie before following your tutorial, he needs a 16GB card. My 8GB card got only 4GB left with the 11-25-15 jessie distro and that shrank to 2GB right after upgrade. The opencv build needs about 2.4GB. I ended up mounting a hard drive for the build folder. My build took 7 hours to make, which is way above your mark. Will try to install opencv for python 3.5 that I compiled. It’ll be a challenge. I installed it in an opt subfolder in /usr/local
Adrian Rosebrock
Thanks for sharing your install experience!
jamaludin indra
thank’s for your tip
when i entered this command
pip install numpy
i get error:
ImportError: No module named pip
how can i fix this..?
thanks….
Adrian Rosebrock
Please see Step #3 of this tutorial to install pip.
Thomas Overby
This might be one of the most structured tutorials i have ever seen. Thank you for the effort and the time you spent to make this quality guide! Really, really nice work. Again, thank you for this!
Adrian Rosebrock
Thanks for the kind words Thomas, I’m happy the tutorial worked for you 🙂
Alex
Hello Adrian,
I am on step 4 trying to compile Opencv but when i enter the command
cd ~/opencv-3.1.0/
it returns
bash: cd: /home/pi/opencv-3.1.0/: No such file or directory.
I replaced the 3.0.0 with 3.1.0 for both opencv and opencv_contrib.
I followed all the steps and I am in the (cv) virtual environment.
Do you have an idea why this might be happening?
Adrian Rosebrock
You’ll need to double check where you downloaded the original
opencv-3.1.0
file to. It seems like it was not downloaded to your home directory. If you did download it to the home directory, you need to unzip it first.Carlos
“make -j4” crashes my Raspberry Pi 3 instead I used “make” only (no other parameters).
Adrian Rosebrock
Thanks for sharing. My Raspberry Pi 3 should be here soon and then I’ll be testing this tutorial — and possibly creating a new one, if need be.
Mark
ok i think the problem was I used sudo pip install numpy which told me it was already installed but I assume using sudo it wasn’t checking the cv environment?
Adrian Rosebrock
The
cv
virtual environment is entirely independent than your system install of Python (and associated packages). Therefore, you need to install NumPy into thecv
virtual environment as the blog demonstrates how to do.Vit Hovorka
Firstly I would like to express my respect to you concerning your great contribution to machine vision community. Hopefully some day I’ll gain enough knowledge in machine vision in order to be also useful to community 🙂 But now I’m on beginning of my learning.
And I would like to thank you specially for this detailed tutorial.
Adrian Rosebrock
Thanks so much for the kind words Vit 🙂
MichauPe
Hello Adrian, hello All.
First of all – great tutorial. I`ve installed openCV on rpi2 with success. Now I have rpi 3 and all have been going well since cmake part. I cant find why it throws a error: “CMake Error At CMAkeLists.txt.:107 = Cmake fails to determine the bitness of target platform”.
I have fresh installation of Jessie , cmake version is 3.0.2. In txt file is statement tha this is a popular CMake conf mistake. What i have to do now, please help 😛 ?
Adrian Rosebrock
I don’t own a Raspberry Pi 3 yet — mine was placed on back order, unfortunately — so I can’t speak directly to what the problem is. Once I have a Raspberry Pi 3, I’ll try the tutorial myself and create an updated version if need be.
MichauPe
In order of deep investigation i have managed to install openCV 3.1.0 on rpi 3 with this cmake command first:
and make command without -j4.
I wish this can save a night to somebody 🙂
Adrian Rosebrock
Thanks for sharing Michal! It looks like the main addition is
CMAKE_SIZEOF_VOID_P
. I presume this is likely because of the 64-bit architecture of the Pi 3. Once I get my hands on a Raspberry Pi 3 (I have an order placed, it’s in the process of shipping), I’ll be sure to compile OpenCV myself and likely create a separate tutorial.Yazheng
Hello Adrian. Thanks for your great work!
Followed your tutorial, I have successfully installed OpenCV 3 with Python 2.7 on Raspbian Jessie. But when I ran the sample file called facedetect.py, there was a warning: unable to open video source. It cannot display the video captured by picamera or even the picamera cannot capture the object. Do you have any idea about this condition? Thanks again.
Adrian Rosebrock
I would double check that your Raspberry Pi camera module is connected properly. Also make sure that you have ran
raspi-config
, enabled the camera module, and rebooted your system.Jasper
I got some errors with cmake. I thought some dependencies were missing, but no: turns out that I overlooked the OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.0.0/modules setting which was set to the wrong version (I use 3.1.0). I also changed all settings for building examples to OFF. Now cmake runs fine.
Maybe this will help some other poor fellow who, like me, knows nothing about compiling…
Thanks Adrian for the great tutorial!
Adrian Rosebrock
Thanks for sharing Jasper!
anu
do we need net connection for make -j4 command in installing opencv for raspberry pi
Adrian Rosebrock
No, the
make -j4
command does not require an internet connection. It is only used to compile OpenCV after all dependencies have been downloaded and installed.Prateek Xaxa
THANK YOU very much … I cannot describe how much I needed it
Adrian Rosebrock
No problem, I’m happy the tutorial worked for you! 🙂
mike
rpi-update killed by SD card. Using a brand new RasberryPi3.
I had to erase the card and reload raspian. Not sure that this step is required!
Adrian Rosebrock
Thanks for the tip Mike. I’ll have my Raspberry Pi 3 later this week and I’ll be sure to test the install tutorial (and likely create a new one specific to the Raspberry Pi).
Kevin
This past weekend I have successfully and painlessly installed OpenCV 3 on my Raspberry Pi 3 following this post.
fyi I also previously discovered that sudo rpi-update screwed up my card when I tried it on the latest Jessie version from the RPi website. You might want to consider removing that line from your blogpost.
I also successfully and painlessly got my RPi camera working by following your blogpost “Accessing the Raspberry Pi Camera with OpenCV and Python”
I am now working through my copy of Practical Python and OpenCV + Case Studies on my RPi.
So far I’ve been following it using the cv python environment but I’ve now come to a small hurdle at Chapter 7 Histograms where there appears to be no module matplotlib in the cv environment.
How easy is it to install matplotlib in the cv environment and how is it done ?
Is the same situation likely to be the case when I move onto the later Chapters and Case Studies which use mahotas, scikit-learn and scikit-image ?
As an interim workaround I have installed matplotlib directly onto my RPi so I can run the code in Chapter 07 but only outside the cv environment. I would like to know how to do it the right way.
I am so glad I’ve found your blog. Many thanks
Adrian Rosebrock
Hey Kevin, thanks for the comment. My Pi 3 will be here in the next few days, then I can run through the tutorial myself and I’ll make any necessary updates (such as removing the
rpi-update
command).Also, congrats on getting OpenCV installed on your Pi along with the camera module setup, that’s awesome! As for installing
matplotlib
(or any other package that is pip-installable), you just need to enter thecv
virtual environment first and then install:This will install
matplotlib
into thecv
Python virtual environment.If you run into any with
matplotlib
, I would suggest consulting this blog post.npnancy
thank you for ur video:). everything went well but when i gave import cv2 it shows importerror: no module named cv2 in the terminal but when i change the path using cd usr/bin/local/python2.7/site-packages/ there is no error in import cv2 but when i run in idle again it shows error:( what shoukd i do now?
Adrian Rosebrock
Please see the “Troubleshooting” section of this blog post. It sounds like you are not in the
cv
virtual environment prior to importing OpenCV:nancy
I used your commands
$ workon cv
$ python
>>> import cv2
still i get error as
importerror: no module named cv2
Adrian Rosebrock
It seems like you forgot to sym-link the
cv2.so
file into thesite-packages
directory of your virtual environment. Go back to Step #5 and make sure the sym-link is valid and working.nancy
yeah i worked.i did what you have mentioned in step5 for python2.7
GAURAV PUROHIT
i think you forget to did source and workon cv command
then only it will work
Stefan Arnold
Very nice and smooth installation !!
Thank you very much !!
Minor problem – when you don’t stick to the tutorial literally:
When you work on an external file-base due to space-limitations on the internal SD: Be careful with the link/mount to your filesystem. E.g. “symlinks” will irritate the make-process.
Anu
Smooth installation.I installed it and it is working properly.Thank you so much for your tutorial
🙂
Adrian Rosebrock
No problem, I’m happy the tutorial worked for you 🙂
vj
hi adrian… thanks for ur video!!!!:) my code shows ” no module named drawmatches” when i try to import sift and surf it shows no module named “sift” plz help me 🙁
Adrian Rosebrock
You need to consult this tutorial on accessing SIFT and SURF with OpenCV 3. This should help clear up any issues.
Red
I would go for a cross-compilation here. When building OpenCV it tends to eat up a lot of resources – CPU, RAM and storage. The first two one can cope with but the last – storage – can actually lead to failure in finishing the build process. And even if able to build it there might not be enough space left on the SD card for the installation because it copies the files from the build folder to their respective places in the system.
oussema
raspberry pi2
raspbian jessie
AttributeError: ‘module’ object has no attribute ‘SURF’
you can help me please
Adrian Rosebrock
This blog post will take care of the problem 🙂
Steve Ryan
Adrian- AWESOME tutorial! -and this from someone who never uses uppercase. Seriously, this was fantastically detailed and spot-on procedure to get OpenCV working on RPi Jessie. I stumbled quite a bit until I found this post, and then in a mere 2 hours (1.8 of which was waiting for the actual build) I was done!
Kudos, my friend- I am deeply in your debt!
Adrian Rosebrock
Thanks Steve! I’m so happy the tutorial helped you out! Congrats on getting OpenCV installed on your Pi, enjoy.
Evgeny
Hi Adrian,
Just wanted to thank you for this amazing tutorial and your books! Bought the course on Sat, already done with the first book and now I’m enjoying Case Studies. These are definitely worth buying.
One q comment – there will be some changes needed in this tutorial in order to make everything work smoothly on RPI 3. You already gave some explanations in the comments, but this also would be great to have some notes on step 4/5/ somewhere else.
Thanks!
Adrian Rosebrock
Thanks for the comment Evgeny — and it’s great to hear that you are enjoying Practical Python and OpenCV! I’m actually just wrapping up an install tutorial for the Raspberry Pi 3, so be sure to keep an eye on the PyImageSearch blog, it should be published in the next 3-4 weeks.
gaspar teleki
Adrian Rosebrock!
I spent days trying get opencv working on my raspi B+.
No luck.
I have a job and a nice family so im not willing to spend more time fight the compatibility chaos with raspberry. I want to test my ideas on the pi machine, have fun…thats all:)
Do you know of any existing wheezy/jessie image (with WORKING opencv/python2.7 on it) i could download?
Bests!
Gaspar
Adrian Rosebrock
I’m sorry to hear about the install problems, that is super frustrating! At the current time, I don’t have a downloadable Raspbian
.img
with OpenCV + Python pre-configured and pre-installed, but that’s something I’m currently working on. Expect to have an announcement within the next couple of weeks 🙂gaspar teleki
Dear Adrian,
Thanx for your reply for both of my messages.
Happy to hear that there might be a professional computer vision image avaible.
I think many people is googling around to find one.
According to free space:
i removed libre office by:
sudo apt-get remove –purge libreoffice*
Deleted the opencv folders and zip fles.
Mentioned Error:
Pip install scipy takes bunch of hous to “wheel” and finishes with error for me. Sorry i couldnot provide error…formatted the sd after that. To archive the same error i would need ti try to install again.
Virtual environment speed:
It seemd to me without measuring that executing a py took longer in the virtual enviroment. No measurement i made!!! So its not official.
How could i measure it? Do you have any suggestion?
Have a nice day!
Adrian Rosebrock
Thanks for the tip on removing Libre Office, I’ve made note of that. As for profile Python code (and associated binaries), I would consult the Python documentation.
Gaspar Teleki
Dear Adrian!
You are doing a great work doing tutorials!
Ive installed opencv310 on my pi B+ with the above given method.
it looks ok.
BUT:
I have some issues after install:
1. No free space left on my 8 gig card (i guess i should delete opencv zip files and the unzipped folders, but im not sure)
2. Cant do :pip install scipy + pip install matplotlib (i get tons of errors) = cant run my .py files requiring matplotlib.
3. virtualenv seems to slow down the pi and makes things complicated (not foolproof).
Is there any advantages an average user can get from virtualenv?
Plesase reply me if you have the time.
Bests!
Gaspar
Adrian Rosebrock
1. After you have compiled and installed OpenCV, you should absolutely delete the .zip files and the original source code directories. This will free up a bunch of space. You might also want to consider deleting the Wolfram Engine to free up space, as well as Open Office if it’s installed.
2. It’s hard to tell what the error message is without seeing your error message.
3. The virtual environment will not slow down the Pi at all. Do you have any benchmarks that make you think that? The primary benefit of using virtual environments is that you can create a sequestered, independent environment for each project you work on. This allows you to have different versions of various libraries on the same machine, all without conflicting with each other.
Dave
Hi, I was successful in installing opencv and running it. However, once in the virtual environment I am unable to import any additional modules (ie. PIL). How do I import additional modules once in the virtual CV environment?
Thanks for your help.
Adrian Rosebrock
You need to install each Python module into your virtual environment. Keep in mind that the virtual environment is independent from all other Python installs in your system. For example, to install PIL (or the more preferred Pillow):
FELIPE MARTINEZ
I wrote ” wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.0.0.zip ” and there is a problem “Cannot write to ‘oponcv.zip’ (No space left on device)” Whatt hapen? help me
Adrian Rosebrock
Please see my reply to “Mohamed” and “Matt” above. You either need to (1) expand the filesystem on your Pi using
raspi-config
or (2) free up space on your Pi by deleting unneeded files. In an extreme case, you might need to purchase a larger micro-SD card.ravishankarkm
Hi, I have followed your steps …………..while doing make -j2 its showing NO space left on device …………. complilation terminated ………. and i am using raspberry pi B+ board.
regards,
ravishankar
Adrian Rosebrock
Have you expanded your Raspberry Pi to use the entire filesystem? Make sure you executed
raspi-config
, selected the first option to expand the filesystem, and then rebooted your Pi. Ideally, you should have an 8GB or 16GB card when installing OpenCV.ravishanakarkm
yes. I have expanded the filesystem but no use . But i have not tried on 16 GB . Let me try. thank you for replay.
regards,
ravishankar
Alex
Worked for me on a Raspberry Pi 3 running Raspbian Jessie with OpenCV 3.1.0
Thank you.
Adrian Rosebrock
Awesome, I’m happy to hear it Alex! 🙂
ravishankarkm
Thank you . Yes it is working now. This time i used 16GB.
Regards,
Ravishankarkm
sathya
Hey, Im looking for instructions that will help me Install cv2 (OpenCV) with bindings to Python 2.7 in RASPBIAN JESSIE. PLEASE HELP. DESPERATE. FOR A COLLEGE PROJECT.
Adrian Rosebrock
Please see the blog post you are commenting on. It demonstrates how to install OpenCV (cv2 bindings) with Python 2.7 and Raspbian Jessie.
Manasi Shinde
i have tried and tried this tutorial about 7 times and finally open cv3 was installed!! i have spent more than 24 hours of my life. This tutorial saved me. Thanks a million.
i completed my installation completely, i did a test run and it worked. but when i opened the gui via vnc, the cv2 module did not exist.
i guess this is 7th time unlucky.
Adrian Rosebrock
Congrats on getting OpenCV installed Manasi! Also, after opening a GUI via VNC, make sure you use the
workon
command to access your Python virtual environment before you try to import cv2:Manasi Shinde
it throws an error ‘workon command doesn’t exist’
Adrian Rosebrock
Please see the answer to the first question in the “Troubleshooting” section of this blog post — it details how to resolve this problem.
Noam
Should I specifically install libgtk 2.0 or just the newest version?
Adrian Rosebrock
libgtk-3.0 should work, but I’ve tended to use libgtk-2.0. If you try libgtk-3.0 and it works for you, definitely let me know!
AndrewR
Hi Adrian,
Thank you very much for the tutorial.
I’ve done the installation I can check it in the terminal, but Python 2.7 shell (IDLE) was saying: “no module name cv2”.
I’ve done a “sudo apt-get install python-opencv” and now the IDLE is saying that I have OpenCV 2.4. In the terminal I still have the correct one 3.1.0.
Could you please explain me how to remove the 2.4 from the IDLE and then how to add 3.1.0 to the IDLE.
I hope you will answer my question.
Regards,
AndrewR
Adrian Rosebrock
You will need to use
apt-get
to remove OpenCV 2.4; however, at this point I would suggest reformatting the card and starting over. The reason I say this is because the apt-get version of OpenCV is very old and likely also installed a bunch of extra dependencies you don’t need.Secondly, make sure you see the “Troubleshooting” section of this post as it details the primary reasons why you are unable to import the cv2 bindings.
AndrewR
Thank you Adrian! Your work is huge and is highly appreciated!
Regards, AndrewR.
Adrian Rosebrock
No problem, happy I could help!
Jim
Has the issue regarding pyserial/GPIO library install to the virtual environment been resolved?
Adrian Rosebrock
It’s not an “issue”, it’s just a difference in how you install the script. Just access the virtual environment and install GPIO into the environment:
Here is an excellent tutorial on the basics of virtual environments that I recommend everyone give a read.
Jim
Very nice work, but it would very helpful if you could add instructions to test a simple python/opencv script at the end of the installation instruction for the purpose of verifying installation and giving the new user a hand in learning how to setup a simple script in the new cv virtual environment.
Adrian Rosebrock
Thanks for the feedback Jim. I’ve done that on previous OpenCV install posts, I’m not sure why I didn’t for this one. Also, if you’re looking to put your OpenCV + Raspberry Pi install to use, be sure to take a look at Practical Python and OpenCV which contains a bunch of tutorials to get you started quickly.
Reza
Adrian , i have problem , the problem is mkdir no space left on device , can you help me to resolve this ?? thanks
Adrian Rosebrock
Make sure you have fully expanded the disk on your Raspberry Pi by using
sudo raspi-config
and selecting the first option to resize the disk, then rebooting. If you have already done this and there is still no space left, you’ll either need to (1) delete files on the disk to make more room or (2) get a larger micro-SD card.israel
my raspberry pi turned off during the make. when i use the make it doesn’t go past 71% it looks like its looping. How do i get past it? I tried make clean and then make but that didn’t work. Would i have to recreate the build folder?
Adrian Rosebrock
If your Pi shut off during the
make
command, then I would suggest deleting thebuild
directory and then re-creating it. If you tried compiling using multiple cores, then the shut off likely broke the build.israel
Thanks, yeah during the multiple core compilation it broke. I just used the regular single core make command. Now its all gravy:) I reinstalled the build directory and installed dependencies and compiled it. Thanks for this tutorial.
Adrian Rosebrock
Nice, congrats on resolving the issue 🙂
Adolfo
Hi there!
I’m facing a problem I am not able to solve by myself (I am completely new to RPi and all this building and compiling things :))
When I try to cmake opencv, being on cv virtual env, I get this error message:
CMake Error: The source directory “/home/pi/opencv-3.0.0/build/BUILD_EXAMPLES=ON” does not exist.
I tried to use -U with al keys to remove them from cache, but it keeps throwing the same error.
Any clue? I’m lost
Adrian Rosebrock
It sounds like you’re not copying and pasting the cmake command correctly. Make sure you copy and paste the full command.
Adolfo
Yep, I was missing the two trailling periods.
Dumitru Popa
Thank you, nice work !
Adrian Rosebrock
Thanks Dumitru!
Adolfo
Hi everybody,
Just in case some is or will get stuck at the beginning of step 4: the two periods at the end of the cmake line are meaningful!! I was getting an error message because I thought (ignorant me) they just meant you could add more parameters.
Silly me!
Stewart Baskin
Hi Adrian. Thanks for the great tutorial! Have you made the tutorial for the Pi 3?
Thanks!
Adrian Rosebrock
You bet. You can find the Raspberry Pi 3 tutorial here.
Jenny Ching
Hi Adrian,
I got to step 4 (examining the output of CMake) and noticed that both the Interpreter AND numpy are both NOT in the virtual environment (cv). Which steps do I have to go back to to correct these? Thanks!
Adrian Rosebrock
To start, make sure you are in the
cv
virtual environment before you run CMake. If you are, then you’ll need to manually supply the paths to the interpreter and NumPy directories. For more information on how to do this, please see the comments section of this post.Jenny Ching
I double checked that I was in the cv environment before I ran CMake however when i run CMake I still get this…
Python 2:
— Interpreter: /usr/bin/python2.7 (ver 2.7.9)
— Libraries: /usr/lib/arm-linux-gnueabihf/libpython2.7.so (ver 2.7.9)
— numpy: /usr/lib/python2.7/dist-packages/numpy/core/include (ver 1.8.2)
— packages path: lib/python2.7/dist-packages
Help!
Adrian Rosebrock
It actually looks like the issue is related to your “numpy” and “packages path” variables. To resolve the Numpy issue, have you installed NumPy into the
cv
virtual environment?You can actually just leave the “packages path” variable alone and then manually copy the
cv2.so
file into yoursite-packages
directory after OpenCV compiles and installs.Akshay Jethani
hii,
after giving the command of sudo apt-get upgrade,the pi is taking to long to upgrade like 4 hours sometimes, even after 4 hours its still upgrading and showing connection failed in between, can u plz help me regarding this issue.
Adrian Rosebrock
It sounds like you may not have a very strong internet connection. I would suggest making sure you have a good internet connection before running the upgrade step.
Charles Baker
Hello,
I have followed this tutorial exactly and have installed OpenCV 3.1.0 without error however I still cannot seem to access any of the modules associated with opencv_contrib (i.e. xfeatures2d to use SIFT and SURF). I have been pulling my hair out over this, I have tried reinstalling many times, I include the -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.1.0/modules \
line when using cmake, there are no errors, the path provided is where my opencv_contrib folder is actually located so it is not as if I am pointed to the wrong directory, however I simply get an AttributeError: ‘module’ object has no attribute ‘xfeatures2d’ when attempting to use xfeatures2d
Adrian Rosebrock
Take a look at the output of CMake; specifically, where it outputs the list of modules to be compiled. Do you see any of the contrib modules in there? If not, it really sounds like the path to the contrib modules could be incorrect.
jasson
Hi, Adrian. I have met the same issue. When I used “-lopencv_contrib” in g++ cmd, it gave an error as ” can’t find -lopencv_contrib”. Do you know why? And where can I find the opencv_contrib lib that I have installed? Thank you very much for your help!
Ragunaath
make command resumed after a power failure. Will it present a problem?
Adrian Rosebrock
It could, depending on where in the compile stage you were. I would suggest running
make clean
and then recompiling to give yourself a fresh start.Collin Sale
Hi Adrian,
nice one, .. works fine and I already received your hardcopy 🙂
Thanks a Lot
one thing: since I installed Samba-Share, a 5″ display and some other things before I started opven CV, I did actually run out of space on my 8GB SD, just like Bill mentioned earlier.
—
Bill Kearson January 3, 2016 at 10:09 am #
Using a 8G card I was almost out of space
—
Maybe you could add a Step #7: CleanUp the SD-Card 🙂
And mention on Step #1 that 8GB is just about to fit if you don’t install anything else earlier.
Thanks and best Regards
Collin
Adrian Rosebrock
Thanks for the suggestion Collin, I’ll definitely look into this!
Jesus De Jesus
Hellow Adrian, I resolve the problem of the comand wget, but now when I run the comand:
wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.0.0.zip
the comand line says:
–2016-02-03 17:25:20– https://github.com/Itseez/opencv/archive/3.0.0.zip
Resolving github.com (github.com)… 192.30.252.131
Connecting to github.com (github.com)|192.30.252.131|:443… connected.
ERROR: The certificate of ‘github.com’ is not trusted.
ERROR: The certificate of ‘github.com’ is not yet activated.
The certificate has not yet been activated
So, If you can help me with that I would appreciate it.
Adrian Rosebrock
Hi Jesus — try adding the
--no-check-certificate
switch to wget, like this:$ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.0.0.zip --no-check-certificate
This should take care of the error.
Lukas Vosyka
Hi Adrian,
by now you became my number 1 resource for checking OpenCV questions, inspirations and how-to’s! Thanks so much for your contribution and excellent step-by-step tutorials. I am eventually new to this topic (coming from Windows and .NET ^^) and you helped me a lot. This thread was also especially helpful since I got to do some projects on rapsi and OpenCV.
I’ll definitely get your hardcopy bundle and looking forward implementing some cool stuff with Raspi and OpenCV 😀
Thanks again for your very detailed and admirable contribution to this topic about OpenCV! Guess you’ll hear from me 😉
Cheers,
Lukas
Adrian Rosebrock
Thanks for the comment Lukas — and it’s awesome to hear that I could be of help 🙂
ghanendra
Helloo..!! Adrian
How to add these commands to autostart when I power on my Raspberry Pi ?
source ~/.profile
workon cv
vncviewer :1
python ball_tracking.py picamera 1
I want to open my python script using the virtual environment workon cv every time when i power up the Pi.
Adrian Rosebrock
Stay tuned for the blog post in 2 weeks where I demonstrate how to run commands + Python scripts on boot 🙂
ghanendra
OK!! Adrian. I am waiting for your fantastic posts. More and more is coming to learn.
Maugy
Wow, as someone who is trying to learn Linux, this is by far the clearest and best written tutorial I have ever seen!
Thank you!
Adrian Rosebrock
Thanks Maugy, I appreciate that 🙂
Vikram Bajaj
Thanks for this awesome tutorial Adrian!
Worked like a charm!
Adrian Rosebrock
Fantastic, I’m glad to hear it Vikram! 🙂
Marc Boudreau
Hi Adrian,
When you say “For some reason, unbeknownst to me, when compiling the Python 3 bindings the output .so file is named cv2.cpython-34m.so rather than cv2.so .”, —I think that’s fixed.
I got “cv2.so”
When I do the check, on the (cv)raspberrypi$ directory, everything works for the python check.
However when I do the version check of CV2, doesn’t work.
If I change directory to (cv) raspberrypi/.virtualenvs/cv/lib/python3.4/site-packages/
and check the version, I get same message as you.
When I start adding computer vision functionality, I’m afraid it wont find libraries.
I must of done Something wrong.
Is there easy fix or it’s best to reformat and redo procedure?
Adrian Rosebrock
Are you absolutely sure that you compiled OpenCV with Python 3 bindings? Double-check your output of CMake to make sure this is correct. You’ll typically see the
cv2.so
file when compiling Python 2.7 bindings.Ashutosh
Hi Adrian,
Thanks for awesome tutorial.I have installed opencv and python as told by you.
But when I use the picamera, I get considerable lag between input and output shown on result window.Please suggest something…..
Adrian Rosebrock
How are you accessing your Pi camera? Natively via HDMI + monitor? Or over VNC/SSH? Also, which model Pi are you using? All of these can affect the lag you are experiencing.
Steve Flynn
Adrian,
When i run a saved python script as in “sudo python test_video.py” i get the cv2 module does not exist error. Howover, when i run the import directly from the command line it works perfectly. In both cases i am inside the virtual environment and see the (cv) on the left. Does this have something to do with the virtual environment or did something not work.
Steve Fynn
Adrian Rosebrock
This does have to do with the virtual environment. Please read this blog post for more information, including the solution.
Jit Craig
awesome guide to installing opencv. every step worked correctly. now am getting ready to get my hands dirty with some real CV projects on my RPi 2
Adrian Rosebrock
Fantastic, I’m happy to hear the tutorial worked for you Jit! 🙂
zoe
Thank you so much!!! That is really helpful!!!
Adrian Rosebrock
No problem, I’m glad I could help Zoe!
Randy
Following this tutorial everything seem to work fine and completed without error but at the end the cv2.so is not present on my system.
Any ideas?
Adrian Rosebrock
Hey Randy — if you’re not seeing the
cv2.so
file, then either there was an error during the CMake/make step or the Python bindings were not build. Check the output of CMake to ensure that the python2/python3 bindings are in the list of modules to be build.Nick Shaver
Adrian – thanks very much for all of your python+opencv tutorials. You’ve saved me dozens of hours of wheel reinventing. I’ve used this particular tutorial several times. I’ve recently learned about checkinstall, and have been using it to keep from having to compile the same thing over and over. Dropping it in place of the “make install” creates a deb file that can be used to install opencv3 on a similar machine, though I’m not sure if that works with your virtual wrapper. Do you see any reason not to use checkinstall, assuming versions haven’t changed since the deb was made?
Adrian Rosebrock
Wow, that’s really cool! I had not heard about checkinstall before, do you have any links to the documentation so I can readup on it?
Nick Shaver
There’s not really a whole lot to checkinstall. I just apt-get it on debian boxes, and at the point of any program that I’ve compiled I skip the “make install”, and instead as a normal user (not root), I simply type “checkinstall –install=no”. It will prompt for a few things, I accept its defaults with the exception of the package name and version. With opencv3, for instance, I’ll set package-name=opencv, and version=3.1.0, and then it lit it rip. You can type in dependencies but I haven’t tried that yet. It’ll churn for 5 minutes or so on a raspberry pi. After it’s done, I use dpkg to install the deb file that it created. That way if things work, I know for certain that they’re only working because the deb file made them work. That’s the only thing I do as root – “dpkg -i opencv_3.1.0.deb”. I’ve used it on some other programs as well (mjpeg-streamer for instance) and it worked great. It supposedly will also created rpm files, but I use debian everywhere so I haven’t tried that. http://checkinstall.izto.org/
Adrian Rosebrock
This is really cool, thanks for sharing Nick. I’ll absolutely be looking into this!
Sid
awesome tutorial on getting opencv installed on Rpi. Easy to follow and nicely explained! I’ve already signed up for your class..
btw,,two things i had to do in addition were
1) add WITH_GTK=ON in Cmake to get rid of some run time errors to enable QT to get the window working.
2) ‘sudo modprobe bcm2835-v4l2’ to load the v4l2 driver so that cv2.VideoCaptire(0) works.
Tony Pitman
When I do the cmake command I get several lines like the following. They may not be errors, but just configuration responses. Can you verify they are normal?
Here is the github gist
https://gist.github.com/tpitman/9c4267220067c144e012e62a03670d9a
Adrian Rosebrock
Everything looks okay to me. It’s okay for various libraries not to be found, that’s perfectly normal behavior — it all depends on what you are installing, etc. What really matters is your “Python 2/Python 3” configuration sections.
moayed
Adrian !
finally i got openCV on my raspberry pi B+ 😀
thank you alot
your book or private lessons teach how to detect any objects by raspberry pi camera module or USB cam
Adrian Rosebrock
Congrats Moayed, that’s awesome!
Brendan Allen
Hi,
When I am trying to edit the profile file I am getting a message telling me that Permission is denied, and I cannot seem to gain access in order to change the file. Do you have any suggestions to how I can access this file to make the appropriate changes?
Thanks,
Brendan
Adrian Rosebrock
That’s strange that you’re getting a permission error since the
.profile
file should belong to the “pi” user. But in either cause, you can force edit the file using sudo:$ sudo nano /home/pi/.profile
Mohsen
hi.
i did all those steps and i sucessed to install opencv.
Thank you very much.
i think an instruction for install opencv 3.0.0 + opencv-contrib for python and on windows
is usefull too.
good luck
merad
first thanks for your instructions.
when i type import cv2 or import numpy in python 2(idle)
it gives me an error:
import error: no module named cv2
what do i od?
Adrian Rosebrock
Please see my reply to “Vishwa” above. You should also read the “Troubleshooting” section of this blog post.
Sunny Lee
Really thanks for your post! It helped a lot.
However, I’ve been thinking, why the virtualenv is required. Since if I used virtualenv to install opencv, I can’t import any other library after that such as matplotlib and drawnow library which belonged to global environment. That will be better if cv2 library is available in global environment so that any user can access to the library. I understand that virtualenv is necessary for certain Python projects but for most cases, especially for learner like me, I think that it is not required. What do you think if I go installing openCV without using virtualenvs?
Adrian Rosebrock
You certainly can install OpenCV without a Python virtual environment. It’s not a required. But I highly recommend it. You can learn more about why I recommend using Python virtual environments in the first half of this blog post.
Tuomo
Thanks for making the effort and writing such excellent instructions! This helped me to install OpenCV 3.1 on Raspberry Pi 3.
Adrian Rosebrock
Glad to hear it Tuomo! 🙂
Mike Crawford
Thanks Adrian for this install doc. For some reason, I tried to install for python3 but instead I got the python2.7 install. I definitely used mkvirtualenv cv -p python3 ( I checked the command history). Note, the output of cmake shows the python (for build): python2.7.
Is there something else that needs to be configured to get the python3 install?
Thanks
Mike
Adrian Rosebrock
Hey Mike — the “python (for build)” is really buggy. It will pretty much always report that bindings for Python 2.7 (instead of Python 3) are being compiled. I would ignore that and instead focus on the “Python 3” section above it and ensure the NumPy, library, site-packages, etc. settings are correct.
Chen Sa
Hey Adrian,
Im trying to install openCV via a usb storage, since my Pi does not have enough space to compile opencv there. Thing is, the CMAKE result points to the /usr path python while trying to build on the usb stick. However, when I try to build on the Pi instead, it does point to the virtualenv. Of course I made sure to always be in the cv enviroment.
Any idea why this might be the case? I am kinda new to the Pi and linux in general, so it is somewhat hard to find the cause.
I’d appreciate some advice, thanks!
Chen
Adrian Rosebrock
Hmm, that’s quite strange. Can you run CMake on your SD card to confirm the issue is related to USB stick? Whether you are using your SD card or the USB stick to compile OpenCV shouldn’t affect the paths that CMake detects.
Iqbal Aribaskara
Hello Adrian, Thank so much for the complete and eloquently written tutorial. I had a lot of fun following them, currently reading your free 21 days crash course.
I have a question, I’m building something that uses opencv for input, and doing some physical stuff via GPIO for the output.
The thing is to use GPIO, I need to run my script under sudo, but since opencv is installed under a virtual environment, I get unrecognized cv2 error every time I run my script.
Is there any way I can link to the installed opencv from outside the virtual environment? Or should I just install opencv outside the virtual env?
Thanks in advance.
Adrian Rosebrock
You bet. Just follow this tutorial on accessing RPi.GPIO from virtual environments.
Loic
Hi,
The installation went all good with me. But as I rebooted my raspberry pi 2 the command “workon cv” could not be found. Eventhoug i use de “source ~/.profile” command.
Adrian Rosebrock
If your
workon
command could not be found after reboot, then there was likely an issue updating your~/.profile
file. Go back to the step where you edited the~/.profile
file and reapply the edits.Lukas
Hi Adrian.
Thanks for your guide here. I followed every step and began to compile it with the command: “make -j4”. It all went well until 93%. Now it is frozen and doesn’t continue anymore. Can I just take away the power of the Pi 2 and start again or will I break something and have to begin again?
Adrian Rosebrock
I would suggest using ctrl+c to force stop the compile. Then delete your
build
directory, recreate it, re-run CMake, and then build using only a single thread:$ make
Hopefully that should resolve the issue.
ben
hello Mr Adrian i follow all steps without any error msg but when i run python and type import cv2 i got this msg :
importerror no module named cv2
Adrian Rosebrock
If you are having trouble importing
cv2
, please see the “Troubleshooting” section of this blog post for more details on how to resolve the issue.PMARINA
Hey, I am using openCV with the raspberry pi’s GPIO pins. I managed to install RPi.GPIO with pip so that I can use sudo python3 in order to access the gpio pins from the cv virtual environment. Unfortunately, I was unable to do this for the openCV library itself. Could you help me link or reinstall openCV so that I can do sudo python3 and still have access to openCV?
This is the same issue as Iqbal, except I need to be able to use openCV when I do sudo python3 to run programs/console.
Adrian Rosebrock
So if I understand your question correctly, you have OpenCV + RPi.GPIO installed as the “pi” user, but you need to run the Python script as “root”?
If so, I would launch a root shell, access the virtual environment, and then run the script:
Rafał
Hey Adrian !
You’ve done a great work, I’m a new to a virtual environments, could you tell me how to add the module SMBus to the virtual environment? it’s installing in raspbian typing apt-get install python-smbus, but when I’m in virtualenv Cv the smbus is not working unfortunately.
Greetings and Keep up the good work,
Rafał
Adrian Rosebrock
I haven’t used the SMBus library before, but I’m willing to bet you can use pip to install it. I would suggest starting with this tutorial on virtual environments and GPIO — I think it will provide the solution to your problem.
AMAN KHATRI
As always,Thank You Adrian for this tutorial.
enrique
hi Adrian
I’ve been using your notes to install opencv on raspbian jessie lite ,,,, I never get a failure installing all neccesary software, but when I trying to import opencv within python environment it does not work ……….. Do you know why?
Adrian Rosebrock
It’s hard to say exactly why without having physical access to your machine. I suggest you look at the “Troubleshooting” section of this blog post for more information. The Raspbian vs. Raspbian Lite shouldn’t make a difference.
Gaby Borjas
Great tutorial for the installation of OpenCV.
I am extremely new to raspberry pi, openCV and linux so I am a little lost. I wanted to continue with a couple of tutorials for facial recognition, but I get the following error ImportError: No module named cv2/ How can I resolve this?
Adrian Rosebrock
If you are having an issue related to importing the
cv2
module please see the “Troubleshooting” section of this blog post. It details common reasons why the OpenCV bindings may not be importing.Dan
Ok, sorry, I figured out what I did wrong. I didn’t CD into build, I was in make. I accidentally got it to work in make which made me go back and see what I did wrong and that’s when I realized I was in the wrong folder. I’m new to the command line and linux. So it did work in the make folder, is that going to mess anything up? Should I try and delete that if it’s possible?
Adrian Rosebrock
Without knowing the exact steps you’ve made I’d suggest just deleting the directories, unzipping the OpenCV code, and trying again. Theoretically this shouldn’t mess anything up, but again, without knowing what commands you executed I cannot guarantee this.
Koray
Hey Adrian, Nice Tutorial
Im having some issues when im trying to do the command
“mkvirtualenv cv”
and
” mkvirtualenv cv -p python3″
It always says
“Command not found”
I already looked at the Troubleshooting and it counldnt fix my Problem.
Im new to OpenVC and Linux. If you could help that would be awesome thanks.
Im German so sorry for my bad english ;).
Adrian Rosebrock
If you’ve already looked at the Troubleshooting section of the tutorial then it’s pretty hard to diagnose the error without having physical access to your machine. My guess is that your
.profile
isn’t updated properly, you didn’tsource
it, or perhaps more likely, there was an error installing virtualenv or virtualenvwrapper and you simply missed the error message.I would suggest trying to re-install both virtualenv and virtualenvwrapper and ensure both install without an error message.
Juan M
Hi Adrian,
Thanks for your tutorial, I followed your steps and OpenCv was succesfully installed. However, I found some troubles which I could solve by some previous comments. Here follows what worked for me:
1. SD of 8Gb is not enough, solution -> use an external hard drive with EXT4.
* for windows users I did not find a software to write, only to read Ext2fsd. So, running a liveCD could help.
As you mentioned, make the files in this EXT4 USB and install from it worked perfect!!
2. The path for final steps did not worked, it was different, solution -> install mlocate
$ sudo apt-get install mlocate
$ sudo updatedb
$ locate cv2.so
once it returned me the path, I just changed the one you mentioned.
3. The path for the virtual environment did not was available even running the workon cv (it appeared the (cv) user@machine…solution -> run it anyway at the end you could make the symbolic link which was recommend in step 5.
Thanks Adrian, you are doing a great job by making easier image processing in raspberry!
Adrian Rosebrock
Congrats on getting OpenCV installed, nice job! To address your points:
1. I recently installed OpenCV on an 8GB card without an issue. I’m honestly not sure why this would happen on your card.
2. Where did the
cv2.so
file end up being?Adrian G.
cv2 location was removed in my comment above. It’s installed here;
/usr/local/lib/python3.4/dist-packages/cv2.cpython-34m.so
Adrian Rosebrock
In that case, just move to the
site-packages
directory and rename:$ mv /usr/local/lib/python3.4/dist-packages/cv2.cpython-34m.so /usr/local/lib/python3.4/site-packages/cv2.so
Johnny
Success! Also, amazing support here Adrian.
Adrian Rosebrock
Nice job Johnny!
Erik
Hi Adrian! Thanks for this awesome tutorial. I Just got a brand new RPi 3 and already started to learn and practice CV!
Unfortunately I encountered a problem trying to use SIFT. I Installed Opencv with the contrib package, checked that they’re the same version (3.1.0) etc.
What I get is the infamous “‘module’ object has no attribute ‘xfeatures2d'”, how can i fix this?
For starters, how can I make sure that the contrib packages are installed successfully? And if not, what can I do to fix this? do I have to re-compile the whole thing?
Thanks!!
Adrian Rosebrock
Awesome, it’s great to hear that you’re interested in learning OpenCV!
If you are getting an error related to
xfeatures2d
then you compiled and installed OpenCV withoutopencv_contrib
.Yes, you will have to recompile the whole thing. You can avoid the issue by making sure you supply the correct path to the
-D OPENCV_EXTRA_MODULES_PATH
as I do in the CMake command in the tutorial.Hetansh
hey i cannot find the ./profile file in my /home/pi/ what can be the reason?
any help is appreciated thankyou.
Adrian Rosebrock
If it’s not there, simply create it.
john
how to create it?
Adrian Rosebrock
Create it/open it using your favorite text editor:
$ nano ~/.profile
Malik Mehboob
i have install successfully but when i open python 2 IDLE shell it gives error ; cv2 no module found :/ help me
Adrian Rosebrock
Please see the “Troubleshooting” section of this blog post where I discuss the vast majority of errors you will see when trying to import the cv2 module.
David Ferrara
Hi Adrian,
Thanks for the great tutorial. I’m facing the error when compiling in Python: ImportError: No module named cv2. I think my problem lies in the cmake part. When I examine the output of cmake, the Libraries says NO. Do you have any idea why this is, and how to fix it? I’ve tried doing it both with ON and OFF as you said in your update.
Thanks so much!
Adrian Rosebrock
Hey David — it’s hard to say without seeing your exact CMake output, but I discuss many of the errors related to importing OpenCV and compiling it in the “Troubleshooting” section of this blog post. If you are not getting the “Libraries” section automatically determined then I’m guessing that you’re not in the “cv” virtual environment before running CMake.
For what it’s worth, I also offer a downloadable Raspbian .img file with OpenCV pre-configured and pre-installed inside Practical Python and OpenCV. This would be a great way to get started learning OpenCV quickly. Just download the Raspbian .img file, flash it to your card, and boot.
Gabi
Hi,
Can I remove the opencv-3.1.0 after the build?
Adrian Rosebrock
Yes, once OpenCV has been successfully installed you can remove opencv-3.1.0 and the contrib directory as well.
vaishali
Hi,
I need to cross compile qt + opencv on raspberrypi 2B. I am able to cross compile simple qt code. but not with opencv. Can you please help..
judson antu
hey adrian, i followed your instructions and everything is ok. but i now dont want to use the virtual env. but now i cant import cv2 into my python idle. what should i do?
Adrian Rosebrock
If you don’t want to use your virtual environment you should copy the
cv2.so
file into the/usr/local/lib/python-X/site-packages
where you need to replace “X” with your Python version (2.7, 3.4, 3.5, etc.)judson antu
thanks adrian. that worked well. i have another doubt, is the ffmpeg file available with the installation. i am not able to capture video from a file. on my desktop running windows, all i had to do was to copy the dll to the python directory. but i cant file anything similar here. any help would be appreciated
Adrian Rosebrock
It’s hard to say without having access to your physical machine, but normally if you want to use FFMPEG you need to compile OpenCV with FFMPEG support enabled.
Sergio Ponce
Hi adrian, i have the same problema, how can i compile with support FFMPEG?
Thanks!
Adrian Rosebrock
You would need to enable FFMPEG via the
-D -WITH_FFMPEG=1
flag enabled. I would also suggest runningccmake
(note the two “c” characters in “ccmake”) to help you debug the configuration.Lina
Hello Adrian,
thank you for your great tutorial ^^
I am sorry to bother you, but could you please help me?
I already try this tutorial, and it went okay until step 11. then I found a problem, when I try using python and import cv2,
it said : ” ImportError: No module named cv2″
I read all comment and replies and found that I didn’t update my ~/.profile file. Then I try this tutorial from step 0 and I am stuck in step 7. I update my ~/.profile file using vi, also using nano and follow your video tutorial, but it’s said “no such file in directory”.
So I search it manually in my memory and found it in pi/home, so the file does exist. I don’t know why it’s said “no such file in directory”. I also try to remove the old ~/.profile file and make a new one and add the lines like step 7. but when I type source ~/.profile, it won’t work again.
could you please help me? I don’t know how to fix it. I am a beginner in linux and rapberry pi. And I already do this tutorial (from step 0) for a couple time and I found that some files are written double in my memory, is that okay? Or I need to erase the old installation before start a new one? But I don’t know how to erase it. please help me..
thank you before, thank you
Lina
Adrian Rosebrock
If you do not have a
~/.profile
file, that’s okay — create it and save it.Andrew
I would like to add my thanks to all of the others. This is a beautifully written guide and there is no way I would have done this without your help. Thank you so much for this.
Adrian Rosebrock
Thank you Andrew, I’m happy the tutorial helped you! 🙂
Chris Ryan
Hey great work.
I’m having one issue. Every time I open a terminal I try $ source ~/.profile and then $ workon cv but I’m getting “-bash: workon: command not found”.
Am I doing something wrong?
Adrian Rosebrock
Hey Chris — it sounds like your
~/.profile
file was not updated correctly. Please check the “Troubleshooting” section of this post for more details.Chris Ryan
Thanks. Think I messed that step up. Sorted now
Adrian Rosebrock
Awesome, glad to hear it 🙂
Sahin
Thank You very much. I’ve tried many tutorials before. and finally this worked.
Adrian Rosebrock
Congrats on getting OpenCV installed Sahin, nice job!
Rijul Paul
Hey Adrian,I am working with a Debian Jessie and trying to install opencv on that..Do you have any tutorial for that??
Adrian Rosebrock
If you are using Debian then follow this this Ubuntu tutorial. Ubuntu is Debian-based and all instructions will work on your system.
Jeff Ward
While building OpenCV 3.1.0, I encountered “…fatal error: hdf5.h: No such file or directory”.
To solve this problem, I manually applied the fix described here:
https://github.com/daveselinger/opencv/commit/0a776b08a738fdc3d6d5a59925ce56df757b8bc4
by adding
“`
find_package(HDF5)
include_directories(${HDF5_INCLUDE_DIRS})
“`
to the end of `/home/pi/opencv-3.1.0/modules/python/common.cmake`.
Strathy
I’m also having this issue with HDF5 … I’ve added the FIND and INCLUDE listed above, but it still will not compile.
I get:
CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
HDF5_C_INCLUDE_DIR
Adrian Rosebrock
Can you trying to disable HDF5 completely? Update your “cmake” command to include the following flag:
-D WITH_HDF5=OFF
İbrahim
Hi Adrian, thanks for this tutorial. However i get an issue when i try to compile. the “make” command gets me an error that is “No targets specified and no makefile found. Stop.” i searched but did not find any solution. What could be the problem?
Adrian Rosebrock
If you are getting this error it’s either because (1) you are not in the “build” directory when you executed “make” or (2) CMake exited with an error and you need to resolve the error first.
Usman Aftab
Hey Adrian
Thanks man for a detailed procedure for Opencv 3 install on jessie
I am working on a image processing project for that purpose i collected above 300 images for database but i am having trouble in making a database which will mach image taken through camera to the images in database someone suggested flann index Can you guide me in this regard
Adrian Rosebrock
I discuss how to create image datasets inside both Practical Python and OpenCV and the PyImageSearch Gurus course.
I’m not sure what your database looks like, but if you’re trying to perform keypoint matching, I would suggest reading through the final chapter of Practical Python and OpenCV. Otherwise, the PyImageSearch Gurus course demonstrates how to build an image search engine that can scale to millions of images.
Aman garg
hey adrian,
Thanks for the tutorial. It has been a great help.
Since, opencv has been installed using virtualenv, will it only work in the (cv) environment ? Is there any way that I don’t need to log into it? For example, if I need to run some scripts for opencv? will they always require (cv) ?
Adrian Rosebrock
You can still use OpenCV outside of the “cv” virtual environment provided that you have the
cv2.so
Python bindings in your systemsite-packages
directory.Tiberio Martinez
Hi Adrian,
Big thanks for the fantastic tutorial.
It worked like a charm!
Adrian Rosebrock
Great job getting OpenCV installed Tiberio!
Chase
I also have the “no module named cv2″problem. could you please give me a short test code working on Python3(IDLE) which can import cv2 perfectly. THX in advance.
Adrian Rosebrock
If you are having trouble importing the “cv2” library into your shell please see the “Troubleshooting” section of this guide. Also, make sure you are using the terminal/console version of IDLE rather than the GUI version of IDLE since the GUI version does not respect virtual environments.
joshua
failed to create symbolic link ‘cv2.so’: File exists
Adrian Rosebrock
If the file already exists you can (1) make sure that the sym-link is valid and import-able into Python or (2) delete the
cv2.so
sym-link and re-create it.Joe
Do you need to do “make” on the virual environment cv? thanks
Adrian Rosebrock
As long as you executed CMake in the virtual environment (and CMake exited without an error) you can run “make” outside the virtual environment if you wish.
Snigdh
hii i was trying to install it but i am getting no module named pip found error .i tried installing pip but it doesnt work. help me through.
Adrian Rosebrock
You should be able to install pip via:
Is there a particular error message you get when you try to run these commands?
movaid
Having the same problem. (Not a network issue, I’m certain)
I get the error:
Resolving bootstrap.pypa.io (bootstrap.pypa.io)… failed: Name or service not known.
wget: unable to resolve host address ‘bootstrap.pypa.io’
movaid
Fixed it:
Figured internet was working fine when pinging 8.8.8.8 worked fine. Realised it was a DNS error as pinging google.com failed.
To edit the DNS servers:
sudo nano /etc/resolv.conf
Add in the lines (can replace with any DNS server addresses):
nameserver 8.8.8.8
nameserver 8.8.4.4
Adrian Rosebrock
Congrats on resolving the issue, Movaid!
Adam
Hey Adrian. Great tutorial! You are very thorough and detailed. Everything worked smoothly for me.
Adam
Also, what size SD card are you using on your RPi?
Thanks!
Adrian Rosebrock
I’ve been using a 16GB card for the latest version of Raspbian. However, I’ve also used an 8GB card before without an issue. If you run into space issues make sure you delete the Wolfram Engine along with LibreOffice — that will free up ~1GB of space.
Dave Xanatos
Fantastic tutorial, thanks very much. I’m installing OpenCV 3.2.0 and I’m watching the results of the make -j4 command, and it’s about 46% complete, so far with only one error noted in two functions: “warning: array subscript is above array bounds [-Warray-bounds]” The module is contours.cpp under cv_surface_matching. I’m running Rasbian Jessie, did all the updates, on a Pi 3B+, and while I saw the errors, it seemed to not phase the installation any, it is proceeding smoothly as I type. Is this anything I should worry about, and if so, is there an easy way to fix the issue, or would I need to start from the beginning and reinstall opencv?
This is my first project on the RPi, having graduated up from microcontrollers like the PICs, Propeller and Basic Stamps. My goal is facial recognition and object tracking, as well as text recognition and reading, in a robotic project. Your tutorial is fantastic and has guided me perfectly (although I had to teach myself the vi editor again to edit ~./profile… last used vi in 1986 I believe!) Thanks very, very much for all the time and effort you have put into this. I will be visiting your kickstarter and getting that book!
Adrian Rosebrock
Hey Dave — how long has the command been stuck at the 46% point? If it’s been awhile (> 20 minutes) I would stop the compile, delete your “build” directory, re-create the “build” directory, re-run CMake, and then run “make” (without the -j4). This will avoid any race conditions that might be happening with your compile now.
Dave Xanatos
Hi Adrian, thanks for the really quick response! Actually, it zoomed along until 86% and hung building something from the non-photorealistic section of something. After about 45 minutes I figured it wasn’t coming back. Just deleted the build directory… starting back at CMake. Thanks very much, it’s great not feeling like I’m totally on my own, but it’s a blast learning all this new stuff.
Dave Xanatos
Installation of OpenCV 3.2 completed successfully, verified as described (import cv2, cv2.__version__ returns ‘3.2.0’ – THANK YOU!!!!! Just in time, UPS Tracking reports my first Pi Camera should be delivered here tomorrow. I am immensely happy with your tutorials here. My ultimate goal is stereoscopic vision on two Pis, with the ability to greet people by face recognition, control two “eyes” via servos, speak (via a small hardware text to speech system I already have a dozen of [V-Stamp]), respond to voice commands and queries, and read from text placed in the visual range. Just a “small” initial project 🙂 You’ve been an enormous help in getting this project out of the single-digits of probability and into the double digits! 🙂 Thanks again.
Adrian Rosebrock
Congrats on getting OpenCV installed Dave, awesome job!
Mesay
It’s a great tutorial..thanks!
Randy Cahya Wihandika
Great! This tutorial also works for OpenCV 3.2.0! Thank you!
Adrian Rosebrock
Yep, this blog post also works with OpenCV 3.2.0 as well! Congrats on getting OpenCV installed 🙂
Emrick
Hi Adrian, I have a problem with the installation. After completing all steps, when i try to import cv2 in my Virtual environment, i get an error : no module named ‘cv2’.
I checked the /.virtualenvs/cv/lib/python3.4/site-packages folder and i found the file cv2.so, but when i open it, i see that it is empty .. What do i have to do ?
Adrian Rosebrock
You mentioned that the file is “empty” — can you verify that your sym-link is pointing to a valid
cv2.so
file?Colin Rutter
Adrian, hi.
Thanks for the great tutorial. I have one issue however. At step 4 when i check the cmake output.
the Python 2.7 section has entries for the iterpreter only. There are no entries for libraries, numpy or packages !
The python 3 section is correct as per your post. I installed numpy within the cv environment.
what steps can i take to correct this cmake error or can i edit the cmake output file to add the correct paths before compile ? if so which file and where is it located ?
many thanks
Adrian Rosebrock
You can only compile OpenCV for Python for one Python version at a time. It sounds like you created a Python 3 virtual environment and are now trying to compile OpenCV for it?
larry barello
I successfully ran through the steps for py 2.7. I retraced back to make a virt env for py 3 (cv3), installed numpy then on step 4 executing cmake, the build script *always* comes up with py 2.7 as the target. It isn’t obvious, to me, what needs to be cleaned up to have both environments going at the same time (I’ll probably delete 2.7 – but I am just exercising my lame Linux skills working up to playing with the Pi2 camera)
I suppose I could just start over and build for the py3 target…
Otherwise, thanks for the great tutorial. So much to learn.
Adrian Rosebrock
The build script saying that Python 2.7 as the target is buggy — it’s not always correct. I’ve seen situations where it always reports Python 2.7 as the target build but provided your Python 3 section is correct, it builds Python 3 bindings.
larry barello
Ok, just rebuilt opencv (make clean, make build), then voila! the library was stuffed into /usr/local/lib/python3.4/site_packages/, a couple symlinks later my cv3 environment is up and running.
Question: do you need to rebuild all of opencv to make the python 3 bindings? or is there a shortcut…
Thanks!
Adrian Rosebrock
Congrats on getting OpenCV installed Larry, nice job!
As for rebuilding OpenCV, if you want to build new bindings for a different Python version you would indeed have to re-run CMake and make.
João Balão
Great tutorial Adrien! The steps were very clear and well written.
I only had some trouble when compiling the whole thing, somehow I never had enough space on my 8GB sd card to finish it successfully (I didn’t know it would require about 3GB of free space). I had to uninstall some applications, but in the end it was worth it.
I can finally start my projects, thanks a lot!
Adrian Rosebrock
Congrats on getting OpenCV installed João, nice job!
Kerem Kambur
Hi Adrian !
Quite useful and simplified guide thanks for this first of all ! I’ve tried lots of ways including your way but I couldn’t successfully installed face recognizer extra modules.. While I try to import cv2.createLBPHFaceRecognizer(), IDLE returns No Module Found. Is there anything additional I’ve to do after your guide ?
Thanks again.
Regards.
Adrian Rosebrock
I assume you’re using OpenCV 3 as detailed in the tutorial? If so, all face recognition code was re-organized into the
cv2.faces.*
sub-module.Babak
Great tutorial Adrien! . I followed every step and began to compile it with the command: “make -j4” and “make” It all went well until 87%. Now it is frozen and doesn’t continue anymore. it seems that can not build opencv extra modules. then I use this following cmake that used for runing matlab mfile on raspberry pi and successfully to end, is it usable? please guide me about this problem.
“cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D INSTALL_C_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_GTK=ON ..”
Adrian Rosebrock
It seems probable that there is some race condition in the compile using multiple cores. Try restarting the compile using only a single core:
This should resolve the issue.
Suganya
Hi Adrian
I am using Raspbian that was installed using NOOBs. I am struggling with OpenCV installation at cmake. Sometime it stops at 19% other time at 25%. Please tell me which of your tutorial I have to follow. I have wasted 5 days in this.
Adrian Rosebrock
It sounds like your Raspberry Pi may be running into race conditions when using multiple cores. Try compiling with only a single core:
That should resolve the issue.
Ali Essa
Hi Adrian,
I am using OpenCV for my school science fair project. I could not download the dev tools nor the image I/O packages. it gave me a warning that the packages cannot be authenticated!
do you have any idea how to fix this error ?
Adrian Rosebrock
It sounds like you might be using an older version of Raspbian or you haven’t ran
sudo apt-get update
to refresh your package definitions. If you’re still running into problems, take a look at the Quickstart Bundle and Hardcopy Bundle of Practical Python and OpenCV which include a pre-configured Raspbian .img with OpenCV pre-installed.Prathamesh Pattar
Hi Adrian,
I am trying to install Opencv on Raspberry Pi 3.
While going through all the steps the raspberry pi hangs at 87% while executing
“$ make -j4”
can you please help me in solving this?
Thanks in advance
Adrian Rosebrock
Please see my reply to “Babak” above.
joseph
Hello sir i came across a problem during installation of the opencv as the installation froze at 87% and i couldn’t do anything but switch my raspberry pi off and on and now am in the opencv and i tried “make -j4” again to start re-installation and i got an error “make: *** No targets specified and no makefile found. Stop”. please what would you suggest i do.
Adrian Rosebrock
If you are getting a “No targets specified and no makefile found” go back to the CMake step as there was an error running CMake.
Sergio Ponce
Hi!, i finished the installation and i now i want to play a video from ip camera but i can’t, do you know how can i do it? besides when i type import cv2 in Python IDLE this is not work
Adrian Rosebrock
Please see the previous comments on this post regarding IDLE. You need to be using the command line to access your virtual environment, not the GUI version of IDLE.
arani
Hi Adrian, how if the process take a long time than what you said? but it stuck in 87% since 6 hours ago. Its when I tried to compile openCV (make -j4). Iam using raspberry pi 3 model B. Is that an error? cause I cant do anything in my rasppi. and I dont know what should I do. Is that ok if I just turn the power off?
Adrian Rosebrock
Please see my reply to “Babak” above as I have already answered this question. Ctrl + c to quit the build. If the Pi is unresponsive you will have to do a hard shutdown.
noteven2degrees
Many thanks, Adrian, for the fantastic tutorial!
I successfully installed cv2 version 3.2.0 on my Raspbery 3 today. The onyly point in the workflow that I had to change was in Step #4:
After using cmake with all the params you specify, subsequent make wouldn’t find the makefile (though cmake completed w/o errors). I simply used cmake .. and was done.
sravan k
i’m stuck please help me
i could not run ‘make’ command it is showing this error
“make: * No rule to make target ‘clean’. Stop.”
Adrian Rosebrock
Your CMake command must have exited with an error. Double-check your CMake output and ensure it runs successfully.
Ashley
Hey Adrian,
When I look in my site-packages directory nothing is in there. Any suggestions?
I’m taking my first programming courses in college and am going to use OpenCV for my final project. I’d greatly appreciate your feedback, Thank you!
Adrian Rosebrock
Check to see if the
cv2.so
bindings are in yourbuild/lib
directory after compiling. If so, manually copy them to yoursite-packages
directory.Ashley
I previously tried to install OpenCV through another walkthrough and ended up doing something to the SD card that I guess was hindering it from compiling correctly. I got a new SD today, started from scratch, followed your instructions to the T, and am happy to say that I successfully installed it. Thanks so much, time to start this project!
Adrian Rosebrock
I’m glad to hear it Ashley, congrats on getting OpenCV installed!
rishabh goyal
hello
thank you for the tutorial with east steps though i am facing some problem while unziping the opencv.zip file.
i downloaded opencv3.2.0 and when i write ‘unzip opencv,zip’
an error occours which says
end-of-central-directory signature not found. either this file is not a zipfile,
or it constitutes one disk of a multi-part archive. in the latter case the central directory and zipfile comment will be found on the last disk of this archive.
unzip: cannot find zipfile directory in one of 3.2.0 or
3.2.0.zip.zip and cannot find 3.2.0.zip.Zip, period.
i have no idea what i did wrong
please help me…
Adrian Rosebrock
It sounds like there was an error downloading your .zip file. Please make sure you have a strong internet connection and then retrying. Otherwise, I notice you typed
unzip opencv,zip
(notice the comma instead of a period). If that is the case, correct the command.john
hi, thank you for your tutorial but i lost when i want to intsall numpy, before that i exit the command window. Anyone have the same problem? how to solve it?
Adrian Rosebrock
Just to clarify, you are having problems installing NumPy?
Ásgeir Örn Loftsson
Please say before the command that there are 2 options python 2 or 3, many geeks are to clever for reading the manual and just rush through the commands. great work and awsome instruction.
Peter Barsznica
Worked great!
Installed both versions of the bindings (under different virtual environments).
Used version 3.2.0: Works for python 2 and 3!
Thanks!
Adrian Rosebrock
Great job Peter, congrats!
Niranjan
Hello Adrian! Its a great tutorial! I am encountering an error before making opencv. Can you help me with that?
Here is the Screenshot of the terminal window
https://ibb.co/dkb6kk
Adrian Rosebrock
Take a look at your output from CMake — it is stating an error occurred during the configuration. Check your CMake output and find this error.
Pablo E.
Great guide Adrian! I had some problems using a 8Gb SD card, the compilation process ate all free space. I had to use a pen drive with enough space and it still building.
Thanks!
Adrian Rosebrock
Hi Pablo — if you are running out of space on your SD card I would also recommend removing the Wolfram Engine and LibreOffice. This will enable you to reclaim ~1GB of space.
Pablo H.
I made an idiots mistake, use FAT instead EXT. FAT doesn’t support symbolic links. I had to repeat all once again. It’s working! Thanks again Adrian.
Linar
Hello, I have installed the library as shown here, via SSH. Via SSH it works, but the console itself doesn’t want the Raspberry PI. How to fix it?
Adrian Rosebrock
It sounds like you aren’t
source
‘ing your.profile
. Open up a console and execute:And from there you should be able to access your Python + OpenCV bindings.
Norah
hey adrian
i’m running the latest version of raspian jessie on raspberry pi 2 v1.1
i followed ur tutorial on installing open cv on raspberry pi 3 but i can’t seem to get the same output as the screenshot you have included about the output after cmake i only seem to have nterpreter variable for python 2 section and all the other variables are shown in python3(although my environment is python 2)
and every time i compile opencv it gives me error 2 can’t find all. i have opencv and opencv contrib of the same version so what could be the problem??
Adrian Rosebrock
Hi Norah — it sounds like you may not be inside the “cv” virtual environment before running CMake. Make sure you are, then delete the “build” directory, re-create it, and re-run CMake.
If you continue to have problems with the OpenCV install, be sure to take a look at the Quickstart Bundle and Hardcopy Bundle of Practical Python and OpenCV which includes a Raspbian .img file pre-configued and pre-installed. Simply flash the .img file to your SD card, boot, and you’re ready to go!
oggy
i successfully reach to step 3 but skip the numpy installation(it took lon time so i aborted), and while compiling i paste the codes and it appears == bash: cmake: command not founf ==
also i turned c_examples=OFF but didnot work
Adrian Rosebrock
Make sure you have installed the CMake command on your system as this tutorial instructs:
$ sudo apt-get install build-essential git cmake pkg-config
Jason
Hey Adrian. I got to step 4 and type the cmake exactly as you have it. When I I was done I would get errors and could not run make -j4. After some time I found when I changed: OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.0.0/modules \
To:
OPENCV_EXTRA_MODULE_PATH=~/opencv_contrib-3.0.0/modules \
I made MODULES to MODULE. That returned no errors and was able to run make -j4. Is this ok to do? And hopefully this helps others who had problems
k.saikrishna reddy
what are all the changes to install opencv on raspberry pi 3.please forward a link if u have one.
Adrian Rosebrock
These instructions will work on the Raspberry Pi 3.
Michal Tkacz
I followed this tutorial on Pi Zero, but had no “Libraries”, “numpy” or “packages path” under “Python3” in the cmake output until I added “-D BUILD_opencv_python3=yes” to the cmake command (before final “..”!). Also I had to remove the build directory before rerunning cmake command, otherwise the output was still be incorrect. Finally I had to use raspi-config to temporarily lower the amount memory for the graphics chip to 16MB, otherwise make command would get stuck at ~83%.
Mathilde
Hello,
Thank you very much for this tutorial. I have the RPi3 and I just finished it and now I have two questions :
– How do I execute some python code ? (I mean, I can do a print or easy thing in the bash, but how to execute a big code ?
– Can I use this installation only with Python, or also with C or C++ for example ?
I am a noob on the RPi…
Adrian Rosebrock
Congrats on getting OpenCV installed on your machine. To answer your questions:
1. I would suggest putting your Python code into a file and executing it via
python your_filename.py
. If you need help getting started learning OpenCV and Python, be sure to go through Practical Python and OpenCV.2. You can also compile C/C++ programs that import OpenCV as well.
Chirantan Das
Hello! I just spent 3 hours installing cv2 on my RPi 2, but to no avail. Everything went very smoothly, even to the point of linking the cv2.so files.
But, when I am trying to import cv2, it is still showing an error.
I am distraught right now. There were no errors.
Any solution?
Chirantan Das
The only change that I made was naming my virtualenv differently.
Adrian Rosebrock
If you changed your virtual environment name, that’s okay, but you do need to smy-link the
cv2.so
files into whatever you named your virtual environment.Secondly, make sure you are using the “workon” command to access your virtual environment before importing the
cv2
library.Geoff Palmer
I too hit the dreaded “No space left on device” error on compiling OpenCV. Here’s how I found an extra 1.3GB on an 8GB card: http://www.tuxlove.co.nz/freeing-space-raspberry-pi/
Once you’ve freed some space, just restart the “make” and it’ll continue on from where it stopped — 30% in in my case
Adrian Rosebrock
In particular, I recommend using an 8GB card (or even better, 16GB), expanding the filesystem, and removing Wolfram Engine and LibreOffice (Option 3 and 4) from the link Geoff supplied.
Lucho
Thanks Adrian, your tutorials are very clean and easy to understand as usual!!!
Adrian Rosebrock
Thank you Lucho 🙂
luciano
For everybody having “ImportError: No module named cv2 error”
This was my error scenario:
– Verified I’m working on the cv virtual environment
– The sym-link is ok(pointing correctly to the /usr/local/lib/python3.4/site-packages
– The import works fine if you run python from the directory .virtualenvs/cv/lib/python3.4/site-packages but not from any other directory.
In my case I’m running python3 and I was able to fix this by installing virtual env with PIP3 instead of PIP(which is the default Raspberry python)
sudo pip3 install virtualenv virtualenvwrapper
Adrian, thanks for this tutorial!!! clean and perfect as usual!
Osama
when i write” cd /usr/local/lib/python3.4/site-packages
it gives the error” bash: cd: /usr/local/lib/python3.4/site-packages : no such file or directory”
Adrian Rosebrock
Double-check your Python 3 version. You might be using Python 3.5.
Osama
when i was writing cmake commands it shows this error ” Cmake error: the source directory “/home/pi/opencv-3.0.0/build/BUILD_EXAMPLES=ON” does not exist.
Specify –help for usage, or press the help button on the Cmake GUI
Adrian Rosebrock
It sounds like you’re copying and pasting the CMake command incorrectly. Go back and restart the CMake step, copying and pasting line-by-line.
Stephen Mann
Do you know, offhand, what version of OpenCV will be preinstalled with Raspian Stretch?
Adrian Rosebrock
Sorry, I do not know that off hand.
Cuningan
Hellol.
Did you have any problem with Pi 3?? i have done this in Pi B+ and all works but in Pi 3 with make -j4 the pi hangs at 87% and i do not know why…
I have orderer a anew Pi3 to test but maybe is not the Pi…
Adrian Rosebrock
It sounds like the
make -j4
make creating a race condition in the compile. Runmake clean
followed by justmake
. This will compile using a single core, which will take longer, but will ideally resolve the race condition.Allen
Hi Adrian,
a million thanks for your well laid out tuts. I’m a ghost student of yours, finally got opencv 3.0.0 with python 3.4 installed on my pi 3 after quite a while of not putting in good effort.
I look forward to many more successful tests and prototyping.
Thanks again.
Adrian Rosebrock
Congrats on getting OpenCV installed on your Raspberry Pi, Allen! Nice job.
Robert
You should include the command to run idle from the terminal once you are operating on the VM. Through searching the web i found “python -m idlelib.idle”. I’m not sure if i missed something in the tutorials or books so forgive me ahead of time.
Adrian Rosebrock
Hi Robert — I’m not sure what you mean by the “command to run idle from terminal”?
Amanda
I’ve had overheating issues both times I’ve done this. The last time it caused my pi to overheat and shutdown, this time i’m at 65% after the `make -j4` command and the CPU temp is 81.7c and has been over 80c for the last 25 minutes or so. this is with heat sinks and an open case. I’m hoping it won’t shut down this time, but I’m not sure if the problem is the pi or not.
Adrian Rosebrock
If you are running into heating issues just compile with one core:
The compile will take longer but will ideally avoid the overheating issue.
syed
i tried your method but error during compilation on make -j4 i tried it with several methods but it is throughing some errors can you help me cross this step
Adrian Rosebrock
Without knowing what your error message I cannot provide you with any suggestions.
juandedios
Good afternoon Adrian, first of all thank you for all the work you are doing, is impressive.
I’m new to Linux, but I’m trying to defend myself. My problem is that I’m installing OPENCV in Raspbian of Raspberry Pi 3 and when I get to the part of compiling OPENCV gives me an error in 41% with FFmpeg. I don’t know exactly how important it is, but I have a lot of interest in starting to investigate OpenCV from Python3.
Adrian Rosebrock
If you’re getting an error related to FFMPEG try compiling with the
-D WITH_FFMPEG=OFF
switch.Secondly, if you are new to Linux I would suggest you take a look at the Quickstart Bundle and Hardcopy Bundle of Practical Python and OpenCV. Both of these bundles include a pre-configured Rasbpian .img file that you simply flash to your SD card and boot. This .img file has OpenCV pre-installed. It’s by far the fastest way to get up and running with OpenCV and the Raspberry Pi.
It’s good that you’re getting into Linux, but if you’re new to it I would suggest starting with something a bit easier than installing OpenCV — going with the .img file is a great way to do this.
carlos guzman
Hello Adrian, thanks for your valuable contributions, please if you can help me, I am in a project and I need to start the Python script when I start the raspberry. I am using GPIO and OPENCV. The problem is that for it to work I need to enter the virtual environment of OPENCV with the commands. source ~ / .profile and workon cv.
How can I do this? The program works fine if I run from the terminal. But I do not know how to do it to start automatically.
Adrian Rosebrock
Hi Carlos — please take a look at this blog post where I demonstrate how to use GPIO and OpenCV together.
Carlos Guzman
Hi Adrian, thanks again. The project requires that it start automatically when you turn on the raspberri pi, I modify the profile file using (sudo nano / etc / profile) and add the name of my script (sudo python /home/pi/auto.py). In this way I check that the GPIO works well.
The problem is when I want to use OPENCV this does not work. I think that happens because I need to enter the virtual environment with the commands. source ~ / .profile and workon cv.
Then as I enter the virtual environment of OPENCV when I start the raspberri pi without having to do this from the terminal manually.
Adrian Rosebrock
Hi Carlos — I actually have a tutorial on running a script on reboot as well 😉
Fernando Pons
Hello Adrian,
Some programming experience but just begining with Python and RPI.
Great tutorial. Im trying to install OpenCV 3.3.0 on my Raspberry 3 on Raspbian jessie.
I created the virtual CV enviroment with Python 3, I can only access Python 3 from inside the enviroment so that’s working well.
Everything goes great until I step 4 when I compile OpenCv with CMake. I don’t get any errors but my screen doesn’t look like the one show where theres a segment describing the routes for Python2, Python3, etc.
Instead I just get the installation messages and the routes do have “cv” in them.
My problem comes when check the install (step 5) where all my files get generated in Python 2 folders, not Python 3. Can I simply copy them frm 1 file to the other?
Obviously I can’t import CV2 from my Python 3 inside cv.
Adrian Rosebrock
When you run CMake are you 100% sure you are in the “cv” virtual environment? CMake should automatically detect the virtual environment and then set your paths accordingly.
If you’re brand new to Python and the Raspberry Pi I would suggest that you take a look at the Hardcopy Bundle and Quickstart bundle of Practical Python and OpenCV. Both of these bundles include a Raspbian .img file with OpenCV pre-configured and pre-installed. You simply have to flash the .img to your SD card and boot! From there OpenCV will be ready to go.
Matthew Black
Hi Adrian, thanks for the tutorial it has been very helpful. I’m trying to implement background subtraction and haven’t been successful so far. I think the module I need is called bgsegm but I am not sure how to install that with the opencv I already have installed. Any help would be much appreciated.
Adrian Rosebrock
I’m not sure on the exact module you are referring to, but you should take a look at the
cv2.GrabCut
function which is implemented in the OpenCV core.Matthew Black
bgsegm just has a few different background subtraction functions, I found out about it in this tutorial:
http://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_video/py_bg_subtraction/py_bg_subtraction.html
Thanks for the reply I’ll check out cv2.Grabcut.
Carlos Guzman
Hi Adrian, I have a problem with the command (sudo crontab -e), I get the following (no crontab for root – using an empty one 888), I found a crontab file in (sudo / nano / etc / crontab), the content is not the one you indicate in the tutorial.
I Already create a file (OneReboot.sh) and it works fine if I run from the terminal, the GPIO, and OPENCV works fine, but the configuration is needed when it reboot. Adrián thanks again for his tutorials and his help.
Adrian Rosebrock
Hi Carlos — unfortunately I’m not sure what the error is here. The issue seems to be specifically related to crontab, but other than that I’m not sure what the exact problem is without physical access to your machine.
Carlos Guzman
Hi – Adrián thanks, I tell you that I followed a comment from (Hai June 26, 2016) in the link that you indicated (( running a script on reboot), and already solve the problem, in this case I do not use crontab. Thank you very much for your help.
Ian Maynard
Awesome tutorial Adrian. I made it to the end and installed it correctly (I think, based on your tutorial) into python 3.4. But when I tried to run a program with python 3.4, it pops error message ” ImportError : no module named ‘cv2’ . That program runs well in python 2.7. I wonder where did I go wrong?
Adrian Rosebrock
If you are trying to install OpenCV for BOTH Python 2.7 and Python 3 then you’ll need to compile OpenCV twice. Once for Python 2.7. And then again for Python 3. You cannot use the same OpenCV install for both Python versions.
Beabars Abaza
Hello Adrian, thank you for this great tutorial! worked all the steps as mentioned and successfully installed opencv.
I’m just having a tiny problem with the virtual environment, when I’m rebooting or using a new terminal window, the command “workon cv” isn’t working also after reloading with “source ~/.profile”, but it works when I run these commands:
# virtualenv and virtualenvwrapper
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh
I’m noob at this, is there any way I could skip doing this and does it affect any data while working on a project?
Adrian Rosebrock
It sounds like you may have not updated your
~/.profile
correctly. Open it up and ensure the virtualenv/virtualenvwrapper commands are there.Beabars Abaza
I ran all the steps again and it worked, it seems when installing or updating some packages some files weren’t installed (I have a bad internet connection) so I made sure to keep watching if there are any missing files and ran the commands and installed everything, eventually it worked.
Thank you for this tutorial!
Adrian Rosebrock
Congrats on getting OpenCV installed, nice job! 🙂
yazan
it make error in -j4 amd in make
what i do ?
Adrian Rosebrock
What is the error you are getting? If you do not share the error myself and other readers cannot help you.
spincraft
Thanks for posting this up.
Where can I post my log file?
I got errors when I tried to compile cv.
Step #4
Thanks!
Hafsah
“Q. When I open up a Python shell and type import cv2 , I get the dreaded ImportError: No module named cv2 error.”
i have other solution for this
try type this in your terminal
$ sudo apt-get install python-opencv
you can see the explain in
https://askubuntu.com/questions/895400/i-installed-python-cv-but-error-showing-no-module-named-cv2
i hope this can help more ppl’s
sorry i had bad gramer ^_^
Adrian Rosebrock
Hi Hafsah — Thanks for the comment. It looks like you didn’t properly link the cv2.so file into your virtual environment. In any case, I’m glad that you were able to install OpenCV using an alternate method. In general, I don’t use or recommend the Debian repository OpenCV because they don’t stay up to date with the latest version, but as you said, this solution will work too.
JoeYang
Hi, Adrian:
Have you published this code on github? https://medium.com/@anant.bhatia92/raspberyy-pi-opencv-door-access-control-surviellance-and-alert-system-7501cb79fa2 ,
or we have to buy your book to get that code ?
Thanks
Joe
Adrian Rosebrock
I didn’t write the blog post that you are linking to. If you’re interested in face recognition, take a look at the PyImageSearch Gurus course.
Brian
Hi, I’m almost there, but I got this:
from /home/pi/opencv-3.0.0/build/modules/core/precomp.hpp:49:
/usr/include/c++/6/cmath:45:23: fatal error: math.h: No such file or directory
#include_next
^
Please advise
Adrian Rosebrock
This seems like an issue related to the precompiled headers. Delete your “build” directory, re-create it, and then update your
cmake
command to include the following switch:-D ENABLE_PRECOMPILED_HEADERS=OFF
This should resolve the issue.
Vikas Kumar Singh
tried still getting same error
riel
hi it worked already. thank you 🙂
its so nice to have your website as my referrence.
keep it up and i hope this website will further grow 🙂
Clara Körting
Hi Vikas,
Did you happen to find a way to avoid the error? I’m at the same step now and tried all the troubleshooting advise I could find in the comments but nothing seemed to help.
Thank you for any ideas!
Clara
riel
hi im getting the same error. i hope there’s a way to resolved this.
alifsuria
this doesnt work..i have do this a couple of time..it just show fatal error at #include
Adrian Rosebrock
Delete your “build” directory. Re-create the “build” directory. Re-run cmake with the
-D ENABLE_PRECOMPILED_HEADERS=OFF
flag. Then run “make”.Matt
Hi Adrian, I am getting the fatal error: In-source builds are not allowed.
I have a build directory in my opencv directory. I am calling the cmake command from the build directory, and am definitely using “..” in the command.
Hopefully you can help, and happy new year!
Thanks, Matt.
nivsan
$ make -j4
hai sir.i used above command in rasberrypi 3 after that i get below error
/usr/include/c++/6/cstdlib:75:25: fatal error: stdlib.h: No such file or directory
#include_next
^
shall you tell solutions sir
Adrian Rosebrock
Please see my reply to Brian on November 30, 2017.
Florian
Hey Adrian, thanks a lot for your tutorial.
I also wanted to reply some further informations about some issues i had. While i was working first on your opencv 2 tutorial i found the link for this opencv3 tutorial. I wanted to switch to the new version without setting up a new raspbian. This leads to some troubles in the virtual cv environment. I found out, in step 4, that i couldn’t see the right python 3 settings in the cmake output. Of course i didn’t deleted my cmake setup and the new cmake call didn’t run over all points.
For me it worked to delete the build folder and follow the steps again, now i need to finish the new build and try out the new opencv setup.
Hope this helps some other people.
Vikas Kumar Singh
Hey while processing the command “make -j4 ” the process got terminated saying fatal error: math,.h : no such file or directory . Please suggest how to resolve it.
Roshan
Hi, I am having error like this /usr/include/c++/6/cmath:45:23: fatal error: math.h: No such file or directory in running the command make….I am using raspberrypi zero
please help me to overcome the error.
Adrian Rosebrock
Please see my reply to “Brian” on November 30, 2017. In general I would recommend ctrl + f and then searching the page for your error message.
Secondly, I do not recommend the Raspberry Pi Zero for computer vision. In most situations it’s far too underpowered. I would recommend a Pi 3 if at all possible.
ali
hello adrian ,when i copy & paste make j4 on rapberry terminal ,h don know why the terminal closed after a few miniutes ,know what i shoud to do?
ali
Hello Adrian thank you for this tutorial , Ifolowing the video but when i arived step 4 and i whrite make -j4 in termival , after a one minituse the terminal closed
Adrian Rosebrock
Hey Ali — can you elaborate more on what you mean by the terminal closed? Did it automatically close and the Pi reboot? Were you SSH’d in to your machine? Did you accidentally close the terminal?
ali
i use the raspberry pi3 ,and that closed automatically after 2% procsecing .
Adrian Rosebrock
I’m still not sure what you mean by “closed”. Please be more specific — keep in mind that myself and others cannot help you if you do not provide more details. Did the Pi just shut off totally? Did the terminal automatically exit?
Dan
It appears that the url for Step 2 is no longer valid (I do not see an archive folder). I have no idea how to use github.com, but I am trying, how do I get past Step 2?
Adrian Rosebrock
Hey Dan — I just checked the URL and it worked for me. Also try the official OpenCV repo (OpenCV wasn’t always on GitHub which is why Itseez posted it):
https://github.com/opencv/opencv/archive/3.0.0.zip
https://github.com/opencv/opencv_contrib/archive/3.0.0.zip
swapnil
$ 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-3.0.0/modules \
-D BUILD_EXAMPLES=ON ..
this code returns “bash: cmake: command not found”
Adrian Rosebrock
Make sure you install “cmake” as we do in the first “apt-get install” command from Step 1:
$ sudo apt-get install build-essential git cmake pkg-config
Victoria
I’m getting the following error when executing the cmake command:
-bash: /usr/bin/cmake: cannot execute binary file: Exec format error
I also made sure that “cmake” was installed.
Adrian Rosebrock
Perhaps share a screenshot of your terminal? It seems like you likely copying and pasting one of the commands incorrectly.
Ricky
Hi Adrian, Thanks a lot, I can installed perfectly fine.
One question though, where should I place my python project folder? inside the cv folder?
Thankss!!
Adrian Rosebrock
You can place your Python code anywhere on your system. Just make sure you are in the “cv” Python virtual environment before executing it.
sha93
hey Adrian thank you very much for the tutorial! i have a question, where have you saved the file demo.py?
Adrian Rosebrock
You can save it anywhere on your system that you like. The location where it is saved does not matter. Download the file, “cd” to the directory you downloaded it to, and execute it.
sha93
hey Adrian, sorry if the question is silly, but my problem is, In “python demo.py”, is “python” the folder name? “demo.py” is inside the folder? and where should i place the folder?
Adrian Rosebrock
“python” is the name of the binary you are executing and “demo.py” is the path to the input script. I would also suggest reading up on the command line and command line arguments before continuing.
Kavesh
I am in step 4 when I write the cmake commands.. It returns
Cmake error at Cmakelists.txt.1:
Parse error. Expected a command name, got unqouted argument with text
Abhishek pandey
Hey adrian, I have installed opencv-3.2.0 on rpi zero w, when i run import cv2 command in IDLE it restarts the shell in both python 2 & 3. When i run the same command in termianl under python environment the result is ‘illegal instruction’. What should i do to rectify this problem?
Adrian Rosebrock
Are you using the GUI version of IDLE? Keep in mind that IDLE will not respect the Python virtual environment — you will need to use the command line instead. Try using the command line and see if leads to the same error.
Shane Angle
from /home/pi/opencv-3.0.0/build/modules/imgcodecs/precomp.hpp:45:
/usr/include/c++/6/cmath:45:23: fatal error: math.h: No such file or directory
#include_next
this is when I make-j4 or j1
What did I do wrong?
Adrian Rosebrock
Hey Shane — I’ve answered this question a few times in the comments thread. The gist is that you need to update your cmake command to turn off pre-compiled headers by adding the following switch:
-D ENABLE_PRECOMPILED_HEADERS=OFF
Do a ctrl + f and search for
ENABLE_PRECOMPILED_HEADERS
text to find the other comments.Adrian Rosebrock
Make sure you a do a “ctrl + f” search on this page and look for “precompiled headers”. I’ve addressed this question before.
The gist is that you need to add the following switch to your cmake command:
-D ENABLE_PRECOMPILED_HEADERS=OFF
And then recompile. But make sure you read the other comments first.
Levi
Hi Adrian,
I did what you mentioned in the previous posts (precompiled hearders off) and still noto working.
Any idea?
Adrian Rosebrock
Hi Levi — send me an email and include the stacktrace of any errors!
Madhu
Hi Adrian,
I am closely following this article and trying to setup with my RPI3+. Initially got the math.h error and it got fixed with your additional switch; but now it stopped the cmake by getting error at 26% for the file /cap_ffmpeg.cpp:45:0… as ” error” ‘acvodec_free_frame’ was not declared in this scope avcodec_free_frame(&picture);
Appreciate your help.
Thanks,
Madhu
Adrian Rosebrock
This sounds like it may be an FFMPEG issue. Try adding an additional switch to your “cmake” command to turn off FFMPEG support:
-D WITH_FFMPEG=OFF
Ramkumar
thanks for all your directions –
robert
Hello, my terminal show this error:
/usr/include/c++/6/cstdlib:75:25: fatal error stdlib.h
How i can fix it?
thank you…
Adrian Rosebrock
Please see my reply to Brian on November 30, 2017.
agilan
HI ADRIAN
We try to install opencv in raspberry pi 3 (using raspbian jessie).
we stuck at step 4. the terminal hang at 87 percentage. we try so many times it still hang at 87 persentage. what is the solution to overcome this problem?
Adrian Rosebrock
It sounds like your Raspberry Pi is either (1) running into a race condition and hanging or (2) running out of swap. I would suggest compiling with just a single core via “make” (no “-j4”). It will take longer but the compile should work. You may also want to try increasing your swap size as I do in this guide.
MaxT
Hi Adrian,
First of all thanks so much for putting this together, really appreciate it,
Now, for the life of me, I cannot get this to build, I am stuck at the build part (make), I tried make -j4, tried make -j2 and tried make only no extra cores, it always gets up to a certain percentage then freezes up, the closest I got to completing my build is 86%. I followed your other thread and also increased my swap to 2048, still to no avail… I’m using an RPI 3 B and opencv 4.1.0
Any ideas? what can I do?
Thanks
Adrian Rosebrock
If you’re having trouble compiling OpenCV from source I would recommend doing a pip install of OpenCV.