Last week we kicked-off the OpenCV 3.0 install fest by detailing. Today we are going to continue the OpenCV 3.0 install instruction series by moving over to the Ubuntu operating system. In the remainder of the post I will provide instructions on how to configure and install OpenCV 3.0 and Python 2.7+ on Ubuntu. I have personally tested these instructions on Ubuntu 14.04, but they should pretty much work on any Debian-based operating system. A quick note before we get started: Yes, OpenCV 3.0 is indeed compatible with Python 3+.
However, the install instructions are slightly different between Python 2.7+ and Python 3+. In an effort to keep each article self-contained and easy to follow, I am creating separate OpenCV 3.0 install tutorials for Python 2.7 and Python 3+.
If you would like to use OpenCV 3.0 and Python 3+ on your Ubuntu system, please keep an eye on this blog — I will be posting OpenCV 3.0 and Python 3+ install instructions later this month. But for the time being, let’s stick with Python 2.7. How to Install OpenCV 3.0 and Python 2.7+ on Ubuntu UPDATE: The tutorial you are reading now covers how to install OpenCV 3.0 with Python 2.7+ bindings on Ubuntu 14.04.
This tutorial still works perfectly, but if you want to install OpenCV on the newer Ubuntu 16.04 with OpenCV 3.1 and Python 2.7 (or Python 3.5) bindings, please use this freshly updated tutorial: This is the second article in the OpenCV 3.0 install-fest series. Last week we covered how to. Today we are going to perform the same OpenCV 3.0 and Python 2.7 installation, only on the Ubuntu operating system. In general, you should find installing OpenCV 3.0 and Python 2.7+ on Ubuntu much easier than installing on OSX.
Step 1: Open up a terminal and update the apt - get package manager followed by upgrading any pre-installed packages. $ git checkout 3.0.0 Update (3 January 2016): You can replace the 3.0.0 version with whatever the current release is (as of right now, it’s 3.1.0 ). Be sure to check for information on the latest release.
As I mentioned, we also need the as well. Without this repository, we won’t have access to standard keypoint detectors and local invariant descriptors (such as SIFT, SURF, etc.) that were available in the OpenCV 2.4.X version. We’ll also be missing out on some of the newer OpenCV 3.0 features like text detection in natural images. D BUILDEXAMPLES = ON.
Update (3 January 2016): In order to build OpenCV 3.1.0, you need to set - D INSTALLCEXAMPLES = 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. Notice how compared to our CMake command is substantially less verbose and requires less manual tweaking — this is because CMake is able to better automatically tune our install parameters (at least compared to OSX). Now we can finally compile OpenCV. Figure 4: We have successfully detected the red game cartridge in the image! Notice how our script was able to successfully detect the red game cartridge in the right portion of the image, followed by drawing a green bounding box surrounding it. Obviously this isn’t the most exciting example in the world — but it has demonstrated that we have OpenCV 3.0 with Python 2.7+ bindings up and running on our Ubuntu system!
So, what’s next? You have a brand new, fresh install of OpenCV on your Ubuntu system — and I’m sure you’re just itching to leverage your new install to build some awesome computer vision apps. But I’m also willing to bet that you’re just getting started learning computer vision and OpenCV, and you’re probably feeling a bit confused and overwhelmed on where exactly to start. Personally, I’m a big fan of learning by example, so a good first step would be to read on accessing your webcam with OpenCV. This tutorial details the exact steps you need to take to (1) capture photos from your webcam and (2) access the raw video stream. And if you’re really interested in leveling-up your computer vision skills, you should definitely check out my book,. My book not only covers the basics of computer vision and image processing, but also teaches you how to solve real world computer vision problems including face detection in images and video streams, object tracking in video, and handwriting recognition.
So let’s put your fresh install of OpenCV on your Ubuntu system to good use — to learn more about the real-world projects you can solve using your new OpenCV install + Practical Python and OpenCV. Summary To celebrate the OpenCV 3.0 release, I am working my way through OpenCV 3.0 and Python 2.7/Python 3.4 installation instructions on OSX, Ubuntu, and the Raspberry Pi. Last week I covered how to.
And today we covered how to install OpenCV 3.0 with Python 2.7 bindings on Ubuntu. I have personally tested these instructions on my own Ubuntu 14.04 machine, but they should work on any Debian-based system. Next week we’ll continue the install-fest and hop back to OSX — this time installing OpenCV 3.0 and Python 3! This will be the first time we’ve used Python 3 on the PyImageSearch blog, so you won’t want to miss it! And please consider subscribing to the PyImageSearch Newsletter by entering your email address in the form below. As we work through the OpenCV install-fest, I’ll be sending out updates as each new OpenCV 3.0 + Python install tutorial is released! I’m constantly struggling with “Missing PythonLibraries” for both the 2.7 and 3.x using an Xubuntu 14.04 LTS.
Development packages for both Python versions are installed. Funny thing is that every second time I run cmake. In the build directory of OpenCV 3’s source directory I get that the libraries for Python 2.7 are found. When I get that I’m also able to build the Python libs for 2.7. For Python 3.x not the same luck sadly. After two months still unable to compile the damn libs hence I’m unable to properly use OpenCV 3 in Python 3. Thanks for an excellent tutorial!
I’m having a samilar problem on Ubuntu 14.04. I’ve solved it by doing a “sudo cp lib/cv2.so /usr/local/lib/python2.7/dist-packages/” – it does feel like a hack and it would be nice to understand why this issue appears. It seems that cmake doesn’t find the python2.7 libraries. I think the problem in my case is that it fails to find the python libraries, I get the: Could NOT find PythonLibs (missing: PYTHONINCLUDEDIRS) (found suitable exact version “2.7.6”) The part corresponding to you screenshot is just “Libraries: No” for the Python 2 section. I’ve also noticed that instead of site-packages it uses the dist-packages. From googling I’ve found a few hints (the best one is probably ) but I’m not familiar enough with cmake to start changing the script and the python-dev is alreade installed.
I installed opencv3 directly to the virtual enviroment so I can keep opencv3 on “cv” and leave opencv2 on the “gurus” enviroment as we move through the pyimagesearch gurus course. Just wanted to share the code here if someone ends up needing it. Cmake -D CMAKEBUILDTYPE=RELEASE -D CMAKEINSTALLPREFIX=$VIRTUALENV/local/ -D INSTALLCEXAMPLES=ON -D INSTALLPYTHONEXAMPLES=ON -D OPENCVEXTRAMODULESPATH=/opencvcontrib/modules -D PYTHONEXECUTABLE=$VIRTUALENV/bin/python -D PYTHONPACKAGESPATH=$VIRTUALENV/lib/python2.7/site-packages -D BUILDEXAMPLES=ON. Just remember to delete CMakeCache.txt if you’ve ran cmake before and to run “workon cv” before running this cmake code. Hello Adrian, I’m not sure if it’s my install, but I performed all the steps, and it looks like cv2 can’t find the reference to the xfeatures2d package Could you check if it’s ok on your PC? Here is what I got: (cv)seb@seb-VirtualBox:/tst-opencv$ python Python 2.7.6 (default, Jun 22 2015, 17:58:13) GCC 4.8.2 on linux2 Type “help”, “copyright”, “credits” or “license” for more information.
import cv2 cv2.version ‘3.0.0’ detector = cv2.xfeatures2d.SIFTcreate Traceback (most recent call last): File “”, line 1, in AttributeError: ‘module’ object has no attribute ‘xfeatures2d’. I got through most of this tutorial fine, but I’m having a problem on the make step.
I get about 40% compiled before getting this error. Make2:. No rule to make target ‘/usr/include/./lib/libavcodec.a’, needed by ‘lib/libopencvvideoio.so.3.0.0’. Make2:.
/usr/bin/env: Node : No Such File Or Directory
Waiting for unfinished jobs. 42% Building CXX object modules/videoio/CMakeFiles/opencvvideoio.dir/src/capffmpeg.cpp.o CMakeFiles/Makefile2:5408: recipe for target ‘modules/videoio/CMakeFiles/opencvvideoio.dir/all’ failed make1:. modules/videoio/CMakeFiles/opencvvideoio.dir/all Error 2 make1:. Waiting for unfinished jobs. It’s having problems making the libavcodec, and I’ve tried several things, including install ffmpeg and other dev packages, but none of them seem to fix the missing rule.
I’ve also re-run the cmake command to see if it makes any difference, but no dice. When going through the tutorial, I did notice that libtiff4-dev is no longer available and has been replaced with libtiff5-dev. I’ve seen some boards suggesting one of the other packages removed features that libavcodec depends on, but I haven’t had any luck fixing it. Any suggestions for how to modify the make command or other packages to install to get over this hump? Back again almost 10 months later. I recently got back into the idea of using OpenCV, after being unsuccessful building it back in August.
I’ve been able to build OpenCV 3.1.0 and use it with FFMPEG turned off, but I’m trying to do the 10-day crash course now, which requires video features. The issue is the same one as before, and despite three solid days of trying different solutions, the problem is still libavcodec.a not having a rule. I’ve posted the problem to the OpenCV board, but at this point, my options are to hope someone gives an answer that apparently doesn’t exist anywhere on the internet, or I find a way to compile OpenCV’s video features without FFMPEG.
Hi Adrian, Thanks for the super clear tutorial. The normal libraries of openCV work fine, but whenever I try to use something from the opencvcontrib repository like SIFT, it says “AttributeError: ‘module’ object has no attribute ‘SIFT'”. Doing a quick dir(cv2) affirms that the functions aren’t there. Do you know what could have gone wrong?
I followed all the steps as best I could. I did notice that when I ran the cmake command it couldn’t find a few packages, but I ran all the apt-get installs as instructed.
Hi Adrian, I was able to install OpenCV and Python with contrib modules but it seems that some contrib modules are missing. For example “text” module seems to be incomplete. This is the output of the command help(“cv2.text”). I was expected to find methods like “loadClassifierNM1”, etc. Any help is appreciated.
Thanks Adrian Help on module cv2.text in cv2: NAME cv2.text FILE (built-in) DATA ERFILTERNMIHSGRAD = 1 ERFILTERNMIHSGrad = 1 ERFILTERNMRGBLGRAD = 0 ERFILTERNMRGBLGrad = 0 ERGROUPINGORIENTATIONANY = 1 ERGROUPINGORIENTATIONHORIZ = 0 OCRDECODERVITERBI = 0 OCRLEVELTEXTLINE = 1 OCRLEVELWORD = 0 (END). Hello May I update my earlier question. I get this at step 10: (cv)john@john-Precision-T1700:/opencvcontrib/build$ cmake -D CMAKEBUILDTYPE=RELEASE D CMAKEINSTALLPREFIX=/usr/local D INSTALLCEXAMPLES=ON D INSTALLPYTHONEXAMPLES=ON D OPENCVEXTRAMODULESPATH=/opencvcontrib/modules D BUILDEXAMPLES=ON.
CMake Error: The source directory “/home/john/opencvcontrib” does not appear to contain CMakeLists.txt. Specify –help for usage, or press the help button on the CMake GUI.
(cv)john@john-Precision-T1700:/opencvcontrib/build$ Any idea what could be ailing me? Hi, I am new to python,opencv. I followed these instructions and installed opencv,python on my machine(unix). It created two main folders opencv and opencv-contrib with many files and sub dirs. I started saving my source files in opencv folder.
Initially simple programs were executing perfectly but now i see for imports like import matplotlib.pyplot as plt import numpy as np import scipy.signal as signal etc, it keeps giving ImportError, “eg:ImportError: No module named matplotlib.pyplot” for many such imports. I then have to google and find the solutions.
I then install the missing module again in the current dir. Exactly which directory should I work into, to avoid this problem? P.S- I understand that this might be a very basic question, but i will greatly appreciate your help.
I am just a beginner student here and for now I am getting so loaded with my assignments that I am not getting time to figure out the problem and its solution by myself, trying to implement algorithms from my assignments is eating up all my time. I followed all the steps you told above. After installing, in am in the cv environment and it successfully imports cv2. I exit the terminal and then open it again and type import cv2 It gives me an error saying cv2 not defined also if i type workon cv it gives me an error saying workon not defined.
I followed each and every step. I hope you can help me out on this one! Thanks Also can this be installed without the virtual env? I really dont want to type workon cv again and again. Can’t we directly install it on the ubuntu system? Nice tutorial, Adrian. If you’re using virtual environments (very reasonable), why do you install the library globally (‘sudo make install’)?
Wouldn’t it be more sensible to keep it in the build directory and point the virtual environment there? Ultimately the reason I ask is because I’d like to have OpenCV 2.x side-by-side with OpenCV 3.x, and it seems to me that virtual environments are the right way to do it, but it also seems to me that installing the lib globally is not conducive to this effort. Also, am having this problem: Once it worked and I closed my terminal window and opened a new one and I type “workon cv”,”python”,”import cv2″ I get errors such as not found or no command, I realised I may not be in the correct directory, can you help me how to open it? As am using Ubuntu for the first time I don’t have much idea Thanks! Hello, I have found the solution to my problem, I have seen that many people are facing the same problem and thought I should share Once you open the terminal window start from step 8 with “$ sudo rm -rf /.cache/pip” and follow all the commands upto ” mkvirtualenv cv” Then you enter cv envirnoment and you can open python and import cv. Actually, those old cv2.cv bindings pre-date OpenCV 2.4!
They are seriously old. I personally have tried this, but you should be able to compile OpenCV with the old functionality as well. Go back to the CMake command and try adding -BUILDopencvworld=ON to he command. The “world” references the old legeacy cv2.cv bindings.
If that switch doesn’t work, try running ccmake. From the build directory and enabling anything related to the “world”. Otherwise, you should consider getting an OpenCV 2.4.X installation going. OpenCV 2.4 still has support for the super old bindings. Drawboard pdf with keygen.
The point of using Python virtual environments is to have an isolated environment for each project you are working on. For example, suppose you are working on a project that requires package X v1.0. But another project you are working on requires package X v2.0. What do you do? Buy another machine?
Install a VM? Instead, you use virtual environments. Each Python virtual environment is entirely isolated from the system install of Python, therefore you can install different versions of Python packages without having to worry about conflicts. In either case, you need to execute the workon command before running your Python script to drop you into the cv virtual environment. Inside this virtual environment, the OpenCV bindings can be found and recognized.
Hi man thanks for the tutorial(and all the other). I have followed all the steps and redone them as well but no luck. When I import cv2 it says no such file. So I checked with sudo find / -name “cv2.s0” and it says its in my home/mathys/.virtualenvs/cv/lib/python2.7/site-packages/cv2.so but when I go into that file it is empty, theres no cv2.so Another thing as you mentioned earlier to check my cmake output under python2 everything looks the same as the sreenshot you gave a link too but where it says libraries it says no instead of something like usr/lib/libpython2.7.so Many thanks Mathys. Hi Adrian, Awesome tutorial but I ran into a snag.
Regardless of what I do, querying the opencv version yields 2.4.8!!! After some digging it turned out that Linux Mint 17.3 Cinnamon have a dependency on opencv, so opencv 2.4.8 was already installed by system. Uninstalling opencv (2.4.8) = remove Cinnamon (among others as well, e.g.
Blender) as well. Cinnamon is Mint’s Desktop Environment, so removing it is not an option. So, here is what I did and it seems to be working so far ? 1. In step 10, in the cmake command, change the installation directory to a local folder, e.g. Use “-D CMAKEINSTALLPREFIX=/home//local ” 2.
Then use “make install”, i.e. Cd to the installation folder and search for “cv2.so” via: $ find./ -iname cv2.so 4. Copy the resulting folder’s path (e.g. /home//local/lib/python2.7/dist-packages) and add it to $PYTHONPATH by doing the following: – Use your favorite text editor to open/create /.profile – Add the line: export PYTHONPATH=$PYTHONPATH:/home//local/lib/python2.7/dist-packages – Save & exit, then $ source /.profile 5.
Loading the cv2 module in Python & checking version now yields 3.1.0, while system dependence on opencv 2.4.8 is uninterrupted. You can still use these same instructions to install OpenCV with Python bindings without the virtual environment. Just skip Step #8 and Step #11. Also, take note of which site-packages directory the output cv2.so bindings will be stored. That will help debug any issues when it comes to import cv2. Alternatively, if you need to call a Python script via a separate application, you can just as easily call a shell script form a separate application.
The shell script can them access the Python virtual environment and then call the Python script,. Hello Adrian!
So I have run this whole tutorial around 5 times now and I am almost certain I did not miss any steps. I watched the other video from your tutorial with the Pi and viewed the troubleshooting section of that page. However, I cant seem to make the error go away.
ImportError: No module named cv2 when i go to /.virtualenvs/cv/lib/python2.7/site-packages/ there is a cv2.so, when I double click it it says “This link cannot be used because its target “/usr/local/lib/python2.7/dist-packages/cv2.so” doesn’t exist.” The /usr/local/lib/python2.7/site-packages directory is always empty. I think it should have the cv2.so file there or something, but it doesn’t have it. I am not sure what I may b doing wrong.
I am fairly new to linux so my lingo is still new, I will try to understand your explanation. Thank you for your time.
Thanks for this easy to follow tutorial. Just to add a little on it if I could. After following all with no Error whatsoever I started having the following error while trying to follow the simple example at the end: (pyhton findgame.py) ImportError: No module named cv2 It happened for the simple reason that, as a beginner, I failed to understand how to update the /.bashrc file The line at Step 8 are supposed to be added to the /.bashrc file. Not to be copy and paste in the terminal which I did in my first attempt. You should copy those lines use the following line to open the /.bashrc file $ sudo nano /.bashrc Copy and paste the code there and source the /.bashrc file. This is how I solved the “ImportError: No module named cv2” Thanks Adrian. Hi Adrian, I am current setting up multiple virtual environments for different configuration combinations (i.e., OpenCV 3 with Python 2.7 and OpenCV 3 with Python 3.6).
I followed the tutorial; everything was good until I built the OpenCV library. The problem is that CMake was not able to pick up the correct Python interpreter in my virtual environment. I force to set it to /home/username/.virtualenvs/cvpy2/bin/python, and this time it was able to find the correct interpreter, but the wrong Numpy include path. I am curious about the Cmake working mechanism and why it didn’t find the correct path and interpreter for me automatically. Is that a problem of my PATH and PYTHONPATY?
Hi Adrin, Its a very nice tutorial for biggners ike me. It helped me alot foe installing and configuring opencv and python on my ubuntu 12.04 pc. Now the issue is when i execute a program findgame.py i am getting a part of output (window size is small) insted of detecting the right portion of the image, followed by drawing a green bounding box surrounding it. The output window is covering only the1/4 of input image instaed of total image. Even for video related programs I am getting a small window instead of getting full window. So, how to rectify this issue Thanks Sandy.
Regarding your first error message, I’m not sure what the exact problem is. It sounds like you may have (somehow) introduced strange aliases to the deactivate and workon commands.
I would suggest re-installing from scratch on a fresh Ubuntu system if you can. Secondly, I would also recommend that you take a look at the Quickstart Bundle and Hardcopy Bundle of my book, which includes a pre-configured Ubuntu VM with OpenCV pre-installed.
This is by far the fastest way to get up and running with OpenCV + Python. Anaconda + Linux If you already have anaconda, use it instead to create / activate workspaces: Step 8: – Create: conda create –name cv python=2.7 – Activate: source activate cv Step 11 will not work, 2 possible causes. 1) The cv2.so is not where it should. (this wasn’t my case) but you can search the file with: sudo find /usr -name “cv2.so” 2) Find the location of conda environment conda info –envs Now: cd (result of 2) # This should work now: ln -s /usr/local/lib/python2.7/dist-packages/cv2.so cv2.so ^ If the file is not in there then replace: ln -s (result of 1) cv2.so After that everything was ok. Hi, just did it!
I think i didnt execute perfectly all the steps and found the same error as other people when i was to import cv2.so in python! Searching in the folders i noticed that cv2.so wasnt in ‘site-packages’ folder but in the ‘dist-packages’ one. Basically i think i messed up using the virtual environment due tu i was following the ubuntu 16.04 guide at first then switched to this. I was able to import cv2.so and run correctly the findgames.py example by avoiding the virtual environment. I am new to linux, hope i wont find any problems in future cause of my imperfect installation procedure.
Hi, Adrian Very good article on OpenCV installation. I have followed all of your steps and it’s working perfectly fine in my virtual environment.
But, I wanted to use Jupyter Notebook for writing my python codes for my research work and I am facing issues to import cv2 inside Jupyter notebook. Can you please help me how to import cv2 inside Jupyter notebook. As far as I know, I am facing problem because of sudo access given in your blog for my virtual environment. I am trying this since October but couldn’t figure out. Any help would be really appreciated. I have tried this page for setting up my Jupyter notebook and I am able to make new kernel for opencv but it doesn’t import opencv.
I tried to install OpenCV through Homebrew by following the steps given in but after installing virtualenv and virtualenvwrapper I updated the path as given. When I source it I get the following -bash: /usr/local/bin/virtualenvwrapper.sh: No such file or directory I have macOS Sierra 10.12.6, Xcode 8.3.3. Then I closed the Terminal and reopened it.
Since then whenever I open the Terminal I see the same line in the second line of the terminal. I made a small change and installed virtualenv using pip -user install virtualenv virtualenvwrapper. @klanomath thank you for acknowledging me about the help vampire. See i was just tired of getting homebrew installed in my mac after trying it for 5 days to work on my project. And i'm new to linux/mac.
Also never used terminal very extensively to install packages or creating virtual environments. So when i cannot do it, i thought some ppl (who are experts) here might had same issue like me and fixed them, and was just in a small hope to tell me how to fix it.
But it looks like i'm a help vampire. THANK YOU, sorry for the troubles. – Sep 19 '17 at 18:47.
(and everyone who comes across this bug in the future) These explain why: (only read this last thread if you're a masochist) As a Node developer, I would think hard before using Debian's packaged version of node, and would use Ubuntu with chrislea's PPA for Node or Debian stable with Node built from source in preference to the stock Debian nodejs package for the foreseeable future. The discussion on makes clear that Debian has a very different idea of the role of Node in the wider world than most of the Node community. (and everyone who comes across this bug in the future) These explain why: (only read this last thread if you're a masochist) As a Node developer, I would think hard before using Debian's packaged version of node, and would use Ubuntu with chrislea's PPA for Node or Debian stable with Node built from source in preference to the stock Debian nodejs package for the foreseeable future. The discussion on makes clear that Debian has a very different idea of the role of Node in the wider world than most of the Node community.
Welcome to LinuxQuestions.org, a friendly and active Linux Community. You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features.
Registration is quick, simple and absolutely free. Note that registered members see fewer ads, and ContentLink is completely disabled once you log in. Are you new to LinuxQuestions.org? Visit the following links: If you have any problems with the registration process or your account login, please. If you need to reset your password,.
Having a problem logging in? Please visit to clear all LQ-related cookies. Introduction to Linux - A Hands on Guide This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter. For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own. To receive this Complete Guide absolutely free.
Unfortunately I am not able to actually display these commands or output due to certain circumstances unless I personally modify all the details out which would be believed to make the information useless in the first place. It is simply being executed itself with '$./FILENAME', '$SUDO./FILENAME ' or '$/path/to/directory' and I am simply being given the generic bash, ' No such file or directory ' although it does display and is existant. Due to these circumstances is why I more or less requested general possibilities.
I do enjoy the idea of the possibility of special-character or white-space. Could this have anything to do between Arch, such as running a 64-bit execute without 64-bit libraries? Is there a way to tell which ARCH the file type is? Quote: Unfortunately I am not able to actually display these commands or output due to certain circumstances unless I personally modify all the details out which would be believed to make the information useless in the first place. It is simply being executed itself with '$./FILENAME', '$SUDO./FILENAME ' or '$/path/to/directory' and I am simply being given the generic bash, Has anyone here ever known a 'branded' (non-generic) bash? One clear specific information about the problem is worth better than a thousand blahs of gorgeous flowery talk.
Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |