master
周文涛 2 years ago
parent bfd96e88e5
commit 2b7c4a0f04

@ -10,4 +10,11 @@ public class OcrConstant {
*
*/
public static String ruleCheckSplitChar="&";
/**
* OCR 0 1
*/
public static String task_Executing_STATUS="0";
public static String task_OVER_STATUS="1";
}

@ -81,7 +81,7 @@ public class OcrApiController {
* message: "成功"
*/
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
@ -99,37 +99,41 @@ public class OcrApiController {
}*/
//1.获取请求参数
String requestId = requestBody.getString("requestId");//请求唯一标识
AssertUtils.notNull(requestId,"[requestId]-不可为空");
String scenes = requestBody.getString("scenes");//场景类型door=门头照片cases=病例bill=票据
AssertUtils.notTrue(!"door".equals(scenes),String.format("暂不支持该场景类型[%s]",scenes));
String ruleId = requestBody.getString("ruleId");//规则标识
AssertUtils.notNull(ruleId,"[ruleId]-不可为空");
OcrRuleCheckVo ruleCheck = ruleCheckService.findById(ruleId);
AssertUtils.notNull(ruleCheck,"[ruleId]-不存在");
Integer priority = requestBody.getInteger("priority");//任务优先级1=加急0=不加急
JSONArray sourceJson = requestBody.getJSONArray("sourceJson");
AssertUtils.notNull(ruleCheck,"[ruleId]-不存在");
AssertUtils.notNull(priority,"[priority]-不可为空");
AssertUtils.notNull(sourceJson,"[sourceJson]-不可为空");
List<JSONObject> sourceJsonList = sourceJson.toJavaList(JSONObject.class);//校验数据源
String sourceImages = requestBody.getString("sourceImages");//ocr图片集
OcrIdentify ocrIdentify=new OcrIdentify();
//2.创建识别任务
if(true){
ocrIdentify.setTaskName(requestId);
ocrIdentify.setStatus("0");
ocrIdentify.setRuleCheck(ruleId);
ocrIdentify.setStartTime(new Date());
ocrIdentify.setTaskType("1681508355389231105");
ocrIdentify.setIdentifyUrl("https://h5.mcnetmart.com/tmp/images/dc_demo1.png");
ocrIdentify.setCreateBy("公共接口");
ocrIdentify.setSourceJson(requestBody.getJSONArray("sourceJson").toJSONString());
ocrIdentify.setStatus("0");//任务进行中
ocrIdentify.setRuleCheck(ruleId);//配置规则检查id
ocrIdentify.setStartTime(new Date());//开始时间
ocrIdentify.setTaskType("1681508355389231105");//任务类型
ocrIdentify.setMetadataConfigId(ruleCheck.getMetadataConfigId());//规则配置的元数据id
ocrIdentify.setTaskSource("API");//任务来源
ocrIdentify.setIdentifyUrl("https://h5.mcnetmart.com/tmp/images/dc_demo1.png");//识别图片地址
ocrIdentify.setCreateBy("API请求");//创建人
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);
//3.请求python ocr识别异步执行
ocrIdentifyService.postSemantic(ocrIdentify.getId(),"https://h5.mcnetmart.com/tmp/images/dc_demo1.png");
return Result.OK("请求成功");
}

@ -272,13 +272,16 @@ public class OcrMetadataConfigController extends JeecgController<OcrMetadataConf
Map<String, Long> countMap = list.stream().map(o->o.getConfigName()).collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
AssertUtils.notTrue(countMap.values().stream().filter(f->f>1).count()>0,"不可有重复的配置名称!");
List<OcrMetadataConfigSaveModel> saveModelList=new ArrayList<>();
OcrMetadataConfigSaveModel ocrMetadataConfigSaveModel=new OcrMetadataConfigSaveModel();
List<OcrMetadataConfigDetail> ocrMetadataConfigDetailList=new ArrayList();
OcrMetadataConfigDetail ocrMetadataConfigDetail=new OcrMetadataConfigDetail();
OcrMetadataConfigSaveModel ocrMetadataConfigSaveModel=null;
List<OcrMetadataConfigDetail> ocrMetadataConfigDetailList=null;
OcrMetadataConfigDetail ocrMetadataConfigDetail=null;
for (OcrMetadataConfigVo configVo : list) {
ocrMetadataConfigSaveModel=new OcrMetadataConfigSaveModel();
ocrMetadataConfigSaveModel.setConfigName(configVo.getConfigName());
String resultName = configVo.getResultName();
String getField = configVo.getGetField();
ocrMetadataConfigSaveModel.setTaskTypeName(configVo.getTaskType());
List<String> resultList=new ArrayList<>();
List<String> getFieldList=new ArrayList<>();
if (StringUtils.isNotBlank(resultName)) {

@ -70,9 +70,9 @@ public class OcrIdentify implements Serializable {
@ApiModelProperty(value = "任务开始时间")
private Date startTime;
/**任务结束时间*/
@Excel(name = "任务结束时间", width = 15, format = "yyyy-MM-dd")
@JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd")
@DateTimeFormat(pattern="yyyy-MM-dd")
@Excel(name = "任务结束时间", width = 15, format = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss")
@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
@ApiModelProperty(value = "任务结束时间")
private Date endTime;
/**元数据配置*/

@ -1,8 +1,10 @@
package org.jeecg.modules.ocr.model;
import com.baomidou.mybatisplus.annotation.TableField;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.jeecg.modules.ocr.entity.OcrMetadataConfigDetail;
import org.jeecgframework.poi.excel.annotation.Excel;
import java.util.ArrayList;
import java.util.List;
@ -19,7 +21,11 @@ public class OcrMetadataConfigSaveModel {
/**配置名称*/
@ApiModelProperty(value = "配置名称")
private String configName;
/**任务类型*/
@ApiModelProperty(value = "任务类型")
private String taskType;
@ApiModelProperty(value = "任务类型名称")
private String taskTypeName;
/**元数据字段列表*/
public List<OcrMetadataConfigDetail> ocrMetadataConfigDetailList=new ArrayList<>();
}

@ -25,4 +25,6 @@ public interface IOcrTaskTypeService extends IService<OcrTaskType> {
* @return true:使,false:使
*/
boolean checkMetadataConfigUse(List<String> taskTypeIdList);
OcrTaskType saveTaskType(String taskTypeName);
}

@ -3,6 +3,7 @@ package org.jeecg.modules.ocr.service.impl;
import cn.hutool.core.util.ArrayUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
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;
@ -22,6 +23,7 @@ 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.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
@ -36,6 +38,7 @@ import java.util.stream.Collectors;
* @Version: V1.0
*/
@Service
@EnableAsync
public class OcrIdentifyServiceImpl extends ServiceImpl<OcrIdentifyMapper, OcrIdentify> implements IOcrIdentifyService {
@Resource
IOcrRuleCheckService ocrRuleCheckService;
@ -64,6 +67,11 @@ public class OcrIdentifyServiceImpl extends ServiceImpl<OcrIdentifyMapper, OcrId
@Override
public void getSemanticInfo(JSONObject responseBody) {
try {
Thread.sleep(10000l);
} catch (InterruptedException e) {
e.printStackTrace();
}
log.debug("打印 ocr 结果:"+responseBody.toString());
String identifyId = responseBody.getString("task_id");//任务id
String imgPath = responseBody.getString("img_path");//图片路径
@ -145,11 +153,12 @@ public class OcrIdentifyServiceImpl extends ServiceImpl<OcrIdentifyMapper, OcrId
}
}
}
}
}else{
rMessage.append(field+"_未查到ocr结果");
ocrResultAdd(ocrResultList,field,inputText,null,0d,imgPath,"ocr结果未获取",false);
fieldRightMap.put(field,false);
}
}
//其中有一个字段 全部都是 失败,则该图片失败.
if (fieldRightMap!=null&&fieldRightMap.values().size()>0&&!fieldRightMap.containsValue(false)) {
@ -165,10 +174,24 @@ public class OcrIdentifyServiceImpl extends ServiceImpl<OcrIdentifyMapper, OcrId
//=========================
}
@Async
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}";
@Override
@Async
public void postSemantic(String id, String identifyUrl) {
//3.请求python ocr识别
JSONObject semanticResponseJson = JSONObject.parseObject(semanticResponse);
semanticResponseJson.put("task_id",id);
this.getSemanticInfo(semanticResponseJson);
//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);
}
public static void mapPutIfTrue(Map map,String key,Boolean flag){
@ -199,7 +222,7 @@ public class OcrIdentifyServiceImpl extends ServiceImpl<OcrIdentifyMapper, OcrId
SourceImage sourceImage = new SourceImage();
sourceImage.setPath(imgPath);
int i = imgPath.lastIndexOf("/");
sourceImage.setFileName(imgPath.substring(i,imgPath.length()));
sourceImage.setFileName(imgPath.substring(i+1,imgPath.length()));
ocrResult.setSourceImage(sourceImage);
}
ocrResult.setFailureReason(failureReason);

@ -7,10 +7,13 @@ import org.apache.commons.lang.StringUtils;
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.OcrTaskType;
import org.jeecg.modules.ocr.mapper.OcrMetadataConfigMapper;
import org.jeecg.modules.ocr.mapper.OcrTaskTypeMapper;
import org.jeecg.modules.ocr.model.OcrMetadataConfigSaveModel;
import org.jeecg.modules.ocr.service.IOcrMetadataConfigDetailService;
import org.jeecg.modules.ocr.service.IOcrMetadataConfigService;
import org.jeecg.modules.ocr.service.IOcrTaskTypeService;
import org.jeecg.modules.ocr.vo.OcrMetadataConfigVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
@ -34,6 +37,8 @@ public class OcrMetadataConfigServiceImpl extends ServiceImpl<OcrMetadataConfigM
IOcrMetadataConfigDetailService ocrMetadataConfigDetailService;
@Resource
OcrMetadataConfigMapper ocrMetadataConfigMapper;
@Resource
IOcrTaskTypeService ocrTaskTypeService;
@Override
public boolean existsByConfigName(String configName, String id) {
LambdaQueryWrapper<OcrMetadataConfig> queryWrapper = new LambdaQueryWrapper<>();
@ -56,6 +61,15 @@ public class OcrMetadataConfigServiceImpl extends ServiceImpl<OcrMetadataConfigM
ocrMetadataConfig.setConfigName(ocrMetadataConfigSaveModel.getConfigName());
Boolean existsFlag=existsByConfigName(ocrMetadataConfigSaveModel.getConfigName(),ocrMetadataConfigSaveModel.getId());
AssertUtils.notTrue(existsFlag, String.format("[配置名称]-%s 已存在", ocrMetadataConfigSaveModel.getConfigName()));
//判断 元数据 所选的 任务类型 是否存在
if (StringUtils.isBlank(ocrMetadataConfigSaveModel.getTaskType())&& StringUtils.isNotBlank(ocrMetadataConfigSaveModel.getTaskTypeName())) {
OcrTaskType ocrTaskType = ocrTaskTypeService.saveTaskType(ocrMetadataConfigSaveModel.getTaskType());
ocrMetadataConfig.setTaskType(ocrTaskType.getId());
}else{
ocrMetadataConfig.setTaskType(ocrMetadataConfigSaveModel.getTaskType());
}
this.save(ocrMetadataConfig);
List<OcrMetadataConfigDetail> ocrMetadataConfigDetailList = ocrMetadataConfigSaveModel.getOcrMetadataConfigDetailList();
if(ocrMetadataConfigDetailList!=null&&ocrMetadataConfigDetailList.size()>0){
@ -152,6 +166,7 @@ public class OcrMetadataConfigServiceImpl extends ServiceImpl<OcrMetadataConfigM
copyEntityVo = new OcrMetadataConfigVo();
copyEntityVo.setId(ocrMetadataConfig.getId());
copyEntityVo.setConfigName(ocrMetadataConfig.getConfigName());
copyEntityVo.setTaskType(ocrMetadataConfig.getTaskType());
if (metadataConfigDetails!=null&&metadataConfigDetails.size()>0) {
arrayTestList = metadataConfigDetails.stream().filter(m -> m.getMetadataConfigId().equals(ocrMetadataConfig.getId())).collect(Collectors.toList());
fieldName = arrayTestList.stream().map(o -> o.getFieldName()).collect(Collectors.joining(","));

@ -62,4 +62,17 @@ public class OcrTaskTypeServiceImpl extends ServiceImpl<OcrTaskTypeMapper, OcrTa
}
return false;
}
@Override
public OcrTaskType saveTaskType(String taskTypeName) {
List<OcrTaskType> list = super.list(new LambdaQueryWrapper<OcrTaskType>().eq(OcrTaskType::getTypeName, taskTypeName));
if (list.size()>0) {
return list.get(0);
}
OcrTaskType ocrTaskType=new OcrTaskType();
ocrTaskType.setTypeName(taskTypeName);
super.save(ocrTaskType);
return ocrTaskType;
}
}

Loading…
Cancel
Save