接入SmartJavaAIOrc

This commit is contained in:
guoshengxiong 2025-08-04 11:34:57 +08:00
parent 395d2a1617
commit 369847e9d3
7 changed files with 18489 additions and 2 deletions

12
pom.xml
View File

@ -58,6 +58,18 @@
</repositories> </repositories>
<dependencies> <dependencies>
<dependency>
<groupId>cn.smartjavaai</groupId>
<artifactId>smartjavaai-ocr</artifactId>
<version>1.0.22</version>
<exclusions>
<!-- 排除 slf4j-simple -->
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency> <dependency>
<groupId>com.tencentcloudapi</groupId> <groupId>com.tencentcloudapi</groupId>
<artifactId>tencentcloud-sdk-java</artifactId> <artifactId>tencentcloud-sdk-java</artifactId>

View File

@ -0,0 +1,88 @@
package com.zhgd.xmgl.call;
import cn.smartjavaai.common.enums.DeviceEnum;
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.factory.OcrModelFactory;
import cn.smartjavaai.ocr.model.common.detect.OcrCommonDetModel;
import cn.smartjavaai.ocr.model.common.recognize.OcrCommonRecModel;
import com.alibaba.fastjson.JSONObject;
import com.zhgd.jeecg.common.execption.OpenAlertException;
import com.zhgd.xmgl.call.api.OcrManufacturer;
import com.zhgd.xmgl.modules.ocr.entity.OcrConfig;
import com.zhgd.xmgl.util.Fileutils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.Base64;
@Slf4j
@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class SmartJavaAIOrcCall implements OcrManufacturer {
public static DeviceEnum device = DeviceEnum.CPU;
private OcrConfig config;
@Override
public OcrConfig getConfig() {
return config;
}
@Override
public void setConfig(OcrConfig config) {
this.config = config;
}
@Override
public String ocrHandwriting(String base64Img) {
try {
OcrCommonRecModel recModel = getRecModel();
OcrInfo ocrInfo = recModel.recognize(Base64.getDecoder().decode(base64Img), new OcrRecOptions());
log.info("OCR识别结果{}", JSONObject.toJSONString(ocrInfo));
return ocrInfo != null ? ocrInfo.getFullText() : null;
} catch (Exception e) {
throw new OpenAlertException("手写文字图片ocr异常请联系管理员。", e);
}
}
/**
* 获取通用识别模型不带方向矫正
*
* @return
*/
public OcrCommonRecModel getRecModel() throws IOException {
OcrRecModelConfig recModelConfig = new OcrRecModelConfig();
//指定文本识别模型
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(Fileutils.getExportTemplateFile("models/ocr/PP-OCRv5_mobile_rec_infer.onnx").getAbsolutePath());
recModelConfig.setDevice(device);
recModelConfig.setTextDetModel(getDetectionModel());
return OcrModelFactory.getInstance().getRecModel(recModelConfig);
}
/**
* 获取文本检测模型
*
* @return
*/
public OcrCommonDetModel getDetectionModel() throws IOException {
OcrDetModelConfig config = new OcrDetModelConfig();
//指定检测模型
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(Fileutils.getExportTemplateFile("models/ocr/PP-PP-OCRv5_mobile_det_infer.onnx").getAbsolutePath());
config.setDevice(device);
return OcrModelFactory.getInstance().getDetModel(config);
}
}

View File

@ -2,6 +2,7 @@ package com.zhgd.xmgl.call.factory;
import com.zhgd.jeecg.common.util.SpringContextUtils; import com.zhgd.jeecg.common.util.SpringContextUtils;
import com.zhgd.xmgl.call.BaiDuOrcCall; import com.zhgd.xmgl.call.BaiDuOrcCall;
import com.zhgd.xmgl.call.SmartJavaAIOrcCall;
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.modules.ocr.service.IOcrConfigService; import com.zhgd.xmgl.modules.ocr.service.IOcrConfigService;
@ -33,6 +34,9 @@ public class OcrManufacturerFactory {
if (Objects.equals(config.getOcrType(), 1)) { if (Objects.equals(config.getOcrType(), 1)) {
manufacturer = SpringContextUtils.getBean(BaiDuOrcCall.class); manufacturer = SpringContextUtils.getBean(BaiDuOrcCall.class);
manufacturer.setConfig(config); manufacturer.setConfig(config);
} else if (Objects.equals(config.getOcrType(), 2)) {
manufacturer = SpringContextUtils.getBean(SmartJavaAIOrcCall.class);
manufacturer.setConfig(config);
} }
return manufacturer; return manufacturer;
} }

View File

@ -40,9 +40,9 @@ public class OcrConfig implements Serializable {
@ApiModelProperty(value = "secret_key") @ApiModelProperty(value = "secret_key")
private java.lang.String secretKey; private java.lang.String secretKey;
/** /**
* 类型1:百度AI * 类型1:百度AI;2:SmartJavaAI;
*/ */
@ApiModelProperty(value = "类型1:百度AI") @ApiModelProperty(value = "类型1:百度AI;2:SmartJavaAI;")
private java.lang.Integer ocrType; private java.lang.Integer ocrType;
/** /**
* 是否启用1是0否 * 是否启用1是0否

File diff suppressed because it is too large Load Diff