虹软SDK的简单使用

Java实现人脸识别,但是又不会自己实现算法,找SDK时发现了虹软。虹软SDK具有免费、识别率高等优点,然后到网上搜这个SDK的教程,没搜到,就自己探索,发现它自带的官方文档其实介绍的挺全面的,本文是由官方文档改编。

虹软SDK的激活

首先到官网注册账号,然后新建应用,然后获取到APP_ID和SDK_KEY

然后点击下载SDK,下好之后的项目结构:

|---doc
| |---ARCSOFT_ARC_FACE_JAVA_DEVELOPER'S_GUIDE.pdf 开发说明文档
|---lib
|---|---Win32/x64/linux64
| |---|---libarcsoft_face.dll 算法库
| |---|---libarcsoft_face_engine.dll 引擎库
| |---|---libarcsoft_face_engine_jni.dll 引擎库
| |---arcsoft-sdk-face-3.0.0.0.jar java依赖库
|---samplecode
| |---FaceEngineTest.java 示例代码
|---releasenotes.txt 说明文件

这里介绍的是windows版本,要将这三个.dll文件所在的文件夹添加到环境变量里面的Path里面去

再把下载的文件里面的jar包导入到项目里面去

然后就可以开始激活了

import com.arcsoft.face.FaceEngine;
import com.arcsoft.face.enums.ErrorInfo; /**
* @author xuziao
* @date 2021/8/21 12:44
*/ public class FaceTestMd {
public void engineTest() {
String appId = "你的APP_ID";
String sdkKey = "你的SDK_KEY";
//实例化一个面部引擎
FaceEngine faceEngine = new FaceEngine();
//这个SDK几乎每一个调用引擎的操作都会返回一个int类型的结果,称为错误码,每一种错误码对应一种程序执行的结果,如果错误对应着错误类型,可以去官网查这个码对应的错误
int errorCode = faceEngine.activeOnline(appId, sdkKey);
if (errorCode != ErrorInfo.MOK.getValue() &&
errorCode != ErrorInfo.MERR_ASF_ALREADY_ACTIVATED.getValue()) {
System.out.println("引擎激活失败:" + errorCode);
} else {
System.out.println("引擎激活成功");
}
} public static void main(String[] args) {
new FaceTestMd().engineTest();
}
}

对引擎进行引擎配置和功能配置

package top.gostack.demo1;

import com.arcsoft.face.EngineConfiguration;
import com.arcsoft.face.FaceEngine;
import com.arcsoft.face.FunctionConfiguration;
import com.arcsoft.face.enums.DetectMode;
import com.arcsoft.face.enums.DetectOrient;
import com.arcsoft.face.enums.ErrorInfo; /**
* @author xuziao
* @date 2021/8/21 12:44
*/ public class FaceTestMd {
public void engineTest() {
String appId = "";
String sdkKey = ""; FaceEngine faceEngine = new FaceEngine(); int errorCode = faceEngine.activeOnline(appId, sdkKey);
if (errorCode != ErrorInfo.MOK.getValue() &&
errorCode != ErrorInfo.MERR_ASF_ALREADY_ACTIVATED.getValue()) {
System.out.println("引擎激活失败:" + errorCode);
} else {
System.out.println("引擎激活成功");
}
//以下为引擎配置
EngineConfiguration engineConfiguration = new EngineConfiguration(); //设置为单张高精度识别
engineConfiguration.setDetectMode(DetectMode.ASF_DETECT_MODE_IMAGE);
//人脸不旋转,为零度
engineConfiguration.setDetectFaceOrientPriority(DetectOrient.ASF_OP_0_ONLY);
//识别的最小人脸比例 = 图片长边 / 人脸框长边的比值
engineConfiguration.setDetectFaceScaleVal(16);
//设置最多能检测的人脸数量
engineConfiguration.setDetectFaceMaxNum(10); //以下为功能设置
final FunctionConfiguration functionConfiguration = new FunctionConfiguration();
//年龄检测
functionConfiguration.setSupportAge(true);
//启用支持人脸检测
functionConfiguration.setSupportFaceDetect(true);
//启用人脸识别
functionConfiguration.setSupportFaceRecognition(true);
//启用性别识别
functionConfiguration.setSupportGender(true);
//启用3D检测
functionConfiguration.setSupportFace3dAngle(true);
//启用RGB活体检测
functionConfiguration.setSupportLiveness(true);
//不启用IR活体检测
functionConfiguration.setSupportIRLiveness(false); //需要防止纸张、屏幕等攻击可以传入 supportLiveness 和 supportIRLiveness, RGB 和 IR 活体检测 //将额外的设置注入到引擎中
engineConfiguration.setFunctionConfiguration(functionConfiguration);
int errorCode2 = faceEngine.init(engineConfiguration);
if (errorCode2 != ErrorInfo.MOK.getValue()) {
System.out.println("初始化引擎失败");
} else {
System.out.println("初始化引擎成功");
}
} public static void main(String[] args) {
new FaceTestMd().engineTest();
}
}

经过以上的操作引擎初始化算是成功了,然后下面可以进行功能测试

人脸相似度比对

package top.gostack.demo1;

import com.arcsoft.face.*;
import com.arcsoft.face.enums.DetectMode;
import com.arcsoft.face.enums.DetectOrient;
import com.arcsoft.face.enums.ErrorInfo;
import com.arcsoft.face.toolkit.ImageInfo; import java.io.File;
import java.util.ArrayList;
import java.util.List; import static com.arcsoft.face.toolkit.ImageFactory.getRGBData; /**
* @author xuziao
* @date 2021/8/21 12:44
*/ public class FaceTestMd {
public void engineTest() {
String appId = "";
String sdkKey = ""; FaceEngine faceEngine = new FaceEngine(); int errorCode = faceEngine.activeOnline(appId, sdkKey);
if (errorCode != ErrorInfo.MOK.getValue() &&
errorCode != ErrorInfo.MERR_ASF_ALREADY_ACTIVATED.getValue()) {
System.out.println("引擎激活失败:" + errorCode);
} else {
System.out.println("引擎激活成功");
}
//以下为引擎配置
EngineConfiguration engineConfiguration = new EngineConfiguration(); //设置为单张高精度识别
engineConfiguration.setDetectMode(DetectMode.ASF_DETECT_MODE_IMAGE);
//人脸不旋转,为零度
engineConfiguration.setDetectFaceOrientPriority(DetectOrient.ASF_OP_0_ONLY);
//识别的最小人脸比例 = 图片长边 / 人脸框长边的比值
engineConfiguration.setDetectFaceScaleVal(16);
//设置最多能检测的人脸数量
engineConfiguration.setDetectFaceMaxNum(10); //以下为功能设置
final FunctionConfiguration functionConfiguration = new FunctionConfiguration();
//年龄检测
functionConfiguration.setSupportAge(true);
//启用支持人脸检测
functionConfiguration.setSupportFaceDetect(true);
//启用人脸识别
functionConfiguration.setSupportFaceRecognition(true);
//启用性别识别
functionConfiguration.setSupportGender(true);
//启用3D检测
functionConfiguration.setSupportFace3dAngle(true);
//启用RGB活体检测
functionConfiguration.setSupportLiveness(true);
//不启用IR活体检测
functionConfiguration.setSupportIRLiveness(false); //需要防止纸张、屏幕等攻击可以传入 supportLiveness 和 supportIRLiveness, RGB 和 IR 活体检测 //将额外的设置注入到引擎中
engineConfiguration.setFunctionConfiguration(functionConfiguration); int errorCode2 = faceEngine.init(engineConfiguration);
if (errorCode2 != ErrorInfo.MOK.getValue()) {
System.out.println("初始化引擎失败");
} else {
System.out.println("初始化引擎成功");
} String imgPath1 = "F:/Spring/SpringMVC/workplace/Ace_Face/resources/wyy1.jpg";
String imgPath2 = "F:/Spring/SpringMVC/workplace/Ace_Face/resources/wyy4.jpg"; FaceFeature faceFeature1 = getFaceFeature(imgPath1, faceEngine);
FaceFeature faceFeature2 = getFaceFeature(imgPath2, faceEngine); //人脸相似度
FaceSimilar faceSimilar = new FaceSimilar();
int errorCode5 = faceEngine.compareFaceFeature(faceFeature1, faceFeature2, faceSimilar);
if (errorCode5 != ErrorInfo.MOK.getValue()) {
System.out.println("人脸对比操作失败!");
} else {
System.out.println("人脸对比成功!");
System.out.println("人脸相似度:" + faceSimilar.getScore());
}
} /**
*
* @param imgPath 传入图片的地址
* @param faceEngine 传入引擎
* @return faceFeature 输出的人脸特征信息
*/ public FaceFeature getFaceFeature(String imgPath, FaceEngine faceEngine) {
//这个getRGBData方法内部调用了几个awt包里面的方法来处理图像数据,由此得到图像数据
ImageInfo imageInfo = getRGBData(new File(imgPath));
//新建一个人脸信息列表,获取到的人脸信息将储存在这个列表里面
List<FaceInfo> faceInfoList = new ArrayList<>(); //向引擎传入从图片分离的信息数据
int errorCode3 = faceEngine.detectFaces(imageInfo.getImageData(), imageInfo.getWidth(), imageInfo.getHeight(),
imageInfo.getImageFormat(), faceInfoList); if (errorCode3 != ErrorInfo.MOK.getValue()) {
System.out.println("数据传入失败");
} else {
System.out.println("数据传入成功");
System.out.println(faceInfoList);
} //下面提取人脸特征
FaceFeature faceFeature = new FaceFeature();
int errorCode4 = faceEngine.extractFaceFeature(imageInfo.getImageData(),
imageInfo.getWidth(), imageInfo.getHeight(), imageInfo.getImageFormat(),
faceInfoList.get(0), faceFeature);
if (errorCode4 != ErrorInfo.MOK.getValue()) {
System.out.println("人脸特征提取失败!");
} else {
System.out.println("人脸特征提取成功!");
System.out.println("特征值大小:" + faceFeature.getFeatureData().length);
} return faceFeature;
} public static void main(String[] args) {
new FaceTestMd().engineTest();
}
}

其余的还有年龄检测,活体检测,性别检测等等,其中在官方文档里面介绍的很清楚,我就不再一一赘述了,直接放上的测试代码和结果

我的测试代码

package top.gostack.demo1;

import com.arcsoft.face.*;
import com.arcsoft.face.enums.DetectMode;
import com.arcsoft.face.enums.DetectOrient;
import com.arcsoft.face.enums.ErrorInfo;
import com.arcsoft.face.toolkit.ImageInfo; import java.io.File;
import java.util.ArrayList;
import java.util.List; import static com.arcsoft.face.toolkit.ImageFactory.getRGBData; /**
* @author xuziao
* @date 2021/8/19 19:12
*/ public class FaceTest {
public void engineTest() {
String appId = "";
String sdkKey = ""; FaceEngine faceEngine = new FaceEngine(); int errorCode = faceEngine.activeOnline(appId, sdkKey);
if (errorCode != ErrorInfo.MOK.getValue() &&
errorCode != ErrorInfo.MERR_ASF_ALREADY_ACTIVATED.getValue())
{
System.out.println("引擎激活失败:"+errorCode);
} else {
System.out.println("引擎激活成功");
} //以下为引擎配置
EngineConfiguration engineConfiguration = new EngineConfiguration(); //设置为单张高精度识别
engineConfiguration.setDetectMode(DetectMode.ASF_DETECT_MODE_IMAGE);
//人脸不旋转,为零度
engineConfiguration.setDetectFaceOrientPriority(DetectOrient.ASF_OP_0_ONLY);
//识别的最小人脸比例 = 图片长边 / 人脸框长边的比值
engineConfiguration.setDetectFaceScaleVal(16);
//设置最多能检测的人脸数量
engineConfiguration.setDetectFaceMaxNum(10); //以下为功能设置
final FunctionConfiguration functionConfiguration = new FunctionConfiguration();
//年龄检测
functionConfiguration.setSupportAge(true);
//启用支持人脸检测
functionConfiguration.setSupportFaceDetect(true);
//启用人脸识别
functionConfiguration.setSupportFaceRecognition(true);
//启用性别识别
functionConfiguration.setSupportGender(true);
//启用3D检测
functionConfiguration.setSupportFace3dAngle(true);
//启用RGB活体检测
functionConfiguration.setSupportLiveness(true);
//不启用IR活体检测
functionConfiguration.setSupportIRLiveness(false); //需要防止纸张、屏幕等攻击可以传入 supportLiveness 和 supportIRLiveness, RGB 和 IR 活体检测 //将额外的设置注入到引擎中
engineConfiguration.setFunctionConfiguration(functionConfiguration); int errorCode2 = faceEngine.init(engineConfiguration);
if (errorCode2 != ErrorInfo.MOK.getValue()) {
System.out.println("初始化引擎失败");
} else {
System.out.println("初始化引擎成功");
} String imgPath1 = "F:/Spring/SpringMVC/workplace/Ace_Face/resources/wyy1.jpg";
String imgPath2 = "F:/Spring/SpringMVC/workplace/Ace_Face/resources/wyy4.jpg"; FaceFeature faceFeature1 = getFaceFeature(imgPath1, faceEngine);
FaceFeature faceFeature2 = getFaceFeature(imgPath2, faceEngine); //人脸相似度
FaceSimilar faceSimilar = new FaceSimilar();
int errorCode5 = faceEngine.compareFaceFeature(faceFeature1, faceFeature2, faceSimilar);
if (errorCode5 != ErrorInfo.MOK.getValue()) {
System.out.println("人脸对比操作失败!");
} else {
System.out.println("人脸对比成功!");
System.out.println("人脸相似度:" + faceSimilar.getScore());
} int errorCode11 = faceEngine.unInit();
if (errorCode11 != ErrorInfo.MOK.getValue()) {
System.out.println("引擎卸载失败,错误码:");
System.out.println(errorCode);
} else {
System.out.println("引擎卸载成功!");
} } /**
*
* @param imgPath 传入图片的地址
* @param faceEngine 传入引擎
* @return faceFeature 输出的人脸特征信息
*/ public FaceFeature getFaceFeature(String imgPath, FaceEngine faceEngine) {
//这个getRGBData方法内部调用了几个awt包里面的方法来处理图像数据,由此得到图像数据
ImageInfo imageInfo = getRGBData(new File(imgPath));
//新建一个人脸信息列表,获取到的人脸信息将储存在这个列表里面
List<FaceInfo> faceInfoList = new ArrayList<>(); //向引擎传入从图片分离的信息数据
int errorCode3 = faceEngine.detectFaces(imageInfo.getImageData(), imageInfo.getWidth(), imageInfo.getHeight(),
imageInfo.getImageFormat(), faceInfoList); if (errorCode3 != ErrorInfo.MOK.getValue()) {
System.out.println("数据传入失败");
} else {
System.out.println("数据传入成功");
System.out.println(faceInfoList);
}
//以下实现属性提取,提取某个属性要启用相关的功能
FunctionConfiguration functionConfiguration = new FunctionConfiguration();
functionConfiguration.setSupportAge(true);
functionConfiguration.setSupportFace3dAngle(true);
functionConfiguration.setSupportGender(true);
functionConfiguration.setSupportLiveness(true); faceEngine.process(imageInfo.getImageData(), imageInfo.getWidth(),
imageInfo.getHeight(), imageInfo.getImageFormat(), faceInfoList, functionConfiguration); //下面提取属性,首先实现process接口
int errorCode6 = faceEngine.process(imageInfo.getImageData(), imageInfo.getWidth(),
imageInfo.getHeight(), imageInfo.getImageFormat(), faceInfoList, functionConfiguration);
if (errorCode6 != ErrorInfo.MOK.getValue()) {
System.out.println("process接口调用失败,错误码:"+errorCode6);
} else {
System.out.println("process接口调用成功!");
} //年龄检测
//创建一个存储年龄的列表
List<AgeInfo> ageInfoList = new ArrayList<>();
int errorCode7 = faceEngine.getAge(ageInfoList);
if (errorCode7 != ErrorInfo.MOK.getValue()) {
System.out.print("获取年龄失败,错误码:");
System.out.println(errorCode7);
} else {
System.out.println("年龄获取成功!");
System.out.println("年龄:" + ageInfoList.get(0).getAge());
} //以下为性别检测
//性别检测
List<GenderInfo> genderInfoList = new ArrayList<GenderInfo>();
int errorCode8 = faceEngine.getGender(genderInfoList);
if (errorCode8 != ErrorInfo.MOK.getValue()) {
System.out.print("获取性别失败,错误码:");
System.out.println(errorCode8);
} else {
System.out.println("性别获取成功!");
System.out.println("性别:" + genderInfoList.get(0).getGender());
} //3D信息检测
List<Face3DAngle> face3DAngleList = new ArrayList<Face3DAngle>();
int errorCode9 = faceEngine.getFace3DAngle(face3DAngleList);
if (errorCode9 != ErrorInfo.MOK.getValue()) {
System.out.println("3D信息检测失败,错误码:"+errorCode9);
} else {
System.out.println("3D信息获取成功!");
System.out.println("3D角度:" + face3DAngleList.get(0).getPitch() + "," +
face3DAngleList.get(0).getRoll() + "," + face3DAngleList.get(0).getYaw());
}
//活体检测
List<LivenessInfo> livenessInfoList = new ArrayList<LivenessInfo>();
int errorCode10 = faceEngine.getLiveness(livenessInfoList);
if (errorCode10 != ErrorInfo.MOK.getValue()) {
System.out.println("活体检测失败,错误码:"+errorCode10);
} else {
System.out.println("活体检测成功");
System.out.println("活体:" + livenessInfoList.get(0).getLiveness());
} //下面提取人脸特征
FaceFeature faceFeature = new FaceFeature();
int errorCode4 = faceEngine.extractFaceFeature(imageInfo.getImageData(),
imageInfo.getWidth(), imageInfo.getHeight(), imageInfo.getImageFormat(),
faceInfoList.get(0), faceFeature);
if (errorCode4 != ErrorInfo.MOK.getValue()) {
System.out.println("人脸特征提取失败!");
} else {
System.out.println("人脸特征提取成功!");
System.out.println("特征值大小:" + faceFeature.getFeatureData().length);
} return faceFeature;
} public static void main(String[] args) {
new FaceTest().engineTest();
}
}

运行结果:

引擎激活成功
初始化引擎成功
数据传入成功
[com.arcsoft.face.Rect(332, 152 - 924, 744),1]
process接口调用成功!
年龄获取成功!
年龄:21
性别获取成功!
性别:1
3D信息获取成功!
3D角度:-15.243982,21.721786,16.343493
活体检测成功
活体:1
人脸特征提取成功!
特征值大小:1032
数据传入成功
[com.arcsoft.face.Rect(345, 163 - 942, 760),1]
process接口调用成功!
年龄获取成功!
年龄:20
性别获取成功!
性别:1
3D信息获取成功!
3D角度:-11.13369,0.69471675,17.24398
活体检测成功
活体:1
人脸特征提取成功!
特征值大小:1032
人脸对比成功!
人脸相似度:0.9812902
引擎卸载成功!

最后注意,要学习这个SDK一定去看官方文档,写的很详细!

软虹sdk基本使用的更多相关文章

  1. Android 第三方加固方案 对比 MD

    常见的第三方加固方案官网介绍 由于安卓APP是基于Java的,所以极容易被破解,一个不经过加固的APP犹如裸奔一样,毫无防备.之前曾有新闻报道,一些专职的APP打包黑产就是专门从各种渠道找到apk,通 ...

  2. 安卓直播开源: RTMP 推流SDK

    前些日子在github上提交了基于GPUImage的IOS直播推流SDK(https://github.com/runner365/GPUImageRtmpPush) 最近整理了android直播推流 ...

  3. Android IOS WebRTC 音视频开发总结(八十一)-- WebRTC靠谱吗?有没有适合的SDK推荐?

    作者:blaker,最早发表在我们的微信公众和[编风网],详见[这里] 支持原创,转载必须注明出处,欢迎关注我的微信公众号blacker(微信ID:blackerteam 或 webrtcorgcn) ...

  4. Android中直播视频技术探究之---采集摄像头Camera视频源数据进行推流(采用金山云SDK)

    一.前言 在之前已经详细介绍了Android中的一种视频数据源:Camera,不了解的同学可以点击进入:Android中Camera使用详解 ,在这篇文章中我们介绍了如何采集摄像头的每一帧数据,然后进 ...

  5. Android开发实例详解之IMF(Android SDK Sample—SoftKeyboard)

    本博前面的文章介绍了Android开发环境的搭建和模拟器的常用操作.本次,将以Android Sample中经典的SoftKeyboard项目为例,详细解析Android上一个小型项目的开发过程和注意 ...

  6. 【SDK编程】

    #include <stdio.h> #include <windows.h> int main() { DeleteFile("C:\\test.txt" ...

  7. 与众不同 windows phone (24) - Input(输入)之软键盘类型, XNA 方式启动软键盘, UIElement 的 Touch 相关事件, 触摸涂鸦

    原文:与众不同 windows phone (24) - Input(输入)之软键盘类型, XNA 方式启动软键盘, UIElement 的 Touch 相关事件, 触摸涂鸦 [索引页][源码下载] ...

  8. LanSoEditor_common ---android平台的视频编辑SDK

    当前版本是LanSoEditor-v1.4 主要使用在音视频的: 裁剪,剪切,分离,合并,转换,拼接,水印,叠加,混合,转码等场合; 我们是针对android平台对ffmpeg做了硬件加速优化,经过多 ...

  9. 安卓平台 全面支持软解和硬解的SDK-Demo源代码开放

    专业做视频编解码的SDK开发工作. 2015年12月1日10:46:55: 更新到1.5.0版本 功能列表: 基本播放: 1,正常播放, 支持MP4,FLV,AVI,TS,3GP,RMVB,WM,WM ...

随机推荐

  1. 【UE4 设计模式】设计模式一些概念

    定义 设计模式是一套被反复使用的.多数人知晓的.经过分类编目的.代码设计经验的总结. 使用设计模式是为了重用代码.让代码更容易被他人理解.保证代码可靠性. 四人帮 GOF ( Gang of Four ...

  2. 《Spring源码深度解析》学习笔记——Spring的整体架构与容器的基本实现

    pring框架是一个分层架构,它包含一系列的功能要素,并被分为大约20个模块,如下图所示 这些模块被总结为以下几个部分: Core Container Core Container(核心容器)包含有C ...

  3. [技术博客]使用pylint实现django项目的代码风格检查

    使用pylint实现django项目的代码风格检查 前言 ​ 一个项目大多都是由一个团队来完成,如果没有统一的代码规范,那么每个人的代码的风格必定会有很大的差别.且不说会存在多个人同时开发同一模块的情 ...

  4. BUAA-OO-JML

    BUAA-OO-JML JML 概念与 toolchain JML 是一种为 Java 程序设计的.遵循 design by contract 范式的.基于 Hoare Logic 构建的 behav ...

  5. Noip模拟14 2021.7.13

    T1 队长快跑 本身dp就不强的小马看到这题并未反映过来是个dp(可能是跟题面太过于像那个黑题的队长快跑相似) 总之,基础dp也没搞出来,不过这题倒是启发了小马以后考试要往dp哪里想想 $dp_{i, ...

  6. 【BZOJ 1419】Red is good [概率DP]

    我 是 Z Z 概率好玄啊(好吧是我太弱.jpg Description 桌面上有R张红牌和B张黑牌,随机打乱顺序后放在桌面上,开始一张一张地翻牌,翻到红牌得到1美元,黑牌则付出1美元.可以随时停止翻 ...

  7. UVA-1016 Silly Sort

    UVA-1016 题目大意:给定一个长度为n的序列,每次操作可以交换任意两个数的位置,代价为两个数的和,求最小代价,将序列排成有序的. 首先,显然需要交换的数一定会形成环: 那么对于每一个环,我们有两 ...

  8. poj 2226 Muddy Fields(最小点覆盖)

    题意: M*N的矩阵,每个格不是*就是#.     *代表水坑,#代表草地. 农民要每次可以用一块宽为1,长不限的木板去铺这个矩阵.要求这块木板不能覆盖草地.木板可以重复覆盖(即一块木板与另一块木板有 ...

  9. Spring Cloud Alibaba 使用RestTemplate进行服务消费

    创建服务提供者工程 创建spring-cloud-alibaba-service-member工程,会员中心服务该服务提供用户会员信息. pom.xml <?xml version=" ...

  10. jQuery中onload与ready区别

    onload和ready的区别document.ready和onload的区别为:加载程度不同.执行次数不同.执行速度不同.1.加载程度不同 document.ready:是DOM结构绘制完毕后就执行 ...