From 5d500c90d84543ce2c762a964f9573556021cdf5 Mon Sep 17 00:00:00 2001 From: zhouwentao <1577701412@qq.com> Date: Thu, 31 Aug 2023 17:26:34 +0800 Subject: [PATCH] updates --- .../jeecg/common/constant/OcrConstant.java | 4 + .../modules/api/controller/ApiController.java | 6 +- .../modules/ocr/init/HandleTransInit.java | 9 +- .../jeecg/modules/ocr/utils/FileOUtils.java | 94 ++++++++++++++++++- 4 files changed, 107 insertions(+), 6 deletions(-) diff --git a/jeecg-boot-base-core/src/main/java/org/jeecg/common/constant/OcrConstant.java b/jeecg-boot-base-core/src/main/java/org/jeecg/common/constant/OcrConstant.java index 152db75..c6a7216 100644 --- a/jeecg-boot-base-core/src/main/java/org/jeecg/common/constant/OcrConstant.java +++ b/jeecg-boot-base-core/src/main/java/org/jeecg/common/constant/OcrConstant.java @@ -32,6 +32,10 @@ public class OcrConstant { public static String api_test2_identify_url="http://127.0.0.1:7003/semantic"; //public static String api_test2_identify_url="http://127.0.0.1:8000/semantic"; + /** + * 网络附件下载目录 + */ + public static String FILE_DOWNLOAD_URL_PREFIX="/data/ocr/upFiles"; /** * 测试环境图片预览地址 */ diff --git a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/api/controller/ApiController.java b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/api/controller/ApiController.java index ce1ddfb..1f2bb23 100644 --- a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/api/controller/ApiController.java +++ b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/api/controller/ApiController.java @@ -252,8 +252,10 @@ public class ApiController { @ApiOperation(value = "调试101") @PostMapping(value = "/test_101") public Result test101(@RequestBody JSONObject jsonObject){ - OcrIdentifyDTO byId = ocrIdentifyService.findById("1692068694831755265"); - ocrIdentifyService.updateTaskResultInfo(byId.getId()); + //OcrIdentifyDTO byId = ocrIdentifyService.findById("1692068694831755265"); + //ocrIdentifyService.updateTaskResultInfo(byId.getId()); + JSONObject semanticResponseJson = JSONObject.parseObject("{\"img_path\":\"https://h5.mcnetmart.com/tmp/images/dc_demo1.png\",\"ocr_time\":3.4992809295654297,\"task_id\":\"1697133769514909698_1\",\"message\":\"成功\",\"semantic_result\":{\"ocr_res\":[[[[136.0,98.0],[276.0,70.0],[281.0,93.0],[141.0,121.0]],[\"深圳康瑞医院\",0.9387677311897278]],[[[139.0,120.0],[284.0,92.0],[288.0,109.0],[142.0,136.0]],[\"Shenzhen Kangrui hospual\",0.9168243408203125]],[[[171.0,284.0],[260.0,268.0],[264.0,292.0],[175.0,308.0]],[\"赵思杰\",0.9619879126548767]],[[[191.0,311.0],[247.0,301.0],[251.0,323.0],[195.0,333.0]],[\"妇产科\",0.9429848790168762]],[[[189.0,346.0],[259.0,332.0],[263.0,350.0],[193.0,364.0]],[\"主治医师\",0.8993821144104004]],[[[198.0,375.0],[263.0,364.0],[267.0,381.0],[201.0,393.0]],[\"202015\",0.9382884502410889]]],\"其他\":[{\"area\":[[139.0,120.0],[284.0,92.0],[288.0,109.0],[142.0,136.0]],\"end\":24,\"ocrText\":\"Shenzhen Kangrui hospual\",\"probability\":0.9846151031007864,\"start\":0,\"text\":\"Shenzhen Kangrui hospual\"},{\"area\":[[189.0,346.0],[259.0,332.0],[263.0,350.0],[193.0,364.0]],\"end\":4,\"ocrText\":\"主治医师\",\"probability\":0.9848503723946322,\"start\":0,\"text\":\"主治医师\"},{\"area\":[[198.0,375.0],[263.0,364.0],[267.0,381.0],[201.0,393.0]],\"end\":6,\"ocrText\":\"202015\",\"probability\":0.9731923600763821,\"start\":0,\"text\":\"202015\"}],\"医院名称\":[{\"area\":[[136.0,98.0],[276.0,70.0],[281.0,93.0],[141.0,121.0]],\"end\":6,\"ocrText\":\"深圳康瑞医院\",\"probability\":0.9856892892579268,\"start\":0,\"text\":\"深圳康瑞医院\"}],\"姓名\":[{\"area\":[[171.0,284.0],[260.0,268.0],[264.0,292.0],[175.0,308.0]],\"end\":3,\"ocrText\":\"赵思杰\",\"probability\":0.9996551423001598,\"start\":0,\"text\":\"赵思杰\"}],\"时间\":[],\"科室\":[{\"area\":[[191.0,311.0],[247.0,301.0],[251.0,323.0],[195.0,333.0]],\"end\":3,\"ocrText\":\"妇产科\",\"probability\":0.9983404968615517,\"start\":0,\"text\":\"妇产科\"}]},\"nlu_time\":16.85913324356079,\"identifyId\":\"1697133769514909698\"}"); + ocrIdentifyService.getSemanticInfo(semanticResponseJson); /*OcrRuleCheckVo ocrRuleCheckVo = byId.getOcrRuleCheckVo(); Map configRuleMap = ocrRuleCheckVo.getConfigRuleMap();*/ return Result.OK(""); diff --git a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/init/HandleTransInit.java b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/init/HandleTransInit.java index 251380a..62cf8e7 100644 --- a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/init/HandleTransInit.java +++ b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/init/HandleTransInit.java @@ -12,6 +12,7 @@ import org.jeecg.modules.ocr.model.TaskModel; import org.jeecg.modules.ocr.service.IOcrIdentifyService; import org.jeecg.modules.ocr.service.IOqcApiErrorLogService; import org.jeecg.modules.ocr.service.impl.TaskService; +import org.jeecg.modules.ocr.utils.FileOUtils; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; @@ -144,10 +145,15 @@ public class HandleTransInit implements ApplicationRunner { String masterTaskId = parameters[0];//主任务redis String task_id = parameters[1]; String image = parameters[2]; + //判断是不是网络图片 + Boolean onlineFile = FileOUtils.isOnlineFile(image); + if (onlineFile) { + //如果是网络图片,则将图片下载 + image = FileOUtils.downLoadFromUrl(image, FileOUtils.getFileName(image), OcrConstant.FILE_DOWNLOAD_URL_PREFIX); + } 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; @@ -155,6 +161,7 @@ public class HandleTransInit implements ApplicationRunner { while (!flag){ try { semanticResponseJson = RestUtil.post(OcrConstant.api_test2_identify_url, requestBody); + //semanticResponseJson = JSONObject.parseObject("{\"img_path\":\"https://h5.mcnetmart.com/tmp/images/dc_demo1.png\",\"ocr_time\":3.4992809295654297,\"task_id\":\"1697133769514909698_1\",\"message\":\"成功\",\"semantic_result\":{\"ocr_res\":[[[[136.0,98.0],[276.0,70.0],[281.0,93.0],[141.0,121.0]],[\"深圳康瑞医院\",0.9387677311897278]],[[[139.0,120.0],[284.0,92.0],[288.0,109.0],[142.0,136.0]],[\"Shenzhen Kangrui hospual\",0.9168243408203125]],[[[171.0,284.0],[260.0,268.0],[264.0,292.0],[175.0,308.0]],[\"赵思杰\",0.9619879126548767]],[[[191.0,311.0],[247.0,301.0],[251.0,323.0],[195.0,333.0]],[\"妇产科\",0.9429848790168762]],[[[189.0,346.0],[259.0,332.0],[263.0,350.0],[193.0,364.0]],[\"主治医师\",0.8993821144104004]],[[[198.0,375.0],[263.0,364.0],[267.0,381.0],[201.0,393.0]],[\"202015\",0.9382884502410889]]],\"其他\":[{\"area\":[[139.0,120.0],[284.0,92.0],[288.0,109.0],[142.0,136.0]],\"end\":24,\"ocrText\":\"Shenzhen Kangrui hospual\",\"probability\":0.9846151031007864,\"start\":0,\"text\":\"Shenzhen Kangrui hospual\"},{\"area\":[[189.0,346.0],[259.0,332.0],[263.0,350.0],[193.0,364.0]],\"end\":4,\"ocrText\":\"主治医师\",\"probability\":0.9848503723946322,\"start\":0,\"text\":\"主治医师\"},{\"area\":[[198.0,375.0],[263.0,364.0],[267.0,381.0],[201.0,393.0]],\"end\":6,\"ocrText\":\"202015\",\"probability\":0.9731923600763821,\"start\":0,\"text\":\"202015\"}],\"医院名称\":[{\"area\":[[136.0,98.0],[276.0,70.0],[281.0,93.0],[141.0,121.0]],\"end\":6,\"ocrText\":\"深圳康瑞医院\",\"probability\":0.9856892892579268,\"start\":0,\"text\":\"深圳康瑞医院\"}],\"姓名\":[{\"area\":[[171.0,284.0],[260.0,268.0],[264.0,292.0],[175.0,308.0]],\"end\":3,\"ocrText\":\"赵思杰\",\"probability\":0.9996551423001598,\"start\":0,\"text\":\"赵思杰\"}],\"时间\":[],\"科室\":[{\"area\":[[191.0,311.0],[247.0,301.0],[251.0,323.0],[195.0,333.0]],\"end\":3,\"ocrText\":\"妇产科\",\"probability\":0.9983404968615517,\"start\":0,\"text\":\"妇产科\"}]},\"nlu_time\":16.85913324356079,\"identifyId\":\"1697133769514909698\"}"); if (semanticResponseJson!=null) { flag=true; } diff --git a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/utils/FileOUtils.java b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/utils/FileOUtils.java index dbf763f..6451f70 100644 --- a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/utils/FileOUtils.java +++ b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/ocr/utils/FileOUtils.java @@ -1,14 +1,16 @@ package org.jeecg.modules.ocr.utils; +import cn.hutool.core.date.DateTime; +import cn.hutool.core.date.DateUtil; import org.apache.commons.lang.StringUtils; -import java.io.File; -import java.io.InputStream; +import java.io.*; +import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; import java.util.List; - +import java.util.UUID; /** * @Description * @Author ZhouWenTao @@ -89,6 +91,92 @@ public class FileOUtils { } } + /** + * 判断是不是网络图片. + * @param fileUrl 图片路径 + * @return true:是网络图片,false:不是网络图片 + */ + public static Boolean isOnlineFile(String fileUrl){ + //网络目录 + try { + URL url = new URL(fileUrl); + // 返回一个 URLConnection 对象,它表示到 URL 所引用的远程对象的连接。 + URLConnection uc = url.openConnection(); + // 打开的连接读取的输入流。 + InputStream in = uc.getInputStream(); + return true; + } catch (Exception e) { + //附件不存在 + return false; + } + } + + public static String getFileName(String fileUrl){ + if (StringUtils.isBlank(fileUrl)) { + return null; + } + return fileUrl.substring(fileUrl.lastIndexOf("/") + 1, fileUrl.length()); + } + + /** + * 从网络Url中下载文件 + * + * @param urlStr 网络路径 + * @param fileName 保存的附件名称 + * @param savePath 保存的目录 + * @throws IOException 异常 + */ + public static String downLoadFromUrl(String urlStr, String fileName, String savePath) { + try { + + URL url = new URL(urlStr); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + // 设置超时间为3秒 + conn.setConnectTimeout(3 * 1000); + // 防止屏蔽程序抓取而返回403错误 + conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); + + // 得到输入流 + InputStream inputStream = conn.getInputStream(); + // 获取字节数组 + byte[] getData = readInputStream(inputStream); + + // 文件保存位置 + File saveDir = new File(savePath); + if (!saveDir.exists()) { + saveDir.mkdir(); + } + File file = new File(saveDir + File.separator + fileName); + FileOutputStream fos = new FileOutputStream(file); + fos.write(getData); + fos.close(); + inputStream.close(); + return saveDir + File.separator + fileName; + } catch (Exception e) { + e.printStackTrace(); + } + return ""; + + } + + /** + * 从输入流中获取字节数组 + * + * @param inputStream 输入流 + * @return 字节数组 + * @throws IOException 异常 + */ + public static byte[] readInputStream(InputStream inputStream) throws IOException { + byte[] buffer = new byte[1024]; + int len = 0; + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + while ((len = inputStream.read(buffer)) != -1) { + bos.write(buffer, 0, len); + } + bos.close(); + return bos.toByteArray(); + } + public static void main(String[] args) { List stringList = fileLists(null,"E:\\workspace_2\\ocr\\images1"); System.out.println(stringList);