|
|
|
@ -1,11 +1,16 @@
|
|
|
|
|
package org.jeecg.modules.ocr.service.impl;
|
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
import org.checkerframework.checker.units.qual.C;
|
|
|
|
|
import org.jeecg.modules.ocr.entity.OcrIdentify;
|
|
|
|
|
import org.jeecg.modules.ocr.entity.OcrIdentifyDetail;
|
|
|
|
|
import org.jeecg.modules.ocr.entity.OcrRuleCheck;
|
|
|
|
|
import org.jeecg.modules.ocr.mapper.OcrIdentifyMapper;
|
|
|
|
|
import org.jeecg.modules.ocr.model.CheckSemanticModel;
|
|
|
|
|
import org.jeecg.modules.ocr.model.OcrResult;
|
|
|
|
|
import org.jeecg.modules.ocr.model.SourceImage;
|
|
|
|
|
import org.jeecg.modules.ocr.service.IOcrIdentifyDetailService;
|
|
|
|
|
import org.jeecg.modules.ocr.service.IOcrIdentifyService;
|
|
|
|
|
import org.jeecg.modules.ocr.service.IOcrRuleCheckDetailService;
|
|
|
|
@ -13,10 +18,14 @@ import org.jeecg.modules.ocr.service.IOcrRuleCheckService;
|
|
|
|
|
import org.jeecg.modules.ocr.vo.OcrIdentifyVo;
|
|
|
|
|
import org.jeecg.modules.ocr.vo.OcrRuleCheckVo;
|
|
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
|
|
import org.springframework.scheduling.annotation.Async;
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -38,7 +47,9 @@ public class OcrIdentifyServiceImpl extends ServiceImpl<OcrIdentifyMapper, OcrId
|
|
|
|
|
public OcrIdentifyVo findById(String id){
|
|
|
|
|
OcrIdentifyVo ocrIdentifyVo=baseMapper.findById(id);
|
|
|
|
|
if (StringUtils.isNotBlank(ocrIdentifyVo.getSourceJson())) {
|
|
|
|
|
ocrIdentifyVo.setSourceJsonObject(JSONObject.parseObject(ocrIdentifyVo.getSourceJson()));
|
|
|
|
|
JSONArray jsonArray = JSONObject.parseArray(ocrIdentifyVo.getSourceJson());
|
|
|
|
|
List<JSONObject> jsonObjects = jsonArray.toJavaList(JSONObject.class);
|
|
|
|
|
ocrIdentifyVo.setSourceJsonObjects(jsonObjects);
|
|
|
|
|
}
|
|
|
|
|
//规则检查配置信息
|
|
|
|
|
String ruleCheck = ocrIdentifyVo.getRuleCheck();
|
|
|
|
@ -82,27 +93,181 @@ public class OcrIdentifyServiceImpl extends ServiceImpl<OcrIdentifyMapper, OcrId
|
|
|
|
|
OcrIdentifyVo ocrIdentifyVo = this.findById(identifyId);
|
|
|
|
|
OcrRuleCheckVo ocrRuleCheckVo = ocrIdentifyVo.getOcrRuleCheckVo();
|
|
|
|
|
|
|
|
|
|
// field,1/0
|
|
|
|
|
Map<String, String> configRuleMap = ocrRuleCheckVo.getConfigRuleMap();
|
|
|
|
|
// field,fieldname
|
|
|
|
|
Map<String, String> fieldMap = ocrRuleCheckVo.getFieldMap();
|
|
|
|
|
//对参数做匹配
|
|
|
|
|
Map<String, CheckSemanticModel> checkSemanticModelMap = getCheckSemanticModelMap(ocrRuleCheckVo.getConfigRuleMap(), ocrRuleCheckVo.getFieldMap(),ocrIdentifyVo.getSourceJsonObjects());
|
|
|
|
|
////
|
|
|
|
|
String text;//ocr 识别的文本,
|
|
|
|
|
Double probability=0d;
|
|
|
|
|
OcrResult copyOcrResult=new OcrResult();
|
|
|
|
|
|
|
|
|
|
JSONObject sourceJsonObject = ocrIdentifyVo.getSourceJsonObject();
|
|
|
|
|
//将 语义化结果 返回的 key 转换成 字段
|
|
|
|
|
for (String field : sourceJsonObject.keySet()) {
|
|
|
|
|
String fieldName = sourceJsonObject.getString(field);
|
|
|
|
|
semanticResultFor: for (String s : semanticResult.keySet()) {
|
|
|
|
|
if (fieldName.contains(s)) {
|
|
|
|
|
break semanticResultFor;
|
|
|
|
|
//用于数据结构化的对象
|
|
|
|
|
List<OcrResult> ocrResultList=new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
StringBuffer rMessage=new StringBuffer();
|
|
|
|
|
rMessage.append(message).append("<br/>");
|
|
|
|
|
Map<String,Boolean> 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识别的文本如果不包含该内容,则算作失败.
|
|
|
|
|
//查看ocr识别返回的字段名称中是否有当前这个字段名称
|
|
|
|
|
if (semanticResult.keySet().contains(fieldName)) {
|
|
|
|
|
//TODO 注意,ocr 识别返回的 字段是多个结果(数组),有一个值匹配上即为正确。
|
|
|
|
|
JSONArray ocrArray = semanticResult.getJSONArray(fieldName);
|
|
|
|
|
ocrArrayFor:for (int i = 0; i < ocrArray.size(); i++) {
|
|
|
|
|
copyOcrResult=new OcrResult();
|
|
|
|
|
JSONObject ocrItem = ocrArray.getJSONObject(i);
|
|
|
|
|
text = ocrItem.getString("text");//ocr 识别的文本
|
|
|
|
|
probability = ocrItem.getDouble("probability");//置信度
|
|
|
|
|
|
|
|
|
|
// 1 不需要校验,只需要有返回ocr识别值
|
|
|
|
|
if ("1".equals(ruleInfo) && StringUtils.isNotBlank(text)) {
|
|
|
|
|
ocrResultAdd(ocrResultList,field,inputText,text,probability,imgPath,"",true);
|
|
|
|
|
fieldRightMap.put(field,true);
|
|
|
|
|
}else{
|
|
|
|
|
if (StringUtils.isBlank(text) ||!text.contains(inputText)) {
|
|
|
|
|
//识别结果为空,识别结果 匹配不上 校验文本
|
|
|
|
|
mapPutIfTrue(fieldRightMap,field,false);
|
|
|
|
|
rMessage.append(value.getFieldName()+"不匹配,");
|
|
|
|
|
ocrResultAdd(ocrResultList,field,inputText,text,probability,imgPath,value.getFieldName()+"不匹配",false);
|
|
|
|
|
}else{
|
|
|
|
|
ocrResultAdd(ocrResultList,field,inputText,text,probability,imgPath,"",true);
|
|
|
|
|
fieldRightMap.put(field,true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for (String s : semanticResult.keySet()) {
|
|
|
|
|
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");
|
|
|
|
|
}else{
|
|
|
|
|
ocrIdentifyDetail.setStatus("0");
|
|
|
|
|
ocrIdentifyDetail.setMessage(rMessage.toString());
|
|
|
|
|
}
|
|
|
|
|
ocrIdentifyDetail.setDataStructured(JSONArray.toJSONString(ocrResultList));//数据结构化
|
|
|
|
|
ocrIdentifyDetailService.updateById(ocrIdentifyDetail);
|
|
|
|
|
}
|
|
|
|
|
//=========================
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Async
|
|
|
|
|
@Override
|
|
|
|
|
public void postSemantic(String id, String identifyUrl) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void mapPutIfTrue(Map map,String key,Boolean flag){
|
|
|
|
|
if (map.containsKey(key) && (Boolean) map.get(key).equals(true)) {
|
|
|
|
|
}else{
|
|
|
|
|
map.put(key,flag);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 将数据 疯转成 OcrResult,追加到 ocrResultList
|
|
|
|
|
* @param ocrResultList
|
|
|
|
|
* @param field
|
|
|
|
|
* @param inputText
|
|
|
|
|
* @param ocrText
|
|
|
|
|
* @param ocrPrecisionRate
|
|
|
|
|
* @param imgPath
|
|
|
|
|
* @param failureReason
|
|
|
|
|
*/
|
|
|
|
|
public static void ocrResultAdd(List<OcrResult> ocrResultList,String field,String inputText,String ocrText,Double ocrPrecisionRate,String imgPath,String failureReason,Boolean ruleValidation){
|
|
|
|
|
OcrResult ocrResult = new OcrResult();
|
|
|
|
|
ocrResult.setTag(field);
|
|
|
|
|
ocrResult.setOcrText(ocrText);
|
|
|
|
|
ocrResult.setInputText(inputText);
|
|
|
|
|
ocrResult.setOcrPrecisionRate(ocrPrecisionRate==null?0d:ocrPrecisionRate);
|
|
|
|
|
|
|
|
|
|
if (StringUtils.isNotBlank(imgPath)) {
|
|
|
|
|
SourceImage sourceImage = new SourceImage();
|
|
|
|
|
sourceImage.setPath(imgPath);
|
|
|
|
|
int i = imgPath.lastIndexOf("/");
|
|
|
|
|
sourceImage.setFileName(imgPath.substring(i,imgPath.length()));
|
|
|
|
|
ocrResult.setSourceImage(sourceImage);
|
|
|
|
|
}
|
|
|
|
|
ocrResult.setFailureReason(failureReason);
|
|
|
|
|
if (ocrResultList==null) {
|
|
|
|
|
ocrResultList=new ArrayList<>();
|
|
|
|
|
}
|
|
|
|
|
ocrResult.setRuleValidation(ruleValidation);
|
|
|
|
|
ocrResultList.add(ocrResult);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 组装 checkSemanticModelMap
|
|
|
|
|
* @param configRuleMap 1/0 map
|
|
|
|
|
* @param fieldMap 字段含义 map
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public Map<String,CheckSemanticModel> getCheckSemanticModelMap(Map<String,String> configRuleMap,Map<String,String> fieldMap, List<JSONObject> sourceJsonObjects){
|
|
|
|
|
Map<String,CheckSemanticModel> checkSemanticModelMap=new LinkedHashMap<>();
|
|
|
|
|
|
|
|
|
|
//校验正确的值
|
|
|
|
|
Map<String,String> inputMap=new LinkedHashMap<>();
|
|
|
|
|
if (sourceJsonObjects!=null&&sourceJsonObjects.size()>0) {
|
|
|
|
|
String tag,inputText;
|
|
|
|
|
for (JSONObject sourceJsonObject : sourceJsonObjects) {
|
|
|
|
|
tag = sourceJsonObject.getString("tag");
|
|
|
|
|
if (StringUtils.isBlank(tag)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
inputText = sourceJsonObject.getString("inputText");
|
|
|
|
|
inputMap.put(tag,inputText);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CheckSemanticModel copyEntity=null;
|
|
|
|
|
String configRule=null,fieldName=null,inputText;
|
|
|
|
|
if (fieldMap!=null) {
|
|
|
|
|
for (String field : fieldMap.keySet()) {
|
|
|
|
|
copyEntity=new CheckSemanticModel();
|
|
|
|
|
copyEntity.setField(field);
|
|
|
|
|
|
|
|
|
|
//1/0
|
|
|
|
|
if (configRuleMap!=null) {
|
|
|
|
|
configRule = configRuleMap.get(field);
|
|
|
|
|
copyEntity.setRuleInfo(configRule);
|
|
|
|
|
}
|
|
|
|
|
//端字段含义
|
|
|
|
|
fieldName = fieldMap.get(field);
|
|
|
|
|
//检查数据
|
|
|
|
|
inputText = inputMap.get(field);
|
|
|
|
|
|
|
|
|
|
copyEntity.setFieldName(fieldName);
|
|
|
|
|
|
|
|
|
|
copyEntity.setInputText(inputText);
|
|
|
|
|
|
|
|
|
|
checkSemanticModelMap.put(field,copyEntity);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return checkSemanticModelMap;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
Map<String,Boolean> fieldRightMap = new LinkedHashMap<>();
|
|
|
|
|
fieldRightMap.put("zhangsan",false);
|
|
|
|
|
fieldRightMap.put("zhangsan4",true);
|
|
|
|
|
mapPutIfTrue(fieldRightMap,"zhangsan",true);
|
|
|
|
|
mapPutIfTrue(fieldRightMap,"zhangsan2",false);
|
|
|
|
|
mapPutIfTrue(fieldRightMap,"zhangsan3",true);
|
|
|
|
|
mapPutIfTrue(fieldRightMap,"zhangsan4",false);
|
|
|
|
|
for (String s : fieldRightMap.keySet()) {
|
|
|
|
|
System.out.println(s+":"+fieldRightMap.get(s));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String ss="[{\"tag\":\"hospitalName\",\"inputText\":\"仁和医院\"},{\"tag\":\"departmentName\",\"inputText\":\"耳鼻喉科\"},{\"tag\":\"doctorName\",\"inputText\":\"张三\"}]";
|
|
|
|
|
JSONArray jsonArray = JSONObject.parseArray(ss);
|
|
|
|
|
//System.out.println(jsonArray);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|