基本使用

人脸识别基本流程

1. (RGB Camera)人脸识别

RGB Camera人脸识别是指用普通的彩色摄像头做活体判断,然后提取人脸特征并比对。

../_images/monc.jpg

伪代码:

DfaceDetect dfaceDetect = new DfaceDetect(dfaceConfig);
DfaceQuality dfaceQuality = new DfaceQuality(dfaceConfig);
DfaceRGBLiveness dfaceRGBLiveness = new DfaceRGBLiveness(dfaceConfig);
DfaceFeature dfaceFeature = new DfaceFeature(dfaceConfig);

//检测人脸,返回人脸box
List<Box> maxFace = dfaceDetect.detectMaxFace(frame);

//判断人脸质量
float qualityScor = faceQuality.predictQuality(frame, maxFace);
if(qualityScore < 0.5){
    print("人脸质量太低");
    return;
}
//RGB活体检测
float liveScore = dfaceRGBLiveness.livenessCheck(frame, maxFace);
if(liveScore < 0.76){
    print("假脸");
    return;
}
//提取人脸特征
byte[] feature = dfaceFeature.extractFeature(frame, maxFace);
//根据特征寻找相似度最高用户
User matchedUser = findTopSimilarityUser(feature);

2. (RGB-IR Camera)人脸识别(推荐使用)

(RGB-IR Camera)人脸识别是指用双目摄像头(彩色+红外)做活体判断,然后提取人脸特征并比对。

../_images/bion.jpg

伪代码:

DfaceDetect dfaceDetect = new DfaceDetect(dfaceConfig);
DfaceQuality dfaceQuality = new DfaceQuality(dfaceConfig);
DfaceIRLiveness dfaceIRLiveness = new DfaceRGBLiveness(dfaceConfig);

//检测RGB画面最大人脸
Box rgbBox = dfaceDetect.detectMaxFace(rgbFrame);
//检测IR画面所有人脸
List<Box> irBoxes = dfaceDetect.detect(irFrame);
//找到跟rgbBox最近的irBox
Box irBox = getNearestIRBox(rgbBox, irBoxes);

//计算rgbBox和irBox重叠区域比值
float iou = calcIOUBetweenTwoBox(rgbBox, irBox);
if(iou < 0.15>){
    print("iou没有满足条件");
    return;
}

//判断人脸质量
float qualityScor = dfaceQuality.predictQuality(irFrame, irBox);
if(qualityScore < 0.45){
    print("人脸质量太低");
    return;
}

//IR活体检测
float liveScore = dfaceIRLiveness.livenessCheck(irFrame, irBox);
if(liveScore < 0.65){
    print("假脸");
    return;
}

DFACE各个功能模块使用

SDK提供人脸检测,人脸特征码提取,活体判断,人脸关键点等各个独立的功能提供相应的处理模块,用户可以按照自己的需求灵活地调用。

1. DfaceDetect

DfaceDetect是人脸检测模块,可实时的实现多人检测,最大脸检测。

DfaceDetect dfaceDetect = new DfaceDetect(dfaceConfig);
//检测所有人脸,返回人脸box列表
List<Box> faceBoxes = dfaceDetect.detectFace(frame);
//检测最大人脸
Box maxFaceBox = dfaceDetect.detectMaxFace(frame);

2. DfaceTracker

DfaceTracker是人脸跟踪模块,可实现实时多人跟踪功能。

//检测人脸
List<Box> faceBoxes = dfaceDetect.detectFace(frame);
//跟踪人脸,需要以上检测人脸的边框输入
List<Box> trackBoxes = dfaceTracker.update(frame, faceBoxes);
for(Box faceBox : trackBoxes){
    //获取track id, 每个人脸跟踪期间保持唯一的track id
    int trackId = faceBox.id;
}

警告

跟踪是预估判断,因此返回的位置信息有可能会超出摄像头画面范围。单独使用最好和摄像头画面范围做下判断,以免越界访问。

3. DfaceFeature

DfaceFeature是人脸特征提取模块,主要用于人脸特征提取。

DfaceConfig dfaceConfig = new DfaceConfig();
//配置特征模型版本,目前支持的人脸特征版本, V3(精度最高), V6_MASK(支持戴口罩的人脸识别)
dfaceConfig.setFeatureVersion(kFEATURE_V3);

DfaceFeature dfaceFeature = new DfaceFeature(dfaceConfig);
//提取人脸特征
byte[] feature = dfaceFeature.extractFeature(frame, maxFaceBox);

4. DfaceLandmarks

DfaceLandmarks是人脸关键点模块,主要用于人脸106关键点和姿态的判断。

DfaceLandmarks dfaceLandmarks = new DfaceLandmarks(dfaceConfig);

//判断106关键点和姿态
LandMarkInfo landMarkInfo = dfaceLandmarks.detectLandmarks(frame, maxFaceBox);
//106个关键点,x0,y0,x1,y1...x105,y105
int[] landmarks = landMarkInfo.landMarks;
//欧拉角yaw
float yaw = landMarkInfo.yaw;
//欧拉角pitch
float pitch = landMarkInfo.pitch;
//欧拉角roll
float roll = landMarkInfo.roll;
//人脸和摄像头的x(横向)偏移
float xOffset = landMarkInfo.xOffset;
//人脸和摄像头的y(纵向)偏移
float yOffset = landMarkInfo.yOffset;
//人脸和摄像头的距离
float zOffset = landMarkInfo.zOffset;

5. DfaceRGBLiveness

DfaceRGBLiveness是RGB活体检测处理模块,常用于活体判断。

DfaceRGBLiveness dfaceRGBLiveness = new dfaceRGBLiveness(dfaceConfig);
//活体判断,输入图片需要减少压缩损失
float score = dfaceRGBLiveness.livenessCheck(frame, maxFaceBox);

6. DfaceIRLiveness

DfaceIRLiveness是红外活体检测处理模块,常用于红外活体判断。

DfaceIRLiveness dfaceIRLiveness = new DfaceIRLiveness(dfaceConfig);
//活体判断
float score = dfaceIRLiveness.livenessCheck(irFrame, irFaceBox);

7. DfaceQuality

DfaceQuality是人脸质量评估模块。

//工厂模式创建DfaceUnitQuality
DfaceQuality dfaceQuality = new DfaceQuality(dfaceConfig);
//质量判断
float score = dfaceQuality.predictQuality(frame, maxFaceBox);

8. DfaceMask

DfaceMask是人脸口罩检测和遮挡检测。

DfaceMask dfaceMask = new DfaceMask(dfaceConfig);
//口罩判断
float score = dfaceMask.predictMask(frame, maxFaceBox);
//眼睛遮挡值
float eyeCoverScore = predictCover(frame, maxFaceBox, kFACE_EYE);
//鼻子遮挡值
float eyeCoverScore = predictCover(frame, maxFaceBox, kFACE_NOSE);
//嘴巴遮挡值
float eyeCoverScore = predictCover(frame, maxFaceBox, kFACE_MOUTH);

9. DfaceTool

DfaceTool工具类,包含了一些图像处理,特征转换和数据处理方法。