【转载】faster_rcnn c++版本的 caffe 封装,动态库(2)

https://www.cnblogs.com/louyihang-loves-baiyan/archive/2016/05/14/5493344.html

在上一篇文章中,我们是将对caffe的调用隔离了出来,可以说相当于原来caffe源码下的tools中cpp文件使用相同,然后自己写了个CMakeLists.txt进行编译。这里是进一步将代码进行分离,封装成libfaster_rcnn.so文件进行使用。对于部分接口,我可能做了一些改动。

目录结构

├── CMakeLists.txt

├── lib

│   ├── CMakeLists.txt

│   ├── faster_rcnn.cpp

│   ├── faster_rcnn.hpp

├── main.cpp

├── pbs_cxx_faster_rcnn_demo.job

在这里main.cpp就是直接调用faster_rcnn.cpp的接口,他的内容也很简单,只是在之前的基础上,再加上libfaster_rcnn.so这个动态库文件

#include "faster_rcnn.hpp"

int main()

{

    string model_file = "/home/lyh1/workspace/py-faster-rcnn/models/pascal_voc/VGG_CNN_M_1024/faster_rcnn_alt_opt/faster_rcnn_test.pt";

    string weights_file = "/home/lyh1/workspace/py-faster-rcnn/output/default/yuanzhang_car/vgg_cnn_m_1024_fast_rcnn_stage2_iter_40000.caffemodel";

    int GPUID=0;

    Caffe::SetDevice(GPUID);

    Caffe::set_mode(Caffe::GPU);

    Detector det = Detector(model_file, weights_file);

    det.Detect("/home/lyh1/workspace/py-faster-rcnn/data/demo/car.jpg");

    return 0;

}

可以看到这里只是include了faster_rcnn.hpp头文件,其对应的CMakeLists.txt文件如下:

#This part is used for compile faster_rcnn_demo.cpp

cmake_minimum_required (VERSION 2.8)

project (main_demo)

add_executable(main main.cpp)

include_directories ( "${PROJECT_SOURCE_DIR}/../caffe-fast-rcnn/include"

    "${PROJECT_SOURCE_DIR}/../lib/nms"

    "${PROJECT_SOURCE_DIR}/lib"

    /share/apps/local/include

    /usr/local/include

    /opt/python/include/python2.7

    /share/apps/opt/intel/mkl/include

    /usr/local/cuda/include )

target_link_libraries(main /home/lyh1/workspace/py-faster-rcnn/faster_cxx_lib/lib/libfaster_rcnn.so

    /home/lyh1/workspace/py-faster-rcnn/caffe-fast-rcnn/build/lib/libcaffe.so

    /home/lyh1/workspace/py-faster-rcnn/lib/nms/gpu_nms.so

    /share/apps/local/lib/libopencv_highgui.so

    /share/apps/local/lib/libopencv_core.so

    /share/apps/local/lib/libopencv_imgproc.so

    /share/apps/local/lib/libopencv_imgcodecs.so

    /share/apps/local/lib/libglog.so

    /share/apps/local/lib/libboost_system.so

    /share/apps/local/lib/libboost_python.so

    /share/apps/local/lib/libglog.so

    /opt/rh/python27/root/usr/lib64/libpython2.7.so

    )

对于faster_rcnn.hpp和faster_rcnn.cpp ,我们需要将他们编译成动态库,下面是他们对应的CMakeLists.txt,在文件中,可以看到跟上面这个区别是用了add_library语句,并且加入了SHARED关键字,SHARED代表动态库。其次,在编译动态库的过程中,是不需要链接的,但是我们知道这个库是依赖别的很多个库的,所以在最后形成可执行文件也就是上面这个CMakeLists.txt,我们需要添加这个动态库所依赖的那些动态库,至此就OK了。编译的话,非常傻瓜cmake .然后在执行make即可。

cmake_minimum_required (VERSION 2.8)

SET (SRC_LIST faster_rcnn.cpp)

include_directories ( "${PROJECT_SOURCE_DIR}/../../caffe-fast-rcnn/include"

    "${PROJECT_SOURCE_DIR}/../../lib/nms"

    /share/apps/local/include

    /usr/local/include

    /opt/python/include/python2.7

    /share/apps/opt/intel/mkl/include

    /usr/local/cuda/include )

add_library(faster_rcnn SHARED ${SRC_LIST})

首先将原来的cpp文件中的声明提取出来,比较简单,就是hpp文件对应cpp文件。如下:

你可能感兴趣的:(【转载】faster_rcnn c++版本的 caffe 封装,动态库(2))