OpenCV与tensorflow静态库依赖的一些问题

1, find_package是在系统环境变量指定的目录中寻找 ***Config.cmake 和 ****Config-version.cmake文件,通常在乌班图下使用apt-get install 命令会自动在相应的目录下生成文件,如果手动编译的话,在执行make install也会在此输出。

2,对于OpenCV静态库的编译,有两种方法

2.1 命令行编译

mkdir build
cd build
cmake -D WITH_IPP=OFF -D CMAKE_BUILD_TYPE=RELEASE -D BUILD_SHARED_LIBS=OFF ..
make -j4
g++ -std=c++11 -L/opt/openCV_2_4_5/lib -I/opt/openCV_2_4_5/include -o ex2 ex2.cpp -L. -lpthread -lz -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dnn -lopencv_dpm -lopencv_fuzzy -lopencv_line_descriptor -lopencv_optflow -lopencv_plot -lopencv_reg -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_rgbd -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_face -lopencv_xfeatures2d -lopencv_shape -lopencv_video -lopencv_ximgproc -lopencv_calib3d -lopencv_features2d -lopencv_flann -lopencv_xobjdetect -lopencv_objdetect -lopencv_ml -lopencv_xphoto -lopencv_videoio -lopencv_imgcodecs -lopencv_photo -lopencv_imgproc -lopencv_core 

这里参考: https://segmentfault.com/a/1190000011586084

2.2 gui界面编译

有些时候可能会报错,然后的话手动cmake编译。先安装cmake-gui,然后执行编译,手动勾选,将shared_libs 钩掉。还可以选择其他的库比如png,tiff等等。

3,加载OpenCV静态库

CMakeList.txt

set(OpenCV_DIR /home/chaofan/opt/opencv-3.4.4/release)

find_package( OpenCV 3 REQUIRED )

include_directories( ${OpenCV_INCLUDE_DIRS} )

add_executable( imageBasics imageBasics.cpp )

target_link_libraries( imageBasics ${OpenCV_LIBS} )

怎么找到opencv的安装路径

去找opencv的时候建立的release或者build的文件夹,主要依据是里面有OpenCVConfig.cmake文件

4,Tensorflow编译及应用C++静态库

clone tensorflow git 仓库

    git clone https://github.com/tensorflow/tensorflow.git
    cd tensorflow

进入 tensorflow 工程下contrib/makefile路径

    tensorflow/contrib/makefile

运行编译脚本

    # MacOS 以及Linux 使用此脚本
    ./build_all_linux.sh

脚本执行完成后我们得到tensorflow静态库以及相应头文件。
下一步我们将Tensorflow静态库头文件及Tensorflow依赖的静态库头文件整理到统一路径下,其他c++ 工程就可以应用这些库文件及头文件。

    # 将tensorflow 头文件以及库文件收集到此路径
    mkdir -p ~/tensorflow_libs/tensorflow
    # include 路径存放tensorflow主要头文件
    mkdir -p ~/tensorflow_libs/tensorflow/include/tensorflow
    # lib 路径存放编译好的静态库
    mkdir -p ~/tensorflow_libs/tensorflow/lib
    # 拷贝tensorflow 主要头文件
    cp -r tensorflow/core ~/tensorflow_libs/tensorflow/include
    # 拷贝tensoflow 静态库
    cp tensorflow/contrib/makefile/gen/lib/libtensorflow-core.a ~/tensorflow_libs/tensorflow/lib
    # 拷贝tensorflow 第三方头文件
    mkdir -p ~/tensorflow_libs/tensorflow/tensorflow_third_party
    cp -r third_party ~/tensorflow_libs/tensorflow/tensorflow_third_party
    # 拷贝gen目录下文件
    cp -r tensorflow/contrib/makefile/gen/host_obj ~/tensorflow_libs/tensorflow/
    cp -r tensorflow/contrib/makefile/gen/proto ~/tensorflow_libs/tensorflow/
    cp -r tensorflow/contrib/makefile/gen/protobuf ~/tensorflow_libs/tensorflow/
    cp -r tensorflow/contrib/makefile/gen/proto_text ~/tensorflow_libs/tensorflow/
    # 拷贝downloads下文件
    # eigen3
    mkdir -p ~/tensorflow_libs/tensorflow/eigen3
    cp -r tensorflow/contrib/makefile/downloads/eigen/Eigen ~/tensorflow_libs/tensorflow/eigen3
    cp -r tensorflow/contrib/makefile/downloads/eigen/unsupported ~/tensorflow_libs/tensorflow/eigen3
    # absl
    cp -r tensorflow/contrib/makefile/downloads/absl ~/tensorflow_libs/tensorflow/
    # nsyc
    mkdir -p ~/tensorflow_libs/tensorflow/nsyc/include
    mkdir -p ~/tensorflow_libs/tensorflow/nsyc/lib
    # 拷贝nsyc 头文件
    cp -r tensorflow/contrib/makefile/downloads/nsync/* ~/tensorflow_libs/tensorflow/nsyc/include
    # 拷贝nsyc 库文件
    cp tensorflow/contrib/makefile/downloads/nsync/builds/default.macos.c++11/libnsync.a ~/tensorflow_libs/tensorflow/nsyc/lib

CMakeLists.txt 如下

    CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
    SET(TENSORFLOW_INCLUDE_PATH ${CMAKE_SOURCE_DIR}/tensorflow/include)
    SET(TENSORFLOW_LIBARY ${CMAKE_SOURCE_DIR}/tensorflow/lib/libtensorflow-core.a)
    MESSAGE(STATUS "TENSORFLOW_INCLUDE_PATH ${TENSORFLOW_INCLUDE_PATH}")
    MESSAGE(STATUS "TENSORFLOW_LIBARY ${TENSORFLOW_LIBARY}")
    SET(TENSORFLOW_PROTOBUF_INCLUDE_PATH ${CMAKE_SOURCE_DIR}/tensorflow/protobuf/include)
    SET(TENSORFLOW_PROTOBUF_LIBRARY_PATH ${CMAKE_SOURCE_DIR}/tensorflow/protobuf/lib)
    SET(TENSORFLOW_PROTOBUF_LIBRARY ${TENSORFLOW_PROTOBUF_LIBRARY_PATH}/libprotobuf.a)
    SET(TENSORFLOW_PROTOBUF_LITE_LIBRARY ${TENSORFLOW_PROTOBUF_LIBRARY_PATH}/libprotobuf-lite.a)
    SET(TENSORFLOW_PROTOC_LIBRARY ${TENSORFLOW_PROTOBUF_LIBRARY_PATH}/libprotoc.a)
    MESSAGE(STATUS "TENSORFLOW_PROTOBUF_INCLUDE_PATH ${TENSORFLOW_PROTOBUF_INCLUDE_PATH}")
    MESSAGE(STATUS "TENSORFLOW_PROTOBUF_LIBRARY_PATH ${TENSORFLOW_PROTOBUF_LIBRARY_PATH}")
    SET(TENSORFLOW_NSYNC_INCLUDE_PATH ${CMAKE_SOURCE_DIR}/tensorflow/nsync/include)
    SET(TENSORFLOW_NSYNC_LIBRARY_PATH ${CMAKE_SOURCE_DIR}/tensorflow/nsync/lib)
    MESSAGE(STATUS "TENSORFLOW_NSYNC_INCLUDE_PATH ${TENSORFLOW_NSYNC_INCLUDE_PATH}")
    MESSAGE(STATUS "TENSORFLOW_NSYNC_LIBRARY_PATH ${TENSORFLOW_NSYNC_LIBRARY_PATH}")
    SET(TENSORFLOW_NSYNC_LIBRARY ${TENSORFLOW_NSYNC_LIBRARY_PATH}/libnsync.a)
    SET(TENSORFLOW_PROTO_INCLUDE_PATH ${CMAKE_SOURCE_DIR}/tensorflow/proto)
    SET(TENSORFLOW_PROTO_TEXT_INCLUDE_PATH ${CMAKE_SOURCE_DIR}/tensorflow/proto_text)
    SET(TENSORFLOW_HOST_OBJ_INCLUDE_PATH ${CMAKE_SOURCE_DIR}/tensorflow/host_obj)
    SET(TENSORFLOW_EIGEN_INCLUDE_PATH ${CMAKE_SOURCE_DIR}/tensorflow/eigen3)
    SET(TENSORFLOW_ABSL_INCLUDE_PATH ${CMAKE_SOURCE_DIR}/tensorflow/absl)
    SET(TENSORFLOW_THIRD_PARTY_INCLUDE_PATH ${CMAKE_SOURCE_DIR}/tensorflow/tensorflow_third_party)
    MESSAGE(STATUS "TENSORFLOW_PROTO_INCLUDE_PATH ${TENSORFLOW_PROTO_INCLUDE_PATH}")
    MESSAGE(STATUS "TENSORFLOW_PROTO_TEXT_INCLUDE_PATH ${TENSORFLOW_PROTO_TEXT_INCLUDE_PATH}")
    MESSAGE(STATUS "TENSORFLOW_HOST_OBJ_INCLUDE_PATH ${TENSORFLOW_HOST_OBJ_INCLUDE_PATH}")
    MESSAGE(STATUS "TENSORFLOW_EIGEN_INCLUDE_PATH ${TENSORFLOW_EIGEN_INCLUDE_PATH}")
    MESSAGE(STATUS "TENSORFLOW_ABSL_INCLUDE_PATH ${TENSORFLOW_ABSL_INCLUDE_PATH}")
    MESSAGE(STATUS "TENSORFLOW_THIRD_PARTY_INCLUDE_PATH ${TENSORFLOW_THIRD_PARTY_INCLUDE_PATH}")
    INCLUDE_DIRECTORIES(${TENSORFLOW_PROTOBUF_INCLUDE_PATH})
    INCLUDE_DIRECTORIES(${TENSORFLOW_INCLUDE_PATH})
    INCLUDE_DIRECTORIES(${TENSORFLOW_PROTO_INCLUDE_PATH})
    INCLUDE_DIRECTORIES(${TENSORFLOW_PROTO_TEXT_INCLUDE_PATH})
    INCLUDE_DIRECTORIES(${TENSORFLOW_HOST_OBJ_INCLUDE_PATH})
    INCLUDE_DIRECTORIES(${TENSORFLOW_EIGEN_INCLUDE_PATH})
    INCLUDE_DIRECTORIES(${TENSORFLOW_ABSL_INCLUDE_PATH})
    INCLUDE_DIRECTORIES(${TENSORFLOW_NSYNC_INCLUDE_PATH})
    INCLUDE_DIRECTORIES(${TENSORFLOW_THIRD_PARTY_INCLUDE_PATH})
    ADD_EXECUTABLE(load_model load_model.cpp)
    SET(LOAD_MODEL_LIBRARIES
        ${TENSORFLOW_PROTOBUF_LIBRARY}
        ${TENSORFLOW_PROTOC_LIBRARY}
        ${TENSORFLOW_NSYNC_LIBRARY}
        ${TENSORFLOW_LIBARY})
    SET(LDFLAGS "-std=c++11 -msse4.1 -fPIC -O3 -march=native -Wall -finline-functions -undefined dynamic_lookup -all_load")
    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}${LDFLAGS}")
    MESSAGE(STATUS "CMAKE_CXX_COMPILER: ${CMAKE_CXX_COMPILER}")
    MESSAGE(STATUS "CMAKE_CXX_FLAGS: ${CMAKE_CXX_FLAGS}")
    TARGET_LINK_LIBRARIES(load_model ${LOAD_MODEL_LIBRARIES} ${CMAKE_CXX_FLAGS})

编译 

    mkdir -p build && cd build
    cmake ..
    make
    # 将编译好的二进制文件拷贝到上级目录
    cp load_model ..

参考:

https://ce39906.github.io/2018/09/11/Tensorflow-%E7%BC%96%E8%AF%91%E5%8F%8A%E5%BA%94%E7%94%A8C-%E9%9D%99%E6%80%81%E5%BA%93/

2,ldd 查看程序依赖库

用来查看程序运行所需的共享库,常用来解决程序因缺少某个库文件而不能运行的一些问题。

/opt/app/todeav1/test$ldd test
libstdc++.so.6 => /usr/lib64/libstdc++.so.6 (0x00000039a7e00000)
libm.so.6 => /lib64/libm.so.6 (0x0000003996400000)
libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00000039a5600000)
libc.so.6 => /lib64/libc.so.6 (0x0000003995800000)
/lib64/ld-linux-x86-64.so.2 (0x0000003995400000)
  • 第一列:程序需要依赖什么库
  • 第二列: 系统提供的与程序需要的库所对应的库
  • 第三列:库加载的开始地址
     

如果依赖的某个库找不到,可以通过这个命令迅速定位问题所在。

https://linuxtools-rst.readthedocs.io/zh_CN/latest/tool/ldd.html

 

你可能感兴趣的:(机器学习机器学习实战,工程系,Opencv系列)