master
周文涛 2 years ago
parent 2c78c3e01f
commit 25236bb71a

@ -6,6 +6,7 @@ import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result; import org.jeecg.common.api.vo.Result;
@ -23,6 +24,7 @@ import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.ocr.entity.OcrIdentify; import org.jeecg.modules.ocr.entity.OcrIdentify;
import org.jeecg.modules.ocr.entity.OcrIdentifyDetail; import org.jeecg.modules.ocr.entity.OcrIdentifyDetail;
import org.jeecg.modules.ocr.entity.OcrMetadataConfig; import org.jeecg.modules.ocr.entity.OcrMetadataConfig;
import org.jeecg.modules.ocr.model.OcrResult2;
import org.jeecg.modules.ocr.service.*; import org.jeecg.modules.ocr.service.*;
import org.jeecg.common.system.base.controller.JeecgController; import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.ocr.service.impl.TaskService; import org.jeecg.modules.ocr.service.impl.TaskService;
@ -115,6 +117,15 @@ public class OcrIdentifyController extends JeecgController<OcrIdentify, IOcrIden
IPage<OcrIdentify> pageList = ocrIdentifyService.page(page, queryWrapper); IPage<OcrIdentify> pageList = ocrIdentifyService.page(page, queryWrapper);
if (pageList != null && pageList.getRecords() != null) { if (pageList != null && pageList.getRecords() != null) {
for (OcrIdentify record : pageList.getRecords()) { for (OcrIdentify record : pageList.getRecords()) {
if (StringUtils.isNotBlank(record.getTaskResultInfo())) {
List<OcrResult2> ocrResult2s = JSONObject.parseArray(record.getTaskResultInfo()).toJavaList(OcrResult2.class);
int tagNum = ocrResult2s.size();
int successNum = (int) ocrResult2s.stream().filter(o -> o.getRuleValidation()).count();
record.setTagNum(tagNum);
record.setTagSuccessNum(successNum);
record.setSuccessRate(successNum!=0?successNum+"/"+successNum:"0");
record.setTaskResultInfoList(ocrResult2s);
}
OcrRuleCheckVo ocrRuleCheckVo = ocrRuleCheckMap.get(record.getRuleCheck()); OcrRuleCheckVo ocrRuleCheckVo = ocrRuleCheckMap.get(record.getRuleCheck());
if (ocrRuleCheckVo!=null && StringUtils.isNotBlank(ocrRuleCheckVo.getMetadataConfigId())) { if (ocrRuleCheckVo!=null && StringUtils.isNotBlank(ocrRuleCheckVo.getMetadataConfigId())) {
record.setMetadataConfigId(ocrRuleCheckVo.getMetadataConfigId()); record.setMetadataConfigId(ocrRuleCheckVo.getMetadataConfigId());

@ -1,7 +1,9 @@
package org.jeecg.modules.ocr.entity; package org.jeecg.modules.ocr.entity;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List;
import com.alibaba.fastjson.annotation.JSONField; import com.alibaba.fastjson.annotation.JSONField;
import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.IdType;
@ -10,6 +12,7 @@ import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data; import lombok.Data;
import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonFormat;
import org.jeecg.modules.ocr.model.OcrResult2;
import org.springframework.format.annotation.DateTimeFormat; import org.springframework.format.annotation.DateTimeFormat;
import org.jeecgframework.poi.excel.annotation.Excel; import org.jeecgframework.poi.excel.annotation.Excel;
import org.jeecg.common.aspect.annotation.Dict; import org.jeecg.common.aspect.annotation.Dict;
@ -119,6 +122,15 @@ public class OcrIdentify implements Serializable {
@ApiModelProperty(value = "通知状态 0待通知,1已通知") @ApiModelProperty(value = "通知状态 0待通知,1已通知")
public String noticeStatus; public String noticeStatus;
@ApiModelProperty(value = "任务失败描述信息")
private String errorMsg;
/**任务匹配结果*/
@ApiModelProperty(value = "任务匹配结果 0匹配失败,1匹配成功 -1匹配中")
private String taskResult;
/*匹配结果信息*/
private String taskResultInfo;
//======================非数据库映射 //======================非数据库映射
/**任务类型_字段中文*/ /**任务类型_字段中文*/
@TableField(exist = false) @TableField(exist = false)
@ -131,11 +143,13 @@ public class OcrIdentify implements Serializable {
@TableField(exist = false) @TableField(exist = false)
private String taskType_dictText; private String taskType_dictText;
@ApiModelProperty(value = "任务失败描述信息") @TableField(exist = false)
private String errorMsg; private List<OcrResult2> taskResultInfoList=new ArrayList<>();
/**任务匹配结果*/ @TableField(exist = false)
@ApiModelProperty(value = "任务匹配结果 0匹配失败,1匹配成功 -1匹配中") private Integer tagNum=0;
private String taskResult; @TableField(exist = false)
/*匹配结果信息*/ private Integer tagSuccessNum=0;
private String taskResultInfo; @TableField(exist = false)
private String successRate="0";
//================================================================
} }

@ -0,0 +1,194 @@
package org.jeecg.modules.ocr.init;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.constant.OcrConstant;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.common.util.RestUtil;
import org.jeecg.modules.ocr.model.TaskModel;
import org.jeecg.modules.ocr.service.IOcrIdentifyService;
import org.jeecg.modules.ocr.service.impl.TaskService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Async;
import javax.annotation.Resource;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
/**
* @Description ocr .
* @Author ZhouWenTao
* @Date 2023/8/4 11:39
*/
@Configuration
@Slf4j
public class HandleCallbacklnit implements ApplicationRunner {
@Resource
RedisUtil redisUtil;
@Resource
TaskService taskService;
@Resource
IOcrIdentifyService ocrIdentifyService;
@Value("${system.project.enableHandleTask}")
private boolean enableHandleTask;
public static void main(String[] args) {
}
/**
*
* @param args
*/
@Override
@Async
public void run(ApplicationArguments args) throws UnknownHostException {
/*if (enableHandleTask) {
try {
Thread.sleep(5000l);
} catch (InterruptedException e) {
e.printStackTrace();
}
//循环获取 有没有需要 回调 的任务.
while (true) {
try {
Thread.sleep(10000l);
} catch (InterruptedException e) {
e.printStackTrace();
}
List<TaskModel> taskList = taskService.getTaskList();
if (taskList.size()==0) {
continue;
}
for (TaskModel taskModel : taskList) {
//刷新redis执行中
flushTask(taskModel.getTaskId(), 1);
//执行
executeTaskp(taskModel);
//刷新待运行任务
log.error("任务已执行:"+taskModel.getTaskId());
}
}
}*/
}
/**
*
* @param taskId
* @param status 1 2
*/
public void flushTask(String taskId, int status) {
List<TaskModel> taskList = taskService.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_identify_" + 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_identify_" + 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);
//nohup python app.py & tail -f nohup.out &
//更新主任务,正在识别中. conda activate ai
ocrIdentifyService.updateMasterTaskStartTime(task_id.split("_")[0]);
boolean flag=false;
JSONObject semanticResponseJson =null;
while (!flag){
try {
semanticResponseJson = RestUtil.post(OcrConstant.api_test2_identify_url, requestBody);
if (semanticResponseJson!=null) {
flag=true;
}
}catch (org.springframework.web.client.ResourceAccessException e){
log.error(e.getMessage());
log.error("正在重试...");
}
}
semanticResponseJson.put("identifyId", masterTaskId);
log.info("ocr识别返回数据:");
log.info(semanticResponseJson.toJSONString());
ocrIdentifyService.getSemanticInfo(semanticResponseJson);
//该子任务已执行,判断主任务是否残留
String masterTask = (String) redisUtil.get("identify_" + 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.del("identify_" + masterTaskId);
//刷新Ocr识别任务状态
ocrIdentifyService.updateOcrIdentifyStatus(masterTaskId, "1");
log.error("更新任务状态id:" + masterTaskId);
} else {
//主任务还存在,刷新主任务明细
redisUtil.set("identify_" + masterTaskId, collect);
}
}
} else {
Thread.sleep(2000l);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
//模拟执行成功
String overTaskIds = (String) redisUtil.get("over_task_"+taskModel.getTaskType());
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_"+taskModel.getTaskType(), overTaskIds);
log.error(taskModel.getTaskLevel() + "-级别," + taskModel.getTaskId() + "-已执行");
//从3中任务集中删除该任务
flushTask(taskModel.getTaskId(), 2);
}
}

@ -4,6 +4,8 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.jeecg.modules.ocr.entity.OcrIdentify; import org.jeecg.modules.ocr.entity.OcrIdentify;
import org.jeecg.modules.ocr.vo.OcrIdentifyVo; import org.jeecg.modules.ocr.vo.OcrIdentifyVo;
import java.util.List;
/** /**
* @Description: ocr * @Description: ocr
* @Author: jeecg-boot * @Author: jeecg-boot
@ -13,4 +15,10 @@ import org.jeecg.modules.ocr.vo.OcrIdentifyVo;
public interface OcrIdentifyMapper extends BaseMapper<OcrIdentify> { public interface OcrIdentifyMapper extends BaseMapper<OcrIdentify> {
OcrIdentifyVo findById(String id); OcrIdentifyVo findById(String id);
/**
* .
* @return
*/
List<OcrIdentifyVo> findListOfWlyCallBack();
} }

@ -5,4 +5,8 @@
<select id="findById" resultType="org.jeecg.modules.ocr.vo.OcrIdentifyVo"> <select id="findById" resultType="org.jeecg.modules.ocr.vo.OcrIdentifyVo">
SELECT * FROM ocr_identify WHERE id = #{id} SELECT * FROM ocr_identify WHERE id = #{id}
</select> </select>
<select id="findListOfWlyCallBack" resultType="org.jeecg.modules.ocr.vo.OcrIdentifyVo">
SELECT identify.* FROM ocr_identify identify
</select>
</mapper> </mapper>
Loading…
Cancel
Save