Skip to content

Instantly share code, notes, and snippets.

@mixaz
Last active February 6, 2019 10:02
Show Gist options
  • Save mixaz/f24ce8cc40b68f827d31168aef7d6800 to your computer and use it in GitHub Desktop.
Save mixaz/f24ce8cc40b68f827d31168aef7d6800 to your computer and use it in GitHub Desktop.
Installing TensorFlow app with OpenCV shared lib dependency on Beaglebone Black (BBB)

Building a TF app with dependencies (OpenCV) for ARM target (Beaglebone Black).

  1. Build (cross compile) TF application using https://github.com/lhelontra/tensorflow-on-arm, using Bazel and TF binary in-source build (tf_cc_binary bazel target). It will have no dependencies, except ones added by your code (OpenCV in my case). No TF runtime needs to be installed on target device.

tensorflow-on-arm config needs changes to build your app target, instead default build-pip-package. Building apps with C API (tf_cc_binary) is described at https://www.tensorflow.org/guide/extend/cc (not available at time of this writting).

For your app Bazel BUILD script you will probably need to specify dependencies (OpenCV libs and include headers in my case), which in turn may depend on their dependencies, so I used -rpath-link option to point to target's sysroot lib folders.

  1. On target device install OpenCV dependencies. I built OpenCV on device (had to disable Python modules in OpenCV configuration, since BBB has 500Mb RAM and hung on that step), so I installed dev packages, per https://www.pyimagesearch.com/2018/09/26/install-opencv-4-on-your-raspberry-pi/ "Step #2: Install OpenCV 4 dependencies on your Raspberry Pi".

If you install prebuilt OpenCV libs, you can skip some steps there, and install only following:

$ sudo apt-get install libjpeg-dev libpng-dev libtiff-dev
$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
$ sudo apt-get install libxvidcore-dev libx264-dev

Following installs GTK3 libs dev package, but not dev may be enough (haven't tested)

$sudo apt-get install libgtk-3-dev

I used following cmake options to build OpenCV:

cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
    -D ENABLE_NEON=ON \
    -D ENABLE_VFPV3=ON \
    -D BUILD_TESTS=OFF \
    -D BUILD_EXAMPLES=OFF \
    -D OPENCV_ENABLE_NONFREE=ON \
    -D INSTALL_PYTHON_EXAMPLES=OFF \
    -D BUILD_opencv_java=OFF \
    -D BUILD_opencv_python=OFF \
    -D BUILD_opencv_python2=OFF \
    -D BUILD_opencv_python3=OFF \
    -D BUILD_SHARED_LIBS=ON \
    ..

then run make and make install. If you installed the libs to /usr/local/lib then you may need to run sudo ldconfig to update ld pathes.

After that you can start the TF app binary. To check if it has all dependencies resolved, run ldd <app binary>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment