|
|
@@ -1,16 +1,18 @@
|
|
|
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 io.github.hzkitty.RapidOCR;
|
|
|
+import io.github.hzkitty.entity.OcrConfig;
|
|
|
+import io.github.hzkitty.entity.OcrResult;
|
|
|
+import io.github.hzkitty.entity.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.ByteArrayOutputStream;
|
|
|
import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
import java.net.MalformedURLException;
|
|
|
import java.net.URI;
|
|
|
import java.net.URISyntaxException;
|
|
|
@@ -26,11 +28,14 @@ import java.net.URL;
|
|
|
*/
|
|
|
|
|
|
public class RapidOCRTest {
|
|
|
- private static InferenceEngine engine;
|
|
|
+ private static RapidOCR rapidOCR;
|
|
|
@BeforeAll
|
|
|
static void init(){
|
|
|
- System.setProperty("java.io.tmpdir", System.getenv("user.dir") + "\\temp");
|
|
|
- engine = InferenceEngine.getInstance(Model.ONNX_PPOCR_V4);
|
|
|
+ OcrConfig ocrConfig = new OcrConfig();
|
|
|
+ // 手动设置关键参数(适配32x32图片)
|
|
|
+ ocrConfig.Det.setLimitSideLen(32);
|
|
|
+
|
|
|
+ rapidOCR = new RapidOCR(ocrConfig);
|
|
|
}
|
|
|
//参数测试,输入"imgs/a.jpg",预期输出"逸"
|
|
|
@CsvSource({"imgs/a.jpg,逸"})
|
|
|
@@ -41,10 +46,12 @@ public class RapidOCRTest {
|
|
|
//获取资源路径下的图片文件
|
|
|
URI uri = Thread.currentThread().getContextClassLoader().getResource(fileName).toURI();
|
|
|
String imgUrl = new File(uri).getAbsolutePath();
|
|
|
- OcrResult ocrResult = engine.runOcr(imgUrl);
|
|
|
+ OcrResult ocrResult = rapidOCR.run(imgUrl);
|
|
|
System.out.println(result=ocrResult.getStrRes().trim());
|
|
|
} catch (URISyntaxException e) {
|
|
|
throw new RuntimeException(e);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
}
|
|
|
Assertions.assertEquals(expected, result);
|
|
|
}
|
|
|
@@ -53,17 +60,25 @@ public class RapidOCRTest {
|
|
|
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);
|
|
|
+ byte[] bytes = urlToBytes(url);
|
|
|
+ OcrResult ocrResult = rapidOCR.run(bytes, new ParamConfig());
|
|
|
result = ocrResult.getStrRes().trim();
|
|
|
- } catch (MalformedURLException e) {
|
|
|
+ } catch (Exception e) {
|
|
|
throw new RuntimeException(e);
|
|
|
}
|
|
|
Assertions.assertEquals(expected, result);
|
|
|
|
|
|
}
|
|
|
+ private byte[] urlToBytes(String urlStr) throws IOException {
|
|
|
+ URL url = new URL(urlStr);
|
|
|
+ try (InputStream in = url.openStream();
|
|
|
+ ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
|
|
|
+ byte[] buffer = new byte[4096];
|
|
|
+ int len;
|
|
|
+ while ((len = in.read(buffer)) != -1) {
|
|
|
+ bos.write(buffer, 0, len);
|
|
|
+ }
|
|
|
+ return bos.toByteArray();
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|