|
|
@@ -1,20 +1,242 @@
|
|
|
package com.example.faceverification.service.impl;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
|
|
|
+import com.example.faceverification.Dto.DataDto;
|
|
|
+import com.example.faceverification.config.SearchByImageConfig;
|
|
|
import com.example.faceverification.model.IdentityComparisonRecord;
|
|
|
import com.example.faceverification.mapper.IdentityComparisonRecordMapper;
|
|
|
+import com.example.faceverification.response.BaseResponse;
|
|
|
+import com.example.faceverification.response.enums.StatusEnum;
|
|
|
import com.example.faceverification.service.IdentityComparisonRecordService;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.example.faceverification.utils.Base64Utils;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
/**
|
|
|
* <p>
|
|
|
- * 服务实现类
|
|
|
+ * 服务实现类
|
|
|
* </p>
|
|
|
*
|
|
|
* @author ceshi
|
|
|
* @since 2023-04-27
|
|
|
*/
|
|
|
@Service
|
|
|
+@Slf4j
|
|
|
public class IdentityComparisonRecordServiceImpl extends ServiceImpl<IdentityComparisonRecordMapper, IdentityComparisonRecord> implements IdentityComparisonRecordService {
|
|
|
+ String taskId = "";
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public synchronized BaseResponse comparison(DataDto data) {
|
|
|
+ log.info("人脸开始对比");
|
|
|
+ if (ObjectUtils.isEmpty(data)) {
|
|
|
+ return BaseResponse.error(StatusEnum.PARAMETER_FAIL);
|
|
|
+ }
|
|
|
+ Map<String, Object> query = null;
|
|
|
+ IdentityComparisonRecord comparisonRecord = new IdentityComparisonRecord();
|
|
|
+ comparisonRecord.setName(data.getName());
|
|
|
+ comparisonRecord.setCategory(data.getCategory());
|
|
|
+ comparisonRecord.setPictureUrl(data.getUrl());
|
|
|
+ comparisonRecord.setIdentityCard(data.getIdCard());
|
|
|
+ comparisonRecord.setStudentNumber(data.getStudentNumber());
|
|
|
+ comparisonRecord.setDeleted(0L);
|
|
|
+ synchronized (this) {
|
|
|
+ String url = data.getUrl();
|
|
|
+ try {
|
|
|
+
|
|
|
+ String s = taskID();
|
|
|
+ target(s, url);
|
|
|
+ control(s);
|
|
|
+ // 获取进度
|
|
|
+ String s1 = get(s);
|
|
|
+ while (!"100".equals(s1)) {
|
|
|
+ s1 = get(s);
|
|
|
+ }
|
|
|
+ query = query(s);
|
|
|
+ String s2 = query.get("Total").toString();
|
|
|
+ Integer integer = Integer.valueOf(s2);
|
|
|
+ if (0>=integer) {
|
|
|
+ log.info("人脸对比失败");
|
|
|
+ comparisonRecord.setResults(1);
|
|
|
+ this.save(comparisonRecord);
|
|
|
+ return BaseResponse.error(StatusEnum.FACE_FAIL);
|
|
|
+ }
|
|
|
+
|
|
|
+ String personList = query.get("PersonList").toString();
|
|
|
+ JSONArray jsonArray = JSONArray.parseArray(personList);
|
|
|
+ for (int i = 0; i < jsonArray.size(); i++) {
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(jsonArray.get(i).toString());
|
|
|
+ JSONObject targetInfo = jsonObject.getJSONObject("TargetInfo");
|
|
|
+// 获取姓名
|
|
|
+ String personName = targetInfo.getString("PersonName");
|
|
|
+ log.info("找到对比图片的姓名【{}】",personName);
|
|
|
+// 获取证件号
|
|
|
+ JSONArray identificationList = jsonObject.getJSONArray("IdentificationList");
|
|
|
+ if (ObjectUtils.isNotEmpty(identificationList)) {
|
|
|
+ for (int j = 0; j < identificationList.size(); j++) {
|
|
|
+ JSONObject jsonObject1 = JSONObject.parseObject(identificationList.get(j).toString());
|
|
|
+ String number = jsonObject1.getString("Number");
|
|
|
+ log.info("找到对比图片的证件号【{}】",number);
|
|
|
+ if (data.getIdCard().equals(number)||data.getStudentNumber().equals(number)) {
|
|
|
+ comparisonRecord.setResults(0);
|
|
|
+ this.save(comparisonRecord);
|
|
|
+ return BaseResponse.ok(StatusEnum.FACE_SUCCESS);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (data.getName().equals(personName)) {
|
|
|
+ log.info("人脸对比成功");
|
|
|
+ comparisonRecord.setResults(0);
|
|
|
+ this.save(comparisonRecord);
|
|
|
+ return BaseResponse.ok(StatusEnum.FACE_SUCCESS);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("人脸对比报错");
|
|
|
+ } finally {
|
|
|
+ delect(taskId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ comparisonRecord.setResults(1);
|
|
|
+ log.info("人脸对比失败");
|
|
|
+ this.save(comparisonRecord);
|
|
|
+ return BaseResponse.error(StatusEnum.FACE_FAIL);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建图搜任务
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public String taskID() {
|
|
|
+ HashMap<String, Object> map = new HashMap<>();
|
|
|
+ Map<String, Object> post = SearchByImageConfig.searchByImageRecord(map, "POST", "", "/LAPI/V1.0/Smart/SearchByImage/Task");
|
|
|
+ taskId = post.get("TaskId").toString();
|
|
|
+ return taskId;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public Map target(String id, String url) {
|
|
|
+// 添加检索参数
|
|
|
+ Map<String, Object> FileInfo = new HashMap<String, Object>();
|
|
|
+ FileInfo.put("Type", 1);
|
|
|
+ String s = Base64Utils.imageUrlToBase64(url);
|
|
|
+// String s = Base64Utils.GetImageStr(url);
|
|
|
+ s = s.replace("\r\n", "")/*.replace("+","")*/;
|
|
|
+
|
|
|
+ FileInfo.put("Size", s.length());
|
|
|
+
|
|
|
+ FileInfo.put("Data", s);
|
|
|
+
|
|
|
+
|
|
|
+ ArrayList<Map> FileInfos = new ArrayList<>();
|
|
|
+ FileInfos.add(FileInfo);
|
|
|
+
|
|
|
+ Map<String, Object> TargetInfo = new HashMap<String, Object>();
|
|
|
+ TargetInfo.put("SimilarityThreshold", 80);
|
|
|
+ TargetInfo.put("TargetInfoList", FileInfos);
|
|
|
+ TargetInfo.put("TargetNum", 1);
|
|
|
+
|
|
|
+ HashMap<String, Object> SearchTarget = new HashMap<String, Object>();
|
|
|
+ SearchTarget.put("TargetInfo", TargetInfo);
|
|
|
+
|
|
|
+ Map<String, Object> CompareLib = new HashMap<String, Object>();
|
|
|
+ CompareLib.put("LibType", 0);
|
|
|
+ ArrayList<Integer> strings = new ArrayList<>();
|
|
|
+ HashMap<String, Object> map1 = new HashMap<>();
|
|
|
+ Map<String, Object> get = SearchByImageConfig.searchByImageRecord(map1, "GET", "", "/LAPI/V1.0/PeopleLibraries/BasicInfo");
|
|
|
+ String num = get.get("Num").toString();
|
|
|
+ JSONArray jsonObject = JSONObject.parseArray(get.get("LibList").toString());
|
|
|
+ for (int i = 0; i < jsonObject.size(); i++) {
|
|
|
+ Map map = JSONObject.parseObject(jsonObject.get(i).toString(), Map.class);
|
|
|
+ String id1 = map.get("ID").toString();
|
|
|
+ strings.add(Integer.valueOf(id1));
|
|
|
+ }
|
|
|
+ CompareLib.put("LibNum", num);
|
|
|
+ CompareLib.put("LibList", strings);
|
|
|
+
|
|
|
+ Map<String, Object> CompareTarget = new HashMap<String, Object>();
|
|
|
+ CompareTarget.put("Type", 1);
|
|
|
+ CompareTarget.put("CompareLib", CompareLib);
|
|
|
+
|
|
|
+
|
|
|
+ JSONObject map2 = new JSONObject();
|
|
|
+ map2.put("SearchTarget", SearchTarget);
|
|
|
+ map2.put("CompareTarget", CompareTarget);
|
|
|
+ String jsonString = map2.toJSONString();
|
|
|
+
|
|
|
+ HashMap<String, Object> map = new HashMap<>();
|
|
|
+ Map<String, Object> post1 = SearchByImageConfig.searchByImageRecord(map, "POST", jsonString, "/LAPI/V1.0/Smart/SearchByImage/Task/" + id + "/Target");
|
|
|
+
|
|
|
+ return post1;
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 启动
|
|
|
+ *
|
|
|
+ * @param id
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public void control(String id) {
|
|
|
+ // 启动
|
|
|
+ JSONObject jsonObject1 = new JSONObject();
|
|
|
+ jsonObject1.put("Control", 0);
|
|
|
+ String s = jsonObject1.toJSONString();
|
|
|
+ HashMap<String, Object> map3 = new HashMap<>();
|
|
|
+ Map<String, Object> put = SearchByImageConfig.searchByImageRecord(map3, "PUT", s, "/LAPI/V1.0/Smart/SearchByImage/Task/" + id + "/Control");
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取进度
|
|
|
+ *
|
|
|
+ * @param id
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public String get(String id) {
|
|
|
+ HashMap<String, Object> map = new HashMap<>();
|
|
|
+ Map<String, Object> get = SearchByImageConfig.searchByImageRecord(map, "GET", "", "/LAPI/V1.0/Smart/SearchByImage/Progress?TaskID=" + id);
|
|
|
+ String percent = get.get("Percent").toString();
|
|
|
+ return percent;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取对比结果
|
|
|
+ *
|
|
|
+ * @param id
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public Map<String, Object> query(String id) {
|
|
|
+ HashMap<String, Object> map = new HashMap<>();
|
|
|
+
|
|
|
+ Map<String, Object> get = SearchByImageConfig.searchByImageRecord(map, "GET", "", "/LAPI/V1.0/Smart/SearchByImage/Query?Task=" + id + "&Limit=12&Offset=0");
|
|
|
+ return get;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除图搜任务
|
|
|
+ *
|
|
|
+ * @param id
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public void delect(String id) {
|
|
|
+ HashMap<String, Object> map = new HashMap<>();
|
|
|
+ Map<String, Object> post = SearchByImageConfig.searchByImageRecord(map, "DELETE", "", "/LAPI/V1.0/Smart/SearchByImage/Task/" + id);
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
}
|