|
|
|
@ -7,6 +7,10 @@ import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
|
|
|
import org.apache.commons.lang3.ArrayUtils;
|
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
import org.checkerframework.checker.units.qual.C;
|
|
|
|
|
import org.jeecg.common.api.vo.Result;
|
|
|
|
|
import org.jeecg.common.constant.OcrConstant;
|
|
|
|
|
import org.jeecg.common.util.RedisUtil;
|
|
|
|
|
import org.jeecg.common.util.RestUtil;
|
|
|
|
|
import org.jeecg.modules.ocr.entity.OcrIdentify;
|
|
|
|
|
import org.jeecg.modules.ocr.entity.OcrIdentifyDetail;
|
|
|
|
|
import org.jeecg.modules.ocr.entity.OcrRuleCheck;
|
|
|
|
@ -14,10 +18,8 @@ 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;
|
|
|
|
|
import org.jeecg.modules.ocr.service.IOcrRuleCheckService;
|
|
|
|
|
import org.jeecg.modules.ocr.model.TaskModel;
|
|
|
|
|
import org.jeecg.modules.ocr.service.*;
|
|
|
|
|
import org.jeecg.modules.ocr.utils.ArrayCUtils;
|
|
|
|
|
import org.jeecg.modules.ocr.vo.OcrIdentifyVo;
|
|
|
|
|
import org.jeecg.modules.ocr.vo.OcrRuleCheckVo;
|
|
|
|
@ -26,6 +28,8 @@ import org.springframework.scheduling.annotation.Async;
|
|
|
|
|
import org.springframework.scheduling.annotation.EnableAsync;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
|
|
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
|
import java.util.*;
|
|
|
|
@ -34,7 +38,7 @@ import java.util.stream.Collectors;
|
|
|
|
|
/**
|
|
|
|
|
* @Description: ocr识别
|
|
|
|
|
* @Author: jeecg-boot
|
|
|
|
|
* @Date: 2023-07-14
|
|
|
|
|
* @Date: 2023-07-14
|
|
|
|
|
* @Version: V1.0
|
|
|
|
|
*/
|
|
|
|
|
@Service
|
|
|
|
@ -46,10 +50,14 @@ public class OcrIdentifyServiceImpl extends ServiceImpl<OcrIdentifyMapper, OcrId
|
|
|
|
|
IOcrIdentifyDetailService ocrIdentifyDetailService;
|
|
|
|
|
@Resource
|
|
|
|
|
IOcrRuleCheckDetailService ocrRuleCheckDetailService;
|
|
|
|
|
@Resource
|
|
|
|
|
IOcrApiCallStatisticsService ocrApiCallStatisticsService;
|
|
|
|
|
@Resource
|
|
|
|
|
RedisUtil redisUtil;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public OcrIdentifyVo findById(String id){
|
|
|
|
|
OcrIdentifyVo ocrIdentifyVo=baseMapper.findById(id);
|
|
|
|
|
public OcrIdentifyVo findById(String id) {
|
|
|
|
|
OcrIdentifyVo ocrIdentifyVo = baseMapper.findById(id);
|
|
|
|
|
if (StringUtils.isNotBlank(ocrIdentifyVo.getSourceJson())) {
|
|
|
|
|
JSONArray jsonArray = JSONObject.parseArray(ocrIdentifyVo.getSourceJson());
|
|
|
|
|
List<JSONObject> jsonObjects = jsonArray.toJavaList(JSONObject.class);
|
|
|
|
@ -68,16 +76,17 @@ public class OcrIdentifyServiceImpl extends ServiceImpl<OcrIdentifyMapper, OcrId
|
|
|
|
|
@Override
|
|
|
|
|
public void getSemanticInfo(JSONObject responseBody) {
|
|
|
|
|
try {
|
|
|
|
|
ocrApiCallStatisticsService.saveLogs("通用识别", "/ocr/ocrApi/identify", 1);
|
|
|
|
|
Thread.sleep(10000l);
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
log.debug("打印 ocr 结果:"+responseBody.toString());
|
|
|
|
|
log.debug("打印 ocr 结果:" + responseBody.toString());
|
|
|
|
|
String identifyId = responseBody.getString("task_id");//任务id
|
|
|
|
|
String imgPath = responseBody.getString("img_path");//图片路径
|
|
|
|
|
String imgName = null;//图片名称
|
|
|
|
|
if (StringUtils.isNotBlank(imgPath)) {
|
|
|
|
|
imgName = imgPath.substring(imgPath.lastIndexOf("/")+1,imgPath.length());
|
|
|
|
|
imgName = imgPath.substring(imgPath.lastIndexOf("/") + 1, imgPath.length());
|
|
|
|
|
}
|
|
|
|
|
String message = responseBody.getString("message");//描述
|
|
|
|
|
String taskStatus = responseBody.getString("taskStatus");//任务是否完成
|
|
|
|
@ -85,7 +94,7 @@ public class OcrIdentifyServiceImpl extends ServiceImpl<OcrIdentifyMapper, OcrId
|
|
|
|
|
JSONObject semanticResult = responseBody.getJSONObject("semantic_result");//语义化结果
|
|
|
|
|
// 入库记录
|
|
|
|
|
OcrIdentifyDetail ocrIdentifyDetail = new OcrIdentifyDetail();
|
|
|
|
|
if (semanticResult!=null) {
|
|
|
|
|
if (semanticResult != null) {
|
|
|
|
|
ocrIdentifyDetail.setIdentifyId(identifyId);
|
|
|
|
|
ocrIdentifyDetail.setImageName(imgName);
|
|
|
|
|
ocrIdentifyDetail.setImageUrl(imgPath);
|
|
|
|
@ -98,21 +107,22 @@ public class OcrIdentifyServiceImpl extends ServiceImpl<OcrIdentifyMapper, OcrId
|
|
|
|
|
//===================
|
|
|
|
|
String ocrIdentifyDetailId = ocrIdentifyDetail.getId();//明细id
|
|
|
|
|
// 进行数据化 结构
|
|
|
|
|
if (semanticResult!=null) {
|
|
|
|
|
if (semanticResult != null) {
|
|
|
|
|
OcrIdentifyVo ocrIdentifyVo = this.findById(identifyId);
|
|
|
|
|
OcrRuleCheckVo ocrRuleCheckVo = ocrIdentifyVo.getOcrRuleCheckVo();
|
|
|
|
|
|
|
|
|
|
Map<String, CheckSemanticModel> checkSemanticModelMap = getCheckSemanticModelMap(ocrRuleCheckVo.getConfigRuleMap(), ocrRuleCheckVo.getFieldMap(),ocrIdentifyVo.getSourceJsonObjects());
|
|
|
|
|
Map<String, CheckSemanticModel> checkSemanticModelMap = getCheckSemanticModelMap(ocrRuleCheckVo.getConfigRuleMap(), ocrRuleCheckVo.getFieldMap(), ocrIdentifyVo.getSourceJsonObjects());
|
|
|
|
|
////
|
|
|
|
|
String text;//ocr 识别的文本,
|
|
|
|
|
Double probability=0d;
|
|
|
|
|
Double probability = 0d;
|
|
|
|
|
//用于数据结构化的对象
|
|
|
|
|
List<OcrResult> ocrResultList=new ArrayList<>();
|
|
|
|
|
List<OcrResult> ocrResultList = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
StringBuffer rMessage=new StringBuffer();
|
|
|
|
|
Map<String,Boolean> fieldRightMap=new LinkedHashMap<>();//存放 字段判断正确map
|
|
|
|
|
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-不绝对判断
|
|
|
|
@ -125,45 +135,46 @@ public class OcrIdentifyServiceImpl extends ServiceImpl<OcrIdentifyMapper, OcrId
|
|
|
|
|
//查看ocr识别返回的字段名称中是否有当前这个字段名称
|
|
|
|
|
if (b) {
|
|
|
|
|
//TODO 注意,ocr 识别返回的 字段是多个结果(数组),有一个值匹配上即为正确
|
|
|
|
|
List<JSONObject> ocrArray=new ArrayList<>();
|
|
|
|
|
List<JSONObject> ocrArray = new ArrayList<>();
|
|
|
|
|
for (String s : fieldNameList) {
|
|
|
|
|
JSONArray jsonArray = semanticResult.getJSONArray(s);
|
|
|
|
|
if (jsonArray!=null&& jsonArray.size()>0) {
|
|
|
|
|
if (jsonArray != null && jsonArray.size() > 0) {
|
|
|
|
|
ocrArray.addAll(jsonArray.toJavaList(JSONObject.class));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ocrArrayFor:for (int i = 0; i < ocrArray.size(); i++) {
|
|
|
|
|
ocrArrayFor:
|
|
|
|
|
for (int i = 0; i < ocrArray.size(); i++) {
|
|
|
|
|
JSONObject ocrItem = ocrArray.get(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)) {
|
|
|
|
|
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()+"不匹配<br>");
|
|
|
|
|
ocrResultAdd(ocrResultList,field,inputText,text,probability,imgPath,value.getFieldName()+"不匹配",false);
|
|
|
|
|
}else{
|
|
|
|
|
ocrResultAdd(ocrResultList,field,inputText,text,probability,imgPath,"",true);
|
|
|
|
|
fieldRightMap.put(field,true);
|
|
|
|
|
mapPutIfTrue(fieldRightMap, field, false);
|
|
|
|
|
rMessage.append(value.getFieldName() + "不匹配<br>");
|
|
|
|
|
ocrResultAdd(ocrResultList, field, inputText, text, probability, imgPath, value.getFieldName() + "不匹配", false);
|
|
|
|
|
} else {
|
|
|
|
|
ocrResultAdd(ocrResultList, field, inputText, text, probability, imgPath, "", true);
|
|
|
|
|
fieldRightMap.put(field, true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}else{
|
|
|
|
|
rMessage.append(field+"_未查到ocr结果<br>");
|
|
|
|
|
ocrResultAdd(ocrResultList,field,inputText,null,0d,imgPath,"ocr结果未获取",false);
|
|
|
|
|
fieldRightMap.put(field,false);
|
|
|
|
|
} else {
|
|
|
|
|
rMessage.append(field + "_未查到ocr结果<br>");
|
|
|
|
|
ocrResultAdd(ocrResultList, field, inputText, null, 0d, imgPath, "ocr结果未获取", false);
|
|
|
|
|
fieldRightMap.put(field, false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//其中有一个字段 全部都是 失败,则该图片失败.
|
|
|
|
|
if (fieldRightMap!=null&&fieldRightMap.values().size()>0&&!fieldRightMap.containsValue(false)) {
|
|
|
|
|
if (fieldRightMap != null && fieldRightMap.values().size() > 0 && !fieldRightMap.containsValue(false)) {
|
|
|
|
|
ocrIdentifyDetail.setStatus("0");//全部通过
|
|
|
|
|
}else{
|
|
|
|
|
} else {
|
|
|
|
|
ocrIdentifyDetail.setStatus("1");//有失败的
|
|
|
|
|
ocrIdentifyDetail.setMessage(rMessage.toString());
|
|
|
|
|
}
|
|
|
|
@ -174,35 +185,62 @@ public class OcrIdentifyServiceImpl extends ServiceImpl<OcrIdentifyMapper, OcrId
|
|
|
|
|
//=========================
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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}";
|
|
|
|
|
|
|
|
|
|
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}";
|
|
|
|
|
public static String semanticUrl = "http://111.202.228.113:7003/semantic";
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
@Async
|
|
|
|
|
public void postSemantic(String id, String identifyUrl) {
|
|
|
|
|
public void postSemantic(OcrIdentify ocrIdentify, List<String> identifyUrlList) {
|
|
|
|
|
//3.请求python ocr识别
|
|
|
|
|
JSONObject semanticResponseJson = JSONObject.parseObject(semanticResponse);
|
|
|
|
|
semanticResponseJson.put("task_id",id);
|
|
|
|
|
this.getSemanticInfo(semanticResponseJson);
|
|
|
|
|
JSONObject requestBody = new JSONObject();
|
|
|
|
|
String id = ocrIdentify.getId();
|
|
|
|
|
String identifyUrl;//识别图片
|
|
|
|
|
String priority = ocrIdentify.getPriority();//加急状态;//任务优先级
|
|
|
|
|
String taskId;
|
|
|
|
|
JSONObject semanticResponseJson = new JSONObject();
|
|
|
|
|
|
|
|
|
|
//将主任务存到redis.
|
|
|
|
|
StringBuffer task_master = new StringBuffer();
|
|
|
|
|
for (int i = 0; i < identifyUrlList.size(); i++) {
|
|
|
|
|
taskId = String.format(id + "_" + (i + 1));
|
|
|
|
|
task_master.append(taskId).append(",");
|
|
|
|
|
}
|
|
|
|
|
redisUtil.set("identify_" + id, task_master.toString());
|
|
|
|
|
//执行子任务.
|
|
|
|
|
for (int i = 0; i < identifyUrlList.size(); i++) {
|
|
|
|
|
identifyUrl = identifyUrlList.get(i);
|
|
|
|
|
taskId = String.format(id + "_" + (i + 1));
|
|
|
|
|
requestBody.put("taskId", taskId);
|
|
|
|
|
requestBody.put("taskLevel", Integer.valueOf(priority));
|
|
|
|
|
requestBody.put("taskType", "identify");
|
|
|
|
|
requestBody.put("parameter",id+","+taskId+","+identifyUrl);
|
|
|
|
|
//发送任务请求 到 redis
|
|
|
|
|
pushTask(requestBody);
|
|
|
|
|
/*semanticResponseJson = RestUtil.post(OcrConstant.api_test_identify_url, requestBody);
|
|
|
|
|
semanticResponseJson.put("task_id", id);
|
|
|
|
|
this.getSemanticInfo(semanticResponseJson);*/
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void updateOcrIdentifyStatus(String id,String status) {
|
|
|
|
|
//4.更新主任务状态
|
|
|
|
|
OcrIdentify ocrIdentify=new OcrIdentify();
|
|
|
|
|
LambdaUpdateWrapper<OcrIdentify> updateWrapper = new LambdaUpdateWrapper<OcrIdentify>();
|
|
|
|
|
updateWrapper.eq(OcrIdentify::getId, id);
|
|
|
|
|
updateWrapper.set(OcrIdentify::getEndTime, new Date());
|
|
|
|
|
updateWrapper.set(OcrIdentify::getStatus,"1");
|
|
|
|
|
this.update(updateWrapper);
|
|
|
|
|
updateWrapper.set(OcrIdentify::getStatus, "1");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void mapPutIfTrue(Map map,String key,Boolean flag){
|
|
|
|
|
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);
|
|
|
|
|
} else {
|
|
|
|
|
map.put(key, flag);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 将数据 疯转成 OcrResult,追加到 ocrResultList
|
|
|
|
|
*
|
|
|
|
|
* @param ocrResultList
|
|
|
|
|
* @param field
|
|
|
|
|
* @param inputText
|
|
|
|
@ -211,23 +249,23 @@ public class OcrIdentifyServiceImpl extends ServiceImpl<OcrIdentifyMapper, OcrId
|
|
|
|
|
* @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){
|
|
|
|
|
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);
|
|
|
|
|
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+1,imgPath.length()));
|
|
|
|
|
sourceImage.setFileName(imgPath.substring(i + 1, imgPath.length()));
|
|
|
|
|
ocrResult.setSourceImage(sourceImage);
|
|
|
|
|
}
|
|
|
|
|
ocrResult.setFailureReason(failureReason);
|
|
|
|
|
if (ocrResultList==null) {
|
|
|
|
|
ocrResultList=new ArrayList<>();
|
|
|
|
|
if (ocrResultList == null) {
|
|
|
|
|
ocrResultList = new ArrayList<>();
|
|
|
|
|
}
|
|
|
|
|
ocrResult.setRuleValidation(ruleValidation);
|
|
|
|
|
ocrResultList.add(ocrResult);
|
|
|
|
@ -235,37 +273,38 @@ public class OcrIdentifyServiceImpl extends ServiceImpl<OcrIdentifyMapper, OcrId
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 组装 checkSemanticModelMap
|
|
|
|
|
*
|
|
|
|
|
* @param configRuleMap 1/0 map
|
|
|
|
|
* @param fieldMap 字段含义 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<>();
|
|
|
|
|
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;
|
|
|
|
|
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);
|
|
|
|
|
inputMap.put(tag, inputText);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CheckSemanticModel copyEntity=null;
|
|
|
|
|
String configRule=null,fieldName=null,inputText;
|
|
|
|
|
if (fieldMap!=null) {
|
|
|
|
|
CheckSemanticModel copyEntity = null;
|
|
|
|
|
String configRule = null, fieldName = null, inputText;
|
|
|
|
|
if (fieldMap != null) {
|
|
|
|
|
for (String field : fieldMap.keySet()) {
|
|
|
|
|
copyEntity=new CheckSemanticModel();
|
|
|
|
|
copyEntity = new CheckSemanticModel();
|
|
|
|
|
copyEntity.setField(field);
|
|
|
|
|
|
|
|
|
|
//1/0
|
|
|
|
|
if (configRuleMap!=null) {
|
|
|
|
|
if (configRuleMap != null) {
|
|
|
|
|
configRule = configRuleMap.get(field);
|
|
|
|
|
copyEntity.setRuleInfo(configRule);
|
|
|
|
|
}
|
|
|
|
@ -278,27 +317,212 @@ public class OcrIdentifyServiceImpl extends ServiceImpl<OcrIdentifyMapper, OcrId
|
|
|
|
|
|
|
|
|
|
copyEntity.setInputText(inputText);
|
|
|
|
|
|
|
|
|
|
checkSemanticModelMap.put(field,copyEntity);
|
|
|
|
|
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));
|
|
|
|
|
/**
|
|
|
|
|
* 发送任务
|
|
|
|
|
* @param jsonObject
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public Result pushTask(JSONObject jsonObject) {
|
|
|
|
|
//获取优先级1的任务
|
|
|
|
|
String taskId = jsonObject.getString("taskId");
|
|
|
|
|
String taskType = jsonObject.getString("taskType");
|
|
|
|
|
int taskLevel= jsonObject.getInteger("taskLevel");
|
|
|
|
|
String parameter = jsonObject.getString("parameter");
|
|
|
|
|
String task_ = (String) redisUtil.get("task_"+taskLevel);
|
|
|
|
|
if (task_ == null || task_.equals("[]")) {
|
|
|
|
|
//无历史任务
|
|
|
|
|
TaskModel task = new TaskModel(taskId,taskLevel,taskType,"0",parameter);
|
|
|
|
|
List<TaskModel> jsonObjects = Arrays.asList(task);
|
|
|
|
|
task_ = JSONObject.toJSONString(jsonObjects);
|
|
|
|
|
} else {
|
|
|
|
|
//有历史任务
|
|
|
|
|
JSONArray jsonArray = JSONObject.parseArray(task_);
|
|
|
|
|
if (jsonArray != null) {
|
|
|
|
|
List<TaskModel> taskList = jsonArray.toJavaList(TaskModel.class);
|
|
|
|
|
List<TaskModel> staskList = taskList.stream().filter(t -> t.getTaskId().equals(taskId)).collect(Collectors.toList());
|
|
|
|
|
TaskModel task = null;
|
|
|
|
|
if (staskList==null || staskList.size()==0) {
|
|
|
|
|
//该任务不存在, 看看 库表里 该任务是否已执行成功了
|
|
|
|
|
String overTask = (String) redisUtil.get("over_task");
|
|
|
|
|
if (org.apache.commons.lang.StringUtils.isNotBlank(overTask)&&Arrays.asList(overTask.split(",")).contains(taskId)) {
|
|
|
|
|
//库表里已执行过
|
|
|
|
|
return Result.OK("该任务已执行结束");
|
|
|
|
|
} else {
|
|
|
|
|
//库表中未执行过,追加任务
|
|
|
|
|
taskList.add(new TaskModel(taskId, taskLevel, taskType, "0", parameter));
|
|
|
|
|
task_ = JSONObject.toJSONString(taskList);
|
|
|
|
|
}
|
|
|
|
|
} else if ("0".equals(staskList.get(0).getTaskStatus())) {
|
|
|
|
|
//该任务 待运行
|
|
|
|
|
return Result.OK("该任务还处于排队中");
|
|
|
|
|
} else if ("1".equals(staskList.get(0).getTaskStatus())) {
|
|
|
|
|
//该任务 运行中
|
|
|
|
|
return Result.OK("该任务还处于运行中");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//存入redis
|
|
|
|
|
redisUtil.set("task_"+taskLevel, task_);
|
|
|
|
|
//执行任务
|
|
|
|
|
executeTask();
|
|
|
|
|
return Result.OK("已追加到任务");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
|
|
@Async
|
|
|
|
|
public void executeTask() {
|
|
|
|
|
//获取任务
|
|
|
|
|
List<TaskModel> taskList = getTaskList();
|
|
|
|
|
//查看是否有执行中的任务
|
|
|
|
|
long executingCount = taskList.stream().filter(t -> t.getTaskStatus().equals("1")).count();
|
|
|
|
|
if (executingCount > 0) {
|
|
|
|
|
//该方法正在执行中
|
|
|
|
|
log.error("该方法正在执行中");
|
|
|
|
|
}
|
|
|
|
|
List<TaskModel> waitingTaskList = taskList.stream().filter(t -> t.getTaskStatus().equals("0")).collect(Collectors.toList());
|
|
|
|
|
if (waitingTaskList != null) {
|
|
|
|
|
while (waitingTaskList.size() > 0) {
|
|
|
|
|
//取第一个任务
|
|
|
|
|
TaskModel taskModel = waitingTaskList.get(0);
|
|
|
|
|
waitingTaskList.get(0).setTaskStatus("1");
|
|
|
|
|
//刷新redis,执行中
|
|
|
|
|
flushTask(taskModel.getTaskId(), 1);
|
|
|
|
|
//执行
|
|
|
|
|
executeTaskp(taskModel);
|
|
|
|
|
//刷新已知任务
|
|
|
|
|
taskList = getTaskList();
|
|
|
|
|
//刷新待运行任务
|
|
|
|
|
waitingTaskList = taskList.stream().filter(t -> t.getTaskStatus().equals("0")).collect(Collectors.toList());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
log.error("任务已全部执行");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 刷新任务状态
|
|
|
|
|
*
|
|
|
|
|
* @param taskId
|
|
|
|
|
* @param status 状态 1 运行中,2 已结束
|
|
|
|
|
*/
|
|
|
|
|
public void flushTask(String taskId, int status) {
|
|
|
|
|
List<TaskModel> taskList = 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_" + 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_" + 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);
|
|
|
|
|
JSONObject semanticResponseJson = RestUtil.post(OcrConstant.api_test_identify_url, requestBody);
|
|
|
|
|
this.getSemanticInfo(semanticResponseJson);
|
|
|
|
|
//该子任务已执行,判断主任务是否残留
|
|
|
|
|
String masterTask = (String) redisUtil.get(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.removeAll(masterTaskId);
|
|
|
|
|
//刷新Ocr识别任务状态
|
|
|
|
|
updateOcrIdentifyStatus(masterTaskId,"1");
|
|
|
|
|
}else{
|
|
|
|
|
//主任务还存在,刷新主任务明细
|
|
|
|
|
redisUtil.set(masterTaskId,collect);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
Thread.sleep(20000l);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String ss="[{\"tag\":\"hospitalName\",\"inputText\":\"仁和医院\"},{\"tag\":\"departmentName\",\"inputText\":\"耳鼻喉科\"},{\"tag\":\"doctorName\",\"inputText\":\"张三\"}]";
|
|
|
|
|
JSONArray jsonArray = JSONObject.parseArray(ss);
|
|
|
|
|
//System.out.println(jsonArray);
|
|
|
|
|
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
//模拟执行成功
|
|
|
|
|
String overTaskIds = (String) redisUtil.get("over_task");
|
|
|
|
|
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", overTaskIds);
|
|
|
|
|
log.error(taskModel.getTaskLevel() + "-级别," + taskModel.getTaskId() + "-已执行");
|
|
|
|
|
//从3中任务集中,删除该任务
|
|
|
|
|
flushTask(taskModel.getTaskId(), 2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//获取全部任务
|
|
|
|
|
public List<TaskModel> getTaskList() {
|
|
|
|
|
String task_0 = (String) redisUtil.get("task_0");
|
|
|
|
|
String task_1 = (String) redisUtil.get("task_1");
|
|
|
|
|
/*String task_2 = (String) redisUtil.get("task_2");
|
|
|
|
|
String task_3 = (String) redisUtil.get("task_3");*/
|
|
|
|
|
JSONArray task0JsonArray = JSONObject.parseArray(task_0);
|
|
|
|
|
JSONArray task1JsonArray = JSONObject.parseArray(task_1);
|
|
|
|
|
/*JSONArray task2JsonArray = JSONObject.parseArray(task_2);
|
|
|
|
|
JSONArray task3JsonArray = JSONObject.parseArray(task_3);*/
|
|
|
|
|
List<TaskModel> taskList = new ArrayList<>();
|
|
|
|
|
if (task0JsonArray != null) {
|
|
|
|
|
taskList.addAll(task0JsonArray.toJavaList(TaskModel.class));
|
|
|
|
|
}
|
|
|
|
|
if (task1JsonArray != null) {
|
|
|
|
|
taskList.addAll(task1JsonArray.toJavaList(TaskModel.class));
|
|
|
|
|
}
|
|
|
|
|
/*if (task2JsonArray != null) {
|
|
|
|
|
taskList.addAll(task2JsonArray.toJavaList(TaskModel.class));
|
|
|
|
|
}
|
|
|
|
|
if (task3JsonArray != null) {
|
|
|
|
|
taskList.addAll(task3JsonArray.toJavaList(TaskModel.class));
|
|
|
|
|
}*/
|
|
|
|
|
return taskList;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|