소스 검색

FaceVerifyTest

deason 2 년 전
부모
커밋
5fdbedeba6

+ 5 - 0
examcloud-core-oe-student-base/pom.xml

@@ -32,6 +32,11 @@
             <artifactId>examcloud-support</artifactId>
             <version>${project.version}</version>
         </dependency>
+        <dependency>
+            <groupId>cn.com.qmth.examcloud.starters</groupId>
+            <artifactId>examcloud-face-verify-starter</artifactId>
+            <version>${project.version}</version>
+        </dependency>
         <dependency>
             <groupId>cn.com.qmth.examcloud.starters</groupId>
             <artifactId>examcloud-crypto-starter</artifactId>

+ 154 - 0
examcloud-core-oe-student-starter/src/test/java/cn/com/qmth/examcloud/core/oe/student/test/FaceVerifyTest.java

@@ -0,0 +1,154 @@
+package cn.com.qmth.examcloud.core.oe.student.test;
+
+import cn.com.qmth.examcloud.commons.helpers.poi.ExcelReader;
+import cn.com.qmth.examcloud.commons.util.FileUtil;
+import cn.com.qmth.examcloud.starters.face.verify.FaceVerifyProperties;
+import cn.com.qmth.examcloud.starters.face.verify.service.impl.FaceVerifyServiceImpl;
+import org.apache.commons.lang3.StringUtils;
+import org.junit.Test;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.atomic.AtomicInteger;
+
+public class FaceVerifyTest {
+
+    private ExecutorService service = Executors.newCachedThreadPool();
+
+    private AtomicInteger passNum = new AtomicInteger();
+
+    private static final FaceVerifyServiceImpl faceVerifyService;
+
+    private static final String dir = "D:\\home\\captures-0";
+
+    static {
+        System.setProperty("log.commonLevel", "INFO");
+
+        FaceVerifyProperties properties = new FaceVerifyProperties();
+        properties.setBaiduLocalEnabled(true);
+        properties.setBaiduLocalAppId("test");
+        properties.setBaiduLocalUrlPrefix("http://192.168.10.102:8301");
+
+        faceVerifyService = new FaceVerifyServiceImpl();
+        faceVerifyService.setProperties(properties);
+    }
+
+    // @Test
+    public void demo() throws Exception {
+        String excelFile = "C:\\Users\\deason\\Desktop\\比对结果-不通过.xlsx";
+        List<String[]> lines = ExcelReader.readSheetBySax(excelFile, 1, 8);
+
+        int batchNum = 100;
+        int totalLines = lines.size() - 1;
+        List<String[]> batchList = new ArrayList<>();
+        long startTime = System.currentTimeMillis();
+
+        for (int n = 1; n < lines.size(); n++) {
+            if (n > 500) {
+                // break;
+            }
+
+            String[] line = lines.get(n);
+            batchList.add(line);
+
+            if (n % batchNum == 0) {
+                concurrentRun(batchList);
+                batchList.clear();
+
+                long cost = (System.currentTimeMillis() - startTime) / 1000L;
+                System.out.printf("lineIndex:%s  进度:%s%%  耗时:%s秒 %n", n, n * 100f / totalLines, cost);
+            }
+        }
+
+        if (!batchList.isEmpty()) {
+            concurrentRun(batchList);
+        }
+
+        long cost = (System.currentTimeMillis() - startTime) / 1000L;
+        float passRate = passNum.get() * 100f / totalLines;
+        System.out.printf("totalLines:%s  passNum:%s  通过率:%s%%  耗时:%s秒", totalLines, passNum.get(), passRate, cost);
+
+        service.shutdown();
+    }
+
+    private void concurrentRun(List<String[]> batchList) {
+        int threadNum = batchList.size();
+        final CountDownLatch commander = new CountDownLatch(1);
+        final CountDownLatch worker = new CountDownLatch(threadNum);
+
+        for (int n = 0; n < threadNum; n++) {
+            String[] line = batchList.get(n);
+            final String studentId = line[0];
+            final String capturePhotoUrl = fixPhotoUrl(line[6], false);
+            final String photoUrl = fixPhotoUrl(line[7], true);
+            // System.out.println(studentId + " | " + photoUrl + " | " + capturePhotoUrl);
+
+            Runnable runnable = () -> {
+                try {
+                    // System.out.println(Thread.currentThread().getName());
+
+                    String photoPath = dir + "/" + studentId + "/" + FileUtil.getFileName(photoUrl);
+                    File photoFile = new File(photoPath);
+                    if (!photoFile.exists()) {
+                        FileUtil.saveImageToFile(photoUrl, photoPath);
+                    }
+
+                    String capturePhotoPath = dir + "/" + studentId + "/" + FileUtil.getFileName(capturePhotoUrl);
+                    File capturePhotoFile = new File(capturePhotoPath);
+                    if (!capturePhotoFile.exists()) {
+                        FileUtil.saveImageToFile(capturePhotoUrl, capturePhotoPath);
+                    }
+
+                    // FaceResult result = faceVerifyService.faceCompareByBaidu(photoFile, capturePhotoFile);
+                    // System.out.println(new JsonHelper().toJson(result));
+                    // if (result.isPass()) {
+                    passNum.incrementAndGet();
+                    // }
+                } catch (Exception e) {
+                    e.printStackTrace();
+                } finally {
+                    worker.countDown();
+                }
+            };
+
+            service.execute(runnable);
+        }
+
+        try {
+            commander.countDown();
+            worker.await();
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+    private String fixPhotoUrl(String url, boolean isBasePhoto) {
+        if (StringUtils.isEmpty(url)) {
+            return "";
+        }
+
+        if (url.toLowerCase().startsWith("http")) {
+            return url;
+        }
+
+        final String prefix = "https://ecs-static.qmth.com.cn/";
+        if (url.startsWith("aliyun-1://")) {
+            return url.replace("aliyun-1://", prefix);
+        } else if (url.startsWith("aliyun-2://")) {
+            return url.replace("aliyun-2://", prefix);
+        } else if (url.startsWith("upyun-1://")) {
+            return url.replace("upyun-1://", prefix);
+        } else {
+            if (isBasePhoto && !url.contains("student_base_photo")) {
+                url = "student_base_photo/" + url;
+            }
+        }
+
+        return prefix + url;
+    }
+
+}

+ 1 - 1
examcloud-core-oe-student-starter/src/test/java/cn/com/qmth/examcloud/core/oe/student/test/OeStudentTest.java

@@ -10,7 +10,7 @@ import org.springframework.test.context.junit4.SpringRunner;
 @SpringBootTest(classes = OEStudentApp.class)
 public class OeStudentTest {
 
-    @Test
+    // @Test
     public void demo() throws Exception {
 
     }