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

相关文章

mybatis执行insert返回id实现详解

《mybatis执行insert返回id实现详解》MyBatis插入操作默认返回受影响行数,需通过useGeneratedKeys+keyProperty或selectKey获取主键ID,确保主键为自... 目录 两种方式获取自增 ID:1. ​​useGeneratedKeys+keyProperty(推

Spring Boot集成Druid实现数据源管理与监控的详细步骤

《SpringBoot集成Druid实现数据源管理与监控的详细步骤》本文介绍如何在SpringBoot项目中集成Druid数据库连接池,包括环境搭建、Maven依赖配置、SpringBoot配置文件... 目录1. 引言1.1 环境准备1.2 Druid介绍2. 配置Druid连接池3. 查看Druid监控

Python通用唯一标识符模块uuid使用案例详解

《Python通用唯一标识符模块uuid使用案例详解》Pythonuuid模块用于生成128位全局唯一标识符,支持UUID1-5版本,适用于分布式系统、数据库主键等场景,需注意隐私、碰撞概率及存储优... 目录简介核心功能1. UUID版本2. UUID属性3. 命名空间使用场景1. 生成唯一标识符2. 数

Linux在线解压jar包的实现方式

《Linux在线解压jar包的实现方式》:本文主要介绍Linux在线解压jar包的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux在线解压jar包解压 jar包的步骤总结Linux在线解压jar包在 Centos 中解压 jar 包可以使用 u

Java中读取YAML文件配置信息常见问题及解决方法

《Java中读取YAML文件配置信息常见问题及解决方法》:本文主要介绍Java中读取YAML文件配置信息常见问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 目录1 使用Spring Boot的@ConfigurationProperties2. 使用@Valu

创建Java keystore文件的完整指南及详细步骤

《创建Javakeystore文件的完整指南及详细步骤》本文详解Java中keystore的创建与配置,涵盖私钥管理、自签名与CA证书生成、SSL/TLS应用,强调安全存储及验证机制,确保通信加密和... 目录1. 秘密键(私钥)的理解与管理私钥的定义与重要性私钥的管理策略私钥的生成与存储2. 证书的创建与

浅析Spring如何控制Bean的加载顺序

《浅析Spring如何控制Bean的加载顺序》在大多数情况下,我们不需要手动控制Bean的加载顺序,因为Spring的IoC容器足够智能,但在某些特殊场景下,这种隐式的依赖关系可能不存在,下面我们就来... 目录核心原则:依赖驱动加载手动控制 Bean 加载顺序的方法方法 1:使用@DependsOn(最直

SpringBoot中如何使用Assert进行断言校验

《SpringBoot中如何使用Assert进行断言校验》Java提供了内置的assert机制,而Spring框架也提供了更强大的Assert工具类来帮助开发者进行参数校验和状态检查,下... 目录前言一、Java 原生assert简介1.1 使用方式1.2 示例代码1.3 优缺点分析二、Spring Fr

Linux系统性能检测命令详解

《Linux系统性能检测命令详解》本文介绍了Linux系统常用的监控命令(如top、vmstat、iostat、htop等)及其参数功能,涵盖进程状态、内存使用、磁盘I/O、系统负载等多维度资源监控,... 目录toppsuptimevmstatIOStatiotopslabtophtopdstatnmon

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期