birdy
birdy

Reputation: 9636

error importing opencv in python

I installed opencv using these commands on my mac:

$ wget http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.4.3/OpenCV-2.4.3.tar.bz2
$ tar xjvf OpenCV-2.4.3.tar.bz2
$ cd OpenCV-2.4.3
$ mkdir build; cd build
$ cmake -G "Unix Makefiles" -D CMAKE_OSX_ARCHITECTURES=i386 -D CMAKE_C_FLAGS=-m32 -D CMAKE_CXX_FLAGS=-m32 ..
$ make -j4
$ sudo make install

then I tried to use it by importing it but got the error below:

~/opencv-2.4.4/build $ python
Python 2.7.3 (default, Mar 28 2013, 14:31:14) 
[GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.66))] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/cv.py", line 3, in <module>
    from cv2.cv import *
ImportError: dlopen(/usr/local/lib/python2.7/site-packages/cv2.so, 2): no suitable image found.  Did find:
    /usr/local/lib/python2.7/site-packages/cv2.so: mach-o, but wrong architecture

Updates

While compiling OpenCV I noticed that its picking up python libraries from a previous version (one installed by apple):

-- Could NOT find PythonLibs: Found unsuitable version "2.7.2", but required is exact version "2.7.3" (found /usr/lib/libpython2.7.dylib)

--   Python:
--     Interpreter:                 /usr/local/bin/python2 (ver 2.7.3)
--     Libraries:                   /usr/lib/libpython2.7.dylib (ver 2.7.2)
--     numpy:                       /usr/local/lib/python2.7/site-packages/numpy/core/include (ver 1.7.0)
--     packages path:               lib/python2.7/site-packages

How do I fix this?

Upvotes: 2

Views: 3648

Answers (2)

julien lacour
julien lacour

Reputation: 1

From my experience, (arm-linux-gcc (uclibc) cross-compilation from debian),

Python was trying to load the right file with dlopen (/usr/lib/python2.7/site-packages/cv2.so), but I got "File not found" whereas file exists.

On my board :

$ python
>>> import cv2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: File not found

In fact, the dynamic load was trying to load recursively dependant libraries (of course). However, in my case, libstdc++ was not embed on my board whereas opencv needed it :

Host side:

$ arm-linux-readelf -d ./lib/cv2.so 
Dynamic section at offset 0x24197c contains 23 entries:
Tag        Type                         Name/Value
0x00000001 (NEEDED)                     Shared library: [libpython2.7.so.1.0]
0x00000001 (NEEDED)                     Shared library: [libstdc++.so.6]
0x00000001 (NEEDED)                     Shared library: [libgcc_s.so.1]
0x00000001 (NEEDED)                     Shared library: [libc.so.0]*

I hope it can help.

Upvotes: 0

abarnert
abarnert

Reputation: 365587

The error tells you that the problem is "mach-o, but wrong architecture". That generally means that you're trying to load a 32-bit library in a 64-bit app, or vice-versa (or similarly, for Intel vs. PowerPC).

Whichever Python you've installed, it's likely that you got a universal build that can run as either a 32-bit app or a 64-bit app, but by default it runs as 64-bit. But you're explicitly building OpenCV for 32-bit:

$ cmake -G "Unix Makefiles" -D CMAKE_OSX_ARCHITECTURES=i386 -D CMAKE_C_FLAGS=-m32 -D CMAKE_CXX_FLAGS=-m32 ..

If that was a mistake, just rebuild and don't do that.

If you want it to be 32-bit, you can start a Universal python in 32-bit mode, or install a 32-bit Python.


Meanwhile, you're trying to run this with a Homebrew-installed Python 2.7.3, but there's a good chance that you're building it against Apple's pre-installed 2.7.2 (or, worse, partially building it one way, partially another).

First, the usual way people add Homebrew to their PATH doesn't affect any commands run with sudo, so if sudo make install does anything to find your Python, it will likely find a different one than cmake and make did.

Second, both Apple's Python and Homebrew's think they own /usr/local/lib/python2.7 (and also think they have exclusive rights to install tools/scripts like /usr/local/bin/pip-2.7), which makes this even more painful.

Upvotes: 1

Related Questions