master
周文涛 2 years ago
parent 156e642572
commit 5e66e6f273

@ -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"); //登录验证码接口排除

@ -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<String> 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<JSONObject> 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("请求成功");
}

@ -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<OcrIdentifyMapper, OcrId
@Override
public void getSemanticInfo(JSONObject responseBody) {
log.debug("打印 ocr 结果:"+responseBody.toString());
String identifyId = responseBody.getString("identifyId");//任务id
String identifyId = responseBody.getString("task_id");//任务id
String imgPath = responseBody.getString("img_path");//图片路径
String imgName = null;//图片名称
if (StringUtils.isNotBlank(imgPath)) {
@ -103,7 +104,7 @@ public class OcrIdentifyServiceImpl extends ServiceImpl<OcrIdentifyMapper, OcrId
List<OcrResult> ocrResultList=new ArrayList<>();
StringBuffer rMessage=new StringBuffer();
rMessage.append(message).append("<br/>");
rMessage.append("<br/>");
Map<String,Boolean> fieldRightMap=new LinkedHashMap<>();//存放 字段判断正确map
//==========================
checkSemanticFor: for (CheckSemanticModel value : checkSemanticModelMap.values()) {
@ -111,13 +112,24 @@ public class OcrIdentifyServiceImpl extends ServiceImpl<OcrIdentifyMapper, OcrId
String fieldName = value.getFieldName();//校验的字段名称
String ruleInfo = value.getRuleInfo();//是否绝对判断 0-绝对判断1-不绝对判断
String inputText = value.getInputText();//校验文本 ocr识别的文本如果不包含该内容则算作失败.
if (StringUtils.isBlank(inputText)) {
continue;
}
List<String> 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<JSONObject> 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 ServiceImpl<OcrIdentifyMapper, OcrId
}
rMessage.append(field+"_未查到ocr结果,");
ocrResultAdd(ocrResultList,field,inputText,null,0d,imgPath,"未查到ocr结果",false);
ocrResultList.add(copyOcrResult);
fieldRightMap.put(field,false);
}
//其中有一个字段 全部都是 失败,则该图片失败.
if (fieldRightMap!=null&&fieldRightMap.values().size()>0&&!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);
}
//=========================
}

@ -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<OcrRuleCheckMapper, Ocr
@Override
public OcrRuleCheckVo findById(String id) {
OcrRuleCheckVo ocrRuleCheckVo = baseMapper.findById(id);
AssertUtils.notNull(ocrRuleCheckVo,id+"-规则检查配置不存在");
if (StringUtils.isNotBlank(ocrRuleCheckVo.getConfigRule())) {
String configRule = ocrRuleCheckVo.getConfigRule();
String[] split = configRule.split(OcrConstant.ruleCheckSplitChar);

@ -0,0 +1,25 @@
package org.jeecg.modules.ocr.utils;
import lombok.extern.slf4j.Slf4j;
import java.util.List;
/**
* @Description
* @Author ZhouWenTao
* @Date 2023/7/24 14:46
*/
@Slf4j
public class ArrayCUtils {
public static boolean containsStringList(List<String> objects,List<String> objects2){
boolean flag=false;
for (String object : objects) {
o2For:for (String o : objects2) {
if (object.equals(o)) {
return true;
}
}
}
return flag;
}
}
Loading…
Cancel
Save