Why cv2.so missing after opencv installed?

2019-01-08 11:26发布

问题:

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 ?

回答1:

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.



回答2:

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



回答3:

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



回答4:

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.



回答5:

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


回答6:

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



回答7:

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.



回答8:

in my case it was a problem with cmake

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

If cmake is not yet installed:
sudo apt-get install cmake
If cmake is already installed: ' sudo apt-get upgrade `

for more information



回答9:

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).



标签: opencv