dlib在android端的集成

http://dlib.net dlib 源码下载。

环境参考: android-ndk-r14b ,opencv 3.4 , dlib 19.19

  1. opencv官方编译好了可用于android平台的so以及jni header 提供了jni和java版本的SDK两中调用方式,集成比较简单,不走赘述。

1. dlib 集成

  1. 下载dlib源码,我使用19.19

    解压后的目录如下
1
2
3
4
dlib
docs
examples
tools
  1. 将 dlib 目录整体拷贝到 studio 的 src/main/cpp 目录下

  2. 配置 build.gradle文件

1
2
3
4
5
6
externalNativeBuild {
cmake {
arguments "-DCMAKE_BUILD_TYPE=Release .."
cppFlags "-std=c++11 -frtti -fexceptions -Os -O2"
}
}
  1. cmake 文件处理
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73

cmake_minimum_required(VERSION 3.4.1)

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.


# 设置native目录
set(NATIVE_DIR ${CMAKE_SOURCE_DIR}/src/main/cpp)
# 设置dlib
include(${NATIVE_DIR}/dlib/cmake)

include_directories(${JNI_LIBS_DIR}/includes)

set(JNI_LIBS_DIR ${CMAKE_SOURCE_DIR}/src/main/cpp)

add_library( # Sets the name of the library.
native-lib

# Sets the library as a shared library.
SHARED

# Provides a relative path to your source file(s).
src/main/cpp/native-lib.cpp
src/main/cpp/matutil.cpp
src/main/cpp/effective.cpp
src/main/cpp/face_detector.cpp
)

include_directories(${CMAKE_SOURCE_DIR}/src/main/cpp)

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

add_library(libopencv_java # 库名字
SHARED
IMPORTED)
set_target_properties( libopencv_java
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/libs/armeabi-v7a/libopencv_java.so)


find_library( # Sets the name of the path variable.
log-lib

# Specifies the name of the NDK library that
# you want CMake to locate.
log )

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
native-lib
libopencv_java
android
jnigraphics
dlib
# Links the target library to the log library
# included in the NDK.
${log-lib} )


# 指定release编译选项
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -s -O3 -Wall")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -s -O3 -Wall")

  1. 添加cpp源文件处理

    在 dlib 文件夹下添加文件 opencv_dlib_bridge.h 内容如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <string>
#include <sstream>

using namespace std;
namespace std{
template <typename T> std::string to_string(const T& n)
{
std::ostringstream stm;
stm << n;
return stm.str();
}

template <typename T> T round(T v)
{
return (v>0)?(v+0.5):(v-0.5);
}
}

demo 参考: https://github.com/kongxs/face