Commit e7875eeb99dd59a1ea24fabacae29ab5894c8a20

Authored by liujun001
1 parent 0ac09b45

人脸识别

Bsth-admin/pom.xml
... ... @@ -68,7 +68,7 @@
68 68 <version>1.6.2</version>
69 69 </dependency>
70 70  
71   - <!-- Mysql驱动包 -->
  71 + <!-- Mysql驱动包 -->
72 72 <dependency>
73 73 <groupId>com.mysql</groupId>
74 74 <artifactId>mysql-connector-j</artifactId>
... ... @@ -113,6 +113,12 @@
113 113 <version>4.5.13</version>
114 114 </dependency>
115 115  
  116 + <dependency>
  117 + <groupId>com.arcsoft.face</groupId>
  118 + <artifactId>arcsoft-sdk-face</artifactId>
  119 + <version>3.0.0</version>
  120 + </dependency>
  121 +
116 122 </dependencies>
117 123  
118 124 <build>
... ... @@ -132,15 +138,15 @@
132 138 </execution>
133 139 </executions>
134 140 </plugin>
135   - <plugin>
136   - <groupId>org.apache.maven.plugins</groupId>
137   - <artifactId>maven-war-plugin</artifactId>
138   - <version>3.1.0</version>
  141 + <plugin>
  142 + <groupId>org.apache.maven.plugins</groupId>
  143 + <artifactId>maven-war-plugin</artifactId>
  144 + <version>3.1.0</version>
139 145 <configuration>
140 146 <failOnMissingWebXml>false</failOnMissingWebXml>
141 147 <warName>${project.artifactId}</warName>
142   - </configuration>
143   - </plugin>
  148 + </configuration>
  149 + </plugin>
144 150 </plugins>
145 151 <finalName>${project.artifactId}-sign</finalName>
146 152 </build>
... ...
Bsth-admin/src/main/java/com/ruoyi/config/BsthSystemConfig.java
... ... @@ -16,4 +16,12 @@ public class BsthSystemConfig {
16 16  
17 17 @Value("${bsth.upload.video.basePath}")
18 18 private String videoBasePath;
  19 +
  20 + @Value("${bsth.face.app.id}")
  21 + private String faceAppId;
  22 + @Value("${bsth.face.sdk.key}")
  23 + private String faceSdkKey;
  24 + @Value("${bsth.face.lib.path}")
  25 + private String faceLibPath;
  26 +
19 27 }
... ...
Bsth-admin/src/main/java/com/ruoyi/config/FaceEngineConfig.java 0 → 100644
  1 +package com.ruoyi.config;
  2 +
  3 +import com.arcsoft.face.ActiveFileInfo;
  4 +import com.arcsoft.face.EngineConfiguration;
  5 +import com.arcsoft.face.FaceEngine;
  6 +import com.arcsoft.face.FunctionConfiguration;
  7 +import com.arcsoft.face.enums.DetectMode;
  8 +import com.arcsoft.face.enums.DetectOrient;
  9 +import com.arcsoft.face.enums.ErrorInfo;
  10 +import com.ruoyi.exception.InterruptException;
  11 +import lombok.extern.slf4j.Slf4j;
  12 +import org.springframework.context.annotation.Bean;
  13 +import org.springframework.stereotype.Component;
  14 +
  15 +import javax.annotation.PreDestroy;
  16 +import java.util.Objects;
  17 +
  18 +/**
  19 + * @author liujun
  20 + * @date 2024年07月25日 16:23
  21 + */
  22 +@Slf4j
  23 +@Component
  24 +public class FaceEngineConfig {
  25 + private FaceEngine faceEngine;
  26 +
  27 + @Bean
  28 + public FaceEngine createFaceEngine(BsthSystemConfig bsthSystemConfig) {
  29 + FaceEngine faceEngine = new FaceEngine(bsthSystemConfig.getFaceLibPath());
  30 +// FaceEngine faceEngine = new FaceEngine("D:\\\\arcsoft_lib");
  31 + //激活引擎
  32 + int errorCode = faceEngine.activeOnline(bsthSystemConfig.getFaceAppId(), bsthSystemConfig.getFaceSdkKey());
  33 +
  34 + if (errorCode != ErrorInfo.MOK.getValue() && errorCode != ErrorInfo.MERR_ASF_ALREADY_ACTIVATED.getValue()) {
  35 + throw new InterruptException("引擎激活失败");
  36 + }
  37 +
  38 +
  39 + ActiveFileInfo activeFileInfo = new ActiveFileInfo();
  40 + errorCode = faceEngine.getActiveFileInfo(activeFileInfo);
  41 + if (errorCode != ErrorInfo.MOK.getValue() && errorCode != ErrorInfo.MERR_ASF_ALREADY_ACTIVATED.getValue()) {
  42 + throw new InterruptException("获取激活文件信息失败");
  43 + }
  44 +
  45 + //引擎配置
  46 + EngineConfiguration engineConfiguration = new EngineConfiguration();
  47 + engineConfiguration.setDetectMode(DetectMode.ASF_DETECT_MODE_IMAGE);
  48 + engineConfiguration.setDetectFaceOrientPriority(DetectOrient.ASF_OP_ALL_OUT);
  49 + engineConfiguration.setDetectFaceMaxNum(10);
  50 + engineConfiguration.setDetectFaceScaleVal(16);
  51 + //功能配置
  52 + FunctionConfiguration functionConfiguration = new FunctionConfiguration();
  53 + functionConfiguration.setSupportAge(true);
  54 + functionConfiguration.setSupportFace3dAngle(true);
  55 + functionConfiguration.setSupportFaceDetect(true);
  56 + functionConfiguration.setSupportFaceRecognition(true);
  57 + functionConfiguration.setSupportGender(true);
  58 + functionConfiguration.setSupportLiveness(true);
  59 + functionConfiguration.setSupportIRLiveness(true);
  60 + engineConfiguration.setFunctionConfiguration(functionConfiguration);
  61 +
  62 +
  63 + //初始化引擎
  64 + errorCode = faceEngine.init(engineConfiguration);
  65 +
  66 + if (errorCode != ErrorInfo.MOK.getValue()) {
  67 + throw new InterruptException("初始化引擎失败");
  68 + }
  69 +
  70 + return faceEngine;
  71 + }
  72 +
  73 + @PreDestroy
  74 + public void faceEngineConfigPreDestory() {
  75 + if (Objects.nonNull(faceEngine)) {
  76 + faceEngine.unInit();
  77 + }
  78 + }
  79 +}
... ...
Bsth-admin/src/main/java/com/ruoyi/domain/driver/NewDriver.java
1 1 package com.ruoyi.domain.driver;
2 2  
3   -import lombok.Data;
4 3 import com.baomidou.mybatisplus.annotation.IdType;
5 4 import com.baomidou.mybatisplus.annotation.TableField;
6 5 import com.baomidou.mybatisplus.annotation.TableId;
7 6 import com.baomidou.mybatisplus.annotation.TableName;
8   -import org.apache.commons.lang3.StringUtils;
  7 +import com.ruoyi.common.annotation.Excel;
  8 +import lombok.Data;
9 9 import lombok.EqualsAndHashCode;
10 10 import lombok.experimental.Accessors;
11 11 import lombok.extern.slf4j.Slf4j;
12   -import com.ruoyi.common.annotation.Excel;
13 12  
14 13 @Data
15 14 @Slf4j
... ... @@ -112,6 +111,9 @@ public class NewDriver {
112 111 @Excel(name = "头像")
113 112 private String image;
114 113  
  114 + @TableField(exist = false)
  115 + private float imageScore;
  116 +
115 117  
116 118 /***更新日期*/
117 119 @Excel(name = "更新日期")
... ...
Bsth-admin/src/main/java/com/ruoyi/service/impl/dss/FaceServiceImpl.java
1 1 package com.ruoyi.service.impl.dss;
2 2  
  3 +import cn.hutool.core.codec.Base64;
  4 +import com.arcsoft.face.FaceEngine;
  5 +import com.arcsoft.face.FaceFeature;
  6 +import com.arcsoft.face.FaceInfo;
  7 +import com.arcsoft.face.FaceSimilar;
  8 +import com.arcsoft.face.enums.ErrorInfo;
  9 +import com.arcsoft.face.toolkit.ImageInfo;
  10 +import com.ruoyi.config.BsthSystemConfig;
3 11 import com.ruoyi.domain.driver.NewDriver;
4 12 import com.ruoyi.service.driver.NewDriverService;
5 13 import com.ruoyi.service.dss.FaceService;
6 14 import lombok.extern.slf4j.Slf4j;
7 15 import org.apache.commons.collections4.CollectionUtils;
  16 +import org.apache.commons.lang3.StringUtils;
8 17 import org.springframework.beans.factory.annotation.Autowired;
9 18 import org.springframework.stereotype.Service;
10 19  
11   -import java.util.List;
  20 +import java.io.File;
  21 +import java.util.*;
  22 +
  23 +import static com.arcsoft.face.toolkit.ImageFactory.getRGBData;
12 24  
13 25 /**
14 26 * @author liujun
... ... @@ -19,18 +31,74 @@ import java.util.List;
19 31 public class FaceServiceImpl implements FaceService {
20 32 @Autowired
21 33 private NewDriverService newDriverService;
  34 + @Autowired
  35 + private FaceEngine faceEngine;
  36 + @Autowired
  37 + private BsthSystemConfig bsthSystemConfig;
  38 +
22 39  
23 40 @Override
24 41 public NewDriver checkFace(NewDriver driver) {
25 42 List<NewDriver> drivers = newDriverService.list(new NewDriver());
26   - return checkFace(drivers);
  43 + return checkFace(drivers, driver);
27 44 }
28 45  
29   - private NewDriver checkFace(List<NewDriver> drivers) {
  46 + private NewDriver checkFace(List<NewDriver> drivers, NewDriver sourceDriver) {
30 47 if (CollectionUtils.isEmpty(drivers)) {
31 48 return null;
32 49 }
33 50  
34 51 return null;
35 52 }
  53 +
  54 + private float checkFaceScore(NewDriver dr, FaceFeature sourceFaceFeature) {
  55 + if (StringUtils.isEmpty(dr.getImage())) {
  56 + log.error("头像路径为空;[{}]", dr);
  57 + return -1;
  58 + }
  59 + StringBuilder builder = new StringBuilder();
  60 + builder.append(bsthSystemConfig.getImageBasePath());
  61 + builder.append(File.separator);
  62 + builder.append(dr.getImage());
  63 +
  64 + File targetFile = new File(builder.toString());
  65 + if (!targetFile.exists() || !targetFile.isFile()) {
  66 + log.error("没有比对的图片;[{}]", dr);
  67 + return -1;
  68 + }
  69 +
  70 + ImageInfo target = getRGBData(targetFile);
  71 + FaceFeature targetFaceFeature = generateFaceFeature(dr, target);
  72 + if (Objects.isNull(targetFaceFeature)) {
  73 + return -1;
  74 + }
  75 +
  76 + //特征比对
  77 + FaceSimilar faceSimilar = new FaceSimilar();
  78 + int errorCode = faceEngine.compareFaceFeature(targetFaceFeature, sourceFaceFeature, faceSimilar);
  79 +
  80 + if (errorCode != ErrorInfo.MOK.getValue()) {
  81 + log.error("人脸对比失败:[{}]", dr);
  82 + return -1;
  83 + }
  84 + return faceSimilar.getScore();
  85 + }
  86 +
  87 + private FaceFeature generateFaceFeature(NewDriver driver, ImageInfo image) {
  88 + List<FaceInfo> faceInfoList = new ArrayList<>();
  89 + int errorCode = faceEngine.detectFaces(image.getImageData(), image.getWidth(), image.getHeight(), image.getImageFormat(), faceInfoList);
  90 + if (errorCode != ErrorInfo.MOK.getValue()) {
  91 + log.error("人脸对比失败,请检查数据:[{}]", driver);
  92 + return null;
  93 + }
  94 +
  95 + FaceFeature faceFeature = new FaceFeature();
  96 + errorCode = faceEngine.extractFaceFeature(image.getImageData(), image.getWidth(), image.getHeight(), image.getImageFormat(), faceInfoList.get(0), faceFeature);
  97 + if (errorCode != ErrorInfo.MOK.getValue()) {
  98 + log.error("提取对比特征失败,请检查数据:[{}]", driver);
  99 + return null;
  100 + }
  101 + return faceFeature;
  102 + }
  103 +
36 104 }
... ...
Bsth-admin/src/main/resources/application-druid-dev.yml
... ... @@ -199,5 +199,12 @@ bsth:
199 199 upload:
200 200 image:
201 201 basePath: D:/temp/temp/bsth/upload
202   - vide:
203   - basePath: D:/temp/temp/bsth/upload
204 202 \ No newline at end of file
  203 + video:
  204 + basePath: D:/temp/temp/bsth/upload
  205 + face:
  206 + app:
  207 + id: 8jPk3SNnaoGsd9SidMefgZXg1zbst64jB44vVyx9Cijq
  208 + sdk:
  209 + key: C21s5J1n1rHwXPkvVjubKshtofV5sHXvyUQqSWYxHp2b
  210 + lib:
  211 + path: D:/work/code/jienengjiancha/bsth-alcohol-sign/Bsth-admin/src/main/resources/libs/WIN64
205 212 \ No newline at end of file
... ...
Bsth-admin/src/main/resources/libs/WIN64/libarcsoft_face.dll 0 → 100644
No preview for this file type
Bsth-admin/src/main/resources/libs/WIN64/libarcsoft_face_engine.dll 0 → 100644
No preview for this file type
Bsth-admin/src/main/resources/libs/WIN64/libarcsoft_face_engine_jni.dll 0 → 100644
No preview for this file type
Bsth-admin/src/main/resources/libs/arcsoft-sdk-face-3.0.0.0.jar 0 → 100644
No preview for this file type