master
周文涛 2 years ago
parent e90f0ac226
commit e4b06cc9cc

@ -5,6 +5,7 @@ import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.AssertUtils;
import org.jeecg.common.util.RestUtil;
@ -15,6 +16,7 @@ import org.jeecg.modules.ocr.service.IOcrApiCallStatisticsService;
import org.jeecg.modules.ocr.service.IOcrIdentifyService;
import org.jeecg.modules.ocr.service.IOcrRuleCheckService;
import org.jeecg.modules.ocr.service.IOcrTaskTypeService;
import org.jeecg.modules.ocr.utils.FileOUtils;
import org.jeecg.modules.ocr.vo.OcrRuleCheckVo;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
@ -71,32 +73,14 @@ public class OcrApiController {
//获取识别的图片
List<String> fileList=new ArrayList<>();
if (sourceImages.indexOf("http://")!=-1||sourceImages.indexOf("https://")!=-1) {
List<String> fileUrlList= FileOUtils.fileLists(sourceImages);
for (String fileUrl : fileUrlList) {
//判断附件是否是 图片格式
AssertUtils.isTrue(sourceImages.lastIndexOf(".png")!=-1 || sourceImages.lastIndexOf(".jpg")!=-1||
sourceImages.lastIndexOf(".jpeg")!=-1,"当前网络图片格式不支持");
//网络图片
fileList.add(sourceImages);
}else{
//本地附件
if(false){
File sourceImagesIile = new File(sourceImages);
AssertUtils.isTrue(sourceImagesIile.exists(),"当前本地目录不存在");
File[] files = sourceImagesIile.listFiles();
for (File file : files) {
if (file.getAbsolutePath().lastIndexOf(".png")==-1&&file.getAbsolutePath().lastIndexOf(".jpg")==-1&&
file.getAbsolutePath().lastIndexOf(".jpeg")==-1) {
continue;
if (fileUrl.lastIndexOf(".png")!=-1 || fileUrl.lastIndexOf(".jpg")!=-1|| fileUrl.lastIndexOf(".jpeg")!=-1) {
fileList.add(fileUrl);
}
fileList.add(file.getAbsolutePath());
}
}else{
AssertUtils.notTrue(true,"当前仅支持网络图片");
AssertUtils.isTrue(sourceImages.lastIndexOf(".png")!=-1 || sourceImages.lastIndexOf(".jpg")!=-1||
sourceImages.lastIndexOf(".jpeg")!=-1,"当前图片格式不支持");
}
}
AssertUtils.isTrue(fileList.size()!=0,"未获取到图片真实地址");
AssertUtils.hasSize(fileList,"图片目录下未获取到可使用的图片格式文件");
OcrIdentify ocrIdentify=new OcrIdentify();
//2.创建识别任务
if(true){
@ -108,6 +92,7 @@ public class OcrApiController {
if (scenes.equals("door")) {
ocrIdentify.setTaskType("1683412752926986241");//任务类型
}
ocrIdentify.setPriority(priority.toString());
ocrIdentify.setMetadataConfigId(ruleCheck.getMetadataConfigId());//规则配置的元数据id
ocrIdentify.setTaskSource("API");//任务来源
ocrIdentify.setIdentifyUrl(sourceImages);//识别图片地址
@ -116,9 +101,7 @@ public class OcrApiController {
ocrIdentifyService.save(ocrIdentify);
}
//3.请求python ocr识别异步执行
for (String file : fileList) {
ocrIdentifyService.postSemantic(ocrIdentify,fileList);
}
return Result.OK("请求成功");
}

@ -1,5 +1,6 @@
package org.jeecg.modules.ocr.controller;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
@ -18,6 +19,7 @@ import org.jeecg.modules.ocr.entity.OcrMetadataConfig;
import org.jeecg.modules.ocr.service.IOcrIdentifyService;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.ocr.utils.DownloadTemplateUtil;
import org.jeecg.modules.ocr.utils.FileOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
@ -75,7 +77,19 @@ public class OcrIdentifyController extends JeecgController<OcrIdentify, IOcrIden
// @RequiresPermissions("org.jeecg.modules.ocr:ocr_identify:add")
@PostMapping(value = "/add")
public Result<String> add(@RequestBody OcrIdentify ocrIdentify) {
//获取识别的图片
List<String> fileList=new ArrayList<>();
List<String> fileUrlList= FileOUtils.fileLists(ocrIdentify.getIdentifyUrl());
for (String fileUrl : fileUrlList) {
//判断附件是否是 图片格式
if (fileUrl.lastIndexOf(".png")!=-1 || fileUrl.lastIndexOf(".jpg")!=-1|| fileUrl.lastIndexOf(".jpeg")!=-1) {
fileList.add(fileUrl);
}
}
AssertUtils.hasSize(fileList,"图片目录下未获取到可使用的图片格式文件");
ocrIdentifyService.save(ocrIdentify);
//3.请求python ocr识别异步执行
ocrIdentifyService.postSemantic(ocrIdentify,fileList);
//请求ocr识别接口
//ocrIdentifyService.postSemantic(ocrIdentify.getId(),Arrays.asList(ocrIdentify.getIdentifyUrl()));
return Result.OK("添加成功!");

@ -1,35 +1,29 @@
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;
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;
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.model.TaskModel;
import org.jeecg.modules.ocr.service.*;
import org.jeecg.modules.ocr.utils.ArrayCUtils;
import org.jeecg.modules.ocr.utils.ArrayOUtils;
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;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import javax.annotation.Resource;
import java.util.*;
@ -77,12 +71,12 @@ public class OcrIdentifyServiceImpl extends ServiceImpl<OcrIdentifyMapper, OcrId
public void getSemanticInfo(JSONObject responseBody) {
try {
ocrApiCallStatisticsService.saveLogs("通用识别", "/ocr/ocrApi/identify", 1);
Thread.sleep(10000l);
Thread.sleep(1000l);
} catch (InterruptedException e) {
e.printStackTrace();
}
log.debug("打印 ocr 结果:" + responseBody.toString());
String identifyId = responseBody.getString("task_id");//任务id
String identifyId = responseBody.getString("identifyId");//任务id
String imgPath = responseBody.getString("img_path");//图片路径
String imgName = null;//图片名称
if (StringUtils.isNotBlank(imgPath)) {
@ -131,7 +125,7 @@ public class OcrIdentifyServiceImpl extends ServiceImpl<OcrIdentifyMapper, OcrId
continue;
}
List<String> fieldNameList = Arrays.asList(fieldName.split(","));
boolean b = ArrayCUtils.containsStringList(fieldNameList, semanticResult.keySet().stream().collect(Collectors.toList()));
boolean b = ArrayOUtils.containsStringList(fieldNameList, semanticResult.keySet().stream().collect(Collectors.toList()));
//查看ocr识别返回的字段名称中是否有当前这个字段名称
if (b) {
//TODO 注意ocr 识别返回的 字段是多个结果(数组),有一个值匹配上即为正确
@ -229,6 +223,7 @@ public class OcrIdentifyServiceImpl extends ServiceImpl<OcrIdentifyMapper, OcrId
updateWrapper.eq(OcrIdentify::getId, id);
updateWrapper.set(OcrIdentify::getEndTime, new Date());
updateWrapper.set(OcrIdentify::getStatus, "1");
super.update(updateWrapper);
}
public static void mapPutIfTrue(Map map, String key, Boolean flag) {
@ -457,17 +452,19 @@ public class OcrIdentifyServiceImpl extends ServiceImpl<OcrIdentifyMapper, OcrId
requestBody.put("task_id", task_id);
requestBody.put("img_path", image);
JSONObject semanticResponseJson = RestUtil.post(OcrConstant.api_test_identify_url, requestBody);
semanticResponseJson.put("identifyId",masterTaskId);
this.getSemanticInfo(semanticResponseJson);
//该子任务已执行,判断主任务是否残留
String masterTask = (String) redisUtil.get(masterTaskId);
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(","));
String collect = Arrays.asList(masterTask.split(",")).stream().filter(t -> !t.equals(task_id)).collect(Collectors.joining(","));
if (StringUtils.isBlank(collect)) {
//如果主任务下的子任务已清空删除key
redisUtil.removeAll(masterTaskId);
redisUtil.del(masterTaskId);
//刷新Ocr识别任务状态
updateOcrIdentifyStatus(masterTaskId,"1");
log.debug("更新任务状态id:"+masterTaskId);
}else{
//主任务还存在,刷新主任务明细
redisUtil.set(masterTaskId,collect);

@ -10,7 +10,7 @@ import java.util.List;
* @Date 2023/7/24 14:46
*/
@Slf4j
public class ArrayCUtils {
public class ArrayOUtils {
public static boolean containsStringList(List<String> objects,List<String> objects2){
boolean flag=false;
for (String object : objects) {

@ -0,0 +1,47 @@
package org.jeecg.modules.ocr.utils;
import org.apache.commons.io.FileUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.util.AssertUtils;
import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
/**
* @Description
* @Author ZhouWenTao
* @Date 2023/7/26 13:52
*/
public class FileOUtils {
/**
*
* @param sourceImages
* @return
*/
public static List<String> fileLists(String sourceImages) {
List<String> files=new ArrayList<>();
if (sourceImages.indexOf("http://")!=-1||sourceImages.indexOf("https://")!=-1) {
//网络目录
try {
URL url = new URL(sourceImages);
// 返回一个 URLConnection 对象,它表示到 URL 所引用的远程对象的连接。
URLConnection uc = url.openConnection();
// 打开的连接读取的输入流。
InputStream in = uc.getInputStream();
//附件存在
files.add(sourceImages);
} catch (Exception e) {
//附件不存在
return null;
}
}else{
//本地文件
throw new JeecgBootException("当前不支持本地目录");
}
return files;
}
}
Loading…
Cancel
Save