Why cv2.so missing after opencv installed?

2019-01-08 10:51发布

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 ?

标签: opencv
9条回答
别忘想泡老子
2楼-- · 2019-01-08 11:32

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.

查看更多
Emotional °昔
3楼-- · 2019-01-08 11:32

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

查看更多
做个烂人
4楼-- · 2019-01-08 11:33

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

查看更多
劳资没心,怎么记你
5楼-- · 2019-01-08 11:41

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

查看更多
干净又极端
6楼-- · 2019-01-08 11:44

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

查看更多
女痞
7楼-- · 2019-01-08 11:45

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.

查看更多
登录 后发表回答