车车 7 달 전
부모
커밋
6370db03c7

+ 17 - 39
ktg-common/src/main/java/com/ktg/common/utils/face/ArcSoftMothodUtil.java

@@ -18,14 +18,10 @@ import org.springframework.web.multipart.MultipartFile;
 
 import java.io.File;
 import java.io.IOException;
-import java.io.UnsupportedEncodingException;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Comparator;
-import java.util.List;
+import java.util.*;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
@@ -106,8 +102,6 @@ public class ArcSoftMothodUtil {
         if (StringUtils.isBlank(prodApi)) {
             basePath = "C:" + basePath;
         }
-        // 人脸特征值存储
-        String content;
         // 原文件转存后的路径
         String imagePath;
         // 原文件转存后的前端请求路径
@@ -155,39 +149,12 @@ public class ArcSoftMothodUtil {
         return faceCutVO;
     }
 
-    public static void main(String[] args) throws UnsupportedEncodingException {
-
-
-        /*String strWithCharset = new String(bytes, StandardCharsets.UTF_8); // 使用默认字符集
-        System.out.println(bytes);
-        System.out.println(strWithCharset);
-        byte[] bytes1 = strWithCharset.getBytes(StandardCharsets.UTF_8);
-        for (byte b : bytes1) {
-            System.out.print(b + " ");
-        }*/
-        byte[] bytes = {72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33};
-        String string = Arrays.toString(bytes);
-
-        // Remove the brackets and spaces from the string
-        string = string.substring(1, string.length() - 1).replaceAll("\\s", "");
-
-        // Split the string by commas to get each number as a string
-        String[] numbers = string.split(",");
-        // Create a new byte array with the same length as the original
-        byte[] newBytes = new byte[numbers.length];
-
-        // Convert each string number back to a byte and store it in the new array
-        for (int i = 0; i < numbers.length; i++) {
-            newBytes[i] = Byte.parseByte(numbers[i]);
-        }
-        // Print the new byte array to verify it matches the original
-    }
 
     // 相似度门槛
     private static final double THRESHOLD = 0.8; // THRESHOLD越高,错误率越低,阈值[0,1]
     private static final ExecutorService THREAD_POOL_EXECUTOR = Executors.newFixedThreadPool(4); // 线程池
 
-    public static FaceMatchVO completableFutureComparison(final MultipartFile file, final List<String> matcher) throws IOException {
+    public static FaceMatchVO completableFutureComparison(final MultipartFile file, final Set<String> matcher) throws IOException {
         int errorCode;
         // 提取当前人脸的特征值,比对文件零时存储,人脸存储基础路径
         String profile = RuoYiConfig.getProfile();
@@ -249,10 +216,7 @@ public class ArcSoftMothodUtil {
             List<List<String>> averageMatcherFeatureDataList = Lists.partition(matcherFeatureDataList, matcherFeatureDataList.size() / denominator);
 
             // 构建四个线程进行处理,防止人员过多对比速度太慢
-            CompletableFuture<FaceMatchVO>[] completableFutureArray = averageMatcherFeatureDataList.stream().map(
-                    partitionFeatureData -> CompletableFuture.supplyAsync(
-                            () -> comparison(featureData, new ArrayList<>(partitionFeatureData)), THREAD_POOL_EXECUTOR)
-            ).toArray(CompletableFuture[]::new);
+            CompletableFuture<FaceMatchVO>[] completableFutureArray = averageMatcherFeatureDataList.stream().map(partitionFeatureData -> CompletableFuture.supplyAsync(() -> comparison(featureData, new ArrayList<>(partitionFeatureData)), THREAD_POOL_EXECUTOR)).toArray(CompletableFuture[]::new);
             // 等待所有任务执行完
             CompletableFuture.allOf(completableFutureArray).join();
             List<FaceMatchVO> verificationList = new ArrayList<>();
@@ -274,6 +238,9 @@ public class ArcSoftMothodUtil {
             }
         } catch (Exception e) {
             log.error("人脸比对异常:{}", e.getMessage());
+        } finally {
+            //特征值提取完毕后清除数据
+            removeFile(fileName);
         }
         return null;
     }
@@ -327,4 +294,15 @@ public class ArcSoftMothodUtil {
         return newBytes;
     }
 
+    private static void removeFile(String path) {
+        try {
+            File file = new File(path);
+            if (!file.delete()) {
+                log.error("DAT文件删除失败: " + path);
+            }
+        } catch (Exception e) {
+            log.error("存在异常文件:{}", e);
+        }
+    }
+
 }

+ 0 - 6
ktg-common/src/main/java/com/ktg/common/utils/face/FaceMatchUtil.java

@@ -52,16 +52,10 @@ public class FaceMatchUtil
      * 人脸识别对比
      */
     public static FaceMatchVO faceRecognitionComparison(MultipartFile mfile, List<String> image2) throws IOException {
-        // 记录开始时间
-        long startTime1 = System.currentTimeMillis();
-
         Mat mat1 = conv_mat(mfile); // 转换为灰度图像的Mat对象
         Mat hist1 = computeHistogram(mat1); // 计算第一个图像的直方图
         // 记录结束时间
         long endTime1 = System.currentTimeMillis();
-        // 计算时间差(毫秒)
-        long duration1 = endTime1 - startTime1;
-        System.out.println("Execution time in milliseconds---------: " + duration1);
         ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); // 创建线程池
         List<Future<FaceMatchVO>> futures = new ArrayList<>(); // 存储Future对象的列表
 

+ 2 - 19
ktg-framework/src/main/java/com/ktg/framework/web/service/SysLoginService.java

@@ -284,9 +284,6 @@ public class SysLoginService {
 
     public AjaxResult loginByArcFace(MultipartFile file) throws IOException {
 
-        System.out.println("Execution time in milliseconds进来了-----: ");
-        // 记录开始时间
-        long startTime1 = System.currentTimeMillis();
         Assert.isTrue(file.getSize() > 0, "输入的人脸不能为空!");
         // 获取所有的用户人脸
         List<SysUserCharacteristic> list = iSysUserCharacteristicService.list(Wrappers.<SysUserCharacteristic>lambdaQuery()
@@ -294,16 +291,9 @@ public class SysLoginService {
         Assert.isFalse(list.isEmpty(), "人脸库中暂无您的人脸信息!");
         List<String> collect = list.stream().map(SysUserCharacteristic::getContent).collect(Collectors.toList());
         // 通过人脸获取最相似的用户
-        FaceMatchVO faceMatchVO = ArcSoftMothodUtil.completableFutureComparison(file, collect);
-        // FaceMatchVO faceMatchVO = ArcSoftMothodUtil.completableFutureComparison(file, new HashSet<>(collect));
-        // 记录结束时间
-        long endTime1 = System.currentTimeMillis();
-        // 计算时间差(毫秒)
-        long duration1 = endTime1 - startTime1;
-        System.out.println("Execution time in milliseconds-----: " + duration1);
+        // FaceMatchVO faceMatchVO = ArcSoftMothodUtil.completableFutureComparison(file, collect);
+        FaceMatchVO faceMatchVO = ArcSoftMothodUtil.completableFutureComparison(file, new HashSet<>(collect));
         Assert.notNull(faceMatchVO, "无法根据人脸确定您的身份,请通过其它方式登录!");
-        // 记录开始时间
-        long startTime2 = System.currentTimeMillis();
         String okFace = faceMatchVO.getContent();
         SysUserCharacteristic one = iSysUserCharacteristicService.getOne(Wrappers.<SysUserCharacteristic>lambdaQuery()
                 .eq(SysUserCharacteristic::getContent, okFace)
@@ -314,13 +304,6 @@ public class SysLoginService {
         String token = loginWithoutPassword(sysUser);
         ajax.put(Constants.TOKEN, token);
         ajax.put("nickName", sysUser.getNickName());
-
-        // 记录结束时间
-        long endTime2 = System.currentTimeMillis();
-        // 计算时间差(毫秒)
-        long duration2 = endTime2 - startTime2;
-        System.out.println("Execution time in milliseconds2-----: " + duration2);
-
         return ajax;
     }