Search code examples
pythonmacosopencvosx-snow-leopard

How to use opencv with python on macos


I am porting a python+opencv app developped on windows to mac.

On windows i have the cv2.pyd in my virtualenv site-packages and it goes well.

I am new to mac and I don't understand hot it should work. I didn't find any binaries for opencv python bindings and it seems that the rightway is to use macport.

macport is copying the cv.py and cv2.so in the opts/.../site-packages.

At this point how to use it?

I tried to copy manually these two files but I am getting an error

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

What's wrong?

Update: As asked by @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

It seems there is a mismatch. How to fix it?


Solution

  • It seems there's a mismatch in target architectures. You can try to install opencv in macports with the +universal variant.

    Another option is to update your version of Python. Newer versions are compiled for x86_64 too I believe.

    As a third alternative you can install opencv with the +python27 in macports. You can then select the macports version of Python by doing sudo port select python27.