Java使用虹软SDK实现人脸检测、特征提取、比对

2023-10-12 02:50

本文主要是介绍Java使用虹软SDK实现人脸检测、特征提取、比对,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

最近公司有个业务场景是需要用到人脸识别功能的,正好趁此机会写下这篇文章,以巩固自己不精的技能~

话不多说,开干!

1.环境准备:JDK1.8 + SpringBoot + Maven

2.下载虹软SDK

前往虹软开发者中心

新建应用--填写一些基本信息,完成后如下图

点击下载,然后解压文件,你会得到下图

我们主要是需要libs文件夹下的文件

3.引入jar包

        虹软并没有为spring boot 提供 maven 的引入方式,所以你需要手动将他的jar包集成到本地

        3.1src 同级目录下创建libs 文件夹,将虹软的jar包放到这个文件中

        3.2其次在 pom.xml 文件中将这个jar包引入到项目中

<dependency><groupId>com.arcsoft.face</groupId><artifactId>arcsoft-sdk-face</artifactId><version>3.0.0.0</version><scope>system</scope><systemPath>${basedir}/libs/arcsoft-sdk-face-3.0.0.0.jar</systemPath>
</dependency>

        3.3允许你的项目在打包发布后仍然可以调用本地路径下的jar包

<build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><!-- 加入下面这一行 --><includeSystemScope>true</includeSystemScope></configuration></plugin></plugins>
</build>

4.集成到项目

4.1application.yml配置

arcsoft:appid: *********#你注册应用后所对应的APP_IDsdkkey: ********#你注册应用后所对应的SDK_KEYlibpath: *******#libs目录下的dll文件夹路径,如D:\\libs\\WIN64engine-configuration:       #引擎配置detectMode: IMAGEdetectFaceOrientPriority: ASF_OP_ALL_OUTdetectFaceScale: 32detectFaceMaxNum: 8function-configuration:     #功能配置supportAge: truesupportFace3dAngle: truesupportFaceDetect: truesupportFaceRecognition: truesupportGender: truesupportLiveness: truesupportIRLiveness: true

4.2引擎类

@Data
@ConfigurationProperties(prefix = "arcsoft.engine-configuration")
public class EngineConfigurationProperty {private String detectMode;private String detectFaceOrientPriority;private Integer detectFaceScale;private Integer detectFaceMaxNum;
}

4.3功能类

@Data
@ConfigurationProperties(prefix = "arcsoft.function-configuration")
public class FunConfigurationProperty {private boolean supportFace3dAngle = true;private boolean supportFaceDetect = true;private boolean supportFaceRecognition = true;private boolean supportGender = true;private boolean supportAge = true;private boolean supportLiveness = true;private boolean supportIRLiveness = true;
}

4.4初始化配置类

@Data
@Configuration
@ConfigurationProperties(prefix = "arcsoft")
@EnableConfigurationProperties({ FunConfigurationProperty.class,EngineConfigurationProperty.class})
public class ArcSoftConfig {@Autowiredprivate FunConfigurationProperty funConfigurationProperty;@Autowiredprivate EngineConfigurationProperty engineConfigurationProperty;private String appid;private String sdkkey;private String libpath;@Beanpublic FaceEngine faceEngine(){FaceEngine faceEngine = new FaceEngine(libpath);int errorCode = faceEngine.activeOnline(appid, sdkkey);if (errorCode != ErrorInfo.MOK.getValue() &&errorCode != ErrorInfo.MERR_ASF_ALREADY_ACTIVATED.getValue())throw new RuntimeException("引擎注册失败");EngineConfiguration engineConfiguration = getFaceEngineConfiguration();//初始化引擎errorCode = faceEngine.init(engineConfiguration);if (errorCode != ErrorInfo.MOK.getValue())throw new RuntimeException("初始化引擎失败");return faceEngine;}/*** 初始化引擎配置* @return*/private EngineConfiguration getFaceEngineConfiguration() {EngineConfiguration engineConfiguration = new EngineConfiguration();//配置引擎模式if ("IMAGE".equals(engineConfigurationProperty.getDetectMode()))engineConfiguration.setDetectMode(DetectMode.ASF_DETECT_MODE_IMAGE);elseengineConfiguration.setDetectMode(DetectMode.ASF_DETECT_MODE_VIDEO);//配置人脸角度 全角度 ASF_OP_ALL_OUT 不够准确且检测速度慢switch (engineConfigurationProperty.getDetectFaceOrientPriority()){case "ASF_OP_0_ONLY":engineConfiguration.setDetectFaceOrientPriority(DetectOrient.ASF_OP_0_ONLY);break;case "ASF_OP_90_ONLY":engineConfiguration.setDetectFaceOrientPriority(DetectOrient.ASF_OP_90_ONLY);break;case "ASF_OP_270_ONLY":engineConfiguration.setDetectFaceOrientPriority(DetectOrient.ASF_OP_270_ONLY);break;case "ASF_OP_180_ONLY":engineConfiguration.setDetectFaceOrientPriority(DetectOrient.ASF_OP_180_ONLY);break;case "ASF_OP_ALL_OUT":engineConfiguration.setDetectFaceOrientPriority(DetectOrient.ASF_OP_ALL_OUT);break;default:engineConfiguration.setDetectFaceOrientPriority(DetectOrient.ASF_OP_ALL_OUT);}//设置识别的最小人脸比engineConfiguration.setDetectFaceScaleVal(engineConfigurationProperty.getDetectFaceScale());engineConfiguration.setDetectFaceMaxNum(engineConfigurationProperty.getDetectFaceMaxNum());//功能配置initFuncConfiguration(engineConfiguration);return engineConfiguration;}/*** 功能配置* @param engineConfiguration*/private void initFuncConfiguration(EngineConfiguration engineConfiguration){FunctionConfiguration functionConfiguration = new FunctionConfiguration();//是否支持年龄检测functionConfiguration.setSupportAge(funConfigurationProperty.isSupportAge());//是否支持3d 检测functionConfiguration.setSupportFace3dAngle(funConfigurationProperty.isSupportFace3dAngle());//是否支持人脸检测functionConfiguration.setSupportFaceDetect(funConfigurationProperty.isSupportFaceDetect());//是否支持人脸识别functionConfiguration.setSupportFaceRecognition(funConfigurationProperty.isSupportFaceRecognition());//是否支持性别检测functionConfiguration.setSupportGender(funConfigurationProperty.isSupportGender());//是否支持活体检测functionConfiguration.setSupportLiveness(funConfigurationProperty.isSupportLiveness());//是否至此IR活体检测functionConfiguration.setSupportIRLiveness(funConfigurationProperty.isSupportIRLiveness());engineConfiguration.setFunctionConfiguration(functionConfiguration);}
}

4.5对图片对象封装的工具类

public class ArcSoftUtils {/*** 处理 File 的图片流* @param img* @return*/public static ImageInfoMeta packImageInfoEx(File img){ImageInfo imageInfo = getRGBData(img);return packImageInfoMeta(imageInfo);}/*** 处理 byte[] 的图片流* @param img* @return*/public static ImageInfoMeta packImageInfoMeta(byte[] img){ImageInfo imageInfo = getRGBData(img);return packImageInfoMeta(imageInfo);}/*** 处理 InpuStream 的图片流* @param img* @return*/public static ImageInfoMeta packImageInfoMeta(InputStream img){ImageInfo imageInfo = getRGBData(img);return packImageInfoMeta(imageInfo);}/*** 打包生成 ImageInfoMeta* @param imageInfo* @return*/private static ImageInfoMeta packImageInfoMeta(ImageInfo imageInfo){ImageInfoMeta imageInfoMeta = new ImageInfoMeta(imageInfo);return imageInfoMeta;}/*** 对imageInfo 和 imageInfoEx 的打包对象* @return*/@Datapublic static class ImageInfoMeta{private ImageInfo imageInfo;private ImageInfoEx imageInfoEx;public ImageInfoMeta(ImageInfo imageInfo) {this.imageInfo = imageInfo;imageInfoEx = new ImageInfoEx();imageInfoEx.setHeight(imageInfo.getHeight());imageInfoEx.setWidth(imageInfo.getWidth());imageInfoEx.setImageFormat(imageInfo.getImageFormat());imageInfoEx.setImageDataPlanes(new byte[][]{imageInfo.getImageData()});imageInfoEx.setImageStrides(new int[]{imageInfo.getWidth() * 3});}}}

4.6封装的常用方法工具类

@Component
public class ArcSoftMothodUtils {@Autowiredprivate FaceEngine faceEngine;/*** 人脸检测*/public List<FaceInfo> detectFace(ImageInfoEx imageInfoEx) {if (imageInfoEx == null)return null;List<FaceInfo> faceInfoList = new ArrayList<FaceInfo>();int i = faceEngine.detectFaces(imageInfoEx, DetectModel.ASF_DETECT_MODEL_RGB, faceInfoList);checkEngineResult(i, ErrorInfo.MOK.getValue(), "人脸检测失败");return faceInfoList;}/*** 特征提取*/public FaceFeature extractFaceFeature(List<FaceInfo> faceInfoList, ImageInfoEx imageInfoEx) {if (faceInfoList == null || imageInfoEx == null)return null;FaceFeature faceFeature = new FaceFeature();int i = faceEngine.extractFaceFeature(imageInfoEx, faceInfoList.get(0), faceFeature);checkEngineResult(i, ErrorInfo.MOK.getValue(), "人脸特征提取失败");return faceFeature;}/*** 特征比对*/public FaceSimilar compareFaceFeature(FaceFeature target, FaceFeature source, CompareModel compareModel) {FaceSimilar faceSimilar = new FaceSimilar();int i = faceEngine.compareFaceFeature(target, source, compareModel, faceSimilar);checkEngineResult(i, ErrorInfo.MOK.getValue(), "人脸特征对比失败");return faceSimilar;}/*** 错误检测*/private void checkEngineResult(int errorCode, int sourceCode, String errMsg) {if (errorCode != sourceCode)throw new RuntimeException(errMsg);}
}

4.7测试

@RestController
@RequestMapping("/arcsoft")
public class FaceController {@Autowiredprivate ArcSoftMothodUtils arcSoftMothodUtils;@GetMapping("/detectFace")public Result detectFace(String imgPath) {List<FaceInfo> faceInfo = arcSoftMothodUtils.detectFace(ArcfaceUtils.packImageInfoEx(new File(imgPath)).getImageInfoEx());return Result.succ(faceInfo);}@GetMapping("/extractFaceFeature")public Result extractFaceFeature(String imgPath) {List<FaceInfo> faceInfo = arcSoftMothodUtils.detectFace(ArcfaceUtils.packImageInfoEx(new File(imgPath)).getImageInfoEx());FaceFeature faceFeature = arcSoftMothodUtils.extractFaceFeature(faceInfo, ArcfaceUtils.packImageInfoEx(new File(imgPath)).getImageInfoEx());return Result.succ(faceFeature);}@GetMapping("/compareFaceFeature")public Result compareFaceFeature(String imgPath1,String imgPath2) {List<FaceInfo> faceInfo1 = arcSoftMothodUtils.detectFace(ArcfaceUtils.packImageInfoEx(new File(imgPath1)).getImageInfoEx());FaceFeature faceFeature1 = arcSoftMothodUtils.extractFaceFeature(faceInfo1, ArcfaceUtils.packImageInfoEx(new File(imgPath1)).getImageInfoEx());List<FaceInfo> faceInfo2 = arcSoftMothodUtils.detectFace(ArcfaceUtils.packImageInfoEx(new File(imgPath2)).getImageInfoEx());FaceFeature faceFeature2 = arcSoftMothodUtils.extractFaceFeature(faceInfo2, ArcfaceUtils.packImageInfoEx(new File(imgPath2)).getImageInfoEx());FaceSimilar faceSimilar = arcSoftMothodUtils.compareFaceFeature(faceFeature1, faceFeature2, CompareModel.LIFE_PHOTO);return Result.succ(faceSimilar);}}

到这里整个流程就结束了,其实虹软的几个方法的使用有两种方式,我使用的是第二种,也就是下图红圈中的

最后,附上文档中心--虹软AI-虹软AI开放平台

这篇关于Java使用虹软SDK实现人脸检测、特征提取、比对的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/192698

相关文章

Git可视化管理工具(SourceTree)使用操作大全经典

《Git可视化管理工具(SourceTree)使用操作大全经典》本文详细介绍了SourceTree作为Git可视化管理工具的常用操作,包括连接远程仓库、添加SSH密钥、克隆仓库、设置默认项目目录、代码... 目录前言:连接Gitee or github,获取代码:在SourceTree中添加SSH密钥:Cl

Java NoClassDefFoundError运行时错误分析解决

《JavaNoClassDefFoundError运行时错误分析解决》在Java开发中,NoClassDefFoundError是一种常见的运行时错误,它通常表明Java虚拟机在尝试加载一个类时未能... 目录前言一、问题分析二、报错原因三、解决思路检查类路径配置检查依赖库检查类文件调试类加载器问题四、常见

Java注解之超越Javadoc的元数据利器详解

《Java注解之超越Javadoc的元数据利器详解》本文将深入探讨Java注解的定义、类型、内置注解、自定义注解、保留策略、实际应用场景及最佳实践,无论是初学者还是资深开发者,都能通过本文了解如何利用... 目录什么是注解?注解的类型内置注编程解自定义注解注解的保留策略实际用例最佳实践总结在 Java 编程

Python中模块graphviz使用入门

《Python中模块graphviz使用入门》graphviz是一个用于创建和操作图形的Python库,本文主要介绍了Python中模块graphviz使用入门,具有一定的参考价值,感兴趣的可以了解一... 目录1.安装2. 基本用法2.1 输出图像格式2.2 图像style设置2.3 属性2.4 子图和聚

windows和Linux使用命令行计算文件的MD5值

《windows和Linux使用命令行计算文件的MD5值》在Windows和Linux系统中,您可以使用命令行(终端或命令提示符)来计算文件的MD5值,文章介绍了在Windows和Linux/macO... 目录在Windows上:在linux或MACOS上:总结在Windows上:可以使用certuti

CentOS和Ubuntu系统使用shell脚本创建用户和设置密码

《CentOS和Ubuntu系统使用shell脚本创建用户和设置密码》在Linux系统中,你可以使用useradd命令来创建新用户,使用echo和chpasswd命令来设置密码,本文写了一个shell... 在linux系统中,你可以使用useradd命令来创建新用户,使用echo和chpasswd命令来设

Python使用Matplotlib绘制3D曲面图详解

《Python使用Matplotlib绘制3D曲面图详解》:本文主要介绍Python使用Matplotlib绘制3D曲面图,在Python中,使用Matplotlib库绘制3D曲面图可以通过mpl... 目录准备工作绘制简单的 3D 曲面图绘制 3D 曲面图添加线框和透明度控制图形视角Matplotlib

Pandas中统计汇总可视化函数plot()的使用

《Pandas中统计汇总可视化函数plot()的使用》Pandas提供了许多强大的数据处理和分析功能,其中plot()函数就是其可视化功能的一个重要组成部分,本文主要介绍了Pandas中统计汇总可视化... 目录一、plot()函数简介二、plot()函数的基本用法三、plot()函数的参数详解四、使用pl

使用Python实现IP地址和端口状态检测与监控

《使用Python实现IP地址和端口状态检测与监控》在网络运维和服务器管理中,IP地址和端口的可用性监控是保障业务连续性的基础需求,本文将带你用Python从零打造一个高可用IP监控系统,感兴趣的小伙... 目录概述:为什么需要IP监控系统使用步骤说明1. 环境准备2. 系统部署3. 核心功能配置系统效果展

Java 实用工具类Spring 的 AnnotationUtils详解

《Java实用工具类Spring的AnnotationUtils详解》Spring框架提供了一个强大的注解工具类org.springframework.core.annotation.Annot... 目录前言一、AnnotationUtils 的常用方法二、常见应用场景三、与 JDK 原生注解 API 的