Skip to content

Commit 42ac838

Browse files
author
litongjava
committed
show error info when load model error
1 parent 9d93b37 commit 42ac838

File tree

5 files changed

+37
-27
lines changed

5 files changed

+37
-27
lines changed

readme.md

+7-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
## 开源地址
1313
[gitee](https://gitee.com/ppnt/tools-ocr) | [github](https://github.com/litongjava/tools-ocr)
14+
15+
## required
16+
- Mac OS X 12.6 因为依赖djl 0.25.0
1417
## 安装
1518
> - **安装路径请勿包含中文字符**
1619
> - 本程序使用 JavaFX 开发,提供的安装包中已经包含了Java
@@ -43,10 +46,10 @@ wget https://github.com/litongjava/tools-ocr/releases/download/model-ppocr-v4/ch
4346
```
4447
解压模型
4548
```
46-
mkdir models/ch_PP-OCRv4_rec_infer-onnx
47-
mkdir models/ch_PP-OCRv4_det_infer-onnx
48-
unzip /Users/mac/Downloads/ch_PP-OCRv4_rec_infer-onnx.zip -d models/ch_PP-OCRv4_rec_infer-onnx
49-
unzip /Users/mac/Downloads/ch_PP-OCRv4_det_infer-onnx.zip -d models/ch_PP-OCRv4_det_infer-onnx
49+
mkdir models/ch_PP-OCRv4_rec_infer
50+
mkdir models/ch_PP-OCRv4_det_infer
51+
unzip /Users/mac/Downloads/ch_PP-OCRv4_rec_infer-onnx.zip -d models/ch_PP-OCRv4_rec_infer
52+
unzip /Users/mac/Downloads/ch_PP-OCRv4_det_infer-onnx.zip -d models/ch_PP-OCRv4_det_infer
5053
```
5154

5255
### 构建程序

src/main/java/com/luooqi/ocr/OcrApp.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public static void main(String[] args) {
1919
@Override
2020
public void init() throws Exception {
2121
super.init();
22-
InitConfig.init();
22+
//InitConfig.init();
2323
}
2424

2525

src/main/java/com/luooqi/ocr/config/InitConfig.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.luooqi.ocr.config;
22

3-
import com.luooqi.ocr.local.PaddlePaddleOCRV4;
43
import com.luooqi.ocr.utils.GlobalKeyListener;
54
import com.luooqi.ocr.utils.VoidDispatchService;
65
import org.jnativehook.GlobalScreen;
@@ -23,7 +22,7 @@ public static void init() {
2322
// map.put(ConfigKeys.recName, "ch_PP-OCRv3_rec_infer");
2423
// map.put(ConfigKeys.keysName, "ppocr_keys_v1.txt");
2524
// projectConfig.batchPut(map);
26-
PaddlePaddleOCRV4.INSTANCE.init();
25+
2726

2827
}
2928

src/main/java/com/luooqi/ocr/local/PaddlePaddleOCRV4.java

+14-20
Original file line numberDiff line numberDiff line change
@@ -26,36 +26,30 @@
2626
*/
2727
public enum PaddlePaddleOCRV4 {
2828
INSTANCE;
29-
private OcrV4Detection detection;
30-
private OcrV4Recognition recognition;
31-
private Predictor<Image, NDList> detector;
32-
private Predictor<Image, String> recognizer;
33-
private NDManager manager;
29+
private static OcrV4Detection detection;
30+
private static OcrV4Recognition recognition;
31+
private static Predictor<Image, NDList> detector;
32+
private static Predictor<Image, String> recognizer;
33+
private static NDManager manager;
3434

3535
PaddlePaddleOCRV4() {
36+
}
37+
38+
39+
//noting not to do.but init
40+
public static void init() throws ModelNotFoundException, MalformedModelException, IOException {
3641
detection = new OcrV4Detection();
3742
recognition = new OcrV4Recognition();
3843
ZooModel detectionModel = null;
3944
ZooModel recognitionModel = null;
40-
try {
41-
detectionModel = ModelZoo.loadModel(detection.chDetCriteria());
42-
recognitionModel = ModelZoo.loadModel(recognition.chRecCriteria());
43-
} catch (IOException e) {
44-
e.printStackTrace();
45-
} catch (ModelNotFoundException e) {
46-
e.printStackTrace();
47-
} catch (MalformedModelException e) {
48-
e.printStackTrace();
49-
}
45+
46+
detectionModel = ModelZoo.loadModel(detection.chDetCriteria());
47+
recognitionModel = ModelZoo.loadModel(recognition.chRecCriteria());
48+
5049
detector = detectionModel.newPredictor();
5150

5251
recognizer = recognitionModel.newPredictor();
5352
manager = NDManager.newBaseManager();
54-
}
55-
56-
57-
//noting not to do.but init
58-
public void init() {
5953

6054
}
6155

src/main/java/com/luooqi/ocr/windows/MainForm.java

+14
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package com.luooqi.ocr.windows;
22

3+
import ai.djl.MalformedModelException;
4+
import ai.djl.repository.zoo.ModelNotFoundException;
35
import cn.hutool.core.io.FileTypeUtil;
46
import cn.hutool.core.thread.ThreadUtil;
57
import cn.hutool.core.util.StrUtil;
68
import cn.hutool.log.StaticLog;
79
import com.luooqi.ocr.config.InitConfig;
810
import com.luooqi.ocr.controller.ProcessController;
11+
import com.luooqi.ocr.local.PaddlePaddleOCRV4;
912
import com.luooqi.ocr.model.CaptureInfo;
1013
import com.luooqi.ocr.model.StageInfo;
1114
import com.luooqi.ocr.snap.ScreenCapture;
@@ -30,6 +33,7 @@
3033

3134
import java.awt.image.BufferedImage;
3235
import java.io.File;
36+
import java.io.IOException;
3337
import java.util.HashMap;
3438
import java.util.Map;
3539

@@ -86,6 +90,16 @@ public void init(Stage primaryStage) {
8690
CommUtils.initStage(primaryStage);
8791
mainScene = new Scene(root, 670, 470);
8892
stage.setScene(mainScene);
93+
//启动引擎,加载模型,如果模型加载错误下屏幕显示错误
94+
try {
95+
PaddlePaddleOCRV4.init();
96+
} catch (ModelNotFoundException e) {
97+
textArea.setText("加载模型出现错误" + e.getMessage());
98+
} catch (MalformedModelException e) {
99+
textArea.setText("加载模型出现错误" + e.getMessage());
100+
} catch (IOException e) {
101+
textArea.setText("加载模型出现错误" + e.getMessage());
102+
}
89103
}
90104

91105
private TextArea getCenter() {

0 commit comments

Comments
 (0)