refactor(): 人脸识别功能补充异常排查与权限判断(增加了nacos请求内网图片配置)解决SSL证书问题
parent
20a5a3a4fc
commit
6ae851adf6
|
|
@ -57,16 +57,16 @@ public class StudentSignAddExe {
|
||||||
String faceUrl = null;
|
String faceUrl = null;
|
||||||
try {
|
try {
|
||||||
faceUrl = Base64Util.getBase64String(studentSignE.getFiles()[0]);
|
faceUrl = Base64Util.getBase64String(studentSignE.getFiles()[0]);
|
||||||
log.info("StudentSignAddExe-execute,调用阿里进行人脸识别开始");
|
|
||||||
long startAliTime = System.currentTimeMillis();
|
|
||||||
Boolean environment = false;
|
Boolean environment = false;
|
||||||
log.info("StudentSignAddExe-execute,fileUrlConfig.getPrefixUrl():{}",fileUrlConfig.getPrefixUrl());
|
if (!"https://jpfz.qhdsafety.com/gbsFileTest/".equals(fileUrlConfig.getPrefixUrl())) {
|
||||||
if(!"https://jpfz.qhdsafety.com/gbsFileTest/".equals(fileUrlConfig.getPrefixUrl())){
|
|
||||||
environment = true;
|
environment = true;
|
||||||
}
|
}
|
||||||
|
log.info("StudentSignAddExe-execute,开始进行人脸识别,prefixUrl:{},environment:{}", fileUrlConfig.getPrefixUrl(), environment);
|
||||||
studentSignE.compareFace(student.getUserAvatarUrl(), faceUrl, fileUrlConfig.getPrefixUrl(),environment);
|
long startAliTime = System.currentTimeMillis();
|
||||||
log.info("StudentSignAddExe-execute,调用阿里进行人脸识别结束,耗时:{}",System.currentTimeMillis() - startAliTime);
|
studentSignE.compareFace(student.getUserAvatarUrl(), faceUrl, fileUrlConfig.getPrefixUrl(), environment);
|
||||||
|
log.info("StudentSignAddExe-execute,人脸识别结束,耗时:{}", System.currentTimeMillis() - startAliTime);
|
||||||
|
} catch (BizException e) {
|
||||||
|
throw e;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
|
@ -75,7 +75,12 @@ public class StudentSignAddExe {
|
||||||
String fileName = Tools.get32UUID() + studentSignE.getFiles()[0].getOriginalFilename().substring(studentSignE.getFiles()[0].getOriginalFilename().lastIndexOf("."));
|
String fileName = Tools.get32UUID() + studentSignE.getFiles()[0].getOriginalFilename().substring(studentSignE.getFiles()[0].getOriginalFilename().lastIndexOf("."));
|
||||||
log.info("StudentSignAddExe-execute,保存人脸图片开始");
|
log.info("StudentSignAddExe-execute,保存人脸图片开始");
|
||||||
long l = System.currentTimeMillis();
|
long l = System.currentTimeMillis();
|
||||||
studentSignE.setFaceUrl(zcloudImgFilesFacade.saveFile( faceUrl,fileName, path));
|
try {
|
||||||
|
studentSignE.setFaceUrl(zcloudImgFilesFacade.saveFile(
|
||||||
|
studentSignE.getFiles()[0].getBytes(), fileName, path, stu.getClassCorpinfoId()));
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new BizException("保存人脸图片失败");
|
||||||
|
}
|
||||||
log.info("StudentSignAddExe-execute,保存人脸图片结束,耗时:{}",System.currentTimeMillis() - l);
|
log.info("StudentSignAddExe-execute,保存人脸图片结束,耗时:{}",System.currentTimeMillis() - l);
|
||||||
studentSignE.init(stu.getStudentId(), stu.getClassId(), stu.getClassCorpinfoId());
|
studentSignE.init(stu.getStudentId(), stu.getClassId(), stu.getClassCorpinfoId());
|
||||||
boolean res = false;
|
boolean res = false;
|
||||||
|
|
|
||||||
|
|
@ -6,11 +6,17 @@ import com.alibaba.fastjson.JSONObject;
|
||||||
import com.jjb.saas.framework.domain.model.BaseE;
|
import com.jjb.saas.framework.domain.model.BaseE;
|
||||||
import com.zcloud.gbscommon.utils.Base64Util;
|
import com.zcloud.gbscommon.utils.Base64Util;
|
||||||
import com.zcloud.gbscommon.utils.FaceUtil;
|
import com.zcloud.gbscommon.utils.FaceUtil;
|
||||||
import com.zcloud.gbscommon.utils.HttpRequestUtil;
|
|
||||||
import com.zcloud.gbscommon.utils.Tools;
|
import com.zcloud.gbscommon.utils.Tools;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
@ -71,19 +77,16 @@ public class StudentSignE extends BaseE {
|
||||||
public void compareFace(String templateFaceUrl, String faceUrl, String prefixUrl, Boolean environment) throws Exception {
|
public void compareFace(String templateFaceUrl, String faceUrl, String prefixUrl, Boolean environment) throws Exception {
|
||||||
try {
|
try {
|
||||||
String templateFace = Base64Util.urlToBase64(prefixUrl + templateFaceUrl);
|
String templateFace = Base64Util.urlToBase64(prefixUrl + templateFaceUrl);
|
||||||
Map<String, Object> map = new HashMap<String, Object>();
|
|
||||||
map.put("pic1", templateFace);
|
|
||||||
map.put("pic2", faceUrl);
|
|
||||||
String jsonParam = JSON.toJSONString(map);
|
|
||||||
String compareResultStr;
|
String compareResultStr;
|
||||||
if (environment) {
|
if (environment) {
|
||||||
// 正式环境
|
// 正式环境:gbs-face
|
||||||
String compareResult = HttpRequestUtil.doPost("http://192.168.192.201:8971/gbs-face/face/compareFace", jsonParam);
|
Map<String, Object> map = new HashMap<>();
|
||||||
JSONObject jsonObject = JSONObject.parseObject(compareResult);
|
map.put("pic1", templateFace);
|
||||||
jsonObject.getJSONObject("info").getString("confidence");
|
map.put("pic2", faceUrl);
|
||||||
compareResultStr = jsonObject.getJSONObject("info").getString("confidence");
|
String compareResult = callGbsFaceCompare(JSON.toJSONString(map));
|
||||||
|
compareResultStr = parseGbsFaceConfidence(compareResult);
|
||||||
} else {
|
} else {
|
||||||
// 测试环境
|
// 测试环境:阿里云
|
||||||
compareResultStr = FaceUtil.compareFace(templateFace, faceUrl);
|
compareResultStr = FaceUtil.compareFace(templateFace, faceUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -91,11 +94,70 @@ public class StudentSignE extends BaseE {
|
||||||
throw new BizException("人脸不匹配");
|
throw new BizException("人脸不匹配");
|
||||||
}
|
}
|
||||||
this.setStudentSignId(Tools.get32UUID());
|
this.setStudentSignId(Tools.get32UUID());
|
||||||
|
} catch (BizException e) {
|
||||||
|
throw e;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
throw new BizException("人脸比对失败: " + e.getMessage());
|
||||||
throw new BizException("人脸不匹配");
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String callGbsFaceCompare(String jsonParam) throws Exception {
|
||||||
|
URL url = new URL("http://192.168.192.201:8971/gbs-face/face/compareFace");
|
||||||
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||||
|
connection.setRequestMethod("POST");
|
||||||
|
connection.setConnectTimeout(15000);
|
||||||
|
connection.setReadTimeout(60000);
|
||||||
|
connection.setDoOutput(true);
|
||||||
|
connection.setDoInput(true);
|
||||||
|
connection.setRequestProperty("Content-Type", "application/json");
|
||||||
|
connection.setRequestProperty("Accept", "application/json");
|
||||||
|
byte[] body = jsonParam.getBytes(StandardCharsets.UTF_8);
|
||||||
|
connection.setFixedLengthStreamingMode(body.length);
|
||||||
|
try (OutputStream os = connection.getOutputStream()) {
|
||||||
|
os.write(body);
|
||||||
|
}
|
||||||
|
int responseCode = connection.getResponseCode();
|
||||||
|
InputStream is = responseCode >= 200 && responseCode < 300
|
||||||
|
? connection.getInputStream() : connection.getErrorStream();
|
||||||
|
if (is == null) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
StringBuilder result = new StringBuilder();
|
||||||
|
try (BufferedReader br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) {
|
||||||
|
String line;
|
||||||
|
while ((line = br.readLine()) != null) {
|
||||||
|
result.append(line);
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
connection.disconnect();
|
||||||
|
}
|
||||||
|
return result.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private String parseGbsFaceConfidence(String compareResult) {
|
||||||
|
if (compareResult == null || compareResult.trim().isEmpty()) {
|
||||||
|
throw new BizException("人脸比对服务无响应");
|
||||||
|
}
|
||||||
|
JSONObject jsonObject = JSONObject.parseObject(compareResult);
|
||||||
|
if (jsonObject == null) {
|
||||||
|
throw new BizException("人脸比对服务返回异常: " + compareResult);
|
||||||
|
}
|
||||||
|
if ("exception".equals(jsonObject.getString("result"))) {
|
||||||
|
String exceptionMsg = jsonObject.getString("exception");
|
||||||
|
throw new BizException(exceptionMsg != null ? exceptionMsg : "人脸比对服务异常");
|
||||||
|
}
|
||||||
|
JSONObject info = jsonObject.getJSONObject("info");
|
||||||
|
if (info != null && info.getString("confidence") != null) {
|
||||||
|
return info.getString("confidence");
|
||||||
|
}
|
||||||
|
JSONObject data = jsonObject.getJSONObject("data");
|
||||||
|
if (data != null && data.getString("confidence") != null) {
|
||||||
|
return data.getString("confidence");
|
||||||
|
}
|
||||||
|
if (jsonObject.getString("confidence") != null) {
|
||||||
|
return jsonObject.getString("confidence");
|
||||||
|
}
|
||||||
|
throw new BizException("人脸比对服务返回异常: " + compareResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void init(String studentId, String classId, long corpinfoId) {
|
public void init(String studentId, String classId, long corpinfoId) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue