Skip to content

Commit ecdf1b0

Browse files
author
litongjava
committed
增加测试类
1 parent 570bac5 commit ecdf1b0

File tree

3 files changed

+202
-109
lines changed

3 files changed

+202
-109
lines changed

src/main/java/com/benjaminwan/ocrlibrary/OcrEngine.java

Lines changed: 109 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -7,114 +7,114 @@
77
import java.nio.charset.Charset;
88

99
public final class OcrEngine {
10-
/**
11-
* 图像外接白框,用于提升识别率,文字框没有正确框住所有文字时,增加此值。
12-
*/
13-
private int padding;
14-
/**
15-
* 文字框置信度门限,文字框没有正确框住所有文字时,减小此值
16-
*/
17-
private float boxScoreThresh;
18-
19-
private float boxThresh;
20-
/**
21-
* 单个文字框大小倍率,越大时单个文字框越大
22-
*/
23-
private float unClipRatio;
24-
/**
25-
* 启用(1)/禁用(0) 文字方向检测,只有图片倒置的情况下(旋转90~270度的图片),才需要启用文字方向检测
26-
*/
27-
private boolean doAngle;
28-
/**
29-
* 启用(1)/禁用(0) 角度投票(整张图片以最大可能文字方向来识别),当禁用文字方向检测时,此项也不起作用
30-
*/
31-
private boolean mostAngle;
32-
33-
public native boolean setNumThread(int numThread);
34-
35-
public native void initLogger(boolean isConsole, boolean isPartImg, boolean isResultImg);
36-
37-
public native void enableResultText(String imagePath);
38-
39-
public native boolean initModels(String modelsDir, String detName, String clsName, String recName, String keysName);
40-
41-
/**
42-
* GPU0一般为默认GPU,参数选项:使用CPU(-1)/使用GPU0(0)/使用GPU1(1)/...
43-
*/
44-
public native void setGpuIndex(int gpuIndex);
45-
46-
public native String getVersion();
47-
48-
public native OcrResult detect(String input, int padding, int maxSideLen, float boxScoreThresh, float boxThresh, float unClipRatio, boolean doAngle, boolean mostAngle);
49-
50-
public OcrEngine() {
51-
try {
52-
StaticLog.info("java.library.path=" + System.getProperty("java.library.path"));
53-
System.loadLibrary("RapidOcrNcnn");
54-
} catch (Exception e) {
55-
e.printStackTrace();
56-
}
57-
this.padding = 15;
58-
this.boxScoreThresh = 0.25f;
59-
this.boxThresh = 0.3f;
60-
this.unClipRatio = 1.6f;
61-
this.doAngle = true;
62-
this.mostAngle = true;
63-
}
64-
65-
public int getPadding() {
66-
return this.padding;
67-
}
68-
69-
public void setPadding(int i) {
70-
this.padding = i;
71-
}
72-
73-
public float getBoxScoreThresh() {
74-
return this.boxScoreThresh;
75-
}
76-
77-
public void setBoxScoreThresh(float f) {
78-
this.boxScoreThresh = f;
79-
}
80-
81-
public float getBoxThresh() {
82-
return this.boxThresh;
83-
}
84-
85-
public void setBoxThresh(float f) {
86-
this.boxThresh = f;
87-
}
88-
89-
public float getUnClipRatio() {
90-
return this.unClipRatio;
91-
}
92-
93-
public void setUnClipRatio(float f) {
94-
this.unClipRatio = f;
95-
}
96-
97-
public boolean getDoAngle() {
98-
return this.doAngle;
99-
}
100-
101-
public void setDoAngle(boolean z) {
102-
this.doAngle = z;
103-
}
104-
105-
public boolean getMostAngle() {
106-
return this.mostAngle;
107-
}
108-
109-
public void setMostAngle(boolean z) {
110-
this.mostAngle = z;
111-
}
112-
113-
public OcrResult detect(String input) {
114-
return detect(input, 0);
115-
}
116-
117-
public OcrResult detect(String input, int maxSideLen) {
118-
return detect(input, this.padding, maxSideLen, this.boxScoreThresh, this.boxThresh, this.unClipRatio, this.doAngle, this.mostAngle);
10+
/**
11+
* 图像外接白框,用于提升识别率,文字框没有正确框住所有文字时,增加此值。
12+
*/
13+
private int padding;
14+
/**
15+
* 文字框置信度门限,文字框没有正确框住所有文字时,减小此值
16+
*/
17+
private float boxScoreThresh;
18+
19+
private float boxThresh;
20+
/**
21+
* 单个文字框大小倍率,越大时单个文字框越大
22+
*/
23+
private float unClipRatio;
24+
/**
25+
* 启用(1)/禁用(0) 文字方向检测,只有图片倒置的情况下(旋转90~270度的图片),才需要启用文字方向检测
26+
*/
27+
private boolean doAngle;
28+
/**
29+
* 启用(1)/禁用(0) 角度投票(整张图片以最大可能文字方向来识别),当禁用文字方向检测时,此项也不起作用
30+
*/
31+
private boolean mostAngle;
32+
33+
public native boolean setNumThread(int numThread);
34+
35+
public native void initLogger(boolean isConsole, boolean isPartImg, boolean isResultImg);
36+
37+
public native void enableResultText(String imagePath);
38+
39+
public native boolean initModels(String modelsDir, String detName, String clsName, String recName, String keysName);
40+
41+
/**
42+
* GPU0一般为默认GPU,参数选项:使用CPU(-1)/使用GPU0(0)/使用GPU1(1)/...
43+
*/
44+
public native void setGpuIndex(int gpuIndex);
45+
46+
public native String getVersion();
47+
48+
public native OcrResult detect(String input, int padding, int maxSideLen, float boxScoreThresh, float boxThresh, float unClipRatio, boolean doAngle, boolean mostAngle);
49+
50+
public OcrEngine() {
51+
try {
52+
StaticLog.info("java.library.path=" + System.getProperty("java.library.path"));
53+
System.loadLibrary("RapidOcrNcnn");
54+
} catch (Exception e) {
55+
e.printStackTrace();
11956
}
57+
this.padding = 15;
58+
this.boxScoreThresh = 0.25f;
59+
this.boxThresh = 0.3f;
60+
this.unClipRatio = 1.6f;
61+
this.doAngle = true;
62+
this.mostAngle = true;
63+
}
64+
65+
public int getPadding() {
66+
return this.padding;
67+
}
68+
69+
public void setPadding(int i) {
70+
this.padding = i;
71+
}
72+
73+
public float getBoxScoreThresh() {
74+
return this.boxScoreThresh;
75+
}
76+
77+
public void setBoxScoreThresh(float f) {
78+
this.boxScoreThresh = f;
79+
}
80+
81+
public float getBoxThresh() {
82+
return this.boxThresh;
83+
}
84+
85+
public void setBoxThresh(float f) {
86+
this.boxThresh = f;
87+
}
88+
89+
public float getUnClipRatio() {
90+
return this.unClipRatio;
91+
}
92+
93+
public void setUnClipRatio(float f) {
94+
this.unClipRatio = f;
95+
}
96+
97+
public boolean getDoAngle() {
98+
return this.doAngle;
99+
}
100+
101+
public void setDoAngle(boolean z) {
102+
this.doAngle = z;
103+
}
104+
105+
public boolean getMostAngle() {
106+
return this.mostAngle;
107+
}
108+
109+
public void setMostAngle(boolean z) {
110+
this.mostAngle = z;
111+
}
112+
113+
public OcrResult detect(String input) {
114+
return detect(input, 0);
115+
}
116+
117+
public OcrResult detect(String input, int maxSideLen) {
118+
return detect(input, this.padding, maxSideLen, this.boxScoreThresh, this.boxThresh, this.unClipRatio, this.doAngle, this.mostAngle);
119+
}
120120
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.luooqi.ocr.utils;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.lang.reflect.Field;
6+
7+
import lombok.extern.slf4j.Slf4j;
8+
9+
@Slf4j
10+
public class LibraryUtils {
11+
12+
public static void addLibary(String path) {
13+
File file = new File(path);
14+
String absolutePath = file.getAbsolutePath();
15+
log.info("add lib:{}",absolutePath);
16+
if (!file.exists()) {
17+
file.mkdirs();
18+
}
19+
try {
20+
addLibDir(absolutePath);
21+
} catch (IOException e1) {
22+
e1.printStackTrace();
23+
}
24+
}
25+
26+
public static void addLibDir(String s) throws IOException {
27+
try {
28+
Field field = ClassLoader.class.getDeclaredField("usr_paths");
29+
field.setAccessible(true);
30+
String[] paths = (String[]) field.get(null);
31+
for (int i = 0; i < paths.length; i++) {
32+
if (s.equals(paths[i])) {
33+
return;
34+
}
35+
}
36+
String[] tmp = new String[paths.length + 1];
37+
System.arraycopy(paths, 0, tmp, 0, paths.length);
38+
tmp[paths.length] = s;
39+
field.set(null, tmp);
40+
} catch (IllegalAccessException e) {
41+
throw new IOException("Failed to get permissions to set library path");
42+
} catch (NoSuchFieldException e) {
43+
throw new IOException("Failed to get field handle to set library path");
44+
}
45+
}
46+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.benjaminwan.ocrlibrary;
2+
3+
import cn.hutool.log.StaticLog;
4+
import com.luooqi.ocr.utils.LibraryUtils;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.*;
8+
9+
/**
10+
* Created by [email protected] on 10/11/2023_3:01 AM
11+
*/
12+
public class OcrEngineTest {
13+
14+
@Test
15+
public void test1() {
16+
// https://github.com/RapidAI/RapidOcrNcnnLibTest/tree/main/resource/models
17+
18+
String libPath = "D:\\lib\\ocr-lib\\win64\\bin";
19+
LibraryUtils.addLibary(libPath);
20+
21+
OcrEngine ocrEngine = new OcrEngine();
22+
StaticLog.info("version=" + ocrEngine.getVersion());
23+
ocrEngine.setNumThread(8);
24+
//------- init Logger -------
25+
ocrEngine.initLogger(true, false, false);
26+
//ocrEngine.enableResultText("");
27+
ocrEngine.setGpuIndex(-1);
28+
String modelsDir = "D:\\model\\ppocr-v3-NCNN-models";
29+
String detName = "ch_PP-OCRv3_det_infer";
30+
String clsName = "ch_ppocr_mobile_v2.0_cls_infer";
31+
String recName = "ch_PP-OCRv3_rec_infer";
32+
String keysName = "ppocr_keys_v1.txt";
33+
34+
boolean initModelsRet = ocrEngine.initModels(modelsDir, detName, clsName, recName, keysName);
35+
if (!initModelsRet) {
36+
StaticLog.error("Error in models initialization, please check the models/keys path!");
37+
return;
38+
}
39+
StaticLog.info("padding(%d) boxScoreThresh(%f) boxThresh(%f) unClipRatio(%f) doAngle(%b) mostAngle(%b)", ocrEngine.getPadding(), ocrEngine.getBoxScoreThresh(), ocrEngine.getBoxThresh(), ocrEngine.getUnClipRatio(), ocrEngine.getDoAngle(), ocrEngine.getMostAngle());
40+
41+
String imagePath = "D:\\images\\Snipaste_2023-10-11_02-08-03.png";
42+
OcrResult ocrResult = ocrEngine.detect(imagePath);
43+
System.out.println(ocrResult.getStrRes());
44+
45+
}
46+
47+
}

0 commit comments

Comments
 (0)