java人脸识别
文章目录
前言
为什么选择虹软呢?
注册虹软账号,下载SDK
将jar包安装到maven本地仓库
项目实战
导入jar包
编写配置文件
Service
编写测试类
人脸识别更多应用
前言
虹软人脸识别技术是由虹软公司开发的一系列人脸识别技术,包括人脸检测、活体检测、人脸识别等。这些技术基于深度学习算法,能够在复杂环境下快速准确地识别人脸,广泛应用于智能手机、DSC、平板、IP Camera、机器人、智能家居、智能终端等领域
为什么选择虹软呢?
虹软的SDK免费为用户提供使用,只要首次使用时需要联网激活,激活后可离线使用。
使用周期为1年,1年后需要联网再次激活
虹软链接:
虹软人脸识别邀请您https://ai.arcsoft.com.cn/operator/resource/2021/regular/index.html#/invite?sign=473b1a064dfb45faa14eb22e4a1bf796
注册虹软账号,下载SDK
将jar包安装到maven本地仓库
由于虹软的依赖没有提交到maven公共仓库,直接在pom.xml中引入依赖是找不到的
进入到
libs
目录,需要将
arcsoft-sdk-face-3.0.0.0.jar
安装到本地仓库:
mvn install:install-file -Dfile="文件所在路径\arcsoft-sdk-face-3.0.0.0.jar" -DgroupId=com.arcsoft.face -DartifactId=arcsoft-sdk-face -Dversion=3.0.0.0 -Dpackaging=jar -X
-Dfile
:指定你要安装的 jar 包的路径。-DgroupId
:定义此 jar 包的组 ID,一般根据组织名称设定,比如com.arcsoft.face
。-DartifactId
:定义此 jar 包的 artifact ID,一般根据项目名称设定,比如arcsoft-sdk-face
。-Dversion
:指定版本号,3.0.0.0
。-Dpackaging
:定义包的类型,通常为jar
。- -X:打印详细安装信息,方便出现错误后排查
项目实战
导入jar包
<dependency>
<groupId>com.arcsoft.face</groupId>
<artifactId>arcsoft-sdk-face</artifactId>
<version>3.0.0.0</version>
</dependency>
编写配置文件
在resource目录下创建libs目录,将dll文件复制到libs目录下
arcsoft:
appId: AkumSmcBBTdfRwyvedpvjYJGgGpXXshP3kdAQYean8Mr
sdkId: AYBs2ReqwhFxyBpycbDsDt5tim4bpHJ5Q5V9KNtd8yYe
libPath: libs #指向dll文件所在目录
导入配置类
import com.arcsoft.face.*;
import com.arcsoft.face.enums.*;
import com.arcsoft.face.toolkit.ImageFactory;
import com.arcsoft.face.toolkit.ImageInfo;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import static com.arcsoft.face.toolkit.ImageFactory.getRGBData;
@Configuration
@Slf4j
public class FaceEngineConfig {
//从官网获取
@Value("${arcsoft.appId}")
private String appId;
@Value("${arcsoft.sdkId}")
private String sdkKey;
@Value("${arcsoft.libPath}")
private String libPath;
private FaceEngine faceEngine;
/**
* 初始化引擎
*/
@PostConstruct //bean创建后执行
public void init() {
log.info("初始化引擎");
Resource resource = new ClassPathResource(libPath);
String path = "";
try{
path = resource.getFile().getPath();
}catch (IOException e){
log.error("获取lib路径失败");
}
faceEngine = new FaceEngine(path); // 初始化成员变量
//激活引擎
int errorCode = faceEngine.activeOnline(appId, sdkKey);
if (errorCode != ErrorInfo.MOK.getValue() && errorCode != ErrorInfo.MERR_ASF_ALREADY_ACTIVATED.getValue()) {
log.error("引擎激活失败");
throw new RuntimeException("引擎激活失败");
}
ActiveFileInfo activeFileInfo=new ActiveFileInfo();
errorCode = faceEngine.getActiveFileInfo(activeFileInfo);
if (errorCode != ErrorInfo.MOK.getValue() && errorCode != ErrorInfo.MERR_ASF_ALREADY_ACTIVATED.getValue()) {
log.error("获取激活文件信息失败");
throw new RuntimeException("获取激活文件信息失败");
}
//引擎配置
EngineConfiguration engineConfiguration = new EngineConfiguration();
//设置引擎模式为:人脸检测、属性检测、3DAngle检测
engineConfiguration.setDetectMode(DetectMode.ASF_DETECT_MODE_IMAGE);
//设置检测人脸的角度
engineConfiguration.setDetectFaceOrientPriority(DetectOrient.ASF_OP_ALL_OUT);
//设置最大检测的人脸数量
engineConfiguration.setDetectFaceMaxNum(10);
//设置人脸检测的尺寸,16表示检测图片中人脸尺寸为图片长宽的1/16
engineConfiguration.setDetectFaceScaleVal(16);
// 功能配置
FunctionConfiguration functionConfiguration = new FunctionConfiguration();
// 设置是否支持年龄检测
functionConfiguration.setSupportAge(true);
// 设置是否支持3D角度检测
functionConfiguration.setSupportFace3dAngle(true);
// 设置是否支持人脸检测
functionConfiguration.setSupportFaceDetect(true);
// 设置是否支持人脸识别
functionConfiguration.setSupportFaceRecognition(true);
// 设置是否支持性别检测
functionConfiguration.setSupportGender(true);
// 设置是否支持活体检测
functionConfiguration.setSupportLiveness(true);
// 设置是否支持红外活体检测
functionConfiguration.setSupportIRLiveness(true);
// 将功能配置应用到引擎配置中
engineConfiguration.setFunctionConfiguration(functionConfiguration);
//初始化引擎
errorCode = faceEngine.init(engineConfiguration);
if (errorCode != ErrorInfo.MOK.getValue()) {
log.error("初始化引擎失败");
throw new RuntimeException("初始化引擎失败");
}
}
/**
* 传入一张图片,判断是否存在为人脸
*/
public boolean checkIsPortrait(File file){
ImageInfo imageInfo = ImageFactory.getGrayData(file);
//定义人脸列表
List<FaceInfo> faceInfoList = new ArrayList<>();
faceEngine.detectFaces(imageInfo.getImageData(), imageInfo.getWidth(), imageInfo.getHeight(), imageInfo.getImageFormat(), faceInfoList);
//faceInfoList列表为空,说明照片中没有一张人脸,!true
//即true为含有人脸,false没有人脸
return !faceInfoList.isEmpty();
}
/**
* 传入一张图片,获取人脸特征
* @param file
* @return
*/
public FaceFeature getFaceFeature(File file) {
//这个getRGBData方法内部调用了几个awt包里面的方法来处理图像数据,由此得到图像数据
ImageInfo imageInfo = getRGBData(file);
//新建一个人脸信息列表,获取到的人脸信息将储存在这个列表里面
List<FaceInfo> faceInfoList = new ArrayList<>();
//向引擎传入从图片分离的信息数据
int errorCode3 = faceEngine.detectFaces(imageInfo.getImageData(), imageInfo.getWidth(), imageInfo.getHeight(),
imageInfo.getImageFormat(), faceInfoList);
if (errorCode3 != ErrorInfo.MOK.getValue()) {
System.out.println("数据传入失败");
} else {
System.out.println("数据传入成功");
System.out.println(faceInfoList);
}
//提取人脸特征
FaceFeature faceFeature = new FaceFeature();
int errorCode4 = faceEngine.extractFaceFeature(imageInfo.getImageData(),
imageInfo.getWidth(), imageInfo.getHeight(), imageInfo.getImageFormat(),
faceInfoList.get(0), faceFeature);
if (errorCode4 != ErrorInfo.MOK.getValue()) {
System.out.println("人脸特征提取失败!");
} else {
System.out.println("人脸特征提取成功!");
System.out.println("特征值大小:" + faceFeature.getFeatureData().length);
}
return faceFeature;
}
/**
* 传入人脸特征,返回是否相似,相似对大于0.8则认为相似
*/
public boolean compareFace(FaceFeature faceFeature1, FaceFeature faceFeature2) {
//人脸相似度
FaceSimilar faceSimilar = new FaceSimilar();
int errorCode5 = faceEngine.compareFaceFeature(faceFeature1, faceFeature2, faceSimilar);
if (errorCode5 != ErrorInfo.MOK.getValue()) {
System.out.println("人脸对比操作失败!");
return false;
} else {
System.out.println("人脸相似度:" + faceSimilar.getScore());
return faceSimilar.getScore() > 0.8;
}
}
/**
* 返回值是map,map中包含性别、年龄、三维角度、活体
*/
public Map<String, String> getFaceInfo(File file) {
HashMap<String, String> faceInfo = new HashMap<>();
//这个getRGBData方法内部调用了几个awt包里面的方法来处理图像数据,由此得到图像数据
ImageInfo imageInfo = getRGBData(file);
//新建一个人脸信息列表,获取到的人脸信息将储存在这个列表里面
List<FaceInfo> faceInfoList = new ArrayList<>();
//向引擎传入从图片分离的信息数据
int errorCode3 = faceEngine.detectFaces(imageInfo.getImageData(), imageInfo.getWidth(), imageInfo.getHeight(),
imageInfo.getImageFormat(), faceInfoList);
if (errorCode3 != ErrorInfo.MOK.getValue()) {
System.out.println("数据传入失败");
} else {
System.out.println("数据传入成功");
System.out.println(faceInfoList);
}
//以下实现属性提取,提取某个属性要启用相关的功能
FunctionConfiguration functionConfiguration = new FunctionConfiguration();
functionConfiguration.setSupportAge(true);
functionConfiguration.setSupportFace3dAngle(true);
functionConfiguration.setSupportGender(true);
functionConfiguration.setSupportLiveness(true);
faceEngine.process(imageInfo.getImageData(), imageInfo.getWidth(),
imageInfo.getHeight(), imageInfo.getImageFormat(), faceInfoList, functionConfiguration);
//下面提取属性,首先实现process接口
int errorCode6 = faceEngine.process(imageInfo.getImageData(), imageInfo.getWidth(),
imageInfo.getHeight(), imageInfo.getImageFormat(), faceInfoList, functionConfiguration);
if (errorCode6 != ErrorInfo.MOK.getValue()) {
System.out.println("process接口调用失败,错误码:"+errorCode6);
} else {
System.out.println("process接口调用成功!");
}
//创建一个存储年龄的列表
List<AgeInfo> ageInfoList = new ArrayList<>();
int errorCode7 = faceEngine.getAge(ageInfoList);
if (errorCode7 != ErrorInfo.MOK.getValue()) {
System.out.print("获取年龄失败,错误码:");
System.out.println(errorCode7);
} else {
System.out.println("年龄获取成功!");
faceInfo.put("age",String.valueOf(ageInfoList.get(0).getAge()));
System.out.println("年龄:" + ageInfoList.get(0).getAge());
}
//性别检测
List<GenderInfo> genderInfoList = new ArrayList<>();
int errorCode8 = faceEngine.getGender(genderInfoList);
if (errorCode8 != ErrorInfo.MOK.getValue()) {
System.out.print("获取性别失败,错误码:");
System.out.println(errorCode8);
} else {
System.out.println("性别获取成功!");
faceInfo.put("gender",genderInfoList.get(0).getGender()==0 ? "男" : "女");
System.out.println("性别:" + genderInfoList.get(0).getGender());
}
//3D信息检测
List<Face3DAngle> face3DAngleList = new ArrayList<>();
int errorCode9 = faceEngine.getFace3DAngle(face3DAngleList);
if (errorCode9 != ErrorInfo.MOK.getValue()) {
System.out.println("3D信息检测失败,错误码:"+errorCode9);
} else {
System.out.println("3D信息获取成功!");
faceInfo.put("3DAngle",face3DAngleList.get(0).getPitch()+","+face3DAngleList.get(0).getRoll()+","+face3DAngleList.get(0).getYaw());
System.out.println("3D角度:" + face3DAngleList.get(0).getPitch() + "," +
face3DAngleList.get(0).getRoll() + "," + face3DAngleList.get(0).getYaw());
}
//活体检测
List<LivenessInfo> livenessInfoList = new ArrayList<>();
int errorCode10 = faceEngine.getLiveness(livenessInfoList);
if (errorCode10 != ErrorInfo.MOK.getValue()) {
System.out.println("活体检测失败,错误码:"+errorCode10);
} else {
System.out.println("活体检测成功");
faceInfo.put("liveness",String.valueOf(livenessInfoList.get(0).getLiveness()));
System.out.println("活体:" + livenessInfoList.get(0).getLiveness());
}
return faceInfo;
}
}
编写测试类
import com.arcsoft.face.FaceFeature;
import edu.nhky.hhb.config.FaceEngineConfig;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.File;
import java.util.Map;
@SpringBootTest
class TestFaceEngineConfig {
@Autowired
private FaceEngineConfig faceEngineConfig;
//判断是否是人脸
@Test
void CheckIsPortraitTest(){
File file = new File("E:\\Users\\31118\\Pictures\\1.png");
boolean checkIsPortrait = faceEngineConfig.checkIsPortrait(file);
System.out.println(checkIsPortrait);
}
//获取人脸特征
@Test
void getFaceFeatureTest(){
File file = new File("E:\\Users\\31118\\Pictures\\1.png");
FaceFeature faceFeature = faceEngineConfig.getFaceFeature(file);
System.out.println(faceFeature);
}
//比较人脸
@Test
void compareFaceTest(){
File file1 = new File("E:\\Users\\31118\\Pictures\\1.png");
File file2 = new File("E:\\Users\\31118\\Pictures\\1.png");
FaceFeature faceFeature1 = faceEngineConfig.getFaceFeature(file1);
FaceFeature faceFeature = faceEngineConfig.getFaceFeature(file2);
//判断是不是一个人
boolean isSamePerson = faceEngineConfig.compareFace(faceFeature1, faceFeature);
if (isSamePerson){
System.out.println("两种人脸,是同一个人");
}else {
System.out.println("两种人脸,不是同一个人");
}
}
//获取人脸信息
@Test
void getFaceInfoTest(){
File file = new File("E:\\Users\\31118\\Pictures\\1.png");
//获取人脸信息
Map<String, String> faceInfo = faceEngineConfig.getFaceInfo(file);
System.out.println(faceInfo);
}
}
测试结束