1
$\begingroup$

I am using stock Ubuntu 16.04, blender 2.76 install. I am trying to use the python opencv (cv2) module from within blender to do some image processing on a texture.

I have installed opencv with the python 3 cv2 library. The following executes without error

python3 -c "import cv2; cv2.imread('img.png')"

But if I enter the same thing into the blender python console, or call it from within a script and run that script using

blender --python <script.py> 

Then blender immediately segfaults and dumps a crash report file with the following backtrace

# backtrace
blender(BLI_system_backtrace+0x30) [0x1361b10]
blender() [0x97a98e]
/lib/x86_64-linux-gnu/libc.so.6(+0x354b0) [0x7fc443cb94b0]
/usr/lib/x86_64-linux-gnu/libopencv_core.so.2.4(_ZNK2cv3Mat6copyToERKNS_12_OutputArrayE+0x35) [0x7fc435ab8865]
/usr/local/lib/python3.5/site-packages/cv2.cpython-35m-x86_64-linux-gnu.so(+0x813b6) [0x7fc4085373b6]
/usr/local/lib/python3.5/site-packages/cv2.cpython-35m-x86_64-linux-gnu.so(+0x81e84) [0x7fc408537e84]
/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0(PyCFunction_Call+0xe9) [0x7fc44aca51b9]
/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0(PyEval_EvalFrameEx+0x7555) [0x7fc44adbf085]
/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0(PyEval_EvalFrameEx+0x79d9) [0x7fc44adbf509]
/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0(+0x24ac0c) [0x7fc44ae4fc0c]
/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0(PyEval_EvalCodeEx+0x23) [0x7fc44ae4fce3]
/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0(+0xd2a98) [0x7fc44acd7a98]
/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0(PyObject_Call+0x6e) [0x7fc44ad7e8ee]
/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0(+0x225a4c) [0x7fc44ae2aa4c]
/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0(PyObject_Call+0x6e) [0x7fc44ad7e8ee]
/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0(+0x7e279) [0x7fc44ac83279]
/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0(+0x83276) [0x7fc44ac88276]
/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0(PyObject_Call+0x6e) [0x7fc44ad7e8ee]
/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0(PyEval_EvalFrameEx+0x1ccf) [0x7fc44adb97ff]
/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0(+0x24ac0c) [0x7fc44ae4fc0c]
/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0(PyEval_EvalCodeEx+0x23) [0x7fc44ae4fce3]
/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0(PyEval_EvalCode+0x1b) [0x7fc44adb789b]
/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0(PyRun_FileExFlags+0xb2) [0x7fc44add4bc2]
blender() [0xd786b5]
blender() [0x97b1d5]
blender(BLI_argsParse+0xe5) [0x1315e85]
blender(main+0xd77) [0x95cc87]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0) [0x7fc443ca4830]
blender(_start+0x29) [0x97a2b9]

This is the log file if run using LD_DEBUG=files blender --python script.py > blender.log 2>&1 as I saw on another link about segfaults in blender that I have now lost

32577:  opening file=/lib/x86_64-linux-gnu/libgcc_s.so.1 [0]; direct_opencount=2
32577:  

After reading https://developer.blender.org/T43869 and https://developer.blender.org/T43491#288351 I wrote a little python script to diff the output of ldd for both the cv2.so and the blender binary to see if I could see any miss matched shared libraries ... but both cv2.so and the blender binary seem to use the same .so files where there are commonalities

So I am a bit out of ideas at this point. Also I wondering why both blender and opencv rely on libgcc_s.so, is it something to do with pthreads? ... Anyway if anyone has got this working or has made more progress than me it would be great to hear from you .. any help would be much appreciated :)

$\endgroup$
  • $\begingroup$ I have an Ubuntu 16.04.1 server that can run OpenCV scripts within Blender. I compiled OpenCV directly unto Blender 2.78's python 3.5, and it works well. Did you try to run Blender in background mode and see if this happens then as well? blender -b -P script.py $\endgroup$ – TLousky Oct 31 '16 at 10:33
  • $\begingroup$ Oh this is reassuring news that it can work !! ... can you expand on "I compiled OpenCV directly unto Blender 2.78's python 3.5" a little more please, I just followed the instructions here pyimagesearch.com/2015/07/20/… ... any help on this would be appreciated. $\endgroup$ – othane Oct 31 '16 at 20:20
1
$\begingroup$

EDITED:

To get this to work on an Ubuntu 16.04.1 machine with Blender 2.78, I compiled openCV according to these instructions (and these ones) with some changes:

  1. The first time I compiled it to the locally installed python 3 exactly according to instructions.

  2. In the 2nd compilation, I used the following cmake configuration (build) command, which pointed out to the correct blender installation dir on this machine and the respective bundled python libraries and binaries:

    cmake -D CMAKE_BUILD_TYPE=RELEASE \
        -D CMAKE_INSTALL_PREFIX=/opt/blender/2.78/python \
        -D INSTALL_C_EXAMPLES=OFF \
        -D INSTALL_PYTHON_EXAMPLES=OFF \
        -D PYTHON_EXECUTABLE=/opt/blender/2.78/python/bin/python3.5m \
        -D PYTHON_INCLUDE=/opt/blender/2.78/python/include \
        -D PYTHON_LIBRARY=/opt/blender/2.78/python/lib/python3.5 \
        -D PYTHON_PACKAGES_PATH=/opt/blender/2.78/python/lib/python3.5/site-packages \
        -D PYTHON_NUMPY_INCLUDE_DIR=/opt/blender/2.78/python/lib/python3.5/site-packages/numpy/core/include \
        -D BUILD_EXAMPLES=OFF ..
    
  3. I later had to create a symlink to the cv2.so file from the first (standard) compilation directory to the bundled python's include library.

    cd /opt/blender/2.78/python/lib/python3.5/site-packages
    ln -s /usr/local/lib/python3.4/dist-packages/cv2.cpython-34m.so cv2.so
    

It works, although there's a warning about a missing camera file libdc1394 error: Failed to initialize libdc1394. This is probably avoidable through some changes to the build phase but since everything works it doesn't bother me too much.

Note that you do need to install Numpy on the bundled python first. The easiest way is with pip, which you can install on Blender's bundled python according to these instructions.

$\endgroup$
  • 1
    $\begingroup$ Thanks @TLousky this looks great !! ... ill give it a go tonight :) $\endgroup$ – othane Oct 31 '16 at 21:42
  • $\begingroup$ @othane I corrected a few mistakes in the post above, please see the revised example. $\endgroup$ – TLousky Nov 1 '16 at 8:31
  • 1
    $\begingroup$ Cheers I just tried it here ... I found it didn't seem to matter how I compiled opencv, ie using the cmake above, or any of the other links, etc ... what made the difference was using Blender 2.78 from the official site ... I was using Blender from Ubuntu apt-get install blender and this was installing Blender 2.76 into the system paths ... So my conclusion is that either there is a bug using OpenCV with Blender 2.76, or there is a issue with the way the Ubuntu Canonical Blender is packaged/built .... either way 2.78 works for me so that's great ! ... Thanks, big thumbs up :) $\endgroup$ – othane Nov 5 '16 at 3:49
2
$\begingroup$

Today I found an easy way to start using OpenCV in Blender without any (manual) compilation steps. My OS is Ubuntu 18.04 (64bit) and I run Blender 2.79b.

It may be also important to say, that I did not installed Blender from repository (e.g. apt), but simply downloaded the archive and extracted in a sub-folder of my home directory.

I performed following steps to start using OpenCV:

  1. Open Terminal
  2. Change directory (cd) to [blender]/2.79/python/bin
  3. Executed ./python3.5m -m ensurepip (to become pip available)
  4. Executed ./python3.5m -m pip install --upgrade pip (upgrade pip)
  5. Executed ./python3.5m -m pip install opencv-python (Though an unofficial build the version I got was 3.4.2.17)
  6. Executed ./python3.5m -m pip install opencv-contrib-python (I needed SIFT features and they are only available in the contrib package)

After these commands were executed I started Blender and performed a test where I extracted SIFT features from one Image and wrote an augmented result image to filesystem. Worked without issues.

It is that easy, that I would expect it somewhere to be written down in the internet, but unfortunately I have found only instructions which are far more complicated.

Hope this help someone!

$\endgroup$
1
$\begingroup$

Post my installing procedure on Mac 10.12.4 so that someone may find it useful:

  1. Uninstall any python instances except system python 2.7;
  2. Download opencv3.2, and go through a normal compiling procedure. There will be a $INSTALL/lib/python2.7/cv2.so after make install, where $INSTALL is your installing path;
  3. Install python3.5.2 (I use blender 2.78) by installer from python offical website. By default, it will be in /Library/Frameworks/Python.framework/Versions/3.5/;
  4. Recompile opencv by ccmake. This time, set the corresponding python3 paths according the python2 case. The compiled result will be in $INSTALL/lib/python3.5/cv2.cpython-35m-darwin.so;
  5. In the local python path of blender, make a soft link to the compiled opencv library.

Hope this help.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.