Use TensorFlow C++ API with OpenCV3

zong fan
5 min readApr 27, 2018

--

Installing TensorFlow (TF) C++ API is much more complicated and tedious task than its Python version which you could use pip tool to install directly. For C++ API, you have to compile and build it from source and manually install all dependency packages. But considering the great improvement in inference latency performance, such cost is worth!

OpenCV is a powerful tool for image processing which is widely used in computer vision. According to official TensorFlow document, we need to place the project under tensorflow/cc directory and modify it BUILD file to add OpenCV dependencies. Then execute bazel run to compile this project (See this post). But this is not C++ like. Can we use cmake to compile project in a standalone directory but not under tensorflow root path? The answer is yes and here it the solution:

PS: we use Unbuntu 16.04 and python3 as example.

Part 1. Install TensorFlow

  1. (Optional) Install CUDA and cuDNN driver

If GPU card is available, the CUDA driver should be installed to greatly boost both training and inference speed. There are tons of documents so we won’t say more about this. Just refer to this doc.

Modify at 2018.04.28: It seems Tensorflow only support CUDA 9.0 + cuDNN7.0.5 now.

2. Install prerequisites packages

Bazel, Protobuf and Eigen are basic packages.

For TF-master branch (r1.8), Bazel 0.12.0 works fine. (For TF-r1.7, use 0.11.1)

sudo apt-get install openjdk-8-jdkecho "deb [arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.listcurl https://bazel.build/bazel-release.pub.gpg | sudo apt-key add -sudo apt-get update && sudo apt-get install bazel

For we need C++ version, we need to download the source package and compile it (NOT use the linux_x86_64 version). Protobuf 3.5.1 works fine.

sudo apt-get install autoconf automake libtool curl make g++ unzipcurl -OL https://github.com/google/protobuf/releases/download/v3.5.1/protoc-3.5.1.zipunzip protoc-3.5.1.zip && cd protoc-3.5.1./autogen.sh
./configure
make && sudo make install
sudo ldconfig

This package is for matrix calculation and you could just need to download and extract it under your ENV PATH or installing with cmake. Eigen 3.3.4 works fine.

PS: Don’t use libeigen3-dev via apt-get install. This package may be obsolete which cause compiling error.

# Download eigen 3.3.4 from https://github.com/eigenteam/eigen-git-mirror/releases
unzip eigen-git-mirror-3.3.4.zip
cd eigen-git-mirror
mkdir build && cd build
cmake ..
make
sudo make install

3. Install TensorFlow

First, just follow the official installing from source document. Here we use master version (r1.8)

sudo apt-get install python3-numpy python3-dev python3-pip python3-wheel
sudo pip3 install six numpy wheel
git clone https://github.com/tensorflow/tensorflow
cd tensorflow
./configure
# configure build option# install TF python packagebazel build --config=opt --config=cuda //tensorflow/tools/pip_package:build_pip_package
bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
sudo pip install /tmp/tensorflow_pkg/tensorflow-1.8.0-py3-none-any.whl

Then install C++ API. Attention here: if you want to use C API, buildtensorflow/libtensorflow.so, if C++ API, use tensorflow/libtensorflow_cc.so.

NOTE: — config=monolithic is required for making TensorFlow capable with Opencv, otherwise you will fail to read in image by cv::imread. (See Issue #14267)

bazel build -c opt --copt=-mavx --copt=-mavx2 --copt=-mfma --copt=-mfpmath=both --copt=-msse4.2 --config=monolithic //tensorflow:libtensorflow_cc.so
# copy required files into a single path for c++ linkage
sudo mkdir /usr/local/include/tf # make a directory ubder /usr/local/include path
sudo mkdir /usr/local/include/tf/tensorflow
sudo cp -r bazel-genfiles/ /usr/local/include/tf
sudo cp -r tensorflow/cc /usr/local/include/tf/tensorflow
sudo cp -r tensorflow/core /usr/local/include/tf/tensorflow
sudo cp -r third_party /usr/local/include/tf
sudo cp bazel-bin/tensorflow/libtensorflow_cc.so /usr/local/lib
sudo cp bazel-bin/tensorflow/libtensorflow_framework.so /usr/local/lib

4. (Optional) Install/Update Cmake

System default Cmake version (5.4, maybe …) may be too low to compile C++ project successfully. To update cmake , you should choose and download from from this site. Cmake 3.11.1 works fine for me.

tar xcvf cmake-3.11.1-Linux-x86_64.tar.gz
cd cmake-3.11.1-Linux-x86_64
sudo apt-get purge cmake
sudo cp -r bin /usr/
sudo cp -r share /usr/
sudo cp -r doc /usr/share/
sudo cp -r man /usr/share/

Part 2. Install OpenCV

Also tons of documents.

sudo apt-get install build-essential checkinstall cmake pkg-config yasm
sudo apt-get install git gfortran
sudo apt-get install libjpeg8-dev libjasper-dev libpng12-dev
sudo apt-get install libtiff5-dev
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev
sudo apt-get install libxine2-dev libv4l-dev
sudo apt-get install libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev
sudo apt-get install qt5-default libgtk2.0-dev libtbb-dev
sudo apt-get install libatlas-base-dev
sudo apt-get install libfaac-dev libmp3lame-dev libtheora-dev
sudo apt-get install libvorbis-dev libxvidcore-dev
sudo apt-get install libopencore-amrnb-dev libopencore-amrwb-dev
sudo apt-get install x264 v4l-utils
sudo apt-get install python-dev python-pip python3-dev python3-pip
sudo pip3 install numpy scipy matplotlib scikit-image scikit-learn ipython
cd ~
git clone https://github.com/opencv/opencv.git
cd opencv && git checkout 3.4.1
cd ~
git clone https://github.com/opencv/opencv_contrib.git
cd opencv_contrib && git checkout 3.4.1
cd ~/opencv && mkdir build && cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_C_EXAMPLES=ON \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D WITH_TBB=ON \
-D WITH_V4L=ON \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
-D BUILD_EXAMPLES=ON ..
nproc
# substitute 4 by output of nproc
make -j4
sudo make install
sudo sh -c 'echo "/usr/local/lib" >> /etc/ld.so.conf.d/opencv.conf'
sudo ldconfig

Part 3. Test with sample project

This is a good github project for us to test our installation.

Just follow his instruction to modify the TF dependency paths to the place you assigned in previous section.

mkdir build && cd build cmake ..
sudo make
./tf_detector_example

If running successfully without reporting any annoying errors, so, congratulations and you will see pop-up like this:

References:

  1. Building TensorFlow 1.3.0 as a standalone project (Raspberry pi 3 included)
  2. A few notes on using the Tensorflow C++ API
  3. Issue #2412
  4. Issue #1741
  5. Issue #1890

--

--