├── .gitignore
├── README.md
├── java-ee
├── pom.xml
└── src
│ └── main
│ ├── java
│ └── io
│ │ └── github
│ │ └── mymonstercat
│ │ ├── ImageUtil.java
│ │ └── Main.java
│ └── resources
│ └── images
│ ├── 40.png
│ ├── system.png
│ └── test.png
├── pom.xml
└── spring-boot
├── pom.xml
└── src
└── main
├── java
└── io
│ └── github
│ └── mymonstercat
│ ├── OcrTestApplication.java
│ └── controller
│ └── OcrController.java
└── resources
├── application.yml
└── images
├── 40.png
├── system.png
└── test.png
/.gitignore:
--------------------------------------------------------------------------------
1 | target/
2 | !.mvn/wrapper/maven-wrapper.jar
3 | !**/src/main/**/target/
4 | !**/src/test/**/target/
5 |
6 | ### IntelliJ IDEA ###
7 | .idea/
8 | .idea/modules.xml
9 | .idea/jarRepositories.xml
10 | .idea/compiler.xml
11 | .idea/libraries/
12 | *.iws
13 | *.iml
14 | *.ipr
15 |
16 | ### Eclipse ###
17 | .apt_generated
18 | .classpath
19 | .factorypath
20 | .project
21 | .settings
22 | .springBeans
23 | .sts4-cache
24 |
25 | ### NetBeans ###
26 | /nbproject/private/
27 | /nbbuild/
28 | /dist/
29 | /nbdist/
30 | /.nb-gradle/
31 | build/
32 | !**/src/main/**/build/
33 | !**/src/test/**/build/
34 |
35 | ### VS Code ###
36 | .vscode/
37 |
38 | ### Mac OS ###
39 | .DS_Store
40 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # rapidcor-demo
2 |
3 | 本项目使用[RapidOcr-Java](https://github.com/MyMonsterCat/RapidOcr-Java),提供了JavaEE和SpringBoot两种方式的示例使用
--------------------------------------------------------------------------------
/java-ee/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | io.github.mymonstercat
8 | rapidocr-demo
9 | 1.0-SNAPSHOT
10 |
11 |
12 | java-ee
13 |
14 |
15 | 1.8
16 | 1.8
17 | UTF-8
18 |
19 |
20 |
21 |
22 | io.github.mymonstercat
23 | rapidocr
24 |
25 |
26 | io.github.mymonstercat
27 | rapidocr-onnx-platform
28 |
29 |
30 |
31 |
32 |
33 |
34 | junit
35 | junit
36 | 4.13.1
37 | compile
38 |
39 |
40 | cn.hutool
41 | hutool-core
42 | 5.8.20
43 |
44 |
45 | org.projectlombok
46 | lombok
47 | 1.18.28
48 | compile
49 |
50 |
51 | org.slf4j
52 | slf4j-simple
53 | 2.0.7
54 |
55 |
56 |
57 |
58 |
59 |
60 | org.apache.maven.plugins
61 | maven-shade-plugin
62 | 3.2.4
63 |
64 |
65 | package
66 |
67 | shade
68 |
69 |
70 |
71 |
72 | io.github.mymonstercat.Main
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
--------------------------------------------------------------------------------
/java-ee/src/main/java/io/github/mymonstercat/ImageUtil.java:
--------------------------------------------------------------------------------
1 | package io.github.mymonstercat;
2 |
3 | import cn.hutool.core.collection.CollUtil;
4 | import cn.hutool.core.img.ImgUtil;
5 | import cn.hutool.core.lang.Assert;
6 | import com.benjaminwan.ocrlibrary.Point;
7 | import com.benjaminwan.ocrlibrary.TextBlock;
8 | import lombok.extern.slf4j.Slf4j;
9 |
10 | import java.awt.*;
11 | import java.awt.image.BufferedImage;
12 | import java.io.*;
13 | import java.util.List;
14 |
15 |
16 | /**
17 | * 图片工具类
18 | * 参考自 ImageUtil
19 | */
20 | @Slf4j
21 | public class ImageUtil extends ImgUtil {
22 |
23 |
24 | /**
25 | * 绘制图片
26 | *
27 | * 根据传入的坐标点绘制矩形框,并在矩形框底下添加文字
28 | *
29 | *
30 | * @param stream 数据流
31 | * @param blockList 文本块列表
32 | * @return {@link OutputStream }
33 | * @author song_jx
34 | */
35 | public static ByteArrayOutputStream drawImg(InputStream stream, List blockList) {
36 | // 读取输入流中的图片
37 | BufferedImage image = read(stream);
38 |
39 | // 开启画笔绘制
40 | Graphics2D g2d = image.createGraphics();
41 | for (final TextBlock textBlock : blockList) {
42 | final List boxPoint = textBlock.getBoxPoint();
43 | final String text = textBlock.getText();
44 | // 1. 算出矩形框
45 | final Rectangle box = calcRectangle(boxPoint);
46 | final int x = box.x;
47 | final int y = box.y;
48 | final int width = box.width;
49 | final int height = box.height;
50 | // 2. 绘制矩形框
51 | g2d.setColor(Color.RED);
52 | g2d.drawRect(x, y, width, height);
53 | // 3. 在矩形框的左下角添加文字
54 | g2d.setColor(Color.BLACK);
55 | g2d.drawString(text, x, y + height + 15);
56 | }
57 | g2d.dispose();
58 |
59 | ByteArrayOutputStream os = new ByteArrayOutputStream();
60 | write(image, IMAGE_TYPE_PNG, os);
61 | return os;
62 | }
63 |
64 | /**
65 | * 绘制图片
66 | *
67 | * 根据传入的坐标点绘制矩形框,并在矩形框底下添加文字
68 | *
69 | *
70 | * @param imageFile 图片文件
71 | * @param blockList 文本块列表
72 | * @return {@link OutputStream }
73 | * @author song_jx
74 | */
75 | public static ByteArrayOutputStream drawImg(File imageFile, List blockList) {
76 | ByteArrayOutputStream os = null;
77 | try (InputStream is = new FileInputStream(imageFile)) {
78 | os = drawImg(is, blockList);
79 | } catch (Exception e) {
80 | log.error("图片绘制异常", e);
81 | }
82 | return os;
83 | }
84 |
85 | /**
86 | * 计算矩形框
87 | *
88 | * 根据传入的4个坐标点,得出矩形框的左上角及长宽
89 | *
90 | * @param pointList 点列表
91 | * @return {@link Rectangle }
92 | * @author song_jx
93 | */
94 | private static Rectangle calcRectangle(List pointList) {
95 | Assert.isFalse(CollUtil.isEmpty(pointList) || pointList.size() != 4, "需要4个点来构成矩形");
96 | int minX = Integer.MAX_VALUE;
97 | int minY = Integer.MAX_VALUE;
98 | int maxX = Integer.MIN_VALUE;
99 | int maxY = Integer.MIN_VALUE;
100 | // 找到最小和最大的 x、y 坐标
101 | for (Point point : pointList) {
102 | final int x = point.getX();
103 | final int y = point.getY();
104 | if (x < minX) {
105 | minX = x;
106 | }
107 | if (y < minY) {
108 | minY = y;
109 | }
110 | if (x > maxX) {
111 | maxX = x;
112 | }
113 | if (y > maxY) {
114 | maxY = y;
115 | }
116 | }
117 | // 矩形的左上角坐标即是最小x、y
118 | final int width = maxX - minX;
119 | final int height = maxY - minY;
120 | return new Rectangle(minX, minY, width, height);
121 | }
122 |
123 |
124 |
125 | }
126 |
--------------------------------------------------------------------------------
/java-ee/src/main/java/io/github/mymonstercat/Main.java:
--------------------------------------------------------------------------------
1 | package io.github.mymonstercat;
2 |
3 | import cn.hutool.core.io.FileUtil;
4 | import cn.hutool.core.io.IoUtil;
5 | import cn.hutool.core.io.resource.ResourceUtil;
6 | import com.benjaminwan.ocrlibrary.OcrResult;
7 | import com.benjaminwan.ocrlibrary.TextBlock;
8 | import io.github.mymonstercat.ocr.InferenceEngine;
9 | import io.github.mymonstercat.ocr.config.HardwareConfig;
10 | import io.github.mymonstercat.ocr.config.ParamConfig;
11 | import org.junit.Assert;
12 | import org.junit.Test;
13 |
14 | import java.io.ByteArrayInputStream;
15 | import java.io.File;
16 | import java.io.InputStream;
17 | import java.util.ArrayList;
18 |
19 | /**
20 | * @author Monster
21 | */
22 | public class Main {
23 | public static void main(String[] args) {
24 | ParamConfig paramConfig = ParamConfig.getDefaultConfig();
25 | paramConfig.setDoAngle(true);
26 | paramConfig.setMostAngle(true);
27 | InferenceEngine engine = InferenceEngine.getInstance(Model.ONNX_PPOCR_V3);
28 | String savePath = save("/images/system.png");
29 | OcrResult ocrResult = engine.runOcr(savePath, paramConfig);
30 | System.out.println(ocrResult.getStrRes().trim());
31 | }
32 |
33 | @Test
34 | public void NcnnTest() {
35 | InferenceEngine engine = InferenceEngine.getInstance(Model.NCNN_PPOCR_V3);
36 | // 使用NCNN引擎进行识别
37 | OcrResult NCNNResult = engine.runOcr(getResourcePath("/images/40.png"));
38 | Assert.assertEquals("40", NCNNResult.getStrRes().trim().toString());
39 | }
40 |
41 | @Test
42 | public void OnnxTest() {
43 | String imgPath = getResourcePath("/images/40.png");
44 | InferenceEngine engine = InferenceEngine.getInstance(Model.ONNX_PPOCR_V3);
45 | OcrResult ONNXResult = engine.runOcr(imgPath);
46 | Assert.assertEquals("40", ONNXResult.getStrRes().trim().toString());
47 | }
48 |
49 | @Test
50 | public void OnnxDrawTest() {
51 | InferenceEngine engine = InferenceEngine.getInstance(Model.ONNX_PPOCR_V4);
52 | String imgPath = getResourcePath("/images/system.png");
53 | String drawPath = imgPath.replace("40", "40-draw");
54 | File drawFile = new File(drawPath);
55 | // 使用ONNX推理引擎进行识别
56 | // 配置参数
57 | ParamConfig paramConfig = ParamConfig.getDefaultConfig();
58 | paramConfig.setDoAngle(true);
59 | paramConfig.setMostAngle(true);
60 | // 开始识别
61 | OcrResult ONNXResult = engine.runOcr(imgPath, paramConfig);
62 | // 绘制推理结果
63 | ArrayList textBlocks = ONNXResult.getTextBlocks();
64 | FileUtil.copy(imgPath, drawPath, Boolean.TRUE);
65 | ByteArrayInputStream in = IoUtil.toStream(ImageUtil.drawImg(drawFile, textBlocks));
66 | FileUtil.writeFromStream(in, drawFile);
67 | System.out.println("文件保存在: " + drawPath);
68 | Assert.assertEquals("System", ONNXResult.getStrRes().trim().toString());
69 | }
70 |
71 |
72 | @Test
73 | public void paramTest() {
74 | // 配置可变参数
75 | ParamConfig paramConfig = ParamConfig.getDefaultConfig();
76 | paramConfig.setDoAngle(true);
77 | paramConfig.setMostAngle(true);
78 | InferenceEngine engine = InferenceEngine.getInstance(Model.ONNX_PPOCR_V3);
79 | // 开始识别
80 | OcrResult ocrResult = engine.runOcr(getResourcePath("/images/test.png"), paramConfig);
81 | System.out.println(ocrResult);
82 | }
83 |
84 | @Test
85 | public void hardWareTest() {
86 | // 配置可变参数
87 | ParamConfig paramConfig = ParamConfig.getDefaultConfig();
88 | paramConfig.setDoAngle(true);
89 | paramConfig.setMostAngle(true);
90 | // 配置硬件参数:4核CPU,使用GPU0
91 | HardwareConfig hardwareConfig = new HardwareConfig(4, -1);
92 | InferenceEngine engine = InferenceEngine.getInstance(Model.ONNX_PPOCR_V4, hardwareConfig);
93 | // 开始识别
94 | OcrResult ocrResult = engine.runOcr(getResourcePath("/images/test.png"), paramConfig);
95 | System.out.println(ocrResult);
96 | }
97 |
98 | @Test
99 | public void repeatOcr() {
100 | InferenceEngine engine = InferenceEngine.getInstance(Model.ONNX_PPOCR_V3);
101 | String real = "40";
102 | System.out.println("ONNX 1>>>>>>>> ");
103 | OcrResult NCNN_1 = engine.runOcr(getResourcePath("/images/40.png"));
104 | Assert.assertEquals(real, NCNN_1.getStrRes().trim().toString());
105 |
106 | System.out.println("ONNX 2>>>>>>>> ");
107 | OcrResult NCNN_2 = engine.runOcr(getResourcePath("/images/40.png"));
108 | Assert.assertEquals(real, NCNN_2.getStrRes().trim().toString());
109 |
110 | System.out.println("ONNX 3>>>>>>>> ");
111 | OcrResult NCNN_3 = engine.runOcr(getResourcePath("/images/40.png"));
112 | Assert.assertEquals(real, NCNN_3.getStrRes().trim().toString());
113 |
114 | System.out.println("ONNX 4>>>>>>>> ");
115 | OcrResult NCNN_4 = engine.runOcr(getResourcePath("/images/system.png"));
116 | Assert.assertEquals("System", NCNN_4.getStrRes().trim().toString());
117 |
118 | System.out.println("ONNX 5>>>>>>>> ");
119 | OcrResult NCNN_5 = engine.runOcr(getResourcePath("/images/40.png"));
120 | Assert.assertEquals(real, NCNN_5.getStrRes().trim().toString());
121 | }
122 |
123 | private static String getResourcePath(String path) {
124 | return new File(Main.class.getResource(path).getFile()).toString();
125 | }
126 |
127 | private static String save(String path) {
128 | String tempDir = System.getProperty("java.io.tmpdir");
129 | String result = tempDir + "ocrJava/img.png";
130 | InputStream stream = Main.class.getResourceAsStream(path);
131 | File file = FileUtil.writeFromStream(stream, result);
132 | file.deleteOnExit();
133 | return result;
134 | }
135 | }
136 |
--------------------------------------------------------------------------------
/java-ee/src/main/resources/images/40.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MyMonsterCat/rapidocr-demo/269acd98155b41e62380d82240472c7a7153d4fe/java-ee/src/main/resources/images/40.png
--------------------------------------------------------------------------------
/java-ee/src/main/resources/images/system.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MyMonsterCat/rapidocr-demo/269acd98155b41e62380d82240472c7a7153d4fe/java-ee/src/main/resources/images/system.png
--------------------------------------------------------------------------------
/java-ee/src/main/resources/images/test.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MyMonsterCat/rapidocr-demo/269acd98155b41e62380d82240472c7a7153d4fe/java-ee/src/main/resources/images/test.png
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | io.github.mymonstercat
8 | rapidocr-demo
9 | 1.0-SNAPSHOT
10 | pom
11 |
12 | java-ee
13 | spring-boot
14 |
15 |
16 |
17 | 1.8
18 | 1.8
19 | UTF-8
20 |
21 |
22 |
23 |
24 |
25 | linux-x86_64
26 |
27 |
28 | unix
29 | amd64
30 |
31 |
32 |
33 |
34 | io.github.mymonstercat
35 | rapidocr-onnx-linux-x86_64
36 | 1.2.2
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 | io.github.mymonstercat
46 | rapidocr
47 | 0.0.7
48 |
49 |
50 | io.github.mymonstercat
51 | rapidocr-ncnn-platform
52 | 0.0.7
53 |
54 |
55 | io.github.mymonstercat
56 | rapidocr-onnx-platform
57 | 0.0.7
58 |
59 |
60 |
61 |
62 |
63 |
64 |
--------------------------------------------------------------------------------
/spring-boot/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | io.github.mymonstercat
8 | rapidocr-demo
9 | 1.0-SNAPSHOT
10 |
11 |
12 | spring-boot
13 |
14 |
15 | 1.8
16 | 1.8
17 | UTF-8
18 |
19 |
20 |
21 |
22 | org.springframework.boot
23 | spring-boot-starter-web
24 | 2.7.14
25 |
26 |
27 |
28 | org.projectlombok
29 | lombok
30 | 1.18.30
31 | provided
32 |
33 |
34 |
35 |
36 | io.github.mymonstercat
37 | rapidocr
38 |
39 |
40 | slf4j-api
41 | org.slf4j
42 |
43 |
44 |
45 |
46 | io.github.mymonstercat
47 | rapidocr-onnx-platform
48 |
49 |
50 |
51 |
52 |
53 |
54 | org.apache.maven.plugins
55 | maven-shade-plugin
56 | 3.2.4
57 |
58 |
59 | package
60 |
61 | shade
62 |
63 |
64 |
65 |
66 | io.github.mymonstercat.OcrTestApplication
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
--------------------------------------------------------------------------------
/spring-boot/src/main/java/io/github/mymonstercat/OcrTestApplication.java:
--------------------------------------------------------------------------------
1 | package io.github.mymonstercat;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | /**
7 | * @author Monster
8 | */
9 | @SpringBootApplication()
10 | public class OcrTestApplication {
11 |
12 |
13 | public static void main(String[] args) {
14 | SpringApplication.run(OcrTestApplication.class, args);
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/spring-boot/src/main/java/io/github/mymonstercat/controller/OcrController.java:
--------------------------------------------------------------------------------
1 | package io.github.mymonstercat.controller;
2 |
3 | import com.benjaminwan.ocrlibrary.OcrResult;
4 | import io.github.mymonstercat.Model;
5 | import io.github.mymonstercat.ocr.InferenceEngine;
6 | import io.github.mymonstercat.ocr.config.ParamConfig;
7 | import lombok.SneakyThrows;
8 | import org.springframework.web.bind.annotation.*;
9 | import org.springframework.web.multipart.MultipartFile;
10 |
11 | import java.io.File;
12 | import java.io.IOException;
13 |
14 |
15 | /**
16 | * @author Monster
17 | */
18 | @RestController
19 | @RequestMapping("/ocr")
20 | public class OcrController {
21 |
22 | @GetMapping()
23 | public String ocr() {
24 | ParamConfig paramConfig = ParamConfig.getDefaultConfig();
25 | paramConfig.setDoAngle(true);
26 | paramConfig.setMostAngle(true);
27 | InferenceEngine engine = InferenceEngine.getInstance(Model.ONNX_PPOCR_V3);
28 | // 开始识别
29 | OcrResult ocrResult = engine.runOcr(getResourcePath("images/test.png"), paramConfig);
30 | return ocrResult.getStrRes().toString();
31 | }
32 |
33 | @PostMapping
34 | public String ocr(@RequestParam("file") MultipartFile fileUpload) throws IOException {
35 | ParamConfig paramConfig = ParamConfig.getDefaultConfig();
36 | paramConfig.setDoAngle(true);
37 | paramConfig.setMostAngle(true);
38 | InferenceEngine engine = InferenceEngine.getInstance(Model.ONNX_PPOCR_V3);
39 | File file = new File(System.getProperty("java.io.tmpdir") + "ocrJava/test.png");
40 | fileUpload.transferTo(file);
41 | file.deleteOnExit();
42 |
43 | OcrResult ocrResult = engine.runOcr(file.getPath(),paramConfig);
44 | return ocrResult.getStrRes().toString();
45 | }
46 |
47 |
48 | @SneakyThrows
49 | private static String getResourcePath(String path) {
50 | return Thread.currentThread().getContextClassLoader().getResource(path).getPath();
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/spring-boot/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | server:
2 | port: 18080
3 |
4 |
5 | # 日志配置
6 | logging:
7 | level:
8 | io.github.mymonstercat.*: debug
9 |
--------------------------------------------------------------------------------
/spring-boot/src/main/resources/images/40.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MyMonsterCat/rapidocr-demo/269acd98155b41e62380d82240472c7a7153d4fe/spring-boot/src/main/resources/images/40.png
--------------------------------------------------------------------------------
/spring-boot/src/main/resources/images/system.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MyMonsterCat/rapidocr-demo/269acd98155b41e62380d82240472c7a7153d4fe/spring-boot/src/main/resources/images/system.png
--------------------------------------------------------------------------------
/spring-boot/src/main/resources/images/test.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MyMonsterCat/rapidocr-demo/269acd98155b41e62380d82240472c7a7153d4fe/spring-boot/src/main/resources/images/test.png
--------------------------------------------------------------------------------