Commit 5adbbb36 authored by 耿迪迪's avatar 耿迪迪

人脸识别接口 gengdidi

parent 9d39387e
...@@ -66,6 +66,16 @@ ...@@ -66,6 +66,16 @@
<artifactId>artemis-http-client</artifactId> <artifactId>artemis-http-client</artifactId>
<version>1.1.3</version> <version>1.1.3</version>
</dependency> </dependency>
<!--虹软 -->
<dependency>
<groupId>arcsoft</groupId>
<artifactId>arcsoft-sdk-face</artifactId>
<version>3.0.0.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/arcsoft_lib/arcsoft-sdk-face-3.0.0.0.jar</systemPath>
</dependency>
</dependencies> </dependencies>
<build> <build>
...@@ -132,6 +142,7 @@ ...@@ -132,6 +142,7 @@
</resources> </resources>
</configuration> </configuration>
</execution> </execution>
<execution> <execution>
<id>copy-sh</id> <id>copy-sh</id>
<phase>package</phase> <phase>package</phase>
...@@ -152,6 +163,29 @@ ...@@ -152,6 +163,29 @@
</resources> </resources>
</configuration> </configuration>
</execution> </execution>
<execution>
<id>copy-arcsoft-lib</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<encoding>UTF-8</encoding>
<outputDirectory>${project.build.directory}/danger-manage-${project.version}/arcsoft_lib</outputDirectory>
<resources>
<resource>
<directory>arcsoft_lib</directory>
<excludes>
<exclude>
*.jar
</exclude>
</excludes>
</resource>
</resources>
</configuration>
</execution>
<execution> <execution>
<id>copy-dist-jar</id> <id>copy-dist-jar</id>
<phase>package</phase> <phase>package</phase>
......
package com.zehong.web.arcface.factory;
import com.arcsoft.face.EngineConfiguration;
import com.arcsoft.face.FaceEngine;
import com.arcsoft.face.enums.ErrorInfo;
import org.apache.commons.pool2.BasePooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author geng
* 人脸识别引擎初始化
*/
public class FaceEngineFactory extends BasePooledObjectFactory<FaceEngine> {
private static final Logger logger = LoggerFactory.getLogger(FaceEngineFactory.class);
private String appId;
private String sdkKey;
private String sdkLibPath;
private EngineConfiguration engineConfiguration;
public FaceEngineFactory(String sdkLibPath, String appId, String sdkKey, EngineConfiguration engineConfiguration) {
this.sdkLibPath = sdkLibPath;
this.appId = appId;
this.sdkKey = sdkKey;
this.engineConfiguration = engineConfiguration;
}
@Override
public FaceEngine create() throws Exception {
FaceEngine faceEngine = new FaceEngine(System.getProperty("user.dir") + sdkLibPath);
int activeCode = faceEngine.activeOnline(appId, sdkKey);
logger.info("faceEngineActiveCode:" + activeCode + "==========================");
if (activeCode != ErrorInfo.MOK.getValue() && activeCode != ErrorInfo.MERR_ASF_ALREADY_ACTIVATED.getValue()) {
logger.error("引擎激活失败" + activeCode);
throw new Exception("引擎激活失败" + activeCode);
}
int initCode = faceEngine.init(engineConfiguration);
logger.info("faceEngineInitCode:" + initCode + "==========================");
if (initCode != ErrorInfo.MOK.getValue()) {
logger.error("引擎初始化失败" + initCode);
throw new Exception( "引擎初始化失败" + initCode);
}
return faceEngine;
}
@Override
public PooledObject<FaceEngine> wrap(FaceEngine faceEngine) {
return new DefaultPooledObject<>(faceEngine);
}
@Override
public void destroyObject(PooledObject<FaceEngine> p) throws Exception {
FaceEngine faceEngine = p.getObject();
int unInitCode = faceEngine.unInit();
logger.info("faceEngineUnInitCode:" + unInitCode + "==========================");
super.destroyObject(p);
}
}
package com.zehong.web.arcface.util;
import com.arcsoft.face.*;
import com.arcsoft.face.enums.DetectMode;
import com.arcsoft.face.enums.DetectOrient;
import com.arcsoft.face.toolkit.ImageInfo;
import com.zehong.web.arcface.factory.FaceEngineFactory;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import javax.annotation.PostConstruct;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
/**
* @author geng
* 人脸比对
*/
@Component
public class FaceEngineUtils {
private static final Logger logger = LoggerFactory.getLogger(FaceEngineUtils.class);
@Value("${config.arcface-sdk.sdk-lib-path}")
public String sdkLibPath;
@Value("${config.arcface-sdk.app-id}")
public String appId;
@Value("${config.arcface-sdk.sdk-key}")
public String sdkKey;
@Value("${config.arcface-sdk.detect-face-max-num}")
public Integer detectFaceMaxNum;
@Value("${config.arcface-sdk.detect-face-scale-val}")
public Integer detectFaceScaleVal;
@Value("${config.arcface-sdk.thread-pool-size}")
public Integer threadPoolSize;
private GenericObjectPool<FaceEngine> faceEngineGeneralPool;
@PostConstruct
public void init() {
GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
poolConfig.setMaxIdle(threadPoolSize);
poolConfig.setMaxTotal(threadPoolSize);
poolConfig.setMinIdle(threadPoolSize);
poolConfig.setLifo(false);
//引擎配置
EngineConfiguration engineConfiguration = new EngineConfiguration();
engineConfiguration.setDetectMode(DetectMode.ASF_DETECT_MODE_IMAGE);
engineConfiguration.setDetectFaceOrientPriority(DetectOrient.ASF_OP_0_ONLY);
engineConfiguration.setDetectFaceMaxNum(detectFaceMaxNum);
engineConfiguration.setDetectFaceScaleVal(detectFaceScaleVal);
//功能配置
FunctionConfiguration functionConfiguration = new FunctionConfiguration();
functionConfiguration.setSupportFaceDetect(true);
functionConfiguration.setSupportFaceRecognition(true);
engineConfiguration.setFunctionConfiguration(functionConfiguration);
//底层库算法对象池
faceEngineGeneralPool = new GenericObjectPool(new FaceEngineFactory(sdkLibPath, appId, sdkKey, engineConfiguration), poolConfig);
}
/**
* 人脸识别
* @param imageInfo 人脸信息
* @return List<FaceInfo>
*/
public List<FaceInfo> detectFaces(ImageInfo imageInfo) {
FaceEngine faceEngine = null;
try {
faceEngine = faceEngineGeneralPool.borrowObject();
if (faceEngine == null) {
throw new Exception("获取引擎失败");
}
//人脸检测得到人脸列表
List<FaceInfo> faceInfoList = new ArrayList<>();
//人脸检测
int errorCode = faceEngine.detectFaces(imageInfo.getImageData(), imageInfo.getWidth(), imageInfo.getHeight(), imageInfo.getImageFormat(), faceInfoList);
if (errorCode == 0) {
return faceInfoList;
} else {
logger.error("人脸检测失败,errorCode:" + errorCode);
}
} catch (Exception e) {
logger.error("人脸识别系统错误:", e);
} finally {
if (faceEngine != null) {
//释放引擎对象
faceEngineGeneralPool.returnObject(faceEngine);
}
}
return null;
}
/**
* 人脸比对
* @param imageInfo1 待比对图片
* @param imageInfo2 比对图片
* @return Integer
*/
public Integer compareFace(ImageInfo imageInfo1, ImageInfo imageInfo2) throws Exception {
List<FaceInfo> faceInfoList1 = detectFaces(imageInfo1);
List<FaceInfo> faceInfoList2 = detectFaces(imageInfo2);
if (CollectionUtils.isEmpty(faceInfoList1)) {
throw new Exception("照片1未检测到人脸");
}
if (CollectionUtils.isEmpty(faceInfoList2)) {
throw new Exception("照片2未检测到人脸");
}
byte[] feature1 = extractFaceFeature(imageInfo1, faceInfoList1.get(0));
byte[] feature2 = extractFaceFeature(imageInfo2, faceInfoList2.get(0));
FaceEngine faceEngine = null;
try {
faceEngine = faceEngineGeneralPool.borrowObject();
if (faceEngine == null) {
throw new Exception("获取引擎失败");
}
FaceFeature faceFeature1 = new FaceFeature();
faceFeature1.setFeatureData(feature1);
FaceFeature faceFeature2 = new FaceFeature();
faceFeature2.setFeatureData(feature2);
//提取人脸特征
FaceSimilar faceSimilar = new FaceSimilar();
int errorCode = faceEngine.compareFaceFeature(faceFeature1, faceFeature2, faceSimilar);
if (errorCode == 0) {
return plusHundred(faceSimilar.getScore());
} else {
logger.error("特征提取失败,errorCode:" + errorCode);
}
} catch (Exception e) {
logger.error("人脸比对系统错误:", e);
} finally {
if (faceEngine != null) {
//释放引擎对象
faceEngineGeneralPool.returnObject(faceEngine);
}
}
return null;
}
/**
* 人脸特征
* @param imageInfo 图片信息
* @return byte[]
*/
public byte[] extractFaceFeature(ImageInfo imageInfo, FaceInfo faceInfo) {
FaceEngine faceEngine = null;
try {
faceEngine = faceEngineGeneralPool.borrowObject();
if (faceEngine == null) {
throw new Exception("获取引擎失败");
}
FaceFeature faceFeature = new FaceFeature();
//提取人脸特征
int errorCode = faceEngine.extractFaceFeature(imageInfo.getImageData(), imageInfo.getWidth(), imageInfo.getHeight(), imageInfo.getImageFormat(), faceInfo, faceFeature);
if (errorCode == 0) {
return faceFeature.getFeatureData();
} else {
logger.error("特征提取失败,errorCode:" + errorCode);
}
} catch (Exception e) {
logger.error("人脸特征系统错误:", e);
} finally {
if (faceEngine != null) {
//释放引擎对象
faceEngineGeneralPool.returnObject(faceEngine);
}
}
return null;
}
/**
* 百分比转化
* @param value 相似度
* @return int
*/
private int plusHundred(Float value) {
BigDecimal target = new BigDecimal(value);
BigDecimal hundred = new BigDecimal(100f);
return target.multiply(hundred).intValue();
}
}
package com.zehong.web.controller.arcfacecompare;
import com.arcsoft.face.toolkit.ImageFactory;
import com.arcsoft.face.toolkit.ImageInfo;
import com.zehong.web.arcface.util.FaceEngineUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.util.HashMap;
import java.util.Map;
/**
* @author geng
* 人脸识别
*/
@RestController
@RequestMapping("/arcFace")
public class FaceCompareController {
private static final Logger logger = LoggerFactory.getLogger(FaceCompareController.class);
@Autowired
private FaceEngineUtils faceEngineUtil;
/**
* 图片比对
* @param file 待比对图片
* @param compareFile 比对图片
* @return Integer
*/
@PostMapping(value = "/compare")
public Map<String,Object> compare(@RequestParam(value = "file")MultipartFile file, @RequestParam(value = "compareFile")MultipartFile compareFile){
Map<String,Object> result = new HashMap<>(16);
try {
ImageInfo rgbData1 = ImageFactory.getRGBData(file.getBytes());
ImageInfo rgbData2 = ImageFactory.getRGBData(compareFile.getBytes());
result.put("code","0");
result.put("faceSimilar",faceEngineUtil.compareFace(rgbData1,rgbData2));
return result;
} catch (Exception e) {
result.put("code","1");
result.put("msg",e.getMessage());
logger.error("图片比对系统错误:" + e);
return result;
}
}
}
...@@ -109,4 +109,15 @@ zehong: ...@@ -109,4 +109,15 @@ zehong:
# 获取ip地址开关 # 获取ip地址开关
addressEnabled: false addressEnabled: false
# 验证码类型 math 数组计算 char 字符验证 # 验证码类型 math 数组计算 char 字符验证
captchaType: math captchaType:
\ No newline at end of file
#虹软人脸识别配置(windows配置)
config:
arcface-sdk:
#算法引擎路径 示例(Windows配置\danger-manage-admin\arcsoft_lib\WIN64,Linux配置/arcsoft_lib/LINUX64)
sdk-lib-path: \danger-manage-admin\arcsoft_lib\WIN64
app-id: C8vzXcY8iD7yGFCHweh1QcvJaV684GCPk2oCe4JC1rpZ
sdk-key: C8MFKCRxMAsBSDG5W5cL1x1YirGREoojZvFtBzKv3bAP
detect-face-max-num: 10
detect-face-scale-val: 32
thread-pool-size: 5
\ No newline at end of file
...@@ -21,9 +21,9 @@ spring: ...@@ -21,9 +21,9 @@ spring:
druid: druid:
# 主库数据源 # 主库数据源
master: master:
url: jdbc:mysql://localhost:3306/ry?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 url: jdbc:mysql://localhost:33060/danger_manage_area_a?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
username: root username: root
password: root password: zehong_/sjz!D
# 从库数据源 # 从库数据源
slave: slave:
# 从数据源开关/默认关闭 # 从数据源开关/默认关闭
...@@ -73,7 +73,7 @@ spring: ...@@ -73,7 +73,7 @@ spring:
# redis 配置 # redis 配置
redis: redis:
# 地址 # 地址
host: localhost host: 36.138.181.113
# 端口,默认为6379 # 端口,默认为6379
port: 6379 port: 6379
# 数据库索引 # 数据库索引
...@@ -110,3 +110,15 @@ zehong: ...@@ -110,3 +110,15 @@ zehong:
addressEnabled: false addressEnabled: false
# 验证码类型 math 数组计算 char 字符验证 # 验证码类型 math 数组计算 char 字符验证
captchaType: math captchaType: math
#虹软人脸识别配置(linux配置)
config:
arcface-sdk:
#算法引擎路径 示例(Windows配置\danger-manage-admin\arcsoft_lib\WIN64,Linux配置/arcsoft_lib/LINUX64)
sdk-lib-path: /arcsoft_lib/LINUX64
app-id: 3ZM3ayaRriUPWzzy29QsqeynFcpmu9zMVXGjBrQ4rPkL
sdk-key: J1wqG4VVGJYLe5YYT2hQN3xHSqLEwrZJBBRQmuEXkZqc
detect-face-max-num: 10
detect-face-scale-val: 32
thread-pool-size: 5
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment