21

Today I installed opencv 2.4.4 to Ubuntu 12.10

But import cv2 not works.

root@-:~# python
Python 2.7.3 (default, Sep 26 2012, 21:53:58) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named cv2
>>> 

As I understand cv2.so missed, so python don't see where opencv

root@-:~# find / -name "cv.py"
/root/opencv-2.4.4/modules/python/src2/cv.py
root@-:~# find / -name "cv2.so"
root@-:~#

My setup steps look like

wget http://downloads.sourceforge.net/project/opencvlibrary/opencv-unix/2.4.4/OpenCV-2.4.4a.tar.bz2
tar -xjf OpenCV-2.4.4a.tar.bz2
cd opencv-2.4.4
mkdir release
cd release 
cmake -D CMAKE_BUILD_TYPE=RELEASE   -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_PYTHON_SUPPORT=ON ..
make && make install
echo "/usr/local/lib" >> /etc/ld.so.conf.d/opencv.conf
ldconfig  
echo "PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig" >> /etc/bash.bashrc
echo "export PKG_CONFIG_PATH" >> /etc/bash.bashrc

Where is cv2.so ? And why it was missed ?

Alex
  • 1,397
  • 4
  • 15
  • 26
  • Does Cmake correctly detect python on your system? Cmake gives you a pretty long screen output after you call it. – Acorbe Apr 03 '13 at 14:47
  • It should likely be `libcv2.so`, not `cv2.so` (probably located under `/usr/local/lib`) – devnull Apr 03 '13 at 14:49
  • Make sure to `sudo apt-get install python-dev`, `sudo apt-get upgrade cmake`, and inspect the `cmake` output manually to make sure the Python library is found (should match `which python`); if not found, see [this thread](http://stackoverflow.com/questions/24174394/cmake-is-not-able-to-find-python-libraries) for troubleshooting. – user2398029 Feb 08 '16 at 04:51
  • ln -s /usr/local/lib/python3.5/dist-packages/cv2.cpython-35m-x86_64-linux-gnu.so cv2.so – Alex Punnen May 28 '17 at 01:24

9 Answers9

47

How to install opencv(cv2) with python bindings in Linux - Ubuntu/Fedora

  1. Install gcc, g++/gcc-c++, cmake (apt-get or yum, in case of yum use gcc-c++)

    apt-get install gcc, g++, cmake
    
  2. Downlaod latest opencv from openCV's website

  3. Untar it with

    tar -xvf opencv-*
    
  4. Inside the untarred folder make a new folder called release

    mkdir release
    cd release
    

    (or any folder name) and run this command in it

    cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_NEW_PYTHON_SUPPORT=ON -D BUILD_EXAMPLES=ON ..
    

    the .. will pull files from the parents folder and will get the system ready for installation on your platform.

  5. in the release folder run

    make
    
  6. After about 2-3 mins of make processing when its finished run

    sudo make install
    
  7. Export python path

    export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python2.7/site-packages
    

That's it, now go to python and try

>>> import cv2

you should not get any error message.

Tested on python 2.7, should be virtually similar to python 3.x.

Hussain
  • 5,057
  • 6
  • 45
  • 71
Waqas
  • 3,763
  • 1
  • 30
  • 18
  • 2
    Also make sure that the python development headers (sudo apt-get install python-dev) are installed. – honederr82 Jun 28 '13 at 09:23
  • 3
    This didn't work for me on Mac OSX, Python 2.7. I don't see errors at any stage but still can't >>>import cv2 – rd108 Sep 22 '13 at 11:18
  • This also didn't work for me on RHEL6, Python 2.7 set up as a virtual environment. I don't see errors at any stage but still can't import cv2. I dont have cv2.so anywhere on my computer after apparently successful installation. @rd108, did you figure out any solution? – rivu Feb 14 '14 at 18:02
  • 2
    you need to export the Path where Python libraries are installed, like export PYTHONPATH=~/projects/opencv/release/lib:$PYTHONPATH – aqavi_paracha Feb 25 '14 at 05:38
  • 1
    BUILD_NEW_PYTHON_SUPPORT=ON is apparently no longer used. – user2398029 Feb 08 '16 at 04:52
  • I needed to `sudo make install` on the 6th step. Thanks! – Shon Jun 22 '16 at 21:51
  • `BUILD_EXAMPLES=ON` is required to build `cv2.so`. Surprisingly. – Kurt Dec 07 '16 at 22:31
11

I install python-opencv to solve my problem in Ubuntu 14.04 sh sudo apt-get install python-opencv

Wei Yang
  • 199
  • 1
  • 4
  • 1
    OMG Why didn't anyone say that????? So much easier! This provided me opencv 2.4.1, Thanks! – George Jul 11 '16 at 13:02
  • This is indeed a good choice. Users who want the latest versiokn of openCV still need to build the project. – DSdavidDS Aug 04 '17 at 03:21
5

Using raspbian on a rasberry pi I had the problem of the module not being found also. I had three versions of python (2.6, 2.7, and 3.2), be sure you are using python2.7. You can check this by running:

python --version

I found that for my case it was simply that I needed to install python-dev.

sudo apt-get install python-dev

I did Not have to remove and reinstall opencv, I tried my hardest to avoid that, knowing that it takes a few hours to complete the process.

After installing python-dev I went to the file I built the opencv into, for me it was " ~/opencv-2.4.9/release", and told it to make

sudo make

after this I was able to find the cv2.so file. searching for it with:

find / -name "cv2.so"

at this point I found a few files. next I ran just the python to see if it could find "import" them

python 
>>> import cv2

no errors should come up.

>>> import numpy

I have heard that numpy was necessary for opencv to run. From there I believe you should be good to run your script, if no errors come about. I hope this helps.

The page that helped me is listed...

http://opencv-users.1802565.n2.nabble.com/I-can-t-find-cv-so-and-cv2-so-after-compiling-td6671937.html

durkinza
  • 85
  • 1
  • 4
3

I had a similar problem when I manually configured using CMAKE on OSX El Capitan. I had given this additional option:

PYTHON2_PACKAGES_PATH='lib/python2.7/site-packages'

which stopped the cv2.so in that package from getting installed. It seems to install properly in my build folder after I removed it:

PYTHON2_EXECUTABLE='/usr/bin/python2.7'
PYTHON2_INCLUDE_DIR='/usr/include/python2.7'
PYTHON2_LIBRARY='/usr/lib/libpython2.7.dylib' # TODO - Fix for linux
PYTHON2_NUMPY_INCLUDE_DIRS='/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/core/include' # Todo - Fix for linux


cd $OPENCV_DIR

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=$OPENCV_INSTALL_PATH \
                                  -D WITH_CUDA=OFF \
                                  -D BUILD_opencv_python2:BOOL=ON \
                                  -D PYTHON2_EXECUTABLE=$PYTHON2_EXECUTABLE \
                                  -D PYTHON2_INCLUDE_DIR=$PYTHON2_INCLUDE_DIR \
                                  -D PYTHON2_LIBRARY=$PYTHON2_LIBRARY \
                                  -D PYTHON2_NUMPY_INCLUDE_DIRS=$PYTHON2_NUMPY_INCLUDE_DIRS \
                                  -D INSTALL_PYTHON_EXAMPLES:BOOL=ON \
                                  ..
make -j8
make install
ssk
  • 9,045
  • 26
  • 96
  • 169
2

I have this problem in my OS X El Capitan.

I followed the instructions mentioned in this tutorial. Didn't get a successful working install and had the above error of missing cv2.so file in the required folders mentioned and at the python prompt.

Finally figured that using the virtual python setup was causing trouble. So uninstalled with

pip install virtualenv virtualenvwrapper

Then ran

brew link opencv

which threw errors.

And then followed below steps to resolve the issue.

First run

brew link opencv

If it gives an error, try for an automated diagnosis

brew doctor

brew doctor gives a list of problems that could be leading to errors in installation process.

To fix problems in case of conflicting files, run to get a list of all actions which will be performed by overwrite without actually performing them.

To list all files that would be deleted:

  brew link --overwrite --dry-run opencv

followed by this run which will execute the overwrite, assuming you feel that the actions performed by overwrite will take your system to a more stable state.

To force the link and overwrite all conflicting files:

 brew link --overwrite opencv

This tutorial is a simpler alternative.

Vinay Vemula
  • 3,855
  • 1
  • 21
  • 24
2

None of the above worked for me; am in Ubuntu 16.04 on an ec2 instance & had anaconda installed so I just used

conda install opencv for both my conda2 and 3 installs

E. Case
  • 67
  • 1
  • 2
  • 11
2

All above answers did not work for me, however, after a whole day struggling, I finally solved this problem.

To have cv2.so, we need:

  1. At least python 2 or 3 installed. that why people say: sudo apt-get install python-dev. But that's not necessary, in my case, I use anaconda python. (there are many ways to install python)
  2. numpy is also a must. so, whatever python you use, just make sure you have it downloaded. In my case, I use anaconda numpy. (anaconda have it installed already, for normal python, use pip install numpy)

To tell camke where the path is, just take my command as an example:

cmake -D CMAKE_BUILD_TYPE=RELEASE \
      -D CMAKE_INSTALL_PREFIX=/usr/local \
      -D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules \
      -D PYTHON2_EXECUTABLE='/home/parallels/anaconda2/bin/python' \
      -D PYTHON2_LIBRARY='/home/parallels/anaconda2/lib/python2.7' \
      -D PYTHON2_NUMPY_INCLUDE_DIRS='/home/parallels/anaconda2/lib/python2.7/site-packages/numpy/core/include' \
      -D BUILD_EXAMPLES=ON ..

for python3,you should (I'm using anaconda python, so I linked everything to anaconda):

cmake -D CMAKE_BUILD_TYPE=Release \
      -D CMAKE_INSTALL_PREFIX=/usr/local \
      -D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib-3.3.1/modules \
      -D PYTHON3_EXECUTABLE='/home/test/SoftWare/anaconda3/bin/python3.6m' \
      -D PYTHON_INCLUDE_DIR='/home/test/SoftWare/anaconda3/include/python3.6m' \
      -D PYTHON3_LIBRARY='/home/test/SoftWare/anaconda3/lib/libpython3.6m.so' \
      -D PYTHON3_NUMPY_INCLUDE_DIRS='/home/test/SoftWare/anaconda3/lib/python3.6/site-packages/numpy/core/include' \
      -D PYTHON3_PACKAGES_PATH='/home/test/SoftWare/anaconda3/lib/python3.6/site-packages' ..

One thing to remember!!! before you enter cmake ... 1. clean your build folder, 2. Only camke once! otherwise you can not change ** PYTHON3_LIBRARY: NO**...(this is a bug I think)

I know there might be some useless arguments, but I am tired to try to clean them. Here's a screenshot of my cmake print info. screenshot of my cmake info

You can clearly see that, only python2 can generate cv2.so. python3 can not! (Python3 wrappers can not be generated).

0

I came across similar problem. After digging into this a little more I came across a post where it was mentioned that python-numpy package was required. So, I uninstalled the opencv by running the following command in build folder(in your case release folder):

dpkg -r build

Then I removed all the opencv files. I installed python-numpy and python-dev with this command:

sudo apt-get install python-dev python-numpy

Then, after re-running the installation script, import cv2 command in python console doesn't give me any error and is properly imported.

skadoosh
  • 471
  • 1
  • 6
  • 15
0

In my case it was a problem with cmake:

sudo apt install software-properties-common
sudo add-apt-repository ppa:george-edison55/cmake-3.x
sudo apt update

If cmake is not yet installed:

sudo apt install cmake

If cmake is already installed:

sudo apt upgrade

For more information, see this link.

galoget
  • 722
  • 9
  • 15