如何在macos上使用opencv和python

时间:2012-10-26 09:07:58

标签: python macos opencv osx-snow-leopard

我正在移植一个在Windows上开发的python + opencv应用程序到mac。

在Windows上,我的virtualenv网站包中有cv2.pyd,它很顺利。

我是mac新手,我不明白它应该有效。我没有找到任何opencv python绑定的二进制文件,似乎正在使用macport。

macport正在复制opts /.../ site-packages中的cv.py和cv2.so。

此时如何使用它?

我尝试手动复制这两个文件,但收到错误

>>> import cv
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/cv.py", line 1, in <module>
    from cv2.cv import *
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/cv2.so, 2): no suitable image found.  Did find:
        /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/cv2.so: mach-o, but wrong architecture

怎么了?

更新: 正如@Vortexfive所说:

$ file /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/cv2.so
/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/cv2.so: Mach-O 64-bit dynamically linked shared library x86_64

$ file /Library/Frameworks/Python.framework/Versions/2.6/bin/python
/Library/Frameworks/Python.framework/Versions/2.6/bin/python: Mach-O universal binary with 2 architectures
/Library/Frameworks/Python.framework/Versions/2.6/bin/python (for architecture ppc):    Mach-O executable ppc
/Library/Frameworks/Python.framework/Versions/2.6/bin/python (for architecture i386):   Mach-O executable i386

似乎存在不匹配。如何解决?

2 个答案:

答案 0 :(得分:1)

目标体系结构似乎不匹配。您可以尝试使用+ universal变体在macports中安装opencv。

另一种选择是更新您的Python版本。我相信也会为x86_64编译更新的版本。

作为第三种选择,您可以在macports中使用+ python27安装opencv。然后,您可以通过sudo port select python27选择Python的macports版本。

答案 1 :(得分:1)

在我的情况下,拱门匹配:

$ file ../../release.mac/lib/cv2.so 
../../release.mac/lib/cv2.so: Mach-O 64-bit dynamically linked shared library x86_64
$ file `which python`
/usr/bin/python: Mach-O universal binary with 2 architectures
/usr/bin/python (for architecture x86_64):  Mach-O 64-bit executable x86_64
/usr/bin/python (for architecture i386):    Mach-O executable i386

但不知道python是以32位模式运行的:

$ defaults read com.apple.versioner.python Prefer-32-Bit
1

修正:

$ VERSIONER_PYTHON_PREFER_32_BIT=0 python

$ defaults write com.apple.versioner.python Prefer-32-Bit -bool no