master
周文涛 2 years ago
parent 0c7ea504ff
commit f5cfb278e2

@ -13,6 +13,10 @@ public class OcrConstant {
*/
public static String ruleCheckSplitChar="&";
public static String ruleCheckOrChar="||";
public static String ruleCheckValueRightChar="=";
/**
* OCR 0 1
*/

@ -1,6 +1,5 @@
package org.jeecg.modules.ocr.controller;
import java.io.File;
import java.util.*;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
@ -107,8 +106,7 @@ public class OcrIdentifyController extends JeecgController<OcrIdentify, IOcrIden
}
QueryWrapper<OcrIdentify> queryWrapper = QueryGenerator.initQueryWrapper(ocrIdentify, req.getParameterMap());
if (startTime!=null&&endTime!=null) {
queryWrapper.between("startTime",startTime,endTime);
queryWrapper.between("endTime",startTime,endTime);
queryWrapper.between("startTime",startTime,endTime).or(q->q.between("endTime",startTime,endTime));
}
Page<OcrIdentify> page = new Page<>(pageNo, pageSize);
Map<String, OcrRuleCheckVo> ocrRuleCheckMap = ocrRuleCheckService.listToMap(null);

@ -42,8 +42,6 @@ public interface IOcrIdentifyService extends IService<OcrIdentify> {
void updateOcrIdentifyStatus(String id,String status);
/**
*
* @param jsonObject

@ -101,7 +101,7 @@ public class OcrIdentifyServiceImpl extends ServiceImpl<OcrIdentifyMapper, OcrId
ocrIdentifyDetail.setImageName(imgName);
ocrIdentifyDetail.setImageUrl(imgPath);
//ocrIdentifyDetail.setStatus(taskStatus);
ocrIdentifyDetail.setStatus("0");
ocrIdentifyDetail.setStatus("");
ocrIdentifyDetail.setMessage(message);
ocrIdentifyDetail.setExecutionTime(executionTime);
ocrIdentifyDetail.setSemanticResult(semanticResult.toJSONString());
@ -111,19 +111,15 @@ public class OcrIdentifyServiceImpl extends ServiceImpl<OcrIdentifyMapper, OcrId
if (semanticResult != null) {
OcrIdentifyVo ocrIdentifyVo = this.findById(identifyId);
OcrRuleCheckVo ocrRuleCheckVo = ocrIdentifyVo.getOcrRuleCheckVo();
Map<String, CheckSemanticModel> checkSemanticModelMap = getCheckSemanticModelMap(ocrRuleCheckVo.getConfigRuleMap(), ocrRuleCheckVo.getFieldMap(), ocrIdentifyVo.getSourceJsonObjects());
////
String text=null;//ocr 识别的文本,
String text=null;//ocr 识别的文本
Double probability = 0d;
//用于数据结构化的对象
List<OcrResult> ocrResultList = new ArrayList<>();
StringBuffer rMessage = new StringBuffer();
Map<String, Boolean> fieldRightMap = new LinkedHashMap<>();//存放 字段判断正确map
//==========================
checkSemanticFor:
for (CheckSemanticModel value : checkSemanticModelMap.values()) {
checkSemanticFor: for (CheckSemanticModel value : checkSemanticModelMap.values()) {
String field = value.getField();
String fieldName = value.getFieldName();//校验的字段名称
String ruleInfo = value.getRuleInfo();//是否绝对判断 0-绝对判断1-不绝对判断
@ -181,10 +177,17 @@ public class OcrIdentifyServiceImpl extends ServiceImpl<OcrIdentifyMapper, OcrId
}
}
//其中有一个字段 全部都是 失败,则该图片失败.
if (fieldRightMap != null && fieldRightMap.values().size() > 0 && !fieldRightMap.containsValue(false)) {
if (fieldRightMap != null && fieldRightMap.values().size()>0) {
if (!fieldRightMap.containsValue(false)) {
ocrIdentifyDetail.setStatus("1");//全部通过
} else {
} else if(fieldRightMap.containsValue(false) && fieldRightMap.containsValue(true)){
ocrIdentifyDetail.setStatus("2");//有成功,有失败(部分)
ocrIdentifyDetail.setMessage(rMessage.toString());
} else{
ocrIdentifyDetail.setStatus("0");//有失败的
ocrIdentifyDetail.setMessage(rMessage.toString());
}
}else{
ocrIdentifyDetail.setStatus("0");//有失败的
ocrIdentifyDetail.setMessage(rMessage.toString());
}
@ -236,15 +239,15 @@ public class OcrIdentifyServiceImpl extends ServiceImpl<OcrIdentifyMapper, OcrId
@Async
public void updateOcrIdentifyStatus(String id, String status) {
//4.更新主任务状态
OcrIdentifyVo ocrIdentifyVo = this.findById(id);
OcrRuleCheckVo ocrRuleCheckVo = ocrIdentifyVo.getOcrRuleCheckVo();
Map<String, Map<String, String>> configRuleTypeMap = ocrRuleCheckVo.getConfigRuleTypeMap();
LambdaUpdateWrapper<OcrIdentify> updateWrapper = new LambdaUpdateWrapper<OcrIdentify>();
updateWrapper.eq(OcrIdentify::getId, id);
updateWrapper.set(OcrIdentify::getEndTime, new Date());
updateWrapper.set(OcrIdentify::getStatus, "1");
/*获取 明细 的成功或失败.*/
List<OcrIdentifyDetail> identifyDetailList = ocrIdentifyDetailService.listByIdentifyId(id);
if(true) {
if (identifyDetailList != null && identifyDetailList.size() > 0) {
Map<String, OcrResult> fieldRightMap = new LinkedHashMap<>();
Map<String, OcrResult2> fieldRightMap = new LinkedHashMap<>();
String tag = null;
boolean ruleValidation = false;
/////明细中,如果有字段成功的,则会覆盖其他明细的匹配失败的情况,如果全都是失败的,会获取第一次失败的.
@ -252,11 +255,11 @@ public class OcrIdentifyServiceImpl extends ServiceImpl<OcrIdentifyMapper, OcrId
String dataStructured = ocrIdentifyDetail.getDataStructured();
if (org.apache.commons.lang.StringUtils.isNotBlank(dataStructured)) {
JSONArray jsonArray = JSONObject.parseArray(dataStructured);
List<OcrResult> resultList = jsonArray.toJavaList(OcrResult.class);
for (OcrResult result : resultList) {
List<OcrResult2> resultList = jsonArray.toJavaList(OcrResult2.class);
for (OcrResult2 result : resultList) {
tag = result.getTag();
ruleValidation = result.getRuleValidation();
OcrResult lastResult = fieldRightMap.get(tag);
OcrResult2 lastResult = fieldRightMap.get(tag);
if (ruleValidation) {
fieldRightMap.put(tag, result);
} else if (lastResult == null || !lastResult.getRuleValidation()) {
@ -266,38 +269,55 @@ public class OcrIdentifyServiceImpl extends ServiceImpl<OcrIdentifyMapper, OcrId
}
}
//如果明细合并一起,都是成功,则主任务 匹配成功, 否则匹配失败
List<OcrResult> errorResults = fieldRightMap.values().stream().filter(f -> !f.getRuleValidation()).collect(Collectors.toList());
List<OcrResult> ocrResults = fieldRightMap.values().stream().collect(Collectors.toList());
String taskResultInfo = JSONArray.toJSONString(ocrResults);
updateWrapper.set(OcrIdentify::getTaskResultInfo,taskResultInfo);
List<OcrResult2> errorResults = fieldRightMap.values().stream().filter(f -> !f.getRuleValidation()).collect(Collectors.toList());
List<OcrResult2> 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) {
//匹配失败.
String errorMsg = errorResults.stream().map(e -> e.getFailureReason()).collect(Collectors.joining(";"));
updateWrapper.set(OcrIdentify::getErrorMsg, errorMsg);
updateWrapper.set(OcrIdentify::getTaskResult, 0);
} else {
//匹配成功.
updateWrapper.set(OcrIdentify::getTaskResult, 1);
}
}else if (configRuleTypeMap.containsKey(OcrConstant.ruleCheckOrChar)){
Map<String, String> fieldMap = configRuleTypeMap.get(OcrConstant.ruleCheckSplitChar);
//获取成功的字段.
List<String> successFields = ocrResults.stream().filter(o -> o.getRuleValidation()).map(c->c.getTag()).collect(Collectors.toList());
updateWrapper.set(OcrIdentify::getErrorMsg, errorMsg);
updateWrapper.set(OcrIdentify::getTaskResult, 0);
fieldMapFor: for (String s : fieldMap.keySet()) {
if (successFields.contains(s)) {
updateWrapper.set(OcrIdentify::getTaskResult, 1);
break fieldMapFor;
}
}
}
String taskResultInfo = JSONArray.toJSONString(ocrResults);
updateWrapper.set(OcrIdentify::getTaskResultInfo,taskResultInfo);
}
}
super.update(updateWrapper);
/*System.out.println("已通过一条!");*/
}
@Override
@Async
public void updateTaskResultInfo(String id){
OcrIdentifyVo ocrIdentifyVo = this.findById(id);
List<OcrIdentifyDetail> identifyDetails = ocrIdentifyDetailService.listByIdentifyId(ocrIdentifyVo.getId());
/*List<OcrIdentifyDetail> identifyDetails = ocrIdentifyDetailService.listByIdentifyId(ocrIdentifyVo.getId());
for (OcrIdentifyDetail identifyDetail : identifyDetails) {
JSONObject semanticResult = JSONObject.parseObject(identifyDetail.getSemanticResult());
String imgPath = identifyDetail.getImageUrl();
// 进行数据化 结构
if (semanticResult != null) {
OcrRuleCheckVo ocrRuleCheckVo = ocrIdentifyVo.getOcrRuleCheckVo();
if (ocrRuleCheckVo.getConfigName().contains("科室")) {
System.out.println("111");
}
Map<String, CheckSemanticModel> checkSemanticModelMap = getCheckSemanticModelMap(ocrRuleCheckVo.getConfigRuleMap(), ocrRuleCheckVo.getFieldMap(), ocrIdentifyVo.getSourceJsonObjects());
////
String text=null;//ocr 识别的文本,
@ -367,17 +387,30 @@ public class OcrIdentifyServiceImpl extends ServiceImpl<OcrIdentifyMapper, OcrId
}
}
//其中有一个字段 全部都是 失败,则该图片失败.
if (fieldRightMap != null && fieldRightMap.values().size() > 0 && !fieldRightMap.containsValue(false)) {
if (fieldRightMap != null && fieldRightMap.values().size()>0) {
if (!fieldRightMap.containsValue(false)) {
identifyDetail.setStatus("1");//全部通过
} else {
} 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);
}
}
}*/
//=======规则检查配置
OcrRuleCheckVo ocrRuleCheckVo = ocrIdentifyVo.getOcrRuleCheckVo();
Map<String, Map<String, String>> configRuleTypeMap = ocrRuleCheckVo.getConfigRuleTypeMap();
//===================
//4.更新主任务状态
LambdaUpdateWrapper<OcrIdentify> updateWrapper = new LambdaUpdateWrapper<OcrIdentify>();
updateWrapper.eq(OcrIdentify::getId, id);
@ -408,17 +441,37 @@ public class OcrIdentifyServiceImpl extends ServiceImpl<OcrIdentifyMapper, OcrId
//如果明细合并一起,都是成功,则主任务 匹配成功, 否则匹配失败
List<OcrResult2> errorResults = fieldRightMap.values().stream().filter(f -> !f.getRuleValidation()).collect(Collectors.toList());
List<OcrResult2> ocrResults = fieldRightMap.values().stream().collect(Collectors.toList());
String taskResultInfo = JSONArray.toJSONString(ocrResults);
updateWrapper.set(OcrIdentify::getTaskResultInfo,taskResultInfo);
//TODO 后续考虑需要修正复杂程度. 支持 & + || 一起
String errorMsg = errorResults.stream().map(e -> e.getFailureReason()).collect(Collectors.joining(";"));
if (configRuleTypeMap.containsKey(OcrConstant.ruleCheckSplitChar)) {
if (errorResults.size() > 0) {
//匹配失败.
String errorMsg = errorResults.stream().map(e -> e.getFailureReason()).collect(Collectors.joining(";"));
updateWrapper.set(OcrIdentify::getErrorMsg, errorMsg);
updateWrapper.set(OcrIdentify::getTaskResult, 0);
} else {
//匹配成功.
updateWrapper.set(OcrIdentify::getTaskResult, 1);
}
}else if (configRuleTypeMap.containsKey(OcrConstant.ruleCheckOrChar)){
Map<String, String> fieldMap = configRuleTypeMap.get(OcrConstant.ruleCheckSplitChar);
//获取成功的字段.
List<String> successFields = ocrResults.stream().filter(o -> o.getRuleValidation()).map(c->c.getTag()).collect(Collectors.toList());
updateWrapper.set(OcrIdentify::getErrorMsg, errorMsg);
updateWrapper.set(OcrIdentify::getTaskResult, 0);
fieldMapFor: for (String s : fieldMap.keySet()) {
if (successFields.contains(s)) {
updateWrapper.set(OcrIdentify::getTaskResult, 1);
break fieldMapFor;
}
}
}
String taskResultInfo = JSONArray.toJSONString(ocrResults);
updateWrapper.set(OcrIdentify::getTaskResultInfo,taskResultInfo);
}
}
super.update(updateWrapper);

@ -129,20 +129,37 @@ public class OcrRuleCheckServiceImpl extends ServiceImpl<OcrRuleCheckMapper, Ocr
AssertUtils.notNull(ocrRuleCheckVo,id+"-规则检查配置不存在");
if (StringUtils.isNotBlank(ocrRuleCheckVo.getConfigRule())) {
String configRule = ocrRuleCheckVo.getConfigRule();
//TODO {key(&,||):{key(字段):value(1/0)}}
Map<String,Map<String,String>> configRuleTypeMap=new LinkedHashMap<>();
if (configRule.contains(OcrConstant.ruleCheckOrChar)) {
// ||
String[] orSplit = configRule.split(OcrConstant.ruleCheckOrChar);
Map<String,String> configRuleMap=new LinkedHashMap<>();
String field=null;String fieldValue=null;
for (String s : orSplit) {
configRuleMap=new LinkedHashMap<>();
String[] split1 = s.split(OcrConstant.ruleCheckValueRightChar);
if (split1.length<=1) {
continue;
}
configRuleMap.put(split1[0],split1[1]);
}
configRuleTypeMap.put(OcrConstant.ruleCheckOrChar,configRuleMap);
}else {
//&
String[] split = configRule.split(OcrConstant.ruleCheckSplitChar);
//将 `配置规则` 转成 map
Map<String,String> configRuleMap=new LinkedHashMap<>();
for (String s : split) {
String[] split1 = s.split("=");
String[] split1 = s.split(OcrConstant.ruleCheckValueRightChar);
if (split1.length<=1) {
continue;
}
String s1 = split1[0];
String s2 = split1[1];
configRuleMap.put(s1,s2);
configRuleMap.put(split1[0],split1[1]);
}
configRuleTypeMap.put(OcrConstant.ruleCheckSplitChar,configRuleMap);
}
ocrRuleCheckVo.setConfigRuleMap(configRuleMap);
ocrRuleCheckVo.setConfigRuleTypeMap(configRuleTypeMap);
}
if (StringUtils.isNotBlank(ocrRuleCheckVo.getMetadataConfigId())) {
String metadataConfigId = ocrRuleCheckVo.getMetadataConfigId();
@ -155,4 +172,15 @@ public class OcrRuleCheckServiceImpl extends ServiceImpl<OcrRuleCheckMapper, Ocr
}
return ocrRuleCheckVo;
}
public static void main(String[] args) {
String text="hospitalName=1||doctorName=0";
String[] split = text.split("||");
for (String s : split) {
s=s.replace("(","");
}
}
}

@ -14,9 +14,7 @@ import org.jeecgframework.poi.excel.annotation.Excel;
import org.springframework.format.annotation.DateTimeFormat;
import java.io.Serializable;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.*;
/**
* @Description:
@ -34,4 +32,6 @@ public class OcrRuleCheckVo extends OcrRuleCheck {
private Map<String,String> configRuleMap=new LinkedHashMap<>();
private Map<String,String> fieldMap=new LinkedHashMap<>();
Map<String,Map<String,String>> configRuleTypeMap=new LinkedHashMap<>();
}

Loading…
Cancel
Save