作者:HW-LC
最近做的项目需要使用人脸识别算法,本身软工安工部是有这个算法的,但是由于安工部的人脸识别输入数据是直接在底层camera preview流获取的,从效率和安全的角度来看,这个思路是没有问题的,但是无法完全满足我手头的项目,我需要能够自定义人脸识别的输入数据源,所以暂时只能用第三方的人脸识别算法来做Demo了。
之前项目有人用过ArcSoft的人脸识别SDK,已经申请过APPID和APPKEY了,为了简单我就直接用ArcSoft的人脸识别SDK了,这里记录下集成步骤和集成过程中遇到的坑,主要是两大部分:录入人脸数据,人脸识别。
ArcSoft人脸识别SDK文档链接:https://ai.arcsoft.com.cn/manual/arcface_android_fd.html
一:录入人脸数据
(1) 初始化AFD(ArcSoft Face Detect)和AFR(ArcSoft Face Recognize) Engine,具体代码如下:
AFD_FSDKError afdRet = afdEngine.AFD_FSDK_InitialFaceEngine(SDK_APPID, FD_APPKEY,
AFD_FSDKEngine.AFD_OPF_0_HIGHER_EXT, 16, 5);
Log.d(TAG, 'afd engine init ret:' + afdRet.getCode());
AFR_FSDKError afrRet = afrEngine.AFR_FSDK_InitialEngine(SDK_APPID, FR_APPKEY);
Log.d(TAG, 'afr engine init ret:' + afrRet.getCode());
这里面APPID和APPKEY需要自己申请,这里就不公开了。
(2) 检测输入图像的人脸:
ArrayList<AFD_FSDKFace> afdResult = new ArrayList<>();
AFD_FSDKError afdRet = afdEngine.AFD_FSDK_StillImageFaceDetection(srcData, width, height, AFD_FSDKEngine.CP_PAF_NV21, afdResult);
if (afdRet.getCode() != AFD_FSDKError.MOK) {
Log.e(TAG, 'detect face ret:' + afdRet.getCode());
return CommonUtil.getRetBytes(RET_NG, CODE_INTERNAL_ERROR);
}
检测的人脸结果保存在AFD_FSDKFace list中,一般情况下我们只用单人脸场景来做识别,即list size为1。
(3) 提取检测到的人脸数据:
AFR_FSDKFace face = new AFR_FSDKFace();
AFR_FSDKError afrRet = afrEngine.AFR_FSDK_ExtractFRFeature(srcData, width, height, AFR_FSDKEngine.CP_PAF_NV21,
afdResult.get(0).getRect(), afdResult.get(0).getDegree(), face);
if (afrRet.getCode() != AFR_FSDKError.MOK) {
Log.e(TAG, 'get face ret:' + afrRet.getCode());
return CommonUtil.getRetBytes(RET_NG, CODE_INTERNAL_ERROR);
}
使用AFR_FSDK_ExtractFRFeature提取数据,通过检测出来的AFD Face提取得到AFR face,这里面rect和degree的参数需要注意下,degree参数有些demo固定为AFR_FSDKEngine.AFR_FOC_0,这个后面在人脸识别的时候会报MERR_FSDK_FR_MISMATCHED_FEATURE_LEVEL错,这个错误很隐蔽,网上也没人说过,被坑了好久。
(4) 保存人脸数据:
byte[] data = face.getFeatureData();
Log.d(TAG, 'feature data len:' + data.length);
FileOutputStream fs = null;
BufferedOutputStream bos = null;
try {
fs = new FileOutputStream(Environment.getExternalStorageDirectory().getAbsolutePath()
+ '/' + FACE_PATH, true);
bos = new BufferedOutputStream(fs);
bos.write(intToBytes(data.length));
bos.write(data);
} catch (FileNotFoundException e) {
Log.d(TAG, 'exception:' + e.getMessage());
return false;
} catch (IOException e) {
Log.d(TAG, 'exception:' + e.getMessage());
return false;
} finally {
close(bos);
close(fs);
}
一般连续提取5帧face数据保存(人脸和指纹数据在保存的时候,一般都要求多次录入,就是为了提高识别率)。
二:人脸识别
(1) 加载人脸识别数据(前面保存到FACE_FILE路径):
public static void loadFaceInfo() {
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + '/' + FACE_FILE;
FileInputStream fs = null;
BufferedInputStream bis = null;
try {
fs = new FileInputStream(path);
bis = new BufferedInputStream(fs);
byte[] size = new byte[4];
while (bis.read(size) > 0) {
AFR_FSDKFace face = new AFR_FSDKFace();
if (bis.read(face.getFeatureData()) == CommonUtil.bytesToInt(size, 0)) {
faceList.add(face);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
CommonUtil.close(bis);
CommonUtil.close(fs);
}
Log.d(TAG, 'load face info, size:' + faceList.size());
}
存储格式和读取格式保持一致即可,数据保存在faceList中。
(2) 加载AFD Engine和AFR Engine,具体同录入人脸。
(3) 检测输入图像的人脸,具体同录入人脸。
(4) 提取检测到的人脸数据,具体同录入人脸。
(5) 人脸匹配:
AFR_FSDKMatching scoreMatching = new AFR_FSDKMatching();
for (AFR_FSDKFace localFace : faceList) {
afrRet = afrEngine.AFR_FSDK_FacePairMatching(localFace, face, scoreMatching);
Log.d(TAG, 'matching score:' + scoreMatching.getScore());
if (afrRet.getCode() != AFR_FSDKError.MOK) {
Log.e(TAG, 'matching face ret:' + afrRet.getCode());
CommonUtil.getRetBytes(RET_NG, CODE_INTERNAL_ERROR);
continue;
}
if (scoreMatching.getScore() > maxScore) {
maxScore = scoreMatching.getScore();
Log.d(TAG, 'tmp max score:' + maxScore);
}
if (maxScore > SCORE_THRESHOLD) {
break;
}
}
人脸识别得到的AFR Face数据跟录入的AFR Face list(通过loadFaceInfo加载)做匹配,匹配的score如果大于某个阈值(本demo用的是0.6),则认为人脸识别成功,否则认为人脸识别失败。
三:其他
(1) ArcSoft的第三方库在项目中的路径如下:
(2) build.gradle中增加如下:






































苏公安网备 32011402010933号