|
@@ -0,0 +1,69 @@
|
|
|
|
|
+package space.anyi;
|
|
|
|
|
+
|
|
|
|
|
+import com.benjaminwan.ocrlibrary.OcrInput;
|
|
|
|
|
+import com.benjaminwan.ocrlibrary.OcrResult;
|
|
|
|
|
+import io.github.mymonstercat.Model;
|
|
|
|
|
+import io.github.mymonstercat.ocr.InferenceEngine;
|
|
|
|
|
+import io.github.mymonstercat.ocr.config.ParamConfig;
|
|
|
|
|
+import org.junit.jupiter.api.Assertions;
|
|
|
|
|
+import org.junit.jupiter.api.BeforeAll;
|
|
|
|
|
+import org.junit.jupiter.params.ParameterizedTest;
|
|
|
|
|
+import org.junit.jupiter.params.provider.CsvSource;
|
|
|
|
|
+
|
|
|
|
|
+import java.io.File;
|
|
|
|
|
+import java.net.MalformedURLException;
|
|
|
|
|
+import java.net.URI;
|
|
|
|
|
+import java.net.URISyntaxException;
|
|
|
|
|
+import java.net.URL;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * @fileName: RapidOCRTest
|
|
|
|
|
+ * @projectName: amount-verification
|
|
|
|
|
+ * @package: space.anyi
|
|
|
|
|
+ * @author: 杨逸
|
|
|
|
|
+ * @date:2026/4/23 15:35
|
|
|
|
|
+ * @description:
|
|
|
|
|
+ */
|
|
|
|
|
+
|
|
|
|
|
+public class RapidOCRTest {
|
|
|
|
|
+ private static InferenceEngine engine;
|
|
|
|
|
+ @BeforeAll
|
|
|
|
|
+ static void init(){
|
|
|
|
|
+ System.setProperty("java.io.tmpdir", System.getenv("user.dir") + "\\temp");
|
|
|
|
|
+ engine = InferenceEngine.getInstance(Model.ONNX_PPOCR_V4);
|
|
|
|
|
+ }
|
|
|
|
|
+ //参数测试,输入"imgs/a.jpg",预期输出"逸"
|
|
|
|
|
+ @CsvSource({"imgs/a.jpg,逸"})
|
|
|
|
|
+ @ParameterizedTest()
|
|
|
|
|
+ void test(String fileName, String expected) {
|
|
|
|
|
+ String result;
|
|
|
|
|
+ try {
|
|
|
|
|
+ //获取资源路径下的图片文件
|
|
|
|
|
+ URI uri = Thread.currentThread().getContextClassLoader().getResource(fileName).toURI();
|
|
|
|
|
+ String imgUrl = new File(uri).getAbsolutePath();
|
|
|
|
|
+ OcrResult ocrResult = engine.runOcr(imgUrl);
|
|
|
|
|
+ System.out.println(result=ocrResult.getStrRes().trim());
|
|
|
|
|
+ } catch (URISyntaxException e) {
|
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
|
+ }
|
|
|
|
|
+ Assertions.assertEquals(expected, result);
|
|
|
|
|
+ }
|
|
|
|
|
+ @CsvSource({"https://blog.anyi.space/upload/a.jpg,逸"})
|
|
|
|
|
+ @ParameterizedTest
|
|
|
|
|
+ void testByteData(String url, String expected) {
|
|
|
|
|
+ String result;
|
|
|
|
|
+ try {
|
|
|
|
|
+ byte[] bytes = new URL(url).getFile().getBytes();
|
|
|
|
|
+ ParamConfig paramConfig = ParamConfig.getDefaultConfig();
|
|
|
|
|
+ paramConfig.setDoAngle(true);
|
|
|
|
|
+ paramConfig.setMostAngle(true);
|
|
|
|
|
+ OcrInput ocrInput = new OcrInput(bytes);
|
|
|
|
|
+ OcrResult ocrResult = engine.runOcr(ocrInput, paramConfig);
|
|
|
|
|
+ result = ocrResult.getStrRes().trim();
|
|
|
|
|
+ } catch (MalformedURLException e) {
|
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
|
+ }
|
|
|
|
|
+ Assertions.assertEquals(expected, result);
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+}
|