设为首页 加入收藏

TOP

Android 中使用 dlib+opencv 实现动态人脸检测(一)
2019-09-01 23:26:56 】 浏览:117
Tags:Android 使用 dlib opencv 实现 动态 人脸 检测

1 概述

完成 Android 相机预览功能以后,在此基础上我使用 dlib 与 opencv 库做了一个关于人脸检测的 demo。该 demo 在相机预览过程中对人脸进行实时检测,并将检测到的人脸用矩形框描绘出来。具体实现原理如下:

采用双层 View,底层的 TextureView 用于预览,程序从 TextureView 中获取预览帧数据,然后调用 dlib 库对帧数据进行处理,最后将检测结果绘制在顶层的 SurfaceView 中。

2 项目配置

由于项目中用到了 dlib 与 opencv 库,因此需要对其进行配置。主要涉及到以下几个方面:

2.1 C++支持

在项目创建过程中依次选择 Include C++ Support、C++11、Exceptions Support ( -fexceptions )以及 Runtime Type Information Support ( -frtti ) 。最后生成的 build.gradle 文件如下:

defaultConfig {
    applicationId "com.example.lightweh.facedetection"
    minSdkVersion 23
    targetSdkVersion 28
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    externalNativeBuild {
        cmake {
            arguments "-DCMAKE_BUILD_TYPE=Release"
            cppFlags "-std=c++11 -frtti -fexceptions"
        }
    }
}

其中,arguments 参数是后添加上去的,主要用于指定 CMake 的编译模式为 Release,因为在 Debug 模式下 dlib 库中相关算法的运行速度非常慢。前期如果需要调试 C++ 代码,可先将 arguments 参数注释。

2.2 dlib 与 opencv 下载

  • dlib官网下载最新版本的源码,解压后将文件夹中的dlib目录复制到 Android Studio 工程的 cpp 目录下。

  • sourceforge 下载最新的 opencv-android 库,解压后将文件夹中的 native 目录同样复制到 Android Studio 工程的 cpp 目录下,并改名为 opencv。

2.3 CMakeLists 配置

在 CMakeLists 文件中,我们首先包含 dlib 的 cmake 文件,接下来添加 opencv 的 include 文件夹并引入 opencv 的 so 库,同时将 jni_common 目录中的文件及人脸检测相关文件添加至 native-lib 库中,最后进行链接。

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

# 设置dlib
include(${NATIVE_DIR}/dlib/cmake)

# 设置opencv include文件夹
include_directories(${NATIVE_DIR}/opencv/jni/include)

# 设置opencv的so库
add_library(
        libopencv_java3
        SHARED
        IMPORTED)

set_target_properties(
        libopencv_java3
        PROPERTIES
        IMPORTED_LOCATION
        ${NATIVE_DIR}/opencv/libs/${ANDROID_ABI}/libopencv_java3.so)

# 将jni_common目录中所有文件名,存至SRC_LIST中
AUX_SOURCE_DIRECTORY(${NATIVE_DIR}/jni_common SRC_LIST)

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_LIST}
        src/main/cpp/face_detector.h
        src/main/cpp/face_detector.cpp
        src/main/cpp/native-lib.cpp)

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)

target_link_libraries( # Specifies the target library.
        native-lib
        dlib
        libopencv_java3
        jnigraphics
        # 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")

由于 C++ 代码中用到了头文件 "android/bitmap.h",所以链接时需要添加 jnigraphics 库。

3 JNI相关 Java 类定义

3.1 VisionDetRet 类

VisionDetRet 类的相关对象主要负责 C++ 与 Java 之间的数据传递。

public final class VisionDetRet {

    private int mLeft;
    private int mTop;
    private int mRight;
    private int mBottom;

    VisionDetRet() {}

    public VisionDetRet(int l, int t, int r, int b) {
        mLeft = l;
        mTop = t;
        mRight = r;
        mBottom = b;
    }

    public int getLeft() {
        return mLeft;
    }

    public int getTop() {
        return mTop;
    }

    public int getRight() {
        return mRight;
    }

    public int getBottom() {
        return mBottom;
    }
}

3.2 FaceDet 类

FaceDet 类为 JNI 函数调用类,主要定义了一些需要 C++ 实现的 native 方法。

public class FaceDet {
    private static final String TAG = "FaceDet";

    // accessed by native methods
    @SuppressWarnings("unused")
    priv
首页 上一页 1 2 3 4 5 下一页 尾页 1/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇小米6X手机解锁(bl锁) 下一篇Android + https 实现 文件上传

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目