当前位置:   article > 正文

人脸识别4:Android InsightFace实现人脸识别Face Recognition(含源码)

insightface

人脸识别4:Android InsightFace实现人脸识别Face Recognition(含源码)

目录

人脸识别4:Android InsightFace实现人脸识别Face Recognition(含源码)

1. 前言

2. 项目说明

(1)开发版本

(2)依赖库说明(OpenCV+OpenCL+base-utils+TNN)

(3)CMake配置

3. 人脸识别系统

(1)人脸识别的核心算法

(2)人脸检测和关键点检测

(3)人脸校准

(4)人脸特征提取

(5)人脸比对(1:1)

(6)人脸搜索(1:N)

(7)人脸识别优化建议

(8) 运行APP闪退:dlopen failed: library "libomp.so" not found

4. 人脸识别Android Demo效果

5. 人脸识别Python版本源码下载

6. 人脸识别C/C++版本源码下载

7. 人脸识别Android版本源码下载


1. 前言

这是项目《人脸识别Face Recognition》系列之《Android InsightFace实现人脸识别Face Recognition》;项目基于开源ArcFace(也称InsightFace)模型搭建一套完整的Android人脸识别系统(Face Recognition or Face Identification);我们将开发一个简易的、可实时运行的人脸识别Android Demo。Android版本人脸识别模型推理支持CPU和GPU加速,在GPU(OpenCL)加速下,可以达到实时的人脸识别效果,非常适合在Linux开发板和Android系统开发板上部署。

整套人脸识别系统核心算法包含人脸检测和人脸关键点检测,人脸校准,人脸特征提取以及人脸比对(1:1)和人脸搜索(1:N)。本项目人脸识别系统可以达到目前商业级别的人脸识别准确率,在误识率(FAR)0.1%的情况下,可提供99.78%的通过率(TAR);可以满足人脸比对,人脸签到、人脸门禁、人员信息查询、安防监控等人脸识别应用场景。

Android版本人脸检测和人脸识别效果:

【尊重原创,转载请注明出处】https://blog.csdn.net/guyuealian/article/details/130600600


更多项目《人脸识别Face Recognition》系列文章请参考:

  1. 人脸识别1:人脸识别数据集https://blog.csdn.net/guyuealian/article/details/130600545
  2. 人脸识别2:InsightFace实现人脸识别Face Recognition(含源码下载)人脸识别2:InsightFace实现人脸识别Face Recognition(含源码下载)_insightface 识别_AI吃大瓜的博客-CSDN博客
  3. 人脸识别3:C/C++ InsightFace实现人脸识别Face Recognition(含源码)人脸识别3:C/C++ InsightFace实现人脸识别Face Recognition(含源码)_AI吃大瓜的博客-CSDN博客
  4. 人脸识别4:Android InsightFace实现人脸识别Face Recognition(含源码)https://blog.csdn.net/guyuealian/article/details/130600600

ccd0e425827b4cad815f3d05447f37e6.gif


2. 项目说明

项目依赖库主要有OpenCV,base-utils,TNN和OpenCL(用于加速),项目源码已经包含了相关依赖库,且都已经配置好,无需安装;使用Android Studio直接build即可运行App Demo ;

(1)开发版本

Android SDK,NDK,Jave等版本信息,请参考:

2347e36864f34864b8bdfcef9c34d619.png

d8040a495a7c417091eb2c2c7ea766b4.png

(2)依赖库说明(OpenCV+OpenCL+base-utils+TNN)

项目模型推理采用TNN部署框架(支持多线程CPU和GPU加速推理);图像处理采用OpenCV库,模型加速采用OpenCL,在普通手机设备即可达到实时处理。项目Android源码已经配置好OpenCV+OpenCL+base-utils+TNN依赖库,无需重新配置,Android Studio直接build,即可运行。

  • OpenCV:图像处理(如读取图片,图像裁剪等)都需要使用OpenCV库进行处理(无需安装,项目已经配置了)
  • OpenCL:OpenCL用于模型GPU加速,若不使用OpenCL进行模型推理加速,纯C++推理模型,速度会特别特别慢(无需安装,项目已经配置了)
  • base-utils:是个人开发常用的C++库,集成了C/C++ OpenCV等常用的算法:https://github.com/PanJinquan/base-utils (无需安装,项目已经配置了)
  • TNN:模型推理框架:https://github.com/Tencent/TNN (无需安装,项目已经配置了)

(3)CMake配置

人脸识别核心算法均采用C++实现,上层Java应用使用JNI调用底层算法,CMake最低版本3.5.0,这是CMakeLists.txt,其中主要配置OpenCV+OpenCL+base-utils+TNN这四个库:

  1. cmake_minimum_required(VERSION 3.5.0)
  2. project("TNN")
  3. add_compile_options(-fPIC) # fix Bug: can not be used when making a shared object
  4. #set(CMAKE_BUILD_TYPE Release)
  5. #set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Build type (default Debug)" FORCE)
  6. #set(CMAKE_CXX_FLAGS "-Wall -std=c++11 -pthread")
  7. set(CMAKE_BUILD_TYPE "Release" CACHE STRING "set build type to release" FORCE)
  8. # opencv set
  9. # copy `OpenCV-android-sdk/sdk` to `3rdparty/opencv/`
  10. set(OpenCV_DIR ${CMAKE_SOURCE_DIR}/3rdparty/opencv/sdk/native/jni)
  11. find_package(OpenCV REQUIRED)
  12. include_directories(${CMAKE_SOURCE_DIR}/3rdparty/opencv/sdk/native/jni/include)
  13. # base_utils
  14. set(BASE_ROOT 3rdparty/base-utils) # 设置base-utils所在的根目录
  15. add_subdirectory(${BASE_ROOT}/base_utils/ base_build) # 添加子目录到build中
  16. include_directories(${BASE_ROOT}/base_utils/include)
  17. include_directories(${BASE_ROOT}/base_utils/src)
  18. MESSAGE(STATUS "BASE_ROOT = ${BASE_ROOT}")
  19. # TNN set
  20. # Creates and names a library, sets it as either STATIC
  21. # or SHARED, and provides the relative paths to its source code.
  22. # You can define multiple libraries, and CMake builds it for you.
  23. # Gradle automatically packages shared libraries with your APK.
  24. # build for platform
  25. # set(TNN_BUILD_SHARED OFF CACHE BOOL "" FORCE)
  26. if (CMAKE_SYSTEM_NAME MATCHES "Android")
  27. set(TNN_OPENCL_ENABLE ON CACHE BOOL "" FORCE)
  28. set(TNN_ARM_ENABLE ON CACHE BOOL "" FORCE)
  29. set(TNN_BUILD_SHARED OFF CACHE BOOL "" FORCE)
  30. set(TNN_OPENMP_ENABLE ON CACHE BOOL "" FORCE) # Multi-Thread
  31. #set(TNN_HUAWEI_NPU_ENABLE OFF CACHE BOOL "" FORCE)
  32. add_definitions(-DTNN_OPENCL_ENABLE) # for OpenCL GPU
  33. add_definitions(-DTNN_ARM_ENABLE) # for Android CPU
  34. add_definitions(-DDEBUG_ANDROID_ON) # for Android Log
  35. add_definitions(-DPLATFORM_ANDROID)
  36. elseif (CMAKE_SYSTEM_NAME MATCHES "Linux")
  37. set(TNN_OPENCL_ENABLE ON CACHE BOOL "" FORCE)
  38. set(TNN_CPU_ENABLE ON CACHE BOOL "" FORCE)
  39. set(TNN_X86_ENABLE OFF CACHE BOOL "" FORCE)
  40. set(TNN_QUANTIZATION_ENABLE OFF CACHE BOOL "" FORCE)
  41. set(TNN_OPENMP_ENABLE ON CACHE BOOL "" FORCE) # Multi-Thread
  42. add_definitions(-DTNN_OPENCL_ENABLE) # for OpenCL GPU
  43. add_definitions(-DDEBUG_ON) # for WIN/Linux Log
  44. add_definitions(-DDEBUG_LOG_ON) # for WIN/Linux Log
  45. add_definitions(-DDEBUG_IMSHOW_OFF) # for OpenCV show
  46. add_definitions(-DPLATFORM_LINUX)
  47. elseif (CMAKE_SYSTEM_NAME MATCHES "Windows")
  48. set(TNN_OPENCL_ENABLE ON CACHE BOOL "" FORCE)
  49. set(TNN_CPU_ENABLE ON CACHE BOOL "" FORCE)
  50. set(TNN_X86_ENABLE ON CACHE BOOL "" FORCE)
  51. set(TNN_QUANTIZATION_ENABLE OFF CACHE BOOL "" FORCE)
  52. set(TNN_OPENMP_ENABLE ON CACHE BOOL "" FORCE) # Multi-Thread
  53. add_definitions(-DTNN_OPENCL_ENABLE) # for OpenCL GPU
  54. add_definitions(-DDEBUG_ON) # for WIN/Linux Log
  55. add_definitions(-DDEBUG_LOG_ON) # for WIN/Linux Log
  56. add_definitions(-DDEBUG_IMSHOW_OFF) # for OpenCV show
  57. add_definitions(-DPLATFORM_WINDOWS)
  58. endif ()
  59. set(TNN_ROOT 3rdparty/TNN)
  60. include_directories(${TNN_ROOT}/include)
  61. include_directories(${TNN_ROOT}/third_party/opencl/include)
  62. add_subdirectory(${TNN_ROOT}) # 添加外部项目文件夹
  63. set(TNN -Wl,--whole-archive TNN -Wl,--no-whole-archive)# set TNN library
  64. MESSAGE(STATUS "TNN_ROOT = ${TNN_ROOT}")
  65. # NPU Set
  66. if (TNN_HUAWEI_NPU_ENABLE)
  67. add_library(hiai
  68. SHARED
  69. IMPORTED)
  70. set_target_properties(hiai
  71. PROPERTIES
  72. IMPORTED_LOCATION
  73. ${CMAKE_SOURCE_DIR}/src/main/jni/thirdparty/hiai_ddk/${ANDROID_ABI}/libhiai.so)
  74. add_library(hiai_ir
  75. SHARED
  76. IMPORTED)
  77. set_target_properties(hiai_ir
  78. PROPERTIES
  79. IMPORTED_LOCATION
  80. ${CMAKE_SOURCE_DIR}/src/main/jni/thirdparty/hiai_ddk/${ANDROID_ABI}/libhiai_ir.so)
  81. add_library(hiai_ir_build
  82. SHARED
  83. IMPORTED)
  84. set_target_properties(hiai_ir_build
  85. PROPERTIES
  86. IMPORTED_LOCATION
  87. ${CMAKE_SOURCE_DIR}/src/main/jni/thirdparty/hiai_ddk/${ANDROID_ABI}/libhiai_ir_build.so)
  88. endif ()
  89. find_library( # Sets the name of the path variable.
  90. log-lib
  91. # Specifies the name of the NDK library that
  92. # you want CMake to locate.
  93. log)
  94. # Specifies libraries CMake should link to your target library. You
  95. # can link multiple libraries, such as libraries you define in the
  96. # build script, prebuilt third-party libraries, or system libraries.
  97. # dmcv库
  98. include_directories(src)
  99. set(SRC_LIST
  100. src/face_alignment.cpp
  101. src/face_recognizer.cpp
  102. src/face_feature.cpp
  103. src/object_detection.cpp
  104. src/Interpreter.cpp)
  105. MESSAGE(STATUS "DIR_SRCS = ${SRC_LIST}")
  106. # JNI接口库
  107. add_library(tnn_wrapper SHARED jni_interface.cpp ${SRC_LIST})
  108. target_link_libraries( # Specifies the target library.
  109. tnn_wrapper
  110. -ljnigraphics
  111. # Links the target library to the log library
  112. # included in the NDK.
  113. ${log-lib}
  114. ${android-lib}
  115. ${jnigraphics-lib}
  116. ${TNN}
  117. ${OpenCV_LIBS}
  118. base_utils
  119. )
  120. if (TNN_HUAWEI_NPU_ENABLE)
  121. target_link_libraries( # Specifies the target library.
  122. tnn_wrapper hiai hiai_ir hiai_ir_build)
  123. endif ()

3. 人脸识别系统

人脸识别主要包含人脸比对(1:1)人脸搜索(1:N)两大功能,涉及的核心算法主要包含:人脸检测和人脸关键点检测,人脸校准,人脸特征提取以及人脸比对(1:1)和人脸搜索(1:N);当然,实际业务中,可能还会增加人脸质量检测以及活体识别等算法,碍于篇幅,后续再分享活体识别算法。

下图给出本项目人脸识别系统算法实现架构流程图:

11acfa2ee6eb40818544226505ff4a92.png

(1)人脸识别的核心算法

项目实现了人脸识别的核心算法,包含人脸检测和人脸关键点检测,人脸校准,人脸特征提取以及人脸比对(1:1)和人脸搜索(1:N)等功能,可以参文件(src/main/java/com/cv/tnn/model/FaceRecognizer.java),实现人脸识别的基本功能

  1. package com.cv.tnn.model;
  2. import android.graphics.Bitmap;
  3. import android.graphics.BitmapFactory;
  4. import android.util.Log;
  5. import java.io.File;
  6. import java.util.List;
  7. public class FaceRecognizer {
  8. private static final String TAG = "FaceRecognizer";
  9. public FaceRecognizer(String det_model, String rec_model, String root, String database, int model_type, int num_thread, boolean useGPU) {
  10. Log.w(TAG, "det_model =" + det_model);
  11. Log.w(TAG, "rec_model =" + rec_model);
  12. Log.w(TAG, "root =" + root);
  13. Log.w(TAG, "database =" + database);
  14. Log.w(TAG, "model_type=" + model_type);
  15. Log.w(TAG, "num_thread=" + String.valueOf(num_thread));
  16. Log.w(TAG, "useGPU =" + String.valueOf(useGPU));
  17. FileChooseUtil.createFolder(root);
  18. Detector.init(det_model, rec_model, root, database, model_type, num_thread, useGPU);
  19. }
  20. /***
  21. * 进行人脸检测
  22. * @param bitmap 输入图像
  23. * @param det_conf_thresh 人脸检测置信度阈值,范围0.~1.0
  24. * @param det_iou_thresh 人脸检测IOU阈值,范围0.~1.0
  25. * @return
  26. */
  27. public FrameInfo[] detectFace(Bitmap bitmap, float det_conf_thresh, float det_iou_thresh) {
  28. FrameInfo[] result = null;
  29. result = Detector.detectFace(bitmap, det_conf_thresh, det_iou_thresh);
  30. return result;
  31. }
  32. /***
  33. * 通过导入文件夹路径,进行批量注册人脸,
  34. * 请将图片按照[ID-XXXX.jpg]命名,如:张三-image.jpg
  35. * @param folder 文件夹路径
  36. * @param det_conf_thresh 人脸检测置信度阈值,范围0.~1.0
  37. * @param det_iou_thresh 人脸检测IOU阈值,范围0.~1.0
  38. */
  39. public void registerFromFolder(String folder, float det_conf_thresh, float det_iou_thresh) {
  40. Log.w(TAG, "database folder=" + folder);
  41. List<String> image_list = FileChooseUtil.getImagePathFromSD(folder);
  42. for (int i = 0; i < image_list.size(); i++) {
  43. String image_file = image_list.get(i);
  44. FrameInfo[] result = registerFromFile(image_file, det_conf_thresh, det_iou_thresh);
  45. }
  46. }
  47. /***
  48. * 通过导入图片的路径,进行注册
  49. * 请将图片按照[ID-XXXX.jpg]命名,如:张三-image.jpg
  50. * @param image_file 图片的路径
  51. * @param det_conf_thresh 人脸检测置信度阈值,范围0.~1.0
  52. * @param det_iou_thresh 人脸检测IOU阈值,范围0.~1.0
  53. * @return
  54. */
  55. public FrameInfo[] registerFromFile(String image_file, float det_conf_thresh, float det_iou_thresh) {
  56. String[] paths = image_file.split(File.separator);
  57. String basename = paths[paths.length - 1];
  58. String face_id = basename.split("-")[0];
  59. if (face_id.length() == basename.length()) {
  60. Log.w(TAG, "file=" + image_file + ",图片名称不合法,请将图片按照[ID-XXXX.jpg]命名,如:张三-image.jpg");
  61. }
  62. Bitmap bitmap = BitmapFactory.decodeFile(image_file);
  63. FrameInfo[] result = registerFromBitmap(face_id, bitmap, det_conf_thresh, det_iou_thresh);
  64. if (result.length > 0) {
  65. Log.w(TAG, "file=" + image_file + ",注册人脸成功:ID=" + face_id);
  66. } else {
  67. Log.w(TAG, "file=" + image_file + ",注册人脸失败:ID=" + face_id);
  68. }
  69. return result;
  70. }
  71. /***
  72. * 通过导入Bitmap图像,进行注册
  73. * @param face_id 人脸ID
  74. * @param bitmap Bitmap图像
  75. * @param det_conf_thresh 人脸检测置信度阈值,范围0.~1.0
  76. * @param det_iou_thresh 人脸检测IOU阈值,范围0.~1.0
  77. * @return
  78. */
  79. public FrameInfo[] registerFromBitmap(String face_id, Bitmap bitmap, float det_conf_thresh, float det_iou_thresh) {
  80. FrameInfo[] result = null;
  81. //bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
  82. result = Detector.registerFace(face_id, bitmap, det_conf_thresh, det_iou_thresh);
  83. return result;
  84. }
  85. /***
  86. * 人脸识别1:N人脸搜索
  87. * @param bitmap Bitmap图像
  88. * @param max_face 最大人脸个数,默认为-1,表示全部人脸
  89. * @param det_conf_thresh 人脸检测置信度阈值,范围0.~1.0
  90. * @param det_iou_thresh 人脸检测IOU阈值,范围0.~1.0
  91. * @param rec_conf_thresh 人脸识别相似度阈值,范围0.~1.0
  92. * @return
  93. */
  94. public FrameInfo[] detectSearch(Bitmap bitmap, int max_face, float det_conf_thresh, float det_iou_thresh, float rec_conf_thresh) {
  95. FrameInfo[] result = null;
  96. result = Detector.detectSearch(bitmap, max_face, det_conf_thresh, det_iou_thresh, rec_conf_thresh);
  97. return result;
  98. }
  99. /***
  100. * 人脸识别1:1人脸验证,比较两张人脸的相似性
  101. * @param bitmap1 输入第1张人脸图像
  102. * @param bitmap2 输入第2张人脸图像
  103. * @param det_conf_thresh 人脸检测置信度阈值,范围0.~1.0
  104. * @param det_iou_thresh 人脸检测IOU阈值,范围0.~1.0
  105. * @return
  106. */
  107. public float compareFace(Bitmap bitmap1, Bitmap bitmap2, float det_conf_thresh, float det_iou_thresh) {
  108. return Detector.compareFace(bitmap1, bitmap2, det_conf_thresh, det_iou_thresh);
  109. }
  110. /***
  111. * 提取人脸特征(先进行检测,再提取人脸特征)
  112. * @param bitmap 输入人脸图像
  113. * @param max_face 最大人脸个数,默认为-1,表示全部人脸
  114. * @param det_conf_thresh 人脸检测置信度阈值,范围0.~1.0
  115. * @param det_iou_thresh 人脸检测IOU阈值,范围0.~1.0
  116. * @return
  117. */
  118. public FrameInfo[] getFeature(Bitmap bitmap, int max_face, float det_conf_thresh, float det_iou_thresh) {
  119. FrameInfo[] result = null;
  120. result = Detector.getFeature(bitmap, max_face, det_conf_thresh, det_iou_thresh);
  121. return result;
  122. }
  123. /***
  124. * 清空人脸数据库(会删除所有已经注册的人脸数据,谨慎操作)
  125. */
  126. public void clearDatabase() {
  127. Detector.clearDatabase();
  128. }
  129. }

(2)人脸检测和关键点检测

人脸检测的方法比较多,项目Python版本人脸识别提供两种人脸检测方法:一种是基于MTCNN的通用人脸检测模型,另一种是轻量化的、快速的RFB人脸检测模型;这个两个模型都能实现人脸检测,并同时预测人脸的五个关键点(Landmark)。C/C++和Android版本只提供RFB人脸检测和关键点检测模型。

模型Paper源码说明
MTCNNPaperLink
  • 支持人脸检测和人脸关键点检测(5个点)
  • 通用场景人脸检测,计算量较大,适合PC服务器部署
RFBPaperLink
  • 支持人脸检测和人脸关键点检测(5个点)
  • 轻量级人脸检测,适合简单场景人脸检测,计算量较小,适合嵌入式,开发板,Android等终端部署
推荐阅读
相关标签
  

闽ICP备14008679号