From 5e66e6f273541e242ee6c567ce0628a9193e13fa Mon Sep 17 00:00:00 2001 From: zhouwentao <1577701412@qq.com> Date: Mon, 24 Jul 2023 15:10:56 +0800 Subject: [PATCH] updates --- .../org/jeecg/config/shiro/ShiroConfig.java | 1 + .../ocr/controller/OcrApiController.java | 24 +++++++++++- .../service/impl/OcrIdentifyServiceImpl.java | 38 ++++++++++++------- .../service/impl/OcrRuleCheckServiceImpl.java | 2 + .../jeecg/modules/ocr/utils/ArrayCUtils.java | 25 ++++++++++++ 5 files changed, 75 insertions(+), 15 deletions(-) create mode 100644 jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/utils/ArrayCUtils.java diff --git a/jeecg-boot-base-core/src/main/java/org/jeecg/config/shiro/ShiroConfig.java b/jeecg-boot-base-core/src/main/java/org/jeecg/config/shiro/ShiroConfig.java index 2c09b96..baa98b9 100644 --- a/jeecg-boot-base-core/src/main/java/org/jeecg/config/shiro/ShiroConfig.java +++ b/jeecg-boot-base-core/src/main/java/org/jeecg/config/shiro/ShiroConfig.java @@ -76,6 +76,7 @@ public class ShiroConfig { } // 配置不会被拦截的链接 顺序判断 filterChainDefinitionMap.put("/api/**","anon"); + filterChainDefinitionMap.put("/ocrApi/**","anon"); filterChainDefinitionMap.put("/sys/cas/client/validateLogin", "anon"); //cas验证登录 filterChainDefinitionMap.put("/sys/randomImage/**", "anon"); //登录验证码接口排除 diff --git a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/controller/OcrApiController.java b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/controller/OcrApiController.java index df797ff..3990aa4 100644 --- a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/controller/OcrApiController.java +++ b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/controller/OcrApiController.java @@ -9,6 +9,9 @@ import org.jeecg.common.util.AssertUtils; import org.jeecg.common.util.RestUtil; import org.jeecg.modules.ocr.entity.OcrIdentify; import org.jeecg.modules.ocr.service.IOcrIdentifyService; +import org.jeecg.modules.ocr.service.IOcrRuleCheckService; +import org.jeecg.modules.ocr.vo.OcrRuleCheckVo; +import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -29,6 +32,10 @@ import java.util.List; public class OcrApiController { @Resource IOcrIdentifyService ocrIdentifyService; + @Resource + IOcrRuleCheckService ruleCheckService; + + public static final String semanticUrl="http://47.103.213.109:9099/semantic"; /** * 1. 收到 任务 入参 任务id,图片路径[] @@ -77,6 +84,7 @@ public class OcrApiController { public static String semanticResponse="{\"execution_time\":69.1751720905304,\"img_path\":\"./static/ocrData/custom_2_44.jpg\",\"message\":\"成功\",\"semantic_result\":{\"医院名称\":[{\"area\":[[227,382],[441,388],[440,422],[226,416]],\"end\":7,\"ocrText\":\"开封市眼病医院\",\"probability\":0.9677108957485778,\"start\":0,\"text\":\"开封市眼病医院\"}],\"姓名\":[{\"area\":[[566,668],[691,668],[691,712],[566,712]],\"end\":3,\"ocrText\":\"闫利霞\",\"probability\":0.884488371938783,\"start\":0,\"text\":\"闫利霞\"}],\"时间\":[{\"area\":[[701,400],[874,400],[874,456],[701,456]],\"end\":5,\"ocrText\":\"10:40\",\"probability\":0.9626484940814066,\"start\":0,\"text\":\"10:40\"},{\"area\":[[502,422],[667,424],[667,452],[502,450]],\"end\":11,\"ocrText\":\"2023年05月17日\",\"probability\":0.815085233546764,\"start\":0,\"text\":\"2023年05月17日\"}],\"科室\":[]},\"task_id\":1}"; @ApiOperation(value = "通用识别") @RequestMapping("/identify") + @Transactional public Result pushSemantic(@RequestBody JSONObject requestBody){ //String taskId="1681203097437954049";//任务id //List imagesUrl= Arrays.asList("https://h5.mcnetmart.com/tmp/images/dc_demo1.png");//图片路径 @@ -94,22 +102,34 @@ public class OcrApiController { String scenes = requestBody.getString("scenes");//场景类型:door=门头照片,cases=病例,bill=票据 AssertUtils.notTrue(!"door".equals(scenes),String.format("暂不支持该场景类型[%s]",scenes)); String ruleId = requestBody.getString("ruleId");//规则标识 + OcrRuleCheckVo ruleCheck = ruleCheckService.findById(ruleId); + AssertUtils.notNull(ruleCheck,"[ruleId]-不存在"); Integer priority = requestBody.getInteger("priority");//任务优先级:1=加急,0=不加急 JSONArray sourceJson = requestBody.getJSONArray("sourceJson"); List sourceJsonList = sourceJson.toJavaList(JSONObject.class);//校验数据源 String sourceImages = requestBody.getString("sourceImages");//ocr图片集 + OcrIdentify ocrIdentify=new OcrIdentify(); //2.创建识别任务 if(true){ - OcrIdentify ocrIdentify=new OcrIdentify(); ocrIdentify.setTaskName(requestId); ocrIdentify.setStatus("0"); ocrIdentify.setRuleCheck(ruleId); ocrIdentify.setStartTime(new Date()); - //ocrIdentify.setTaskType("") + ocrIdentify.setTaskType("1681508355389231105"); ocrIdentify.setIdentifyUrl("https://h5.mcnetmart.com/tmp/images/dc_demo1.png"); + ocrIdentify.setCreateBy("公共接口"); + ocrIdentify.setSourceJson(requestBody.getJSONArray("sourceJson").toJSONString()); ocrIdentifyService.save(ocrIdentify); } //3.请求python ocr识别 + JSONObject semanticResponseJson = JSONObject.parseObject(semanticResponse); + semanticResponseJson.put("task_id",ocrIdentify.getId()); + ocrIdentifyService.getSemanticInfo(semanticResponseJson); + + //4.更新主任务状态 + ocrIdentify.setStatus("1"); + ocrIdentify.setEndTime(new Date()); + ocrIdentifyService.updateById(ocrIdentify); return Result.OK("请求成功"); } 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 cfb6add..f5fdf81 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 @@ -1,7 +1,9 @@ package org.jeecg.modules.ocr.service.impl; +import cn.hutool.core.util.ArrayUtil; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; +import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.StringUtils; import org.checkerframework.checker.units.qual.C; import org.jeecg.modules.ocr.entity.OcrIdentify; @@ -15,6 +17,7 @@ import org.jeecg.modules.ocr.service.IOcrIdentifyDetailService; import org.jeecg.modules.ocr.service.IOcrIdentifyService; import org.jeecg.modules.ocr.service.IOcrRuleCheckDetailService; import org.jeecg.modules.ocr.service.IOcrRuleCheckService; +import org.jeecg.modules.ocr.utils.ArrayCUtils; import org.jeecg.modules.ocr.vo.OcrIdentifyVo; import org.jeecg.modules.ocr.vo.OcrRuleCheckVo; import org.springframework.beans.BeanUtils; @@ -23,10 +26,8 @@ import org.springframework.stereotype.Service; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import javax.annotation.Resource; -import java.util.ArrayList; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; +import java.util.*; +import java.util.stream.Collectors; /** * @Description: ocr识别 @@ -64,7 +65,7 @@ public class OcrIdentifyServiceImpl extends ServiceImpl ocrResultList=new ArrayList<>(); StringBuffer rMessage=new StringBuffer(); - rMessage.append(message).append("
"); + rMessage.append("
"); Map fieldRightMap=new LinkedHashMap<>();//存放 字段判断正确map //========================== checkSemanticFor: for (CheckSemanticModel value : checkSemanticModelMap.values()) { @@ -111,13 +112,24 @@ public class OcrIdentifyServiceImpl extends ServiceImpl fieldNameList = Arrays.asList(fieldName.split(",")); + boolean b = ArrayCUtils.containsStringList(fieldNameList, semanticResult.keySet().stream().collect(Collectors.toList())); //查看ocr识别返回的字段名称中是否有当前这个字段名称 - if (semanticResult.keySet().contains(fieldName)) { - //TODO 注意,ocr 识别返回的 字段是多个结果(数组),有一个值匹配上即为正确。 - JSONArray ocrArray = semanticResult.getJSONArray(fieldName); + 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)); + } + } ocrArrayFor:for (int i = 0; i < ocrArray.size(); i++) { copyOcrResult=new OcrResult(); - JSONObject ocrItem = ocrArray.getJSONObject(i); + JSONObject ocrItem = ocrArray.get(i); text = ocrItem.getString("text");//ocr 识别的文本 probability = ocrItem.getDouble("probability");//置信度 @@ -140,19 +152,19 @@ public class OcrIdentifyServiceImpl extends ServiceImpl0&&!fieldRightMap.containsValue(false)) { - ocrIdentifyDetail.setStatus("1"); + ocrIdentifyDetail.setStatus("0");//全部通过 }else{ - ocrIdentifyDetail.setStatus("0"); + ocrIdentifyDetail.setStatus("1");//有失败的 ocrIdentifyDetail.setMessage(rMessage.toString()); } ocrIdentifyDetail.setDataStructured(JSONArray.toJSONString(ocrResultList));//数据结构化 ocrIdentifyDetailService.updateById(ocrIdentifyDetail); + } //========================= } diff --git a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/service/impl/OcrRuleCheckServiceImpl.java b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/service/impl/OcrRuleCheckServiceImpl.java index e3bb6f9..8de541a 100644 --- a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/service/impl/OcrRuleCheckServiceImpl.java +++ b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/service/impl/OcrRuleCheckServiceImpl.java @@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import org.apache.commons.lang.StringUtils; import org.jeecg.common.constant.OcrConstant; +import org.jeecg.common.util.AssertUtils; import org.jeecg.modules.ocr.entity.OcrMetadataConfig; import org.jeecg.modules.ocr.entity.OcrMetadataConfigDetail; import org.jeecg.modules.ocr.entity.OcrRuleCheck; @@ -110,6 +111,7 @@ public class OcrRuleCheckServiceImpl extends ServiceImpl objects,List objects2){ + boolean flag=false; + for (String object : objects) { + o2For:for (String o : objects2) { + if (object.equals(o)) { + return true; + } + } + } + return flag; + } +}