Explorar o código

优化百度和face++代码

lideyin %!s(int64=5) %!d(string=hai) anos
pai
achega
093924e473

+ 31 - 24
examcloud-core-oe-face-service/src/main/java/cn/com/qmth/examcloud/core/oe/student/face/service/impl/ExamCaptureProcessStatisticController.java

@@ -1,6 +1,7 @@
 package cn.com.qmth.examcloud.core.oe.student.face.service.impl;
 
 import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.locks.ReentrantLock;
 
 /**
  * @Description 考试照片统计控制器
@@ -14,6 +15,9 @@ public class ExamCaptureProcessStatisticController {
     //人脸比对处理失败数量
     private static AtomicInteger faceCompareFailedCount = new AtomicInteger(0);
 
+    private static ReentrantLock facePPLock = new ReentrantLock();
+    private static ReentrantLock faceLivenessLock = new ReentrantLock();
+
     /**
      * 人脸比对总数量自增
      */
@@ -29,32 +33,33 @@ public class ExamCaptureProcessStatisticController {
     }
 
     /**
-     * 重置人脸比对总数量
-     */
-    public static void resetFaceCompareCount() {
-        faceCompareCount.set(0);
-    }
-
-    /**
-     * 重置人脸比对失败数量
+     * 同步重置人脸比对所有数量
      */
-    public static void resetFaceCompareFailureCount() {
-        faceCompareFailedCount.set(0);
+    public static void resetAllFaceCompareCount() {
+        try {
+            facePPLock.lock();
+            faceCompareCount.set(0);
+            faceCompareFailedCount.set(0);
+        } finally {
+            facePPLock.unlock();
+        }
     }
 
     /**
      * 人脸比对总数量
+     *
      * @return int
      */
-    public static int getFaceCompareCount(){
+    public static int getFaceCompareCount() {
         return faceCompareCount.get();
     }
 
     /**
      * 人脸比对失败数量
+     *
      * @return int
      */
-    public static int getFaceCompareFailedCount(){
+    public static int getFaceCompareFailedCount() {
         return faceCompareFailedCount.get();
     }
 
@@ -90,31 +95,33 @@ public class ExamCaptureProcessStatisticController {
     }
 
     /**
-     * 重置活体检测数量
+     * 重置活体检测所有数量
      */
-    public static void resetFaceLivenessDetectCount() {
-        faceLivenessDetectCount.set(0);
-    }
-
-    /**
-     * 重置活体检测失败数量
-     */
-    public static void resetFaceLivenessDetectFailureCount() {
-        faceLivenessDetectFailedCount.set(0);
+    public static void resetAllFaceLivenessDetectCount() {
+        try {
+            faceLivenessLock.lock();
+            faceLivenessDetectCount.set(0);
+            faceLivenessDetectFailedCount.set(0);
+        } finally {
+            faceLivenessLock.unlock();
+        }
     }
 
     /**
      * 活体检测总数量
+     *
      * @return
      */
-    public static int getFaceLivenessDetectCount(){
+    public static int getFaceLivenessDetectCount() {
         return faceLivenessDetectCount.get();
     }
+
     /**
      * 活体检测失败数量
+     *
      * @return
      */
-    public static int getFaceLivenessDetectFailedCount(){
+    public static int getFaceLivenessDetectFailedCount() {
         return faceLivenessDetectFailedCount.get();
     }
 

+ 2 - 2
examcloud-core-oe-face-service/src/main/java/cn/com/qmth/examcloud/core/oe/student/face/service/impl/ExamCaptureServiceImpl.java

@@ -109,8 +109,8 @@ public class ExamCaptureServiceImpl implements ExamCaptureService {
                 /*//test code
                 jsonHttpResponseHolder =new JsonHttpResponseHolder();
                 jsonHttpResponseHolder.setStatusCode(200);
-                jsonHttpResponseHolder.setRespBody(JSONObject.parseObject("{\"error_message\":\"API_NOT_FOUND\"}"));*/
-
+                jsonHttpResponseHolder.setRespBody(JSONObject.parseObject("{\"error_message\":\"API_NOT_FOUND\"}"));
+*/
                 faceCompareResult = jsonHttpResponseHolder.getRespBody();
 
             } catch (StatusException e) {

+ 34 - 35
examcloud-core-oe-face-starter/src/main/java/cn/com/qmth/examcloud/core/oe/student/face/starter/config/ProcessBaiduFaceLivenessAlarmTask.java

@@ -49,48 +49,47 @@ public class ProcessBaiduFaceLivenessAlarmTask implements ApplicationRunner {
      */
     private void faceLivenessDectectAlarm() {
         while (true) {
-            if (captureLog.isDebugEnabled()){
-                captureLog.debug("[FACE_LIVENESS_ALARM] 进入活体检测" + System.currentTimeMillis() + "....totalCount=" +
-                        ExamCaptureProcessStatisticController.getFaceLivenessDetectCount() + " ,failCount=" +
-                        ExamCaptureProcessStatisticController.getFaceLivenessDetectFailedCount());
-            }
+            try {
+                if (captureLog.isDebugEnabled()) {
+                    captureLog.debug("[FACE_LIVENESS_ALARM] 进入活体检测" + System.currentTimeMillis() + "....totalCount=" +
+                            ExamCaptureProcessStatisticController.getFaceLivenessDetectCount() + " ,failCount=" +
+                            ExamCaptureProcessStatisticController.getFaceLivenessDetectFailedCount());
+                }
 
-            //如果每分钟失败率超过50%则发短信报警,且总数不少于10次则短信报警
-            if (ExamCaptureProcessStatisticController.getFaceLivenessDetectCount() > COUNT_WARN_THRESHOLD &&
-                    ExamCaptureProcessStatisticController.getFaceLivenessDetectFailureRate() > RATE_WARN_THRESHOLD) {
-                SysPropertyCacheBean faceLivenessSmsAssemblyCodeProperty = CacheHelper.getSysProperty("capture.faceLiveness.smsAssemblyCode");
-                if (!faceLivenessSmsAssemblyCodeProperty.getHasValue()) {
-                    if (captureLog.isErrorEnabled()){
-                        captureLog.error("[FACE_LIVENESS_ALARM.] 未配置人脸比对的短信模板代码,totalCount="+ExamCaptureProcessStatisticController.getFaceCompareCount()+",errorCount="+ExamCaptureProcessStatisticController.getFaceCompareFailedCount());
+                //如果每分钟失败率超过50%则发短信报警,且总数不少于10次则短信报警
+                if (ExamCaptureProcessStatisticController.getFaceLivenessDetectCount() > COUNT_WARN_THRESHOLD &&
+                        ExamCaptureProcessStatisticController.getFaceLivenessDetectFailureRate() > RATE_WARN_THRESHOLD) {
+                    SysPropertyCacheBean faceLivenessSmsAssemblyCodeProperty = CacheHelper.getSysProperty("capture.faceLiveness.smsAssemblyCode");
+                    if (!faceLivenessSmsAssemblyCodeProperty.getHasValue()) {
+                        captureLog.error("[FACE_LIVENESS_ALARM.] 未配置人脸比对的短信模板代码,totalCount=" + ExamCaptureProcessStatisticController.getFaceCompareCount() + ",errorCount=" + ExamCaptureProcessStatisticController.getFaceCompareFailedCount());
+                        throw new StatusException("300003", "未配置人脸活体检测的短信模板代码");
                     }
-                    throw new StatusException("300003", "未配置人脸活体检测的短信模板代码");
-                }
-                SysPropertyCacheBean smsPhoneProperty = CacheHelper.getSysProperty("capture.sms.phones");
-                if (!smsPhoneProperty.getHasValue()) {
-                    if (captureLog.isErrorEnabled()){
-                        captureLog.error("[FACE_LIVENESS_ALARM.] 未配置图片处理失败的通知手机号,totalCount="+ExamCaptureProcessStatisticController.getFaceCompareCount()+",errorCount="+ExamCaptureProcessStatisticController.getFaceCompareFailedCount());
+                    SysPropertyCacheBean smsPhoneProperty = CacheHelper.getSysProperty("capture.sms.phones");
+                    if (!smsPhoneProperty.getHasValue()) {
+                        captureLog.error("[FACE_LIVENESS_ALARM.] 未配置图片处理失败的通知手机号,totalCount=" + ExamCaptureProcessStatisticController.getFaceCompareCount() + ",errorCount=" + ExamCaptureProcessStatisticController.getFaceCompareFailedCount());
+                        throw new StatusException("300004", "未配置图片处理失败的通知手机号");
                     }
-                    throw new StatusException("300004", "未配置图片处理失败的通知手机号");
-                }
 
-                List<String> phoneList = Arrays.asList(smsPhoneProperty.getValue().toString().split(","));
-                SendSmsReq sendSmsReq = new SendSmsReq();
-                sendSmsReq.setPhoneList(phoneList);
-                sendSmsReq.setSmsAssemblyCode(faceLivenessSmsAssemblyCodeProperty.getValue().toString());
+                    List<String> phoneList = Arrays.asList(smsPhoneProperty.getValue().toString().split(","));
+                    SendSmsReq sendSmsReq = new SendSmsReq();
+                    sendSmsReq.setPhoneList(phoneList);
+                    sendSmsReq.setSmsAssemblyCode(faceLivenessSmsAssemblyCodeProperty.getValue().toString());
 
-                HashMap<String, String> params = new HashMap<>();
-                params.put("totalCount", String.valueOf(ExamCaptureProcessStatisticController.getFaceLivenessDetectCount()));
-                params.put("errorCount", String.valueOf(ExamCaptureProcessStatisticController.getFaceLivenessDetectFailedCount()));
-                sendSmsReq.setParams(params);
-                try {
-                    smsCloudService.sendSms(sendSmsReq);
-                } catch (Exception e) {
-                    captureLog.error("[PROCESS_FACE_LIVENESS.] 发送短信出现异常", e);
+                    HashMap<String, String> params = new HashMap<>();
+                    params.put("totalCount", String.valueOf(ExamCaptureProcessStatisticController.getFaceLivenessDetectCount()));
+                    params.put("errorCount", String.valueOf(ExamCaptureProcessStatisticController.getFaceLivenessDetectFailedCount()));
+                    sendSmsReq.setParams(params);
+                    try {
+                        smsCloudService.sendSms(sendSmsReq);
+                    } catch (Exception e) {
+                        captureLog.error("[PROCESS_FACE_LIVENESS.] 发送短信出现异常", e);
+                    }
                 }
+                //每1分钟重置一次总数量与失败数量
+                ExamCaptureProcessStatisticController.resetAllFaceLivenessDetectCount();
+            } catch (StatusException e) {
+                captureLog.error("[FACE_LIVENESS_ALARM.] 活体检测预警出现异常", e);
             }
-            //每1分钟重置一次总数量与失败数量
-            ExamCaptureProcessStatisticController.resetFaceLivenessDetectCount();
-            ExamCaptureProcessStatisticController.resetFaceLivenessDetectFailureCount();
 
             //每分钟轮循一次
             Util.sleep(60);

+ 2 - 1
examcloud-core-oe-face-starter/src/main/java/cn/com/qmth/examcloud/core/oe/student/face/starter/config/ProcessBaiduFacelivenessTask.java

@@ -17,6 +17,7 @@ import org.springframework.core.annotation.Order;
 import org.springframework.stereotype.Component;
 
 import java.util.List;
+import java.util.concurrent.TimeUnit;
 
 /**
  * 启动百度活体检测任务
@@ -71,6 +72,7 @@ public class ProcessBaiduFacelivenessTask implements ApplicationRunner {
                         Util.sleep(PropertyHolder.getInt("$capture.queue.offer.sleepSeconds.", 30));
                     }
                 }
+                Util.sleep(4);
             } catch (Exception e) {
                 log.error("300002", "启动百度活体检测队列出现异常", e);
                 captureLog.error("[PROCESS_BAIDUFACELIVENESS." + processBatchNum + "] 百度活体检测出出异常,3秒后重试", e);
@@ -80,7 +82,6 @@ public class ProcessBaiduFacelivenessTask implements ApplicationRunner {
     }
 
 
-
     @Override
     public void run(ApplicationArguments args) {
         new Thread(() -> {

+ 3 - 3
examcloud-core-oe-face-starter/src/main/java/cn/com/qmth/examcloud/core/oe/student/face/starter/config/ProcessFaceCompareAlarmTask.java

@@ -20,6 +20,7 @@ import org.springframework.stereotype.Component;
 import java.util.Arrays;
 import java.util.HashMap;
 import java.util.List;
+import java.util.concurrent.locks.LockSupport;
 
 /**
  * @Description 人脸比对预警任务
@@ -99,10 +100,9 @@ public class ProcessFaceCompareAlarmTask implements ApplicationRunner {
                     }
                 }
                 //每1分钟重置一次总数量与失败数量
-                ExamCaptureProcessStatisticController.resetFaceCompareCount();
-                ExamCaptureProcessStatisticController.resetFaceCompareFailureCount();
+                ExamCaptureProcessStatisticController.resetAllFaceCompareCount();
             } catch (StatusException e) {
-                    captureLog.error("[FACE_COMPARE_ALARM.] 人脸比对出现异常 ",e);
+                    captureLog.error("[FACE_COMPARE_ALARM.] 人脸比对预警出现异常 ",e);
             }
             //每分钟轮循一次
             Util.sleep(60);

+ 2 - 1
examcloud-core-oe-face-starter/src/main/java/cn/com/qmth/examcloud/core/oe/student/face/starter/config/ProcessFaceCompareQueueTask.java

@@ -17,6 +17,7 @@ import org.springframework.core.annotation.Order;
 import org.springframework.stereotype.Component;
 
 import java.util.List;
+import java.util.concurrent.TimeUnit;
 
 /**
  * 启动人脸比对任务
@@ -78,6 +79,7 @@ public class ProcessFaceCompareQueueTask implements ApplicationRunner {
                         Util.sleep(PropertyHolder.getInt("$capture.queue.offer.sleepSeconds.", 30));
                     }
                 }
+                Util.sleep(2);
             } catch (Exception e) {
                 captureLog.error("[PROCESS_FACEPP." + processBatchNum + "] 百度活体检测出出异常,3秒后重试", e);
                 Util.sleep(3);
@@ -86,7 +88,6 @@ public class ProcessFaceCompareQueueTask implements ApplicationRunner {
     }
 
 
-
     @Override
     public void run(ApplicationArguments args) {
         new Thread(() -> {