虹软人脸识别-SpringBoot集成

一、前言

人工智能时代的到来,相信大家已耳濡目染,虹软免费离线开放的人脸识别 SDK,正推动着全行业进入刷脸时代。为了方便开发者接入,虹软提供了多种语言,多种平台的人脸识别SDK的支持,使用场景广泛。产品主要功能有:人脸检测、追踪、特征提取、特征比对、属性检测,活体检测,图像质量检测等。此外,虹软提供的是基于本地算法特征的离线识别SDK,提供全平台的离线支持。
作为一名刚接触人脸识别的初学者,对于虹软极为简洁,方便的SDK接入充满了好奇,想试图应用到web领域,而如今Java最火的web框架非SpringBoot莫属。但对于Java语言,虹软官网暂时还没有提供基于SpringBoot的集成Demo,因此便尝试写个将Java的人脸识别SDK和SpringBoot进行集成的样例,并写此文章进行记录,向广大初学开发者作分享。
此Demo采用Maven作为项目管理工具,并基于Windows x64,Java 8 以及 SpringBoot 2.1.6,SDK是基于虹软人脸识别 SDK3.0。

二、项目结构

SDK依赖Jar包 可从虹软官网获取 点击”免费获取” , ”登录“后 选择 具体“平台/版本/语言“ 获取。
在这里插入图片描述

三、项目依赖

​ pom.xml 依赖包括

  • SpringBoot-Web依赖
  • SpringBoot-Devtools热部署依赖
  • SpringBoot-Freemarker依赖,
  • Hutool,Fastjson, Lombok,Commons-pool2,Guava
  • 虹软人脸识别SDK依赖Jar包
  • SpringBoot-Maven插件
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.6.1</version>
</dependency>

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.59</version>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.6.0</version>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>26.0-jre</version>
</dependency>

<dependency>
<groupId>com.arcsoft.face</groupId>
<artifactId>arcsoft-sdk-face</artifactId>
<version>3.0.0.0</version>
<scope>system</scope>
<systemPath>${
   basedir}/lib/arcsoft-sdk-face-3.0.0.0.jar</systemPath>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
四、项目流程

在这里插入图片描述

五、效果展示

Application启动类 右击 选择 Run Application 即可运行程序,待程序启动完成后,访问 http://127.0.0.1:8089/
在这里插入图片描述

六、核心代码说明
1. application.properties 配置说明
#上传文件 最大值限制
spring.servlet.multipart.max-file-size=100MB
#请求 最大值限制
spring.servlet.multipart.max-request-size=100MB
#请求头 最大值限制
server.max-http-header-size=2MB
#请求体 最大值限制
server.tomcat.max-http-post-size=50MB
#项目访问端口
server.port=8089
#人脸识别引擎库路径
config.arcface-sdk.sdk-lib-path=d:/arcsoft_lib
#sdk appId
config.arcface-sdk.app-id=9iSfMeAhj********************Kes2TpSrd
#sdk sdkKey
config.arcface-sdk.sdk-key=BuRTH3hGs9*******************yP9xu6fiFG7G
#人脸识别 引擎池大小
config.arcface-sdk.detect-pool-size=5
#人脸比对 引擎池大小
config.arcface-sdk.compare-pool-size=5
#关闭freemarker模板引擎缓存
spring.freemarker.cache=false
#模板引擎更新延迟设置为0
spring.freemarker.settings.template_update_delay=0

​ 其中 人脸识别引擎库,APP_ID,SDK_KEY 可通过虹软官网”开发者中心“,进行 “登录”后 在“我的应用“中进行获取。

2. 项目实体类说明

1)UserRamCache 人脸信息存储类

public class UserRamCache {
   
private static  ConcurrentHashMap<String, UserInfo> userInfoMap = new ConcurrentHashMap<>();
public static void addUser(UserInfo userInfo) {
   
        userInfoMap.put(userInfo.getFaceId(), userInfo);
}
public static void removeUser(String faceId) {
   
        userInfoMap.remove(faceId);
}
public static List<UserInfo> getUserList() {
   
        List<UserInfo> userInfoList = Lists.newLinkedList();
for (UserInfo value : userInfoMap.values()) {
   
            userInfoList.add(value);
}
return userInfoList;
}
@Data
public static class UserInfo {
   
//人脸Id
private String faceId;
//人脸名称
private String name;
//人脸特征值
private byte[] faceFeature;
}
}

此类拥有一个 UserInfo的内部类,用于封装人脸信息,userInfoMap以人脸名称为key,UserInfo对象为Value 存储 并提供相应增/删/查功能的方法。
2)ProcessInfo 人脸检测实体类

public class ProcessInfo {
   
//年龄
private int age;
//性别
  • 14
    点赞
  • 79
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
在Spring Boot中集成人脸识别可以通过以下步骤实现: 1. 在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.arcsoft</groupId> <artifactId>arcsoft-face</artifactId> <version>2.2.3</version> </dependency> ``` 2. 在application.properties文件中添加以下配置: ```properties # 人脸识别配置 # 以下配置信息需要到官网申请 # 人脸识别APP ID arcsoft.app-id=your_app_id # 人脸识别SDK KEY arcsoft.sdk-key=your_sdk_key # 人脸识别引擎类型,取值范围为 ASF_DETECT_MODE_VIDEO 或 ASF_DETECT_MODE_IMAGE arcsoft.detect-mode=ASF_DETECT_MODE_VIDEO ``` 3. 创建一个人脸识别服务类,实现人脸检测和人脸比对的功能。以下是一个简单的示例: ```java @Service public class FaceRecognitionService { @Value("${arcsoft.app-id}") private String appId; @Value("${arcsoft.sdk-key}") private String sdkKey; @Value("${arcsoft.detect-mode}") private int detectMode; /** * 初始化人脸识别引擎 */ public void init() { // 初始化引擎 int errorCode = ArcFaceEngine.getInstance().initEngine(appId, sdkKey, detectMode); if (errorCode != ArcFaceEngineErrorCode.CODE_SUCCESS) { throw new RuntimeException("初始化人脸识别引擎失败,错误码:" + errorCode); } } /** * 销毁人脸识别引擎 */ public void destroy() { // 销毁引擎 int errorCode = ArcFaceEngine.getInstance().unInitEngine(); if (errorCode != ArcFaceEngineErrorCode.CODE_SUCCESS) { throw new RuntimeException("销毁人脸识别引擎失败,错误码:" + errorCode); } } /** * 人脸检测 * * @param imageData 图片数据 * @return 人脸信息列表 */ public List<FaceInfo> detectFaces(byte[] imageData) { // 人脸检测 List<FaceInfo> faceInfoList = new ArrayList<>(); int errorCode = ArcFaceEngine.getInstance().detectFaces(imageData, faceInfoList); if (errorCode != ArcFaceEngineErrorCode.CODE_SUCCESS) { throw new RuntimeException("人脸检测失败,错误码:" + errorCode); } return faceInfoList; } /** * 人脸比对 * * @param faceFeature1 人脸特征1 * @param faceFeature2 人脸特征2 * @return 相似度 */ public float compareFaceFeature(byte[] faceFeature1, byte[] faceFeature2) { // 人脸比对 float[] similarity = new float[1]; int errorCode = ArcFaceEngine.getInstance().compareFaceFeature(faceFeature1, faceFeature2, similarity); if (errorCode != ArcFaceEngineErrorCode.CODE_SUCCESS) { throw new RuntimeException("人脸比对失败,错误码:" + errorCode); } return similarity[0]; } } ``` 4. 在Controller中调用人脸识别服务类的方法,实现人脸检测和人脸比对的功能。以下是一个简单的示例: ```java @RestController public class FaceRecognitionController { @Autowired private FaceRecognitionService faceRecognitionService; /** * 人脸检测接口 * * @param image 图片数据 * @return 人脸信息列表 */ @PostMapping("/detectFaces") public List<FaceInfo> detectFaces(@RequestBody byte[] image) { return faceRecognitionService.detectFaces(image); } /** * 人脸比对接口 * * @param faceFeature1 人脸特征1 * @param faceFeature2 人脸特征2 * @return 相似度 */ @PostMapping("/compareFaceFeature") public float compareFaceFeature(@RequestBody byte[] faceFeature1, @RequestBody byte[] faceFeature2) { return faceRecognitionService.compareFaceFeature(faceFeature1, faceFeature2); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值