抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

OpenCV Tensorflow C++API Protobuf eigen3 编译过程

@auther by sizaif

2021-08-10 14:22:25

[TOC]

OpenCV

首先安装OpenCV的依赖文件,在终端运行下面命令:

code
sudo apt-get install build-essential

sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev

sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

#  if errorE: unable to locate libjasper-dev
sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
# or vim sources.list  add  "deb http://security.ubuntu.com/ubuntu xenial-security main" and update 
vim /etc/apt/sources.list

sudo apt update
sudo apt install libjasper1 libjasper-dev

Install compiler and build tools

  • To compile OpenCV you will need a C++ compiler. Usually it is G++/GCC or Clang/LLVM:

    • Install GCC…

      sudo apt install -y g++
    • … or Clang:

      sudo apt install -y clang
  • OpenCV uses CMake build configuration tool:

    sudo apt install -y cmake
  • CMake can generate scripts for different build systems, e.g. make,ninja:

    • Install Make…

      sudo apt install -y make
    • … or Ninja:

      sudo apt install -y ninja-build
  • Install tool for getting and unpacking sources:

    • wget and unzip …

      sudo apt install -y wget unzip
    • … or git :

      sudo apt install -y git

Download sources

There are two methods of getting OpenCV sources:

  • Download snapshot of repository using web browser or any download tool (~80-90Mb) and unpack it…

    wget -O opencv.zip https://github.com/opencv/opencv/archive/master.zipunzip opencv.zipmv opencv-master opencv
  • … or clone repository to local machine using git to get full change history (>470Mb):

    git clone https://github.com/opencv/opencv.gitgit -C opencv checkout master

Note

Snapshots of other branches, releases or commits can be found on the GitHub and the official download page.

Configure and build

  • Create build directory:

    mkdir -p build && cd build
  • Configure - generate build scripts for the preferred build system:

    • For make…

      #自定义路径安装OpenCV 到 /usr/local/opencv 中cmake  -D CMAKE_INSTALL_PREFIX=/usr/local/opencv ../opencv 
    • … or for ninja :

      cmake -GNinja ../opencv
  • Build - run actual compilation process:

    • Using make …

      make -j4
    • … or ninja :

      ninja

Note

Configure process can download some files from the internet to satisfy library dependencies, connection failures can cause some of modules or functionalities to be turned off or behave differently. Refer to the OpenCV installation overview and OpenCV configuration options reference tutorials for details and full configuration options reference.

If you experience problems with the build process, try to clean or recreate the build directory. Changes in the configuration like disabling a dependency, modifying build scripts or switching sources to another branch are not handled very well and can result in broken workspace.

Make can run multiple compilation processes in parallel, -j<NUM> option means “run jobs simultaneously”. Ninja will automatically detect number of available processor cores and does not need -j option.

Check build results

After successful build you will find libraries in the build/lib directory and executables (test, samples, apps) in the build/bin directory:

ls binls lib

CMake package files will be located in the build root:

ls OpenCVConfig*.cmakels OpenCVModules.cmake

Install

Warning

The installation process only copies files to predefined locations and does minor patching. Installing using this method does not integrate opencv into the system package registry and thus, for example, opencv can not be uninstalled automatically. We do not recommend system-wide installation to regular users due to possible conflicts with system packages.

By default OpenCV will be installed to the /usr/local directory, all files will be copied to following locations:

  • /usr/local/bin - executable files
  • /usr/local/lib - libraries (.so)
  • /usr/local/cmake/opencv4 - cmake package
  • /usr/local/include/opencv4 - headers
  • /usr/local/share/opencv4 - other files (e.g. trained cascades in XML format)

Since /usr/local is owned by the root user, the installation should be performed with elevated privileges (sudo):

sudo make install

or

sudo ninja install

Installation root directory can be changed with CMAKE_INSTALL_PREFIX configuration parameter, e.g. -DCMAKE_INSTALL_PREFIX=$HOME/.local to install to current user’s local directory. Installation layout can be changed with OPENCV_*_INSTALL_PATH parameters. See OpenCV configuration options reference for details.


Generated on Tue Apr 20 2021 02:31:39 for OpenCV by doxygen 1.8.13

Tensorflow

bazel

code
# Bazel needs a C++ compiler and unzip / zip in order to work:
sudo apt install g++ unzip zip

# Run the installer
chmod +x bazel-<version>-installer-linux-x86_64.sh
./bazel-<version>-installer-linux-x86_64.sh --user

# Set up your environment
vim ~/.bashrc
export PATH="$PATH:$HOME/bin"
source ~/.bashrc

tensorflow-v2.4.0

code
# 安装以下构建工具以配置开发环境。
sudo apt install python3-dev python3-pip
pip install numpy

# 安装 Bazel

git clone https://github.com/tensorflow/tensorflow.git
cd tensorflow
git checkout branch_name  # r2.2, r2.3, etc.

./configure

# CUDA GPU
bazel build --config=opt --config=cuda //tensorflow:libtensorflow_cc.so

# CPU
bazel build --config=opt //tensorflow:libtensorflow_cc.so

# 编译完成后,  将头文件 ( bazel-bin/tensorflow 和 下 **.so  复制到 /usr/local/include/tf/tensorflow 和 /usr/local/lib
mkdir /usr/local/include/tf
cp -r bazel-bin/tensorflow/* /usr/local/include/tf/tensorflow/
sudo cp -r bazel-bin/tensorflow/*.so /usr/local/lib/

测试

CMakeLists.txt

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)

# project Name & Version
project(STOtest)

# find_package
find_package(OpenCV REQUIRED)
# set location 
# Openblas
set(OpenBlas_DIR /usr/local/OpenBlas)
# eigen
set(Eigen_DIR /usr/local/include/eigen3)
# tensorflow
set(Tensorflow_INCLUDES
            /usr/local/include/tf/
            /usr/local/include/tf/tensorflow/
            /usr/local/include/tf/tensorflow/third-party)

set(Tensorflow_LIBS
/usr/local/lib/libtensorflow_cc.so
/usr/local/lib/libtensorflow_framework.so)

# protobuf
set(Protobuf_INCLUDES /usr/local/include/google)
set(Protobuf_LIBS /usr/local/bin/protoc)
message(STATUS: "OpenBlas_DIR= ${OpenBlas_DIR}")

message(STATUS "OpenCV library status:")
message(STATUS "    config=: ${OpenCV_DIR}")
message(STATUS "    version=: ${OpenCV_VERSION}")
message(STATUS "    libraries=: ${OpenCV_LIBS}")
message(STATUS "    include path=: ${OpenCV_INCLUDE_DIRS}")

# include 
include_directories(
    ${Tensorflow_INCLUDES}
    ${Eigen_DIR}
    ${OpenBlas_DIR}
    ${Protobuf_INCLUDES}
)

# src cpp
aux_source_directory(./src DIR_SRCS)
add_executable( main .${DIR_SRCS})

# target
target_link_libraries(main PRIVATE ${OpenCV_LIBS} ${Tensorflow_LIBS} ${Protobuf_LIBS})
code
//#include "opencv2/highgui.hpp"
//#include "opencv2/imgproc.hpp"
#include <tensorflow/core/platform/env.h>
#include <tensorflow/core/public/session.h>
#include <iostream>
//susing namespace cv;
using namespace std;
using namespace tensorflow;

int main( )
{
    cout << "hello tensorflow" <<endl;
   
    Session* session;
    Status status = NewSession(SessionOptions(), &session);
    if (!status.ok()) {
        cout << status.ToString() << endl;
        cout <<"Session not ok" << endl;
        return 1;
    }
    cout << "Session successfully created."<<endl;
    return 0;
}

Protobuf

1)上Github下载protocbuf源码包

https://github.com/protocolbuffers/protobuf/releases/tag/vX.X.X

2)安装依赖包

sudo apt-get install autoconf automake libtool curl make g++ unzip

3)进入源码目录,执行./autogen.sh生成configure文件

4)依次执行

code
 ./configure
 make
 make check
 sudo make install
 sudo ldconfig # refresh shared library cache.
 
 # 验证版本
siz@ubuntu:~$ protoc --version
libprotoc 3.9.2
siz@ubuntu:~$ whereis protoc
protoc: /usr/local/bin/protoc

Eigen3

简单命令安装

sudo apt-get install libeigen3-dev

源码编译安装

code
http://eigen.tuxfamily.org/index.php?title=Main_Page

git clone https://gitlab.com/libeigen/eigen.git

mkdir build

cd build

cmake ..

sudo make instal

# 安装成功后,头文件路径在 /usr/local/include/eigen3/

# CMakeLists.txt  中添加 eigen3 头文件
include_directories( "/usr/include/eigen3" )

OpenBlas

code
git clone https://github.com/xianyi/OpenBLAS.git
cd OpenBLAS
make
make PREFIX=/usr/local/Openblas install

评论吧



本站总访问量为 访客数为

鲁 ICP 备 20018157 号-1
Copyright 2021 - 2022 sizaif. All Rights Reserved