Quellcode durchsuchen

# feat:修改使用的OCR模型;
- 从tessdata替换为RapidOCR

yang yi vor 6 Monaten
Ursprung
Commit
64d1b14c11
2 geänderte Dateien mit 44 neuen und 34 gelöschten Zeilen
  1. 21 0
      pom.xml
  2. 23 34
      src/main/java/space/anyi/amountVerification/App.java

+ 21 - 0
pom.xml

@@ -67,6 +67,27 @@
             <artifactId>logback-core</artifactId>
             <version>1.4.14</version>
         </dependency>
+        <!--  rapidocr这个必须要引入     -->
+        <!--  可前往maven中央仓库https://central.sonatype.com/artifact/io.github.mymonstercat/rapidocr/versions,查看版本      -->
+        <dependency>
+            <groupId>space.anyi</groupId>
+            <artifactId>rapidocr</artifactId>
+            <version>0.0.7</version>
+        </dependency>
+
+        <!--  一般只需要引入一个,CPU端建议使用onnx,移动端建议使用ncnn     -->
+        <!--  可前往maven中央仓库https://central.sonatype.com/artifact/io.github.mymonstercat/rapidocr-onnx-platform/versions,查看版本      -->
+        <dependency>
+            <groupId>space.anyi</groupId>
+            <artifactId>rapidocr-onnx-platform</artifactId>
+            <version>0.0.7</version>
+        </dependency>
+
+        <!--<dependency>-->
+        <!--    <groupId>io.github.mymonstercat</groupId>-->
+        <!--    <artifactId>rapidocr-ncnn-platform</artifactId>-->
+        <!--    <version>0.0.7</version>-->
+        <!--</dependency>-->
     </dependencies>
 
     <properties>

+ 23 - 34
src/main/java/space/anyi/amountVerification/App.java

@@ -4,6 +4,8 @@ import com.alibaba.excel.EasyExcel;
 import com.alibaba.excel.ExcelWriter;
 import com.alibaba.excel.read.listener.PageReadListener;
 import com.alibaba.excel.write.metadata.WriteSheet;
+import io.github.mymonstercat.Model;
+import io.github.mymonstercat.ocr.InferenceEngine;
 import javafx.application.Application;
 import javafx.application.Platform;
 import javafx.geometry.Insets;
@@ -17,8 +19,6 @@ import javafx.scene.layout.VBox;
 import javafx.stage.DirectoryChooser;
 import javafx.stage.FileChooser;
 import javafx.stage.Stage;
-import net.sourceforge.tess4j.Tesseract;
-import net.sourceforge.tess4j.TesseractException;
 import org.apache.pdfbox.Loader;
 import org.apache.pdfbox.pdmodel.PDDocument;
 import org.apache.pdfbox.text.PDFTextStripper;
@@ -28,7 +28,6 @@ import space.anyi.amountVerification.model.InputModel;
 import space.anyi.amountVerification.model.OutputErrorModel;
 import space.anyi.amountVerification.model.OutputModel;
 
-import javax.imageio.ImageIO;
 import java.awt.image.BufferedImage;
 import java.io.File;
 import java.io.IOException;
@@ -95,34 +94,22 @@ public class App extends Application {
      * 匹配金额使用的正则表达式对象
      * @see Pattern
      */
-    private Pattern pattern;
-    {
-        /**
-         * 代码快初始化匹配金额的正则表达式对象
-         */
-        //String regex = "理赔金额合计:¥(?<money>\\d+\\.\\d{2})";
-        String regex = "理赔金额合计[^\\d]{1,2}[¥¥#Y半]?(?<money>\\d+\\.\\d{2})";
-        this.pattern = Pattern.compile(regex);
-    }
+    private final Pattern pattern;
 
     /**
      * OCR使用的对象
-     * @see Tesseract
+     * @see InferenceEngine
      */
-    private Tesseract tesseract;
-    {
-        /**
-         * 代码块初始化OCR使用的对象
-         */
-        this.tesseract = new Tesseract();
-        String workDir = System.getProperty("user.dir") + "\\tessdata";
-        System.setProperty("TESSDATA_PREFIX",workDir);
-        //this.tesseract.setDatapath(workDir);
-        log.info("tessdata路径:{}",workDir);
-        //this.tesseract.setDatapath("D:\\tools\\tessdata\\fast");
-        this.tesseract.setDatapath("tessdata/");
-        //设置识别语言为中文简体,(如果要设置为英文可改为"eng")
-        this.tesseract.setLanguage("chi_sim");
+    private final InferenceEngine engineOCR;
+
+    public App() {
+        //配置临时目录,用于存放OCR模型
+        System.setProperty("java.io.tmpdir", System.getenv("user.dir") + "\\temp");
+        //初始化OCR使用的对象
+        this.engineOCR = InferenceEngine.getInstance(Model.ONNX_PPOCR_V4);
+        //初始化匹配金额的正则表达式对象
+        String regex = "理赔金额合计[^\\d]{1,2}[¥¥#Y半]?(?<money>\\d+\\.\\d{2})";
+        this.pattern = Pattern.compile(regex);
     }
 
     @Override
@@ -287,10 +274,10 @@ public class App extends Application {
 
     private void startVerification() {
         //如果当前时间大于2025-11-28,则关闭程序
-        if (System.currentTimeMillis() > 1764341281000L) {
-            new Alert(Alert.AlertType.ERROR, "软件已过期,请联系作者获取最新版本").showAndWait();
-            Platform.exit();
-        }
+        //if (System.currentTimeMillis() > 1764341281000L) {
+        //    new Alert(Alert.AlertType.ERROR, "软件已过期,请联系作者获取最新版本").showAndWait();
+        //    Platform.exit();
+        //}
         startButton.setDisable(true);
         String filePath = fileField.getText();
         String folderPath = folderField.getText();
@@ -462,12 +449,14 @@ public class App extends Application {
         //OCR识别
         BufferedImage bufferedImage = null;
         try {
+            //使用RapidOCR进行识别
+            text = engineOCR.runOcr(workOrderFile.getAbsolutePath()).getStrRes().trim();
             //开始识别整张图片中的文字
-            bufferedImage = ImageIO.read(workOrderFile);
+            //bufferedImage = ImageIO.read(workOrderFile);
             //直接处理二进制数据,避免因为文件扩展名错误导致的处理失败
-            text = tesseract.doOCR(bufferedImage);
+            //text = tesseract.doOCR(bufferedImage);
             log.debug("OCR解析{}到{}",workOrderFile.getAbsolutePath(),text);
-        } catch (TesseractException | IOException e) {
+        } catch (Exception e) {
             log.warn("OCR解析{}失败,case:{}",workOrderFile.getAbsolutePath(),e.getMessage());
             e.printStackTrace();
         } finally {