From b758bd4f87cb520c65e64eb3039d57235a6ca808 Mon Sep 17 00:00:00 2001 From: zhouwentao <1577701412@qq.com> Date: Fri, 1 Sep 2023 16:38:55 +0800 Subject: [PATCH] updates --- .../modules/api/controller/ApiController.java | 5 +- .../modules/ocr/init/HandleCallbacklnit.java | 140 -------- .../wlycallback/CallBackWlyRequestBody.java | 19 ++ .../model/wlycallback/CallBackWlyResult.java | 27 ++ .../service/impl/OcrIdentifyServiceImpl.java | 304 +----------------- .../modules/ocr/utils/CallBackWlyUtils.java | 97 ++++++ 6 files changed, 156 insertions(+), 436 deletions(-) create mode 100644 jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/model/wlycallback/CallBackWlyRequestBody.java create mode 100644 jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/model/wlycallback/CallBackWlyResult.java create mode 100644 jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/utils/CallBackWlyUtils.java diff --git a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/api/controller/ApiController.java b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/api/controller/ApiController.java index 1f2bb23..e8eadb9 100644 --- a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/api/controller/ApiController.java +++ b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/api/controller/ApiController.java @@ -252,10 +252,11 @@ public class ApiController { @ApiOperation(value = "调试101") @PostMapping(value = "/test_101") public Result test101(@RequestBody JSONObject jsonObject){ + ocrIdentifyService.callbackWly("1688915746261848065"); //OcrIdentifyDTO byId = ocrIdentifyService.findById("1692068694831755265"); //ocrIdentifyService.updateTaskResultInfo(byId.getId()); - JSONObject semanticResponseJson = JSONObject.parseObject("{\"img_path\":\"https://h5.mcnetmart.com/tmp/images/dc_demo1.png\",\"ocr_time\":3.4992809295654297,\"task_id\":\"1697133769514909698_1\",\"message\":\"成功\",\"semantic_result\":{\"ocr_res\":[[[[136.0,98.0],[276.0,70.0],[281.0,93.0],[141.0,121.0]],[\"深圳康瑞医院\",0.9387677311897278]],[[[139.0,120.0],[284.0,92.0],[288.0,109.0],[142.0,136.0]],[\"Shenzhen Kangrui hospual\",0.9168243408203125]],[[[171.0,284.0],[260.0,268.0],[264.0,292.0],[175.0,308.0]],[\"赵思杰\",0.9619879126548767]],[[[191.0,311.0],[247.0,301.0],[251.0,323.0],[195.0,333.0]],[\"妇产科\",0.9429848790168762]],[[[189.0,346.0],[259.0,332.0],[263.0,350.0],[193.0,364.0]],[\"主治医师\",0.8993821144104004]],[[[198.0,375.0],[263.0,364.0],[267.0,381.0],[201.0,393.0]],[\"202015\",0.9382884502410889]]],\"其他\":[{\"area\":[[139.0,120.0],[284.0,92.0],[288.0,109.0],[142.0,136.0]],\"end\":24,\"ocrText\":\"Shenzhen Kangrui hospual\",\"probability\":0.9846151031007864,\"start\":0,\"text\":\"Shenzhen Kangrui hospual\"},{\"area\":[[189.0,346.0],[259.0,332.0],[263.0,350.0],[193.0,364.0]],\"end\":4,\"ocrText\":\"主治医师\",\"probability\":0.9848503723946322,\"start\":0,\"text\":\"主治医师\"},{\"area\":[[198.0,375.0],[263.0,364.0],[267.0,381.0],[201.0,393.0]],\"end\":6,\"ocrText\":\"202015\",\"probability\":0.9731923600763821,\"start\":0,\"text\":\"202015\"}],\"医院名称\":[{\"area\":[[136.0,98.0],[276.0,70.0],[281.0,93.0],[141.0,121.0]],\"end\":6,\"ocrText\":\"深圳康瑞医院\",\"probability\":0.9856892892579268,\"start\":0,\"text\":\"深圳康瑞医院\"}],\"姓名\":[{\"area\":[[171.0,284.0],[260.0,268.0],[264.0,292.0],[175.0,308.0]],\"end\":3,\"ocrText\":\"赵思杰\",\"probability\":0.9996551423001598,\"start\":0,\"text\":\"赵思杰\"}],\"时间\":[],\"科室\":[{\"area\":[[191.0,311.0],[247.0,301.0],[251.0,323.0],[195.0,333.0]],\"end\":3,\"ocrText\":\"妇产科\",\"probability\":0.9983404968615517,\"start\":0,\"text\":\"妇产科\"}]},\"nlu_time\":16.85913324356079,\"identifyId\":\"1697133769514909698\"}"); - ocrIdentifyService.getSemanticInfo(semanticResponseJson); + //JSONObject semanticResponseJson = JSONObject.parseObject("{\"img_path\":\"https://h5.mcnetmart.com/tmp/images/dc_demo1.png\",\"ocr_time\":3.4992809295654297,\"task_id\":\"1697133769514909698_1\",\"message\":\"成功\",\"semantic_result\":{\"ocr_res\":[[[[136.0,98.0],[276.0,70.0],[281.0,93.0],[141.0,121.0]],[\"深圳康瑞医院\",0.9387677311897278]],[[[139.0,120.0],[284.0,92.0],[288.0,109.0],[142.0,136.0]],[\"Shenzhen Kangrui hospual\",0.9168243408203125]],[[[171.0,284.0],[260.0,268.0],[264.0,292.0],[175.0,308.0]],[\"赵思杰\",0.9619879126548767]],[[[191.0,311.0],[247.0,301.0],[251.0,323.0],[195.0,333.0]],[\"妇产科\",0.9429848790168762]],[[[189.0,346.0],[259.0,332.0],[263.0,350.0],[193.0,364.0]],[\"主治医师\",0.8993821144104004]],[[[198.0,375.0],[263.0,364.0],[267.0,381.0],[201.0,393.0]],[\"202015\",0.9382884502410889]]],\"其他\":[{\"area\":[[139.0,120.0],[284.0,92.0],[288.0,109.0],[142.0,136.0]],\"end\":24,\"ocrText\":\"Shenzhen Kangrui hospual\",\"probability\":0.9846151031007864,\"start\":0,\"text\":\"Shenzhen Kangrui hospual\"},{\"area\":[[189.0,346.0],[259.0,332.0],[263.0,350.0],[193.0,364.0]],\"end\":4,\"ocrText\":\"主治医师\",\"probability\":0.9848503723946322,\"start\":0,\"text\":\"主治医师\"},{\"area\":[[198.0,375.0],[263.0,364.0],[267.0,381.0],[201.0,393.0]],\"end\":6,\"ocrText\":\"202015\",\"probability\":0.9731923600763821,\"start\":0,\"text\":\"202015\"}],\"医院名称\":[{\"area\":[[136.0,98.0],[276.0,70.0],[281.0,93.0],[141.0,121.0]],\"end\":6,\"ocrText\":\"深圳康瑞医院\",\"probability\":0.9856892892579268,\"start\":0,\"text\":\"深圳康瑞医院\"}],\"姓名\":[{\"area\":[[171.0,284.0],[260.0,268.0],[264.0,292.0],[175.0,308.0]],\"end\":3,\"ocrText\":\"赵思杰\",\"probability\":0.9996551423001598,\"start\":0,\"text\":\"赵思杰\"}],\"时间\":[],\"科室\":[{\"area\":[[191.0,311.0],[247.0,301.0],[251.0,323.0],[195.0,333.0]],\"end\":3,\"ocrText\":\"妇产科\",\"probability\":0.9983404968615517,\"start\":0,\"text\":\"妇产科\"}]},\"nlu_time\":16.85913324356079,\"identifyId\":\"1697133769514909698\"}"); + //ocrIdentifyService.getSemanticInfo(semanticResponseJson); /*OcrRuleCheckVo ocrRuleCheckVo = byId.getOcrRuleCheckVo(); Map configRuleMap = ocrRuleCheckVo.getConfigRuleMap();*/ return Result.OK(""); diff --git a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/init/HandleCallbacklnit.java b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/init/HandleCallbacklnit.java index 33f7162..f63624e 100644 --- a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/init/HandleCallbacklnit.java +++ b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/init/HandleCallbacklnit.java @@ -46,7 +46,6 @@ public class HandleCallbacklnit implements ApplicationRunner { @Value("${system.project.enableHandleTask}") private boolean enableHandleTask; /** - * * @param args */ @Override @@ -91,145 +90,6 @@ public class HandleCallbacklnit implements ApplicationRunner { } } - /*if (enableHandleTask) { - try { - Thread.sleep(5000l); - } catch (InterruptedException e) { - e.printStackTrace(); - } - //循环获取 有没有需要 回调 的任务. - while (true) { - try { - Thread.sleep(10000l); - } catch (InterruptedException e) { - e.printStackTrace(); - } - - - List taskList = taskService.getTaskList(); - if (taskList.size()==0) { - continue; - } - for (TaskModel taskModel : taskList) { - //刷新redis,执行中 - flushTask(taskModel.getTaskId(), 1); - //执行 - executeTaskp(taskModel); - //刷新待运行任务 - log.error("任务已执行:"+taskModel.getTaskId()); - } - } - }*/ - } - - /** - * 刷新任务状态 - * @param taskId - * @param status 状态 1 运行中,2 已结束 - */ - public void flushTask(String taskId, int status) { - List taskList = taskService.getTaskList(); - if (taskList != null && taskList.size() > 0) { - TaskModel taskModel = taskList.stream().filter(t -> t.getTaskId().equals(taskId)).findFirst().get(); - if (taskModel != null) { - int taskLevel = taskModel.getTaskLevel(); - String task = (String) redisUtil.get("task_identify_" + taskLevel); - JSONArray taskJsonArray = JSONObject.parseArray(task); - taskList = new ArrayList<>(); - if (task != null) { - taskList.addAll(taskJsonArray.toJavaList(TaskModel.class)); - } - if (status == 1) { - //更新任务状态 - taskList.forEach(t -> { - if (t.getTaskId().equals(taskId)) { - t.setTaskStatus("1"); - } - }); - } else if (status == 2) { - //删除任务 - taskList = taskList.stream().filter(t -> !t.getTaskId().equals(taskId)).collect(Collectors.toList()); - } - String s = JSONObject.toJSONString(taskList); - //存入redis - redisUtil.set("task_identify_" + taskLevel, s); - } - } } - //执行任务 - public void executeTaskp(TaskModel taskModel) { - //睡眠 - try { - String parameter = taskModel.getParameter(); - // 任务类型 - if ("identify".equals(taskModel.getTaskType())) { - String[] parameters = parameter.split(","); - //通用识别 - String masterTaskId = parameters[0];//主任务redis - String task_id = parameters[1]; - String image = parameters[2]; - JSONObject requestBody = new JSONObject(); - requestBody.put("task_id", task_id); - requestBody.put("img_path", image); - //nohup python app.py & tail -f nohup.out & - //更新主任务,正在识别中. conda activate ai - ocrIdentifyService.updateMasterTaskStartTime(task_id.split("_")[0]); - boolean flag=false; - JSONObject semanticResponseJson =null; - while (!flag){ - try { - semanticResponseJson = RestUtil.post(OcrConstant.api_test2_identify_url, requestBody); - if (semanticResponseJson!=null) { - flag=true; - } - }catch (org.springframework.web.client.ResourceAccessException e){ - log.error(e.getMessage()); - log.error("正在重试..."); - } - } - semanticResponseJson.put("identifyId", masterTaskId); - log.info("ocr识别返回数据:"); - log.info(semanticResponseJson.toJSONString()); - ocrIdentifyService.getSemanticInfo(semanticResponseJson); - //该子任务已执行,判断主任务是否残留 - String masterTask = (String) redisUtil.get("identify_" + masterTaskId); - if (StringUtils.isNotBlank(masterTask)) { - //主任务中排除当前任务 - String collect = Arrays.asList(masterTask.split(",")).stream().filter(t -> !t.equals(task_id)).collect(Collectors.joining(",")); - if (StringUtils.isBlank(collect)) { - //如果主任务下的子任务已清空,删除key - redisUtil.del("identify_" + masterTaskId); - //刷新Ocr识别任务状态 - ocrIdentifyService.updateOcrIdentifyStatus(masterTaskId, "1"); - log.error("更新任务状态,id:" + masterTaskId); - } else { - //主任务还存在,刷新主任务明细 - redisUtil.set("identify_" + masterTaskId, collect); - } - } - } else { - Thread.sleep(2000l); - } - } catch (InterruptedException e) { - e.printStackTrace(); - } - //模拟执行成功 - String overTaskIds = (String) redisUtil.get("over_task_"+taskModel.getTaskType()); - if (org.apache.commons.lang.StringUtils.isBlank(overTaskIds)) { - overTaskIds = taskModel.getTaskId(); - } else { - if (!Arrays.asList(overTaskIds.split(",")).contains(taskModel.getTaskId())) { - //该任务未执行过 - overTaskIds += "," + taskModel.getTaskId(); - } else { - //该任务已结束过 - } - } - //存入已执行 redis里 - redisUtil.set("over_task_"+taskModel.getTaskType(), overTaskIds); - log.error(taskModel.getTaskLevel() + "-级别," + taskModel.getTaskId() + "-已执行"); - //从3中任务集中,删除该任务 - flushTask(taskModel.getTaskId(), 2); - } } diff --git a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/model/wlycallback/CallBackWlyRequestBody.java b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/model/wlycallback/CallBackWlyRequestBody.java new file mode 100644 index 0000000..421a7fb --- /dev/null +++ b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/model/wlycallback/CallBackWlyRequestBody.java @@ -0,0 +1,19 @@ +package org.jeecg.modules.ocr.model.wlycallback; + +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +import java.io.Serializable; + +/** + * @Description 回调无量云对象 + * @Author ZhouWenTao + * @Date 2023/9/1 14:42 + */ +@Data +public class CallBackWlyRequestBody implements Serializable { + @ApiModelProperty(value = "请求唯一标识") + private String requestId; + @ApiModelProperty(value = "请求参数") + private CallBackWlyResult result; +} diff --git a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/model/wlycallback/CallBackWlyResult.java b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/model/wlycallback/CallBackWlyResult.java new file mode 100644 index 0000000..7697368 --- /dev/null +++ b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/model/wlycallback/CallBackWlyResult.java @@ -0,0 +1,27 @@ +package org.jeecg.modules.ocr.model.wlycallback; + +import com.alibaba.fastjson.JSONObject; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +import java.io.Serializable; +import java.util.List; + +/** + * @Description + * @Author ZhouWenTao + * @Date 2023/9/1 14:43 + */ +@Data +public class CallBackWlyResult implements Serializable { + @ApiModelProperty(value = "是否通过") + boolean ruleValidation=false; + @ApiModelProperty(value = "检索信息与审核数据的符合度") + String retrieveReviewCompliance; + @ApiModelProperty(value = "图片检索的信息与元数据的符合度百分比") + String imageTagRetrievePercentage; + @ApiModelProperty(value = "任务失败原因,若成功则为空") + String failureReason; + @ApiModelProperty(value = "ocr识别结果集") + List ocrResult; +} diff --git a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/service/impl/OcrIdentifyServiceImpl.java b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/service/impl/OcrIdentifyServiceImpl.java index e6dc991..a7d6da2 100644 --- a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/service/impl/OcrIdentifyServiceImpl.java +++ b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/service/impl/OcrIdentifyServiceImpl.java @@ -23,10 +23,7 @@ import org.jeecg.modules.ocr.model.CheckSemanticModel; import org.jeecg.modules.ocr.model.SourceImage; import org.jeecg.modules.ocr.model.TaskModel; import org.jeecg.modules.ocr.service.*; -import org.jeecg.modules.ocr.utils.ArrayOUtils; -import org.jeecg.modules.ocr.utils.FileOUtils; -import org.jeecg.modules.ocr.utils.ImageUtils; -import org.jeecg.modules.ocr.utils.StrCharUtil; +import org.jeecg.modules.ocr.utils.*; import org.jeecg.modules.ocr.vo.SimulateChecksVO; import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.EnableAsync; @@ -384,231 +381,12 @@ public class OcrIdentifyServiceImpl extends ServiceImpl identifyDetails = ocrIdentifyDetailService.listByIdentifyId(ocrIdentifyDTO.getId()); - /*for (OcrIdentifyDetail identifyDetail : identifyDetails) { - JSONObject semanticResult = JSONObject.parseObject(identifyDetail.getSemanticResult()); - String imgPath = identifyDetail.getImageUrl(); - // 进行数据化 结构 - if (semanticResult != null) { - OcrRuleCheckVo ocrRuleCheckVo = ocrIdentifyVo.getOcrRuleCheckVo(); - Map checkSemanticModelMap = getCheckSemanticModelMap(ocrRuleCheckVo.getConfigRuleMap(), ocrRuleCheckVo.getFieldMap(), ocrIdentifyVo.getSourceJsonObjects()); - //// - String text = null;//ocr 识别的文本, - Double probability = 0d; - //用于数据结构化的对象 - List ocrResultList = new ArrayList<>(); - - StringBuffer rMessage = new StringBuffer(); - Map fieldRightMap = new LinkedHashMap<>();//存放 字段判断正确map - //========================== - checkSemanticFor: - for (CheckSemanticModel value : checkSemanticModelMap.values()) { - String field = value.getField(); - String fieldName = value.getFieldName();//校验的字段名称 - String ruleInfo = value.getRuleInfo();//是否绝对判断 0-绝对判断,1-不绝对判断 - String inputText = value.getInputText();//校验文本 ,ocr识别的文本如果不包含该内容,则算作失败 - List fieldNameList = Arrays.asList(fieldName.split(",")); - text = null; - boolean b = ArrayOUtils.containsStringList(fieldNameList, semanticResult.keySet().stream().collect(Collectors.toList())); - //查看ocr识别返回的字段名称中是否有当前这个字段名称 - if (b) { - //TODO 注意,ocr 识别返回的 字段是多个结果(数组),有一个值匹配上即为正确 - List ocrArray = new ArrayList<>(); - for (String s : fieldNameList) { - JSONArray jsonArray = semanticResult.getJSONArray(s); - if (jsonArray != null && jsonArray.size() > 0) { - ocrArray.addAll(jsonArray.toJavaList(JSONObject.class)); - } - } - if (ocrArray.size() > 0) { - ocrArrayFor: - for (int i = 0; i < ocrArray.size(); i++) { - JSONObject ocrItem = ocrArray.get(i); - text = null; - text = ocrItem.getString("text");//ocr 识别的文本 - probability = ocrItem.getDouble("probability");//置信度 - log.info("ocrItem:"); - log.info(ocrItem.toJSONString()); - if ("101".equals(ruleInfo)) { - if (StringUtils.isBlank(text) || StringUtils.isBlank(inputText)) { - //没识别值 - mapPutIfTrue(fieldRightMap, field, true); - ocrResultAdd(ocrResultList, value.getFieldName(), field, inputText, null, probability, imgPath, value.getFieldName() + "参数不判断", true); - } else { - double v = StrCharUtil.similarityRatio(inputText, text); - if (text.contains(inputText)) { - mapPutIfTrue(fieldRightMap, field, true); - ocrResultAdd(ocrResultList, value.getFieldName(), field, inputText, text, probability, imgPath, "", true, v); - } else { - rMessage.append(value.getFieldName() + "参数不匹配
"); - mapPutIfTrue(fieldRightMap, field, false); - ocrResultAdd(ocrResultList, value.getFieldName(), field, inputText, text, probability, imgPath, value.getFieldName() + "参数不匹配", false, v); - } - } - } else { - if (StringUtils.isBlank(text)) { - //ocr识别参数为空,不通过 - rMessage.append(value.getFieldName() + "参数未获取到结果
"); - mapPutIfTrue(fieldRightMap, field, false); - ocrResultAdd(ocrResultList, value.getFieldName(), field, inputText, null, probability, imgPath, value.getFieldName() + "参数未获取到结果", false); - } else if (StringUtils.isBlank(inputText)) { - //没有输入值. - fieldRightMap.put(field, true); - ocrResultAdd(ocrResultList, value.getFieldName(), field, inputText, text, probability, imgPath, "没有输入值不做匹配", true); - } else if ("0".equals(ruleInfo)) { - //不必校验,有识别到就行,通过 - fieldRightMap.put(field, true); - ocrResultAdd(ocrResultList, value.getFieldName(), field, inputText, text, probability, imgPath, "", true); - } else if (Double.valueOf(ruleInfo) >= 1 && Double.valueOf(ruleInfo) <= 99) { - //在1~99之间,根据精准度匹配 - double v = StrCharUtil.similarityRatio(inputText, text); - if (v >= Double.valueOf(ruleInfo)) { - //准确度 可靠 - fieldRightMap.put(field, true); - ocrResultAdd(ocrResultList, value.getFieldName(), field, inputText, text, probability, imgPath, "", true, v); - } else { - rMessage.append(value.getFieldName() + "参数不匹配
"); - mapPutIfTrue(fieldRightMap, field, false); - ocrResultAdd(ocrResultList, value.getFieldName(), field, inputText, text, probability, imgPath, value.getFieldName() + "参数不匹配", false, v); - } - } else if ("100".equals(ruleInfo)) { - //必定验证参数,必须有值且匹配 - double v = StrCharUtil.similarityRatio(inputText, text); - if (text.equals(inputText)) { - fieldRightMap.put(field, true); - ocrResultAdd(ocrResultList, value.getFieldName(), field, inputText, text, probability, imgPath, "", true, v); - } else { - rMessage.append(value.getFieldName() + "参数不匹配
"); - mapPutIfTrue(fieldRightMap, field, false); - ocrResultAdd(ocrResultList, value.getFieldName(), field, inputText, text, probability, imgPath, value.getFieldName() + "参数不匹配", false, v); - } - } - } - } - } else { - rMessage.append(value.getFieldName() + "参数未获取到结果
"); - mapPutIfTrue(fieldRightMap, field, false); - ocrResultAdd(ocrResultList, value.getFieldName(), field, inputText, null, probability, imgPath, value.getFieldName() + "参数未获取到结果", false); - } - } else { - rMessage.append(value.getFieldName() + "参数未获取到结果
"); - ocrResultAdd(ocrResultList, value.getFieldName(), field, inputText, null, 0d, imgPath, value.getFieldName() + "参数未获取到结果", false); - fieldRightMap.put(field, false); - } - } - //其中有一个字段 全部都是 失败,则该图片失败. - if (fieldRightMap != null && fieldRightMap.values().size() > 0) { - if (!fieldRightMap.containsValue(false)) { - identifyDetail.setStatus("1");//全部通过 - } else if (fieldRightMap.containsValue(false) && fieldRightMap.containsValue(true)) { - identifyDetail.setStatus("2");//有成功,有失败(部分) - identifyDetail.setMessage(rMessage.toString()); - } else { - identifyDetail.setStatus("0");//有失败的 - identifyDetail.setMessage(rMessage.toString()); - } - } else { - identifyDetail.setStatus("0");//有失败的 - identifyDetail.setMessage(rMessage.toString()); - } - identifyDetail.setDataStructured(JSONArray.toJSONString(ocrResultList));//数据结构化 - ocrIdentifyDetailService.updateById(identifyDetail); - } - }*/ //=======规则检查配置 OcrRuleCheckDTO ocrRuleCheckVo = ocrIdentifyDTO.getOcrRuleCheckVo(); Map> configRuleTypeMap = ocrRuleCheckVo.getConfigRuleTypeMap(); //=================== - //4.更新主任务状态 - /*LambdaUpdateWrapper updateWrapper = new LambdaUpdateWrapper(); - updateWrapper.eq(OcrIdentify::getId, id); - List identifyDetailList = ocrIdentifyDetailService.listByIdentifyId(id); - if(true) { - if (identifyDetailList != null && identifyDetailList.size() > 0) { - Map fieldRightMap = new LinkedHashMap<>(); - String tag = null; - boolean ruleValidation = false; - /////明细中,如果有字段成功的,则会覆盖其他明细的匹配失败的情况,如果全都是失败的,会获取第一次失败的. - for (OcrIdentifyDetail ocrIdentifyDetail : identifyDetailList) { - String dataStructured = ocrIdentifyDetail.getDataStructured(); - if (org.apache.commons.lang.StringUtils.isNotBlank(dataStructured)) { - JSONArray jsonArray = JSONObject.parseArray(dataStructured); - List resultList = jsonArray.toJavaList(OcrResult2.class); - for (OcrResult2 result : resultList) { - tag = result.getTag(); - ruleValidation = result.getRuleValidation(); - OcrResult2 lastResult = fieldRightMap.get(tag); - if (ruleValidation) { - fieldRightMap.put(tag, result); - } else if (lastResult == null || !lastResult.getRuleValidation()) { - result.setFailureReason(result.getTagName()+"未获取到结果"); - fieldRightMap.put(tag, result); - } - } - } - } - //如果明细合并一起,都是成功,则主任务 匹配成功, 否则匹配失败 - List errorResults = fieldRightMap.values().stream().filter(f -> !f.getRuleValidation()).collect(Collectors.toList()); - List ocrResults = fieldRightMap.values().stream().collect(Collectors.toList()); - - //TODO 后续考虑需要修正复杂程度. 支持 & + || 一起 - - String errorMsg = errorResults.stream().map(e -> e.getFailureReason()).collect(Collectors.joining(";")); -// if (configRuleTypeMap.containsKey(OcrConstant.ruleCheckSplitChar)) { - if (errorResults.size() > 0) { - //匹配失败. - updateWrapper.set(OcrIdentify::getErrorMsg, errorMsg); - updateWrapper.set(OcrIdentify::getTaskResult, 0); - } else { - //匹配成功. - updateWrapper.set(OcrIdentify::getTaskResult, 1); - } - String taskResultInfo = JSONArray.toJSONString(ocrResults); - updateWrapper.set(OcrIdentify::getStatus,"1"); - updateWrapper.set(OcrIdentify::getEndTime,new Date()); - updateWrapper.set(OcrIdentify::getTaskResultInfo,taskResultInfo); - } - } - super.update(updateWrapper);*/ - } - - public static void main(String[] args) { - List list=new ArrayList<>(); - OcrResultDTO result = new OcrResultDTO(); - result.setTextRate(10d); - list.add(result); - OcrResultDTO result2 = new OcrResultDTO(); - result2.setTextRate(44d); - list.add(result2); - OcrResultDTO result3 = new OcrResultDTO(); - result3.setTextRate(12d); - list.add(result3); - - //获取最小 - Double min = list.stream().sorted((a, b) -> a.getTextRate().compareTo(b.getTextRate())).findFirst().get().getTextRate(); - //获取最大 - Double max = list.stream().sorted((a, b) -> b.getTextRate().compareTo(a.getTextRate())).findFirst().get().getTextRate(); - //获取最大 - //ocrResultList.stream().sorted((a,b)->a.getTextRate()-b.getTextRate()).findAny().get(); - //int stringPercent = getStringPercent("张三", "张三脏"); - //System.out.println(stringPercent); } - private static int getStringPercent(String text, String inputText) { - String[] inputTextSplit = inputText.split("."); - String[] textSplit = text.split("."); - int trueNum = 0; - for (int i = 0; i < inputTextSplit.length; i++) { - if (textSplit.length >= i) { - for (int p = 0; p < textSplit.length; p++) { - trueNum++; - } - } else { - break; - } - } - return 0; - } @Override public void callbackWly(String ocrIdentifyId) { @@ -622,78 +400,16 @@ public class OcrIdentifyServiceImpl extends ServiceImpl queryWrapper = new LambdaQueryWrapper<>(); - queryWrapper.eq(OcrIdentifyDetail::getIdentifyId, ocrIdentifyId); - queryWrapper.isNotNull(OcrIdentifyDetail::getDataStructured); - List identifyDetails = ocrIdentifyDetailService.list(queryWrapper); - if (identifyDetails != null) { - List jsonObjects = new ArrayList<>(); - boolean ruleValidation = false;//失败 - //成功率 - BigDecimal ocrPrecisionRateBigD = new BigDecimal(0); - //总明细数量 - Integer detail_detailCount = 0; - for (OcrIdentifyDetail identifyDetail : identifyDetails) { - List jsonObjects1 = JSONArray.parseArray(identifyDetail.getDataStructured()).toJavaList(JSONObject.class); - if (jsonObjects1 != null) { - long ruleValidationCount = jsonObjects.stream().filter(o -> o.getBooleanValue("ruleValidation") == false).count(); - if (ruleValidationCount == 0) { - ruleValidation = true; - } - double ocrPrecisionRate = jsonObjects.stream().mapToDouble(o -> o.getDouble("ocrPrecisionRate")).sum(); - ocrPrecisionRateBigD = ocrPrecisionRateBigD.add(new BigDecimal(ocrPrecisionRate)); - detail_detailCount += jsonObjects1.size(); - } - jsonObjects.addAll(jsonObjects1); - } - - //组装回调参数 - JSONObject requestBody = new JSONObject(); - requestBody.put("requestId", ocrIdentify.getRequestId()); - requestBody.put("ruleValidation", ruleValidation); - if (detail_detailCount == 0) { - requestBody.put("imageTagRetrievePercentage", 0); - } else { - BigDecimal divide = ocrPrecisionRateBigD.divide(new BigDecimal(detail_detailCount), 2, BigDecimal.ROUND_HALF_UP); - requestBody.put("imageTagRetrievePercentage", divide.doubleValue()); - } - requestBody.put("retrieveReviewCompliance", 0); - requestBody.put("failureReason", ""); - log.info("请求无量云回调接口"); - JSONObject semanticResponseJson = null; - try { - ocrIdentifyCallbackLog.setStartTime(new Date()); - semanticResponseJson = RestUtil.post("https://192.168.1.21:8686/api/task/image/ocr/callback", requestBody); - } catch (org.springframework.web.client.ResourceAccessException e) { - log.error("请求无量云回调接口失败-拒绝连接 (Connection refused)"); - log.error(e.getMessage()); - } catch (Exception e) { - log.info("请求无量云回调接口失败"); - e.printStackTrace(); - } finally { - log.info("回调返回------------------"); - ocrIdentifyCallbackLog.setEndTime(new Date()); - - if (semanticResponseJson != null) { - log.info(semanticResponseJson.toJSONString()); - updateWrapper.set(OcrIdentify::getNoticeStatus, "1"); - super.update(updateWrapper); - ocrIdentifyCallbackLog.setStatus(1); - } else { - log.info("回调返回-----:null"); - ocrIdentifyCallbackLog.setStatus(0); - } - } - } - - } - ocrIdentifyCallbackLogService.save(ocrIdentifyCallbackLog); + List identifyDetailList = ocrIdentifyDetailService.listByIdentifyId(ocrIdentifyId); + ocrIdentifyCallbackLog.setStartTime(date); + boolean b = CallBackWlyUtils.callbackWly(ocrIdentify, identifyDetailList); + ocrIdentifyCallbackLog.setStatus(b?1:0); + ocrIdentifyCallbackLog.setEndTime(new Date()); + ocrIdentifyCallbackLogService.save(ocrIdentifyCallbackLog); + if (b) { + updateWrapper.set(OcrIdentify::getNoticeStatus,"1"); } + update(updateWrapper); } @Override diff --git a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/utils/CallBackWlyUtils.java b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/utils/CallBackWlyUtils.java new file mode 100644 index 0000000..01f1e8f --- /dev/null +++ b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/utils/CallBackWlyUtils.java @@ -0,0 +1,97 @@ +package org.jeecg.modules.ocr.utils; + +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.jeecg.common.util.RestUtil; +import org.jeecg.modules.ocr.entity.OcrIdentify; +import org.jeecg.modules.ocr.entity.OcrIdentifyCallbackLog; +import org.jeecg.modules.ocr.entity.OcrIdentifyDetail; +import org.jeecg.modules.ocr.model.wlycallback.CallBackWlyRequestBody; +import org.jeecg.modules.ocr.model.wlycallback.CallBackWlyResult; +import org.springframework.stereotype.Component; + +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +/** + * @Description + * @Author ZhouWenTao + * @Date 2023/9/1 14:47 + */ +@Component +@Slf4j +public class CallBackWlyUtils { + + public static boolean callbackWly(OcrIdentify ocrIdentify, List identifyDetails) { + String requestId = ocrIdentify.getRequestId(); + //请求对象 + CallBackWlyResult result = new CallBackWlyResult(); + + List jsonObjects = new ArrayList<>(); + boolean ruleValidation = false;//失败 + //成功率 + BigDecimal ocrPrecisionRateBigD = new BigDecimal(0); + //总明细数量 + Integer detail_detailCount = 0; + for (OcrIdentifyDetail identifyDetail : identifyDetails) { + List jsonObjects1 = JSONArray.parseArray(identifyDetail.getDataStructured()).toJavaList(JSONObject.class); + if (jsonObjects1 != null) { + long ruleValidationCount = jsonObjects.stream().filter(o -> o.getBooleanValue("ruleValidation") == false).count(); + if (ruleValidationCount == 0) { + ruleValidation = true; + } + double ocrPrecisionRate = jsonObjects.stream().mapToDouble(o -> o.getDouble("ocrPrecisionRate")).sum(); + ocrPrecisionRateBigD = ocrPrecisionRateBigD.add(new BigDecimal(ocrPrecisionRate)); + detail_detailCount += jsonObjects1.size(); + } + jsonObjects.addAll(jsonObjects1); + } + + //组装回调参数 + double imageTagRetrievePercentage = 0.0d; + if (detail_detailCount > 0) { + BigDecimal divide = ocrPrecisionRateBigD.divide(new BigDecimal(detail_detailCount), 2, RoundingMode.HALF_UP); + imageTagRetrievePercentage = divide.doubleValue(); + } + + result.setRuleValidation(ruleValidation); + result.setImageTagRetrievePercentage(imageTagRetrievePercentage + ""); + result.setRetrieveReviewCompliance("0"); + result.setFailureReason(ocrIdentify.getErrorMsg()); + result.setOcrResult(jsonObjects); + + log.info("请求无量云回调接口"); + JSONObject semanticResponseJson = null; + try { + CallBackWlyRequestBody callBackWlyRequestBody = new CallBackWlyRequestBody(); + callBackWlyRequestBody.setRequestId(requestId); + callBackWlyRequestBody.setResult(result); + System.out.println("============================================="); + System.out.println(JSONObject.toJSONString(callBackWlyRequestBody)); + System.out.println("============================================="); + semanticResponseJson = RestUtil.post("https://192.168.1.21:8686/api/task/image/ocr/callback", JSONObject.parseObject(JSONObject.toJSONString(callBackWlyRequestBody))); + } catch (org.springframework.web.client.ResourceAccessException e) { + log.error("请求无量云回调接口失败-拒绝连接 (Connection refused)"); + log.error(e.getMessage()); + } catch (Exception e) { + log.info("请求无量云回调接口失败"); + e.printStackTrace(); + } finally { + log.info("回调返回------------------"); + if (semanticResponseJson != null) { + log.info(semanticResponseJson.toJSONString()); + return true; + } else { + log.info("回调返回-----:null"); + return false; + } + } + } +}