编译SeetaFace2
1. NDK配置
1.1 cd到下载的目录:cd /home/lven/
1.2 下载:wget https://dl.google.com/android/repository/android-ndk-r21d-linux-x86_64.zip
或者 wget https://dl.google.com/android/repository/android-ndk-r22-linux-x86_64.zip
1.3 解压:unzip android-ndk-r21d-linux-x86_64.zip
1.4 配置(可以不配置,在sh解本配置就好)
## 1. 打开文件
vim /etc/profile
## 2. 尾部插入
## 配置路径
export ANDROID_NDK=/home/lven/tool/android-ndk-r21d
export ANDROID_TOOLCHAINS="$ANDROID_NDK/toolchains/llvm/prebuilt/linux-x86_64"
export ANDROID_SYSROOT="$ANDROID_TOOLCHAINS/sysroot"
## 配置路径到环境变量
export PATH=$ANDROID_NDK:$PATH
export PATH=$ANDROID_TOOLCHAINS:$PATH
export PATH=$ANDROID_SYSROOT:$PATH
## export CC="$ANDROID_TOOLCHAINS/bin/x86_64-linux-android21-clang"
## export CXX="$ANDROID_TOOLCHAINS/bin/x86_64-linux-android21-clang++"
## 3. 刷新
source /etc/profile
2. CMake(3.18.1)配置(配置链接就行)
2.1 cd到下载的目录:cd /home/lven/tool/
2.2 下载:wget https://github.com.cnpmjs.org/Kitware/CMake/releases/download/v3.18.1/cmake-3.18.1-Linux-x86_64.tar.gz
2.3 解压:tar zxvf cmake-3.18.1-Linux-x86_64.tar.gz
2.4 配置环境变量(主要配置2.4.3软链接,其它不用配置)
## 2.4.1环境变量配置(配置这里就行了)
vim /etc/profile
export PATH=$PATH:/home/lven/tool/cmake-3.18.1-Linux-x86_64/bin
## 或者2.4.2 环境变量配置(这个可以不配置)
gedit ~/.bashrc
export PATH=/home/lven/tool/cmake-3.18.1-Linux-x86_64/bin:$PATH
source ~/.bashrc
## 2.4.3 软件连接配置(软连接得配置,不然说找不到cmake)
ln -sf /home/lven/tool/cmake-3.18.1-Linux-x86_64/bin/* /usr/bin/
3. 下载SeetaFace2
3.1 安装git:sudo apt install git
3.2 git clone https://github.com.cnpmjs.org/seetafaceengine/SeetaFace2.git
3.3 编译过程
- 编译 cd 到 SeetaFace2
- 编写face2.sh
#!/bin/bash
rm -rf build
mkdir build
cd build
NDK="/home/lven/tool/android-ndk-r21d"
cmake .. -DCMAKE_INSTALL_PREFIX=install \
-DCMAKE_BUILD_TYPE=MinSizeRel \
-DCMAKE_TOOLCHAIN_FILE=$NDK/build/cmake/android.toolchain.cmake \
-DANDROID_ABI="armeabi-v7a with NEON" \
-DANDROID_PLATFORM=android-21 \
-DBUILD_EXAMPLE=OFF
cmake --build . --config MinSizeRel --target install/strip
- 执行 sh face2.sh
3.4 编译好的头文件和so位置(SeetaFace2/build/install)
- Cmake参数简单讲解
- ANDROID_PLATFORM (android-21) 安卓目标平台
- ANDROID_ABI (armeabi-v7a) 目标ABI
- CMAKE_TOOLCHAIN_FILE ($NDK/build/cmake/android.toolchain.cmake) CMake用于交叉编译的android.toolchain.cmake文件的路径
4. 注意事项
NDK的版本和Cmake的版本要对应,如果不对应会编译报错,生成不了so文件。
5. 人脸关键点识别
- java层代码
识别关键类
/**
* 人脸识别,关键点
*/
public class FaceTrack {
static {
System.loadLibrary("bybcamera");
}
// 人脸类
private Face face;
public FaceTrack(Context context) {
init(context);
}
public Face getFace() {
return face;
}
// 1. 初始化
public void init(Context context) {
Executors.newSingleThreadExecutor().execute(() -> {
// 人脸检测
String fd = loadFacePath(context, "fd_2_00.dat");
// 特征点
String pd5 = loadFacePath(context, "pd_2_00_pts5.dat");
// String pd81 = loadFacePath(context, "pd_2_00_pts81.dat");
// 人脸识别
// String fr = loadFacePath(context, "fr_2_10.dat");
nInitFace2Case(fd, pd5);
});
}
/**
* 2. 人脸识别
*
* @param data yuv420数据
* @param width 图片宽
* @param height 图片高
* @param scale 缩放识别的倍数
* @param isFront 是不是前摄像头
*/
public void track(byte[] data, int width, int height, int scale, boolean isFront) {
face = nTrack(data, width, height, scale, isFront);
}
// 3. 释放资源
public void onDestroy() {
nRelease();
}
private static String loadFacePath(Context context, String fileName) {
File filesDir = context.getFilesDir();
File outFile = new File(filesDir, fileName);
if (outFile.exists() && outFile.isFile()) {
return outFile.getAbsolutePath();
}
try {
InputStream stream = context.getAssets().open(fileName);
OutputStream outputStream = new FileOutputStream(outFile);
byte[] data = new byte[stream.available()];
stream.read(data);
outputStream.write(data);
outputStream.close();
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
return outFile.getAbsolutePath();
}
// ===================native方法========================
// 1. 初始化
private native void nInitFace2Case(String fd, String pd);
// 2. 识别
private native Face nTrack(byte[] data, int width, int height, int scale, boolean isFront);
// 3. 回收
private native void nRelease();
}
native识别结果
public class Face implements Serializable {
// 人脸左上角坐标、宽、高
private int x;
private int y;
private int width;
private int height;
// 图片宽高
private int imgWidth;
private int imgHeight;
// 关键点
private float[] markers;
public Face(int x, int y, int width, int height, int imgWidth, int imgHeight, float[] markers) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.imgWidth = imgWidth;
this.imgHeight = imgHeight;
this.markers = markers;
}
// get 方法自己实现...
}
- native层代码
#include <jni.h>
#include "AndroidLog.h"
#include <seeta/FaceTracker.h>
#include <seeta/FaceLandmarker.h>
#include <seeta/Struct_cv.h>
#include <seeta/Struct.h>
#include <opencv2/opencv.hpp>
seeta::FaceTracker *FD = nullptr;
seeta::FaceLandmarker *FL = nullptr;
using namespace cv;
// 1. 初始化
extern "C"
JNIEXPORT void JNICALL
Java_com_boardour_bybcamera_face_FaceTrack_nInitFace2Case(JNIEnv *env, jobject thiz, jstring fd,
jstring pd) {
// 1. 加载样本路径
const char *fd_path = env->GetStringUTFChars(fd, 0);
const char *pd_path = env->GetStringUTFChars(pd, 0);
// 2. 初始人脸跟踪、关键点识别
seeta::ModelSetting::Device device = seeta::ModelSetting::AUTO;
seeta::ModelSetting FD_model(fd_path, device, SEETA_DEVICE_AUTO);
seeta::ModelSetting FL_model(pd_path, device, SEETA_DEVICE_AUTO);
// 3. 创建对象
FD = new seeta::FaceTracker(FD_model);
FL = new seeta::FaceLandmarker(FL_model);
// 路径回收
env->ReleaseStringUTFChars(fd, fd_path);
env->ReleaseStringUTFChars(pd, pd_path);
}
// 2. 人脸追踪、关键点
extern "C"
JNIEXPORT jobject JNICALL
Java_com_boardour_bybcamera_face_FaceTrack_nTrack(JNIEnv *env, jobject thiz, jbyteArray data,
jint width, jint height, jint scale,
jboolean isFront) {
if (nullptr == FD || nullptr == FL) {
return nullptr;
}
// 1. 加载图片并转成RGBA
jbyte *imgData = env->GetByteArrayElements(data, 0);
// 摄像头数据data
Mat src(height + height / 2, width, CV_8UC1, imgData);
// I420P转成RGBA
cvtColor(src, src, COLOR_YUV420p2RGBA);
if (scale != 1) {
resize(src, src, Size(src.cols / scale, src.rows / scale));
}
if (isFront) { // 前摄
rotate(src, src, ROTATE_90_COUNTERCLOCKWISE); // 逆时针90度
flip(src, src, 1); // y 轴 翻转(镜像操作)
} else { // 后摄
rotate(src, src, ROTATE_90_CLOCKWISE);
}
// 2. 灰度、轮廓增强
cvtColor(src, src, COLOR_BGRA2GRAY);
equalizeHist(src, src);
// 3. seeta::cv::ImageData
seeta::cv::ImageData img = src;
// 4. 人脸追踪
auto faces = FD->track(img);
env->ReleaseByteArrayElements(data, imgData, 0);
LOGE("face size %d", faces.size);
if (faces.size > 0) {
auto face = faces.data[0].pos;
// 关键点
auto marks = FL->mark(img, face);
jfloatArray landmarks = env->NewFloatArray(marks.size() * 2);
int i = 0;
for (auto mark :marks) {
float f[2] = {scale * (float) mark.x, scale * (float) mark.y};
env->SetFloatArrayRegion(landmarks, i, 2, f);
i += 2;
}
// Face(int x, int y, int width, int height,
// int imgWidth, int imgHeight, float[] markers)
// 封装好数据,给Java层
jclass clazz = env->FindClass("com/boardour/bybcamera/face/Face");
jmethodID construct = env->GetMethodID(clazz, "<init>", "(IIIIII[F)V");
jobject jface = env->NewObject(clazz, construct,
scale * face.x,
scale * face.y,
scale * face.width,
scale * face.height,
width,
height,
landmarks);
return jface;
}
return nullptr;
}
extern "C"
JNIEXPORT void JNICALL
Java_com_boardour_bybcamera_face_FaceTrack_nRelease(JNIEnv *env, jobject thiz) {
if (FD != nullptr) {
delete FD;
FD = nullptr;
}
if (FL != nullptr) {
delete FL;
FL = nullptr;
}
}