简体   繁体   English

使用Python 3的opencv 3.0.0 alpha无法导入cv2

[英]opencv 3.0.0 alpha with Python 3 failed to import cv2

I'm using Anaconda 2.1.0 with python 3.4.1 and just built opencv 3.0.0-alpha on Mac OSX Yosemite with following command: 我正在使用带有python 3.4.1的Anaconda 2.1.0,并在Mac OSX Yosemite上使用以下命令构建了opencv 3.0.0-alpha:

cmake -D CMAKE_BUILD_TYPE=RELEASE 
-D BUILD_PERF_TESTS=OFF 
-D BUILD_opencv_python3=ON 
-D CMAKE_OSX_ARCHITECTURES=x86_64 
-D CMAKE_INSTALL_PREFIX=/usr/local 
-D PYTHON3_EXECUTABLE=${ANACONDA}/bin/python3 
-D PYTHON3_LIBRARY=${ANACONDA}/lib/libpython3.4m.dylib 
-D PYTHON3_INCLUDE_DIR=${ANACONDA}/include/python3.4m 
-D PYTHON3_NUMPY_INCLUDE_DIRS=${ANACONDA}/lib/python3.4/site-packages/numpy/core/include 
-D PYTHON3_PACKAGES_PATH=${ANACONDA}/lib/python3.4/site-packages ..

The building seems successful. 这座建筑似乎很成功。 I got libopencv_*.dylib files after building opencv. 构建opencv后我得到了libopencv_*.dylib文件。 but I can't import cv2 module: 但我无法导入cv2模块:

Python 3.4.1 |Anaconda 2.1.0 (x86_64)| (default, Sep 10 2014, 17:24:09)
[GCC 4.2.1 (Apple Inc. build 5577)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: dlopen(/Users/scari/anaconda3/lib/python3.4/site-packages/cv2.so, 2): Library not loaded: libpython3.4m.dylib
  Referenced from: /Users/scari/anaconda3/lib/python3.4/site-packages/cv2.so
  Reason: image not found
>>>

Here's sys.path output: 这是sys.path输出:

>>> sys.path
['', '/Users/scari/anaconda3/lib/python34.zip', '/Users/scari/anaconda3/lib/python3.4', '/Users/scari/anaconda3/lib/python3.4/plat-darwin', '/Users/scari/anaconda3/lib/python3.4/lib-dynload', '/Users/scari/anaconda3/lib/python3.4/site-packages', '/Users/scari/anaconda3/lib/python3.4/site-packages/Sphinx-1.2.3-py3.4.egg', '/Users/scari/anaconda3/lib/python3.4/site-packages/runipy-0.1.1-py3.4.egg', '/Users/scari/anaconda3/lib/python3.4/site-packages/setuptools-5.8-py3.4.egg']

What should I check first? 我应该先检查一下?

OK, I had exactly the same problem. 好的,我有完全相同的问题。

I fixed it thanks to this tutorial : https://gist.github.com/welch/6468594 由于本教程,我修复了它: https//gist.github.com/welch/6468594

libpython3.4m.dylib is not found so you should check cv2.so with this command : 找不到libpython3.4m.dylib所以你应该用这个命令检查cv2.so

otool -L ~/anaconda3/lib/python3.4/site-packages/cv2.so

The first couple of lines of the output should read something like: 输出的前几行应该是这样的:

cv2.so:
cv2.so (compatibility version 0.0.0, current version 0.0.0)
libpython3.4m.dylib (compatibility version 3.4.0, current version 3.4.0)

The problem is the absence of absolute path for libpython3.4m.dylib 问题是缺少libpython3.4m.dylib的绝对路径

You can fix this with the command : 您可以使用以下命令解决此问题:

sudo install_name_tool -change libpython3.4m.dylib ~/anaconda3/lib/libpython3.4m.dylib ~/anaconda3/lib/python3.4/site-packages/cv2.so

This worked for me to use opencv 3 with python 3 in an anaconda ipython notebook 这对我来说在anaconda ipython笔记本中使用opencv 3和python 3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM