ocr的SmartJavaAI离线

This commit is contained in:
guoshengxiong 2025-10-16 16:44:41 +08:00
parent 443b6e6f3c
commit 268d6bbdd6
2 changed files with 48 additions and 4 deletions

34
pom.xml
View File

@ -28,6 +28,8 @@
<commons.version>2.6</commons.version> <commons.version>2.6</commons.version>
<swagger.ui.version>1.9.6</swagger.ui.version> <swagger.ui.version>1.9.6</swagger.ui.version>
<tomcat.version>9.0.30</tomcat.version> <tomcat.version>9.0.30</tomcat.version>
<djl.platform.windows-x86_64>win-x86_64</djl.platform.windows-x86_64>
<djl.platform.linux-x86_64>linux-x86_64</djl.platform.linux-x86_64>
</properties> </properties>
<repositories> <repositories>
@ -92,6 +94,38 @@
</exclusion> </exclusion>
</exclusions> </exclusions>
</dependency> </dependency>
<!--PyTorch离线平台依赖-->
<dependency>
<groupId>ai.djl.pytorch</groupId>
<artifactId>pytorch-native-cpu</artifactId>
<classifier>${djl.platform.linux-x86_64}</classifier>
<version>2.5.1</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>ai.djl.pytorch</groupId>
<artifactId>pytorch-jni</artifactId>
<version>2.5.1-0.32.0</version>
<scope>runtime</scope>
</dependency>
<!--tensorflow离线平台依赖-->
<dependency>
<groupId>ai.djl.tensorflow</groupId>
<artifactId>tensorflow-native-cpu</artifactId>
<classifier>${djl.platform.linux-x86_64}</classifier>
<scope>runtime</scope>
<version>2.16.1</version>
</dependency>
<!--mxnet离线平台依赖-->
<dependency>
<groupId>ai.djl.mxnet</groupId>
<artifactId>mxnet-native-mkl</artifactId>
<classifier>${djl.platform.linux-x86_64}</classifier>
<scope>runtime</scope>
<version>1.9.1</version>
</dependency>
<dependency> <dependency>
<groupId>com.tencentcloudapi</groupId> <groupId>com.tencentcloudapi</groupId>
<artifactId>tencentcloud-sdk-java</artifactId> <artifactId>tencentcloud-sdk-java</artifactId>

View File

@ -14,12 +14,17 @@ import com.alibaba.fastjson.JSONObject;
import com.zhgd.jeecg.common.execption.OpenAlertException; import com.zhgd.jeecg.common.execption.OpenAlertException;
import com.zhgd.xmgl.call.api.OcrManufacturer; import com.zhgd.xmgl.call.api.OcrManufacturer;
import com.zhgd.xmgl.modules.ocr.entity.OcrConfig; import com.zhgd.xmgl.modules.ocr.entity.OcrConfig;
import com.zhgd.xmgl.util.FileUtils; import com.zhgd.xmgl.util.PathUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope; import org.springframework.context.annotation.Scope;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Base64; import java.util.Base64;
@Slf4j @Slf4j
@ -62,8 +67,11 @@ public class SmartJavaAIOcrCall implements OcrManufacturer {
recModelConfig.setRecModelEnum(CommonRecModelEnum.PP_OCR_V5_MOBILE_REC_MODEL); recModelConfig.setRecModelEnum(CommonRecModelEnum.PP_OCR_V5_MOBILE_REC_MODEL);
//指定识别模型位置需要更改为自己的模型路径下载地址请查看文档 //指定识别模型位置需要更改为自己的模型路径下载地址请查看文档
// recModelConfig.setRecModelPath("/Users/xxx/Documents/develop/model/ocr/PP-OCRv5_mobile_rec_infer/PP-OCRv5_mobile_rec_infer.onnx"); // recModelConfig.setRecModelPath("/Users/xxx/Documents/develop/model/ocr/PP-OCRv5_mobile_rec_infer/PP-OCRv5_mobile_rec_infer.onnx");
FileUtils.getExportTemplateFile("/models/ocr/dict.txt"); String tmpdir = PathUtil.getBasePath();
recModelConfig.setRecModelPath(FileUtils.getExportTemplateFile("/models/ocr/PP-OCRv5_mobile_rec_infer.onnx").getAbsolutePath()); Files.copy(new ClassPathResource("/models/ocr/dict.txt").getInputStream(), Paths.get(tmpdir, "dict.txt"), StandardCopyOption.REPLACE_EXISTING);
Path recModelPath = Paths.get(tmpdir, "PP-OCRv5_mobile_rec_infer.onnx");
Files.copy(new ClassPathResource("/models/ocr/PP-OCRv5_mobile_rec_infer.onnx").getInputStream(), recModelPath, StandardCopyOption.REPLACE_EXISTING);
recModelConfig.setRecModelPath(recModelPath.toAbsolutePath().toString());
recModelConfig.setDevice(device); recModelConfig.setDevice(device);
recModelConfig.setTextDetModel(getDetectionModel()); recModelConfig.setTextDetModel(getDetectionModel());
return OcrModelFactory.getInstance().getRecModel(recModelConfig); return OcrModelFactory.getInstance().getRecModel(recModelConfig);
@ -80,7 +88,9 @@ public class SmartJavaAIOcrCall implements OcrManufacturer {
config.setModelEnum(CommonDetModelEnum.PP_OCR_V5_MOBILE_DET_MODEL); config.setModelEnum(CommonDetModelEnum.PP_OCR_V5_MOBILE_DET_MODEL);
//指定模型位置需要更改为自己的模型路径下载地址请查看文档 //指定模型位置需要更改为自己的模型路径下载地址请查看文档
// config.setDetModelPath("/Users/xxx/Documents/develop/model/ocr/PP-OCRv5_mobile_det_infer/PP-OCRv5_mobile_det_infer.onnx"); // config.setDetModelPath("/Users/xxx/Documents/develop/model/ocr/PP-OCRv5_mobile_det_infer/PP-OCRv5_mobile_det_infer.onnx");
config.setDetModelPath(FileUtils.getExportTemplateFile("/models/ocr/PP-OCRv5_mobile_det_infer.onnx").getAbsolutePath()); Path path = Paths.get(PathUtil.getBasePath(), "PP-OCRv5_mobile_det_infer.onnx");
Files.copy(new ClassPathResource("/models/ocr/PP-OCRv5_mobile_det_infer.onnx").getInputStream(), path, StandardCopyOption.REPLACE_EXISTING);
config.setDetModelPath(path.toAbsolutePath().toString());
config.setDevice(device); config.setDevice(device);
return OcrModelFactory.getInstance().getDetModel(config); return OcrModelFactory.getInstance().getDetModel(config);
} }