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

相关文章

Oracle查询优化之高效实现仅查询前10条记录的方法与实践

《Oracle查询优化之高效实现仅查询前10条记录的方法与实践》:本文主要介绍Oracle查询优化之高效实现仅查询前10条记录的相关资料,包括使用ROWNUM、ROW_NUMBER()函数、FET... 目录1. 使用 ROWNUM 查询2. 使用 ROW_NUMBER() 函数3. 使用 FETCH FI

Python脚本实现自动删除C盘临时文件夹

《Python脚本实现自动删除C盘临时文件夹》在日常使用电脑的过程中,临时文件夹往往会积累大量的无用数据,占用宝贵的磁盘空间,下面我们就来看看Python如何通过脚本实现自动删除C盘临时文件夹吧... 目录一、准备工作二、python脚本编写三、脚本解析四、运行脚本五、案例演示六、注意事项七、总结在日常使用

Java实现Excel与HTML互转

《Java实现Excel与HTML互转》Excel是一种电子表格格式,而HTM则是一种用于创建网页的标记语言,虽然两者在用途上存在差异,但有时我们需要将数据从一种格式转换为另一种格式,下面我们就来看看... Excel是一种电子表格格式,广泛用于数据处理和分析,而HTM则是一种用于创建网页的标记语言。虽然两

java图像识别工具类(ImageRecognitionUtils)使用实例详解

《java图像识别工具类(ImageRecognitionUtils)使用实例详解》:本文主要介绍如何在Java中使用OpenCV进行图像识别,包括图像加载、预处理、分类、人脸检测和特征提取等步骤... 目录前言1. 图像识别的背景与作用2. 设计目标3. 项目依赖4. 设计与实现 ImageRecogni

Java中Springboot集成Kafka实现消息发送和接收功能

《Java中Springboot集成Kafka实现消息发送和接收功能》Kafka是一个高吞吐量的分布式发布-订阅消息系统,主要用于处理大规模数据流,它由生产者、消费者、主题、分区和代理等组件构成,Ka... 目录一、Kafka 简介二、Kafka 功能三、POM依赖四、配置文件五、生产者六、消费者一、Kaf

Java访问修饰符public、private、protected及默认访问权限详解

《Java访问修饰符public、private、protected及默认访问权限详解》:本文主要介绍Java访问修饰符public、private、protected及默认访问权限的相关资料,每... 目录前言1. public 访问修饰符特点:示例:适用场景:2. private 访问修饰符特点:示例:

python管理工具之conda安装部署及使用详解

《python管理工具之conda安装部署及使用详解》这篇文章详细介绍了如何安装和使用conda来管理Python环境,它涵盖了从安装部署、镜像源配置到具体的conda使用方法,包括创建、激活、安装包... 目录pytpshheraerUhon管理工具:conda部署+使用一、安装部署1、 下载2、 安装3

Mysql虚拟列的使用场景

《Mysql虚拟列的使用场景》MySQL虚拟列是一种在查询时动态生成的特殊列,它不占用存储空间,可以提高查询效率和数据处理便利性,本文给大家介绍Mysql虚拟列的相关知识,感兴趣的朋友一起看看吧... 目录1. 介绍mysql虚拟列1.1 定义和作用1.2 虚拟列与普通列的区别2. MySQL虚拟列的类型2

详解Java如何向http/https接口发出请求

《详解Java如何向http/https接口发出请求》这篇文章主要为大家详细介绍了Java如何实现向http/https接口发出请求,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 用Java发送web请求所用到的包都在java.net下,在具体使用时可以用如下代码,你可以把它封装成一

使用MongoDB进行数据存储的操作流程

《使用MongoDB进行数据存储的操作流程》在现代应用开发中,数据存储是一个至关重要的部分,随着数据量的增大和复杂性的增加,传统的关系型数据库有时难以应对高并发和大数据量的处理需求,MongoDB作为... 目录什么是MongoDB?MongoDB的优势使用MongoDB进行数据存储1. 安装MongoDB