公司项目需要人脸识别登录,需要支持离线识别,所以无法使用在线的人脸识别的API,于是使用到了离线SDK来对比识别人脸相识度.
获取人脸抓拍的图片需要对接设备,这里不做记录,假设我们已经获取到了人脸图片,只调用SDK将抓拍的人脸图片与已有的进行人脸库图片对比,具体步骤如下:
1. 注册账号,实名认证
https://ai.arcsoft.com.cn/ucenter/user/userlogin
2. 创建应用,在我的应用里面添加SDK,下载sdk到本地
3. 解压SDK,将SDK下的libs文件夹的库文件放到jdk下的lib文件夹下
4. 将SDK libs文件夹下的jar文件导入项目,然后buildpath
5. 编写测试代码,进行人脸比对
package net.nblh.faceRecognition.util.faceCompare; import static com.arcsoft.face.toolkit.ImageFactory.getRGBData; import java.io.File; import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; import com.arcsoft.face.EngineConfiguration; import com.arcsoft.face.FaceEngine; import com.arcsoft.face.FaceFeature; import com.arcsoft.face.FaceInfo; import com.arcsoft.face.FaceSimilar; import com.arcsoft.face.FunctionConfiguration; import com.arcsoft.face.enums.DetectMode; import com.arcsoft.face.enums.DetectOrient; import com.arcsoft.face.enums.ErrorInfo; import com.arcsoft.face.enums.ImageFormat; import com.arcsoft.face.toolkit.ImageInfo; /** * 使用虹软SDK 进行人脸比对 * @author qxl */ public class ArcFaceCompare { /** * 初始化引擎 * @param appId * @param sdkKey * @return */ public static FaceEngine initFaceEngine(String appId,String sdkKey) { FaceEngine faceEngine = new FaceEngine(); // 激活引擎 int activeCode = faceEngine.activeOnline(appId, sdkKey); if (activeCode != ErrorInfo.MOK.getValue() && activeCode != ErrorInfo.MERR_ASF_ALREADY_ACTIVATED.getValue()) { System.out.println("引擎激活失败"); return null; } // 引擎配置 EngineConfiguration engineConfiguration = new EngineConfiguration(); engineConfiguration.setDetectMode(DetectMode.ASF_DETECT_MODE_IMAGE); engineConfiguration.setDetectFaceOrientPriority(DetectOrient.ASF_OP_0_ONLY); // 功能配置 FunctionConfiguration functionConfiguration = new FunctionConfiguration(); functionConfiguration.setSupportAge(true); functionConfiguration.setSupportFace3dAngle(true); functionConfiguration.setSupportFaceDetect(true); functionConfiguration.setSupportFaceRecognition(true); functionConfiguration.setSupportGender(true); functionConfiguration.setSupportLiveness(true); functionConfiguration.setSupportIRLiveness(true); engineConfiguration.setFunctionConfiguration(functionConfiguration); // 初始化引擎 int initCode = faceEngine.init(engineConfiguration); if (initCode != ErrorInfo.MOK.getValue()) { System.out.println("初始化引擎失败"); return null; } return faceEngine; } /** * 人脸图片比对 * @param faceEngine 人脸识别引擎对象 * @param picPath1 人脸图片1路径 * @param picPath2 人脸图片2路径 * @return */ public static float faceCompare(FaceEngine faceEngine,String picPath1,String picPath2) { // 人脸检测 ImageInfo imageInfo = getRGBData(new File(picPath1)); List<FaceInfo> faceInfoList = new ArrayList<FaceInfo>(); int detectCode = faceEngine.detectFaces(imageInfo.getImageData(), imageInfo.getWidth(), imageInfo.getHeight(), ImageFormat.CP_PAF_BGR24, faceInfoList); //System.out.println(faceInfoList); // 特征提取 FaceFeature faceFeature = new FaceFeature(); int extractCode = faceEngine.extractFaceFeature(imageInfo.getImageData(), imageInfo.getWidth(), imageInfo.getHeight(), ImageFormat.CP_PAF_BGR24, faceInfoList.get(0), faceFeature); //System.out.println("特征值大小:" + faceFeature.getFeatureData().length); // 人脸检测2 ImageInfo imageInfo2 = getRGBData(new File(picPath2)); List<FaceInfo> faceInfoList2 = new ArrayList<FaceInfo>(); int detectCode2 = faceEngine.detectFaces(imageInfo2.getImageData(), imageInfo2.getWidth(), imageInfo2.getHeight(), ImageFormat.CP_PAF_BGR24, faceInfoList2); //System.out.println(faceInfoList); // 特征提取2 FaceFeature faceFeature2 = new FaceFeature(); int extractCode2 = faceEngine.extractFaceFeature(imageInfo2.getImageData(), imageInfo2.getWidth(), imageInfo2.getHeight(), ImageFormat.CP_PAF_BGR24, faceInfoList2.get(0), faceFeature2); //System.out.println("特征值大小:" + faceFeature.getFeatureData().length); // 特征比对 FaceFeature targetFaceFeature = new FaceFeature(); targetFaceFeature.setFeatureData(faceFeature.getFeatureData()); FaceFeature sourceFaceFeature = new FaceFeature(); sourceFaceFeature.setFeatureData(faceFeature2.getFeatureData()); FaceSimilar faceSimilar = new FaceSimilar(); int compareCode = faceEngine.compareFaceFeature(targetFaceFeature, sourceFaceFeature, faceSimilar); float score = faceSimilar.getScore(); BigDecimal b = new BigDecimal(score); //四舍五入,保留四位小数 float similarity =b.setScale(4,BigDecimal.ROUND_HALF_UP).floatValue(); System.out.println("相似度:" + similarity); return similarity; } /** * 卸载引擎 * @param faceEngine */ public static void uninstall(FaceEngine faceEngine) { faceEngine.unInit(); } public static void main(String[] args) { String appId = "图2创建应用的APP ID"; String sdkKey = "图2创建应用的SDK Key"; //初始化引擎 FaceEngine faceEngine = initFaceEngine(appId,sdkKey); //进行人脸比对 String picPath1 = "F:\\qxlTest\\1.jpg"; String picPath2 = "F:\\qxlTest\\9.jpg"; float similarity = faceCompare(faceEngine, picPath1, picPath2); //卸载引擎 uninstall(faceEngine); } }
6.运行main方法测试
注意事项:
A: 虹软人脸识别离线 SDK 每台机器对应一个SDK,第一次激活SDK会与机器绑定,不同的机器需要下载新的SDK
B: 离线 SDK自下载之日起,有效期一年,一年使用时间过后,需要重新下载最新版,同时更新对应的 dll文件, appid 和sdkkey,更新之后请更新以上的APP ID 和 SDK KEY
C: 具体详见官网 https://ai.arcsoft.com.cn/
来源:博客园
作者:fy_qxl
链接:https://www.cnblogs.com/huaixiaonian/p/11634136.html