cv2.so with python2.7 and 3.4

asked 2015-12-15 10:47:37 -0500

Terry gravatar image

updated 2015-12-16 08:10:59 -0500

boaz001 gravatar image

Hi, I built cv2.so for python2.7 and python3.4 in raspbian. They both work and I can do import cv2.

However, when I wanted to understand where all the binaries are installed by cmake, I noticed something strange and I didn't understand.

Both py2.7 and py3.4 cv2.so files are depending on the same shared objects. The following is the ldd output

    $ldd py27cv2.so  #python 2.7 
    linux-vdso.so.1 (0x7ee2d000)
    /usr/lib/arm-linux-gnueabihf/libarmmem.so (0x76e66000)
    **libpython2.7.so.1.0 => /usr/lib/arm-linux-gnueabihf/libpython2.7.so.1.0 (0x76b2f000)        **
    libopencv_core.so.3.0 => /lib/libopencv_core.so.3.0 (0x7691c000)
    libopencv_flann.so.3.0 => /lib/libopencv_flann.so.3.0 (0x768c9000)
    libopencv_imgproc.so.3.0 => /lib/libopencv_imgproc.so.3.0 (0x765f8000)
    libopencv_ml.so.3.0 => /lib/libopencv_ml.so.3.0 (0x76561000)


    $ldd py34cv2.so #python 3.4         
    linux-vdso.so.1 (0x7ef97000)
    /usr/lib/arm-linux-gnueabihf/libarmmem.so (0x76dce000)
    **libpython3.4m.so.1.0 => /usr/lib/arm-linux-gnueabihf/libpython3.4m.so.1.0 (0x769fe000)        **
    libopencv_core.so.3.0 => /lib/libopencv_core.so.3.0 (0x767eb000)
    libopencv_flann.so.3.0 => /lib/libopencv_flann.so.3.0 (0x76798000)
    libopencv_imgproc.so.3.0 => /lib/libopencv_imgproc.so.3.0 (0x764c7000)
    libopencv_ml.so.3.0 => /lib/libopencv_ml.so.3.0 (0x76430000)

Do note the offsets in brackets seem to differ between the two versions. Otherwise both py2.7 cv2.so and py34 cv2.so are dependent on the shared objects (libopencv_core.so.3.0 etc).

Is this correct or is something wrong and it may cause problems later. If this right, could you please tell me how this works and why mixing so files is ok here?
Thanks T

edit retag flag offensive close merge delete

Comments

"Is this correct or is something wrong" - nothing wrong, this is correct. only the python wrapper differs, the underlying opencv c++ code is the same.

btw, the recommended way is to build cv2.so statically (cmake -DBUILD_SHARED_LIBS=OFF) , so it no more depends on so's at runtime

berak gravatar imageberak ( 2015-12-17 03:05:15 -0500 )edit

Hey, is this -DBUILD_SHARED_LIBS=OFF the right way? Oh god. That would have saved so much time. It really ought to be mentioned in the website somewhere.

Terry gravatar imageTerry ( 2015-12-17 23:44:19 -0500 )edit

it's mentioned here and here , unfortunately only in java context.

as a sidenote, you'll still need the opencv_ffmpeg so around, if you want read from ipcams or such.

berak gravatar imageberak ( 2015-12-18 00:01:24 -0500 )edit

Hi, What about CMAKE_INSTALL_PREFIX? Everyone is setting it to /usr/local. Can I set it to my home directory ~/usr/local? Infact I thought the good practice was to install everything in your python virtual environment like ~/py27env , ~/py3env ?

Terry gravatar imageTerry ( 2015-12-19 02:10:25 -0500 )edit

sure, correct idea.

berak gravatar imageberak ( 2015-12-19 02:37:10 -0500 )edit

Thanks. Will do so in the future. Mean time I went ahead with /usr/local as install prefix.

I discovered somethings strange. I built opencv under python2.7 virtual env. (Basically virtualenv -p /usr/bin/python2.7 )

I did an out of source build under the directory buildpy27. This built two files under the build tree : buildpy27/lib/cv2.so AND buildpy27/lib/python3/cv2.cpython-34m.so # why the later one??

Any ideas why both .so files are getting built. Does that mean I don't need a seperate opencv build for the python3.4?.

And doing sudo make install created these : /usr/local/lib/python2.7/site-packages/cv2.so AND /usr/local/lib/python3.4/site-packages/cv2.cpython-34m.so So, why this cv2.cpython-34m.so? Wouldn't this be built and installed only when I build under python3 env?

Terry gravatar imageTerry ( 2015-12-19 06:19:16 -0500 )edit