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

@ -10,4 +10,11 @@ public class OcrConstant {
* *
*/ */
public static String ruleCheckSplitChar="&"; 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: "成功" * 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 = "通用识别") @ApiOperation(value = "通用识别")
@RequestMapping("/identify") @RequestMapping("/identify")
@Transactional @Transactional
@ -99,37 +99,41 @@ public class OcrApiController {
}*/ }*/
//1.获取请求参数 //1.获取请求参数
String requestId = requestBody.getString("requestId");//请求唯一标识 String requestId = requestBody.getString("requestId");//请求唯一标识
AssertUtils.notNull(requestId,"[requestId]-不可为空");
String scenes = requestBody.getString("scenes");//场景类型door=门头照片cases=病例bill=票据 String scenes = requestBody.getString("scenes");//场景类型door=门头照片cases=病例bill=票据
AssertUtils.notTrue(!"door".equals(scenes),String.format("暂不支持该场景类型[%s]",scenes)); AssertUtils.notTrue(!"door".equals(scenes),String.format("暂不支持该场景类型[%s]",scenes));
String ruleId = requestBody.getString("ruleId");//规则标识 String ruleId = requestBody.getString("ruleId");//规则标识
AssertUtils.notNull(ruleId,"[ruleId]-不可为空");
OcrRuleCheckVo ruleCheck = ruleCheckService.findById(ruleId); OcrRuleCheckVo ruleCheck = ruleCheckService.findById(ruleId);
AssertUtils.notNull(ruleCheck,"[ruleId]-不存在");
Integer priority = requestBody.getInteger("priority");//任务优先级1=加急0=不加急 Integer priority = requestBody.getInteger("priority");//任务优先级1=加急0=不加急
JSONArray sourceJson = requestBody.getJSONArray("sourceJson"); JSONArray sourceJson = requestBody.getJSONArray("sourceJson");
AssertUtils.notNull(ruleCheck,"[ruleId]-不存在");
AssertUtils.notNull(priority,"[priority]-不可为空");
AssertUtils.notNull(sourceJson,"[sourceJson]-不可为空");
List<JSONObject> sourceJsonList = sourceJson.toJavaList(JSONObject.class);//校验数据源 List<JSONObject> sourceJsonList = sourceJson.toJavaList(JSONObject.class);//校验数据源
String sourceImages = requestBody.getString("sourceImages");//ocr图片集 String sourceImages = requestBody.getString("sourceImages");//ocr图片集
OcrIdentify ocrIdentify=new OcrIdentify(); OcrIdentify ocrIdentify=new OcrIdentify();
//2.创建识别任务 //2.创建识别任务
if(true){ if(true){
ocrIdentify.setTaskName(requestId); ocrIdentify.setTaskName(requestId);
ocrIdentify.setStatus("0"); ocrIdentify.setStatus("0");//任务进行中
ocrIdentify.setRuleCheck(ruleId); ocrIdentify.setRuleCheck(ruleId);//配置规则检查id
ocrIdentify.setStartTime(new Date()); ocrIdentify.setStartTime(new Date());//开始时间
ocrIdentify.setTaskType("1681508355389231105"); ocrIdentify.setTaskType("1681508355389231105");//任务类型
ocrIdentify.setIdentifyUrl("https://h5.mcnetmart.com/tmp/images/dc_demo1.png"); ocrIdentify.setMetadataConfigId(ruleCheck.getMetadataConfigId());//规则配置的元数据id
ocrIdentify.setCreateBy("公共接口"); ocrIdentify.setTaskSource("API");//任务来源
ocrIdentify.setSourceJson(requestBody.getJSONArray("sourceJson").toJSONString()); ocrIdentify.setIdentifyUrl("https://h5.mcnetmart.com/tmp/images/dc_demo1.png");//识别图片地址
ocrIdentify.setCreateBy("API请求");//创建人
ocrIdentify.setSourceJson(requestBody.getJSONArray("sourceJson").toJSONString());//校验数据源
ocrIdentifyService.save(ocrIdentify); ocrIdentifyService.save(ocrIdentify);
} }
//3.请求python ocr识别
JSONObject semanticResponseJson = JSONObject.parseObject(semanticResponse);
semanticResponseJson.put("task_id",ocrIdentify.getId());
ocrIdentifyService.getSemanticInfo(semanticResponseJson);
//4.更新主任务状态 //3.请求python ocr识别异步执行
ocrIdentify.setStatus("1"); ocrIdentifyService.postSemantic(ocrIdentify.getId(),"https://h5.mcnetmart.com/tmp/images/dc_demo1.png");
ocrIdentify.setEndTime(new Date());
ocrIdentifyService.updateById(ocrIdentify);
return Result.OK("请求成功"); 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())); 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,"不可有重复的配置名称!"); AssertUtils.notTrue(countMap.values().stream().filter(f->f>1).count()>0,"不可有重复的配置名称!");
List<OcrMetadataConfigSaveModel> saveModelList=new ArrayList<>(); List<OcrMetadataConfigSaveModel> saveModelList=new ArrayList<>();
OcrMetadataConfigSaveModel ocrMetadataConfigSaveModel=new OcrMetadataConfigSaveModel(); OcrMetadataConfigSaveModel ocrMetadataConfigSaveModel=null;
List<OcrMetadataConfigDetail> ocrMetadataConfigDetailList=new ArrayList(); List<OcrMetadataConfigDetail> ocrMetadataConfigDetailList=null;
OcrMetadataConfigDetail ocrMetadataConfigDetail=new OcrMetadataConfigDetail(); OcrMetadataConfigDetail ocrMetadataConfigDetail=null;
for (OcrMetadataConfigVo configVo : list) { for (OcrMetadataConfigVo configVo : list) {
ocrMetadataConfigSaveModel=new OcrMetadataConfigSaveModel();
ocrMetadataConfigSaveModel.setConfigName(configVo.getConfigName()); ocrMetadataConfigSaveModel.setConfigName(configVo.getConfigName());
String resultName = configVo.getResultName(); String resultName = configVo.getResultName();
String getField = configVo.getGetField(); String getField = configVo.getGetField();
ocrMetadataConfigSaveModel.setTaskTypeName(configVo.getTaskType());
List<String> resultList=new ArrayList<>(); List<String> resultList=new ArrayList<>();
List<String> getFieldList=new ArrayList<>(); List<String> getFieldList=new ArrayList<>();
if (StringUtils.isNotBlank(resultName)) { if (StringUtils.isNotBlank(resultName)) {

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

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

@ -25,4 +25,6 @@ public interface IOcrTaskTypeService extends IService<OcrTaskType> {
* @return true:使,false:使 * @return true:使,false:使
*/ */
boolean checkMetadataConfigUse(List<String> taskTypeIdList); 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 cn.hutool.core.util.ArrayUtil;
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.units.qual.C; 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.jeecg.modules.ocr.vo.OcrRuleCheckVo;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
@ -36,6 +38,7 @@ import java.util.stream.Collectors;
* @Version: V1.0 * @Version: V1.0
*/ */
@Service @Service
@EnableAsync
public class OcrIdentifyServiceImpl extends ServiceImpl<OcrIdentifyMapper, OcrIdentify> implements IOcrIdentifyService { public class OcrIdentifyServiceImpl extends ServiceImpl<OcrIdentifyMapper, OcrIdentify> implements IOcrIdentifyService {
@Resource @Resource
IOcrRuleCheckService ocrRuleCheckService; IOcrRuleCheckService ocrRuleCheckService;
@ -64,6 +67,11 @@ public class OcrIdentifyServiceImpl extends ServiceImpl<OcrIdentifyMapper, OcrId
@Override @Override
public void getSemanticInfo(JSONObject responseBody) { public void getSemanticInfo(JSONObject responseBody) {
try {
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 identifyId = responseBody.getString("task_id");//任务id
String imgPath = responseBody.getString("img_path");//图片路径 String imgPath = responseBody.getString("img_path");//图片路径
@ -145,10 +153,11 @@ 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);
} }
rMessage.append(field+"_未查到ocr结果");
ocrResultAdd(ocrResultList,field,inputText,null,0d,imgPath,"ocr结果未获取",false);
fieldRightMap.put(field,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 @Override
@Async
public void postSemantic(String id, String identifyUrl) { 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){ 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 sourceImage = new SourceImage();
sourceImage.setPath(imgPath); sourceImage.setPath(imgPath);
int i = imgPath.lastIndexOf("/"); int i = imgPath.lastIndexOf("/");
sourceImage.setFileName(imgPath.substring(i,imgPath.length())); sourceImage.setFileName(imgPath.substring(i+1,imgPath.length()));
ocrResult.setSourceImage(sourceImage); ocrResult.setSourceImage(sourceImage);
} }
ocrResult.setFailureReason(failureReason); ocrResult.setFailureReason(failureReason);

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

@ -62,4 +62,17 @@ public class OcrTaskTypeServiceImpl extends ServiceImpl<OcrTaskTypeMapper, OcrTa
} }
return false; 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