【ncnn android】算法移植(四)——ubuntu上搭建环境,测试

1. 安装protobuf

链接:

  1. https://blog.csdn.net/u010918487/article/details/82947157
  2. https://blog.csdn.net/kdchxue/article/details/81046192

2. 编译ncnn

$ cd <ncnn-root-dir>
$ mkdir -p build
$ cd build

# cmake option NCNN_VULKAN for enabling vulkan
$ cmake -DNCNN_VULKAN=OFF ..

$ make -j4
$ make install

报错

In file included from /usr/local/include/google/protobuf/arena.h:48:0,
                 from /usr/local/include/google/protobuf/message.h:118,
                 from /usr/local/include/google/protobuf/text_format.h:48,
                 from /home/yangna/chenjun/ncnn/ncnn-20180704/tools/caffe/caffe2ncnn.cpp:26:
/usr/include/c++/5/typeinfo:39:37: error: expected ‘}’ before end of line
/usr/include/c++/5/typeinfo:39:37: error: expected unqualified-id before end of line
/usr/include/c++/5/typeinfo:39:37: error: expected ‘}’ before end of line
/usr/include/c++/5/typeinfo:39:37: error: expected ‘}’ before end of line
/usr/include/c++/5/typeinfo:39:37: error: expected ‘}’ before end of line
/usr/include/c++/5/typeinfo:39:37: error: expected declaration before end of line
tools/caffe/CMakeFiles/caffe2ncnn.dir/build.make:70: recipe for target 'tools/caffe/CMakeFiles/caffe2ncnn.dir/caffe2ncnn.cpp.o' failed
make[2]: *** [tools/caffe/CMakeFiles/caffe2ncnn.dir/caffe2ncnn.cpp.o] Error 1
CMakeFiles/Makefile2:203: recipe for target 'tools/caffe/CMakeFiles/caffe2ncnn.dir/all' failed
make[1]: *** [tools/caffe/CMakeFiles/caffe2ncnn.dir/all] Error 2
Makefile:127: recipe for target 'all' failed
make: *** [all] Error 2

解决方案:

结果
会在build文件夹下有install/includeinstall/lib两个文件夹,include是*.h头文件;lib是动态链接库。
【ncnn android】算法移植(四)——ubuntu上搭建环境,测试

3. 使用ncnn

  1. 这里以cmakelist.txt为例。需要在cmakelist.txt中包含<ncnn project path>/build/install/include的文件夹,引入libncnn.a库文件。
    cmakelist.txt的关键部分
1. 
include_directories(/home/yangna/chenjun/ncnn/ncnn-20180704/build/install/include)					# 包含头文件

2. 
# 项目中添加libncnn.a库
add_library( libncnn STATIC IMPORTED )
set_target_properties(
        libncnn
        PROPERTIES IMPORTED_LOCATION
        ${DIR}/lib/libncnn.a
)

3. 链接
target_link_libraries(demo 
                      libncnn
                      ${OpenCV_LIBS}
					  )
  1. 这里用centerface做测试。项目中已经有ncnn模型和相关代码。只需要编译通过就行。
  • 修改cmakelist.txt,只需要修改一处
#1.cmake verson
cmake_minimum_required(VERSION 2.8)

# 设置变量
set (DIR /home/yangna/chenjun/ncnn/ncnn-20180704/build/install)			# 只需要修改这一处

#2.project name
project(demo C CXX)

#opencv
find_package(OpenCV REQUIRED)

#3.set environment variable
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

#4. include
include_directories(${DIR}/include
                    ${CMAKE_CURRENT_LIST_DIR}/cpp)

# ncnn lib
add_library( libncnn STATIC IMPORTED )
set_target_properties(
        libncnn
        PROPERTIES IMPORTED_LOCATION
        ${DIR}/lib/libncnn.a
)

# add OPENMP support 
FIND_PACKAGE( OpenMP REQUIRED)
if(OPENMP_FOUND)
message("OPENMP FOUND")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
endif()

#6.source directory
file(GLOB TEST_SRC ${CMAKE_CURRENT_LIST_DIR}/cpp/*.cpp
                   ${CMAKE_CURRENT_LIST_DIR}/demo.cpp)
set(TEST_COMPILE_CODE ${TEST_SRC})


add_executable(demo ${TEST_COMPILE_CODE})

target_link_libraries(demo 
                      libncnn
                      ${OpenCV_LIBS}
					  )
  • 编译项目
cd $project
makedir build
cmake ..
make
  • 执行
./demo 
  • 结果
    【ncnn android】算法移植(四)——ubuntu上搭建环境,测试
上一篇:javaweb 文件上传


下一篇:【ncnn】Ubuntu16.04编译ncnn android