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

相关文章

Tomcat的下载安装与使用教程

《Tomcat的下载安装与使用教程》本文介绍了Tomcat的下载、安装和使用方法,包括在本机和云服务器上部署Tomcat的过程,以及解决启动失败问题的方法... 目录Tomcat的下载安装与使用Tomcat的下载与安装Tomcat在本机运行使用Tomcat在php云服务器上的使用总结Tomcat的下载安装与

SpringBoot基于沙箱环境实现支付宝支付教程

《SpringBoot基于沙箱环境实现支付宝支付教程》本文介绍了如何使用支付宝沙箱环境进行开发测试,包括沙箱环境的介绍、准备步骤、在SpringBoot项目中结合支付宝沙箱进行支付接口的实现与测试... 目录一、支付宝沙箱环境介绍二、沙箱环境准备2.1 注册入驻支付宝开放平台2.2 配置沙箱环境2.3 沙箱

Nginx实现高并发的项目实践

《Nginx实现高并发的项目实践》本文主要介绍了Nginx实现高并发的项目实践,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录使用最新稳定版本的Nginx合理配置工作进程(workers)配置工作进程连接数(worker_co

python中列表list切分的实现

《python中列表list切分的实现》列表是Python中最常用的数据结构之一,经常需要对列表进行切分操作,本文主要介绍了python中列表list切分的实现,文中通过示例代码介绍的非常详细,对大家... 目录一、列表切片的基本用法1.1 基本切片操作1.2 切片的负索引1.3 切片的省略二、列表切分的高

基于Python实现一个PDF特殊字体提取工具

《基于Python实现一个PDF特殊字体提取工具》在PDF文档处理场景中,我们常常需要针对特定格式的文本内容进行提取分析,本文介绍的PDF特殊字体提取器是一款基于Python开发的桌面应用程序感兴趣的... 目录一、应用背景与功能概述二、技术架构与核心组件2.1 技术选型2.2 系统架构三、核心功能实现解析

Python使用PIL库将PNG图片转换为ICO图标的示例代码

《Python使用PIL库将PNG图片转换为ICO图标的示例代码》在软件开发和网站设计中,ICO图标是一种常用的图像格式,特别适用于应用程序图标、网页收藏夹图标等场景,本文将介绍如何使用Python的... 目录引言准备工作代码解析实践操作结果展示结语引言在软件开发和网站设计中,ICO图标是一种常用的图像

使用Java发送邮件到QQ邮箱的完整指南

《使用Java发送邮件到QQ邮箱的完整指南》在现代软件开发中,邮件发送功能是一个常见的需求,无论是用户注册验证、密码重置,还是系统通知,邮件都是一种重要的通信方式,本文将详细介绍如何使用Java编写程... 目录引言1. 准备工作1.1 获取QQ邮箱的SMTP授权码1.2 添加JavaMail依赖2. 实现

MyBatis与其使用方法示例详解

《MyBatis与其使用方法示例详解》MyBatis是一个支持自定义SQL的持久层框架,通过XML文件实现SQL配置和数据映射,简化了JDBC代码的编写,本文给大家介绍MyBatis与其使用方法讲解,... 目录ORM缺优分析MyBATisMyBatis的工作流程MyBatis的基本使用环境准备MyBati

Java嵌套for循环优化方案分享

《Java嵌套for循环优化方案分享》介绍了Java中嵌套for循环的优化方法,包括减少循环次数、合并循环、使用更高效的数据结构、并行处理、预处理和缓存、算法优化、尽量减少对象创建以及本地变量优化,通... 目录Java 嵌套 for 循环优化方案1. 减少循环次数2. 合并循环3. 使用更高效的数据结构4

使用Python开发一个图像标注与OCR识别工具

《使用Python开发一个图像标注与OCR识别工具》:本文主要介绍一个使用Python开发的工具,允许用户在图像上进行矩形标注,使用OCR对标注区域进行文本识别,并将结果保存为Excel文件,感兴... 目录项目简介1. 图像加载与显示2. 矩形标注3. OCR识别4. 标注的保存与加载5. 裁剪与重置图像