From 1b24e93ae9b3b06129b1732ff5fd1637906ad430 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BC=82=E6=B3=8A?= <1913856125@qq.com> Date: Thu, 15 Jan 2026 12:31:09 +0800 Subject: [PATCH] =?UTF-8?q?ocr=E8=AF=86=E5=88=AB=EF=BC=8C=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0pom=E8=AF=86=E5=88=AB=E7=9A=84=E4=BE=9D=E8=B5=96SmartJ?= =?UTF-8?q?avaAl?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 1 + zhyc-module/pom.xml | 128 +++++++++ .../module/app/controller/OrcController.java | 29 ++ .../module/app/util/OcrRecognizeUtil.java | 257 ++++++++++++++++++ .../resources/mapper/work/WorkOrderMapper.xml | 2 +- 5 files changed, 416 insertions(+), 1 deletion(-) create mode 100644 zhyc-module/src/main/java/com/zhyc/module/app/controller/OrcController.java create mode 100644 zhyc-module/src/main/java/com/zhyc/module/app/util/OcrRecognizeUtil.java diff --git a/pom.xml b/pom.xml index b4f8e0f..729e2b1 100644 --- a/pom.xml +++ b/pom.xml @@ -35,6 +35,7 @@ 1.2.13 5.7.12 5.3.39 + diff --git a/zhyc-module/pom.xml b/zhyc-module/pom.xml index 36df6fd..e597815 100644 --- a/zhyc-module/pom.xml +++ b/zhyc-module/pom.xml @@ -12,7 +12,135 @@ zhyc-module + + windows-x86_64 + win-x86_64 + + + + + + + cn.smartjavaai + bom + 1.1.1 + pom + + import + + + + + + cn.smartjavaai + common + 1.1.1 + + + commons-cli + commons-cli + 1.9.0 + + + + commons-io + commons-io + 2.17.0 + + + + org.testng + testng + 7.10.2 + test + + + + + ch.qos.logback + logback-classic + 1.2.3 + + + + com.alibaba + fastjson + 1.2.83 + + + + junit + junit + 4.13.2 + + + + + + + cn.smartjavaai + ocr + + + com.microsoft.onnxruntime + onnxruntime + + + + + + com.microsoft.onnxruntime + onnxruntime + 1.20.0 + runtime + + + + + ai.djl.pytorch + pytorch-jni + 2.7.1-0.34.0 + runtime + + + + + + org.bytedeco + javacpp + 1.5.11 + ${javacv.platform.windows-x86_64} + + + org.bytedeco + ffmpeg + 6.1.1-1.5.10 + ${javacv.platform.windows-x86_64} + + + + org.bytedeco + openblas + 0.3.26-1.5.10 + ${javacv.platform.windows-x86_64} + + + + org.bytedeco + opencv + 4.9.0-1.5.10 + ${javacv.platform.windows-x86_64} + + + + ai.djl.pytorch + pytorch-native-cpu + ${djl.platform.windows-x86_64} + 2.7.1 + runtime + + zhyc zhyc-common diff --git a/zhyc-module/src/main/java/com/zhyc/module/app/controller/OrcController.java b/zhyc-module/src/main/java/com/zhyc/module/app/controller/OrcController.java new file mode 100644 index 0000000..48d277f --- /dev/null +++ b/zhyc-module/src/main/java/com/zhyc/module/app/controller/OrcController.java @@ -0,0 +1,29 @@ +package com.zhyc.module.app.controller; + +import com.zhyc.common.config.RuoYiConfig; +import com.zhyc.common.core.domain.AjaxResult; +import com.zhyc.module.app.util.OcrRecognizeUtil; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/ocr") +public class OrcController { + + @PostMapping + public AjaxResult orcSheepNo(String path) { + // 添加空值检查 + if (path == null || path.trim().isEmpty()) { + return AjaxResult.error("路径不能为空"); + } + + OcrRecognizeUtil ocrRecognizeUtil = new OcrRecognizeUtil(); + // 移除 /profile + path = path.replace("/profile", ""); + path = RuoYiConfig.getProfile() + path; + String recognize =ocrRecognizeUtil.recognizeTwo(path); + return AjaxResult.success(recognize); + } +} diff --git a/zhyc-module/src/main/java/com/zhyc/module/app/util/OcrRecognizeUtil.java b/zhyc-module/src/main/java/com/zhyc/module/app/util/OcrRecognizeUtil.java new file mode 100644 index 0000000..155d198 --- /dev/null +++ b/zhyc-module/src/main/java/com/zhyc/module/app/util/OcrRecognizeUtil.java @@ -0,0 +1,257 @@ +package com.zhyc.module.app.util; + +import ai.djl.modality.cv.Image; +import cn.smartjavaai.common.cv.SmartImageFactory; +import cn.smartjavaai.common.enums.DeviceEnum; +import cn.smartjavaai.common.utils.ImageUtils; +import cn.smartjavaai.ocr.config.DirectionModelConfig; +import cn.smartjavaai.ocr.config.OcrDetModelConfig; +import cn.smartjavaai.ocr.config.OcrRecModelConfig; +import cn.smartjavaai.ocr.config.OcrRecOptions; +import cn.smartjavaai.ocr.entity.OcrInfo; +import cn.smartjavaai.ocr.enums.CommonDetModelEnum; +import cn.smartjavaai.ocr.enums.CommonRecModelEnum; +import cn.smartjavaai.ocr.enums.DirectionModelEnum; +import cn.smartjavaai.ocr.factory.OcrModelFactory; +import cn.smartjavaai.ocr.model.common.detect.OcrCommonDetModel; +import cn.smartjavaai.ocr.model.common.direction.OcrDirectionModel; +import cn.smartjavaai.ocr.model.common.recognize.OcrCommonRecModel; +import com.alibaba.fastjson.JSONObject; +import com.zhyc.common.config.RuoYiConfig; +import lombok.extern.slf4j.Slf4j; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.List; + +/** + * OCR 文本识别 示例 + * 模型下载地址:https://pan.baidu.com/s/1MLfd73Vjdpnuls9-oqc9uw?pwd=1234 提取码: 1234 + * 开发文档:http://doc.smartjavaai.cn/ + */ +/** + * OCR识别工具类 + * 提供文本识别相关的功能,包括获取不同类型的识别模型、文本检测模型、方向检测模型等 + * 支持普通识别、手写识别、带方向矫正的识别以及批量识别等功能 + */ +@Slf4j +public class OcrRecognizeUtil { + + //设备类型 + public static DeviceEnum device =DeviceEnum.CPU; + + + /** + * 测试初始化方法,在所有测试用例执行前调用一次 + * 设置SmartImageFactory使用的引擎为OpenCV,并可选配置缓存路径 + * + * @throws IOException 当初始化过程中发生IO异常时抛出 + */ + @BeforeClass + public static void beforeAll() throws IOException { + SmartImageFactory.setEngine(SmartImageFactory.Engine.OPENCV); + //修改缓存路径 + //Config.setCachePath("/Users/xxx/smartjavaai_cache"); + } + + /** + * 获取通用识别模型(高精确度模型) + * 注意事项:高精度模型,识别准确度高,速度慢 + * + * @return 返回高精度的OCR通用识别模型实例 + */ + public OcrCommonRecModel getProRecModel(){ + OcrRecModelConfig recModelConfig = new OcrRecModelConfig(); + //指定文本识别模型,切换模型需要同时修改modelEnum及modelPath + recModelConfig.setRecModelEnum(CommonRecModelEnum.PP_OCR_V5_SERVER_REC_MODEL); + //指定识别模型位置,需要更改为自己的模型路径(下载地址请查看文档) + recModelConfig.setRecModelPath("C:/wwwroot/ocr/rec/PP-OCRv5_server_rec_infer/PP-OCRv5_server_rec.onnx"); + recModelConfig.setDevice(device); + recModelConfig.setTextDetModel(getProDetectionModel()); + recModelConfig.setDirectionModel(getDirectionModel()); + return OcrModelFactory.getInstance().getRecModel(recModelConfig); + } + + /** + * 获取通用识别模型(极速模型) + * 注意事项:极速模型,识别准确度低,速度快 + * + * @return 返回极速的OCR通用识别模型实例 + */ + public OcrCommonRecModel getFastRecModel(){ + OcrRecModelConfig recModelConfig = new OcrRecModelConfig(); + //指定文本识别模型,切换模型需要同时修改modelEnum及modelPath + recModelConfig.setRecModelEnum(CommonRecModelEnum.PP_OCR_V5_MOBILE_REC_MODEL); + //指定识别模型位置,需要更改为自己的模型路径(下载地址请查看文档) + recModelConfig.setRecModelPath("C:/wwwroot/ocr/rec/PP-OCRv5_mobile_rec_infer/PP-OCRv5_mobile_rec_infer.onnx"); + recModelConfig.setDevice(device); + recModelConfig.setTextDetModel(getFastDetectionModel()); + recModelConfig.setDirectionModel(getDirectionModel()); // 添加方向模型配置 + return OcrModelFactory.getInstance().getRecModel(recModelConfig); + } + + + /** + * 获取文本检测模型(极速模型) + * 注意事项:极速模型,识别准确度低,速度快 + * + * @return 返回极速的OCR通用检测模型实例 + */ + public OcrCommonDetModel getFastDetectionModel() { + OcrDetModelConfig config = new OcrDetModelConfig(); + //指定检测模型,切换模型需要同时修改modelEnum及modelPath + config.setModelEnum(CommonDetModelEnum.PP_OCR_V5_MOBILE_DET_MODEL); + //指定模型位置,需要更改为自己的模型路径(下载地址请查看文档) + config.setDetModelPath("C:/wwwroot/ocr/det/PP-OCRv5_mobile_det_infer/PP-OCRv5_mobile_det_infer.onnx"); + config.setDevice(device); + return OcrModelFactory.getInstance().getDetModel(config); + } + + /** + * 获取文本检测模型(高精确度模型) + * 注意事项:高精度模型,识别准确度高,速度慢 + * + * @return 返回高精度的OCR通用检测模型实例 + */ + public OcrCommonDetModel getProDetectionModel() { + OcrDetModelConfig config = new OcrDetModelConfig(); + //指定检测模型,切换模型需要同时修改modelEnum及modelPath + config.setModelEnum(CommonDetModelEnum.PP_OCR_V5_SERVER_DET_MODEL); + //指定模型位置,需要更改为自己的模型路径(下载地址请查看文档) + config.setDetModelPath("C:/wwwroot/ocr/det/PP-OCRv5_server_det_infer/PP-OCRv5_server_det.onnx"); + config.setDevice(device); + return OcrModelFactory.getInstance().getDetModel(config); + } + + /** + * 获取方向检测模型 + * + * @return 返回OCR方向检测模型实例 + */ + public OcrDirectionModel getDirectionModel(){ + DirectionModelConfig directionModelConfig = new DirectionModelConfig(); + //指定行文本方向检测模型,切换模型需要同时修改modelEnum及modelPath + directionModelConfig.setModelEnum(DirectionModelEnum.PP_LCNET_X0_25); + //指定行文本方向检测模型路径,需要更改为自己的模型路径(下载地址请查看文档) + directionModelConfig.setModelPath("C:/wwwroot/ocr/ori/PP-LCNet_x0_25_textline_ori_infer.onnx"); + directionModelConfig.setDevice(device); + return OcrModelFactory.getInstance().getDirectionModel(directionModelConfig); + } + + + + /** + * 文本识别 + * 支持简体中文、繁体中文、英文、日文四种主要语言,以及手写、竖版、拼音、生僻字 + * 流程:文本检测 -> 文本识别 + * 注意事项: + * 1、批量检测时,模型应统一放在外层 try 中使用,避免重复加载,自动释放资源更安全。 + * 2、模型文件需要放在单独文件夹 + */ + public String recognize(String path){ + try { + OcrCommonRecModel recModel = getFastRecModel(); + //不带方向矫正,分行返回文本 + OcrRecOptions options = new OcrRecOptions(false, true); + //创建Image对象,可以从文件、url、InputStream创建、BufferedImage、Base64创建,具体使用方法可以查看文档 + Image image = SmartImageFactory.getInstance().fromFile(path); + OcrInfo ocrInfo = recModel.recognize(image, options); + log.info("OCR识别结果:{}", JSONObject.toJSONString(ocrInfo.getFullText())); + + // 识别成功后删除原图片 + deleteFile(path); + return ocrInfo.getFullText(); + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + + /** + * 文本识别(手写字) + * 支持简体中文、繁体中文、英文、日文四种主要语言,以及手写、竖版、拼音、生僻字 + * 流程:文本检测 -> 文本识别 + * 注意事项: + * 1、批量检测时,模型应统一放在外层 try 中使用,避免重复加载,自动释放资源更安全。 + * 2、模型文件需要放在单独文件夹 + * 3、识别完成后会删除原图片文件 + */ + public void recognizeHandWriting(String path){ + try { + OcrCommonRecModel recModel = getFastRecModel(); + //创建Image对象,可以从文件、url、InputStream创建、BufferedImage、Base64创建,具体使用方法可以查看文档 + Image image = SmartImageFactory.getInstance().fromFile(path); + OcrInfo ocrInfo = recModel.recognize(image, new OcrRecOptions()); + log.info("OCR识别结果:{}", JSONObject.toJSONString(ocrInfo)); + + // 识别成功后删除原图片 + deleteFile(path); + } catch (Exception e) { + e.printStackTrace(); + } + } + +/** + * 文本识别(带方向矫正) + * 支持简体中文、繁体中文、英文、日文四种主要语言,以及手写、竖版、拼音、生僻字 + * 本方法支持多角度文字识别 + * 流程:文本检测 -> 方向检测 -> 方向矫正 -> 文本识别 + * 注意事项: + * 1、批量检测时,模型应统一放在外层 try 中使用,避免重复加载,自动释放资源更安全。 + * 2、模型文件需要放在单独文件夹 + * 3、识别完成后会删除原图片文件 + */ +public String recognizeTwo(String path){ + try { + OcrCommonRecModel recModel = getFastRecModel(); + //带方向矫正,分行返回文本 + OcrRecOptions options = new OcrRecOptions(true, true); + //创建Image对象,可以从文件、url、InputStream创建、BufferedImage、Base64创建,具体使用方法可以查看文档 + Image image = SmartImageFactory.getInstance().fromFile(path); + OcrInfo ocrInfo = recModel.recognize(image, options); + log.info("OCR识别结果:{}", JSONObject.toJSONString(ocrInfo)); + + // 识别成功后删除原图片 + deleteFile(path); + return ocrInfo.getFullText(); + } catch (Exception e) { + e.printStackTrace(); + } + return null; +} + + + /** + * 删除指定路径的文件 + * + * @param filePath 要删除的文件路径 + * @return 删除是否成功 + */ + private boolean deleteFile(String filePath) { + try { + File file = new File(filePath); + if (file.exists()) { + boolean deleted = file.delete(); + if (deleted) { + log.info("已删除文件: {}", filePath); + } else { + log.warn("删除文件失败: {}", filePath); + } + return deleted; + } else { + log.warn("文件不存在: {}", filePath); + return false; + } + } catch (Exception e) { + log.error("删除文件时发生异常: {}", filePath, e); + return false; + } + } + + +} diff --git a/zhyc-module/src/main/resources/mapper/work/WorkOrderMapper.xml b/zhyc-module/src/main/resources/mapper/work/WorkOrderMapper.xml index eecd519..0937b9a 100644 --- a/zhyc-module/src/main/resources/mapper/work/WorkOrderMapper.xml +++ b/zhyc-module/src/main/resources/mapper/work/WorkOrderMapper.xml @@ -46,7 +46,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" and order_no like concat('%', #{orderNo}, '%') and plan_id = #{planId} - + and biz_type in #{item}