Skip to content

Commit

Permalink
尝试本地OCR
Browse files Browse the repository at this point in the history
  • Loading branch information
洛千 committed Feb 8, 2023
1 parent ae49a2d commit f6cfa13
Show file tree
Hide file tree
Showing 31 changed files with 477 additions and 2,094 deletions.
Binary file added ocr/linux/lib/libRapidOcrNcnn.so
Binary file not shown.
Binary file added ocr/macos/lib/libRapidOcrNcnn.dylib
Binary file not shown.
Binary file added ocr/models/libRapidOcrNcnn.dylib
Binary file not shown.
Binary file added ocr/win32/bin/RapidOcrNcnn.dll
Binary file not shown.
Binary file added ocr/win32/lib/RapidOcrNcnn.lib
Binary file not shown.
Binary file added ocr/win64/bin/RapidOcrNcnn.dll
Binary file not shown.
Binary file added ocr/win64/lib/RapidOcrNcnn.lib
Binary file not shown.
77 changes: 31 additions & 46 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<version>${soft.version}</version>

<properties>
<soft.version>1.2.5</soft.version>
<soft.version>1.2.6</soft.version>
</properties>

<dependencies>
Expand All @@ -23,7 +23,7 @@
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.1.2</version>
<version>5.8.11</version>
</dependency>

<dependency>
Expand All @@ -32,58 +32,16 @@
<version>4.2</version>
</dependency>

<dependency>
<groupId>com.jfoenix</groupId>
<artifactId>jfoenix</artifactId>
<version>8.0.9</version>
</dependency>

<dependency>
<groupId>io.datafx</groupId>
<artifactId>datafx</artifactId>
<version>8.0.1</version>
</dependency>

<dependency>
<groupId>io.datafx</groupId>
<artifactId>flow</artifactId>
<version>8.0.1</version>
</dependency>

<dependency>
<groupId>org.kordamp.ikonli</groupId>
<artifactId>ikonli-javafx</artifactId>
<version>2.4.0</version>
</dependency>

<dependency>
<groupId>org.kordamp.ikonli</groupId>
<artifactId>ikonli-fontawesome5-pack</artifactId>
<version>2.4.0</version>
</dependency>



<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<version>4.13.2</version>
<scope>test</scope>
</dependency>

</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<encoding>UTF-8</encoding>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
Expand All @@ -109,7 +67,7 @@
<version>8.8.3</version>
<configuration>
<mainClass>com.luooqi.ocr.MainFm</mainClass>
<bundler>exe</bundler>
<bundler>dmg</bundler>
<jfxAppOutputDir>${project.build.directory}/app</jfxAppOutputDir>
<nativeOutputDir>${project.build.directory}/native</nativeOutputDir>
<appName>treehole</appName>
Expand All @@ -122,6 +80,33 @@
<installdirChooser>true</installdirChooser>
</bundleArguments>
<nativeReleaseVersion>${soft.version}</nativeReleaseVersion>
<additionalAppResources>${project.basedir}/ocr/models</additionalAppResources>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<configuration>
<encoding>UTF-8</encoding>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
Expand Down
120 changes: 120 additions & 0 deletions src/main/java/com/benjaminwan/ocrlibrary/OcrEngine.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package com.benjaminwan.ocrlibrary;

import cn.hutool.core.io.FileUtil;
import cn.hutool.log.StaticLog;

import java.io.File;
import java.nio.charset.Charset;

public final class OcrEngine {
/**
* 图像外接白框,用于提升识别率,文字框没有正确框住所有文字时,增加此值。
*/
private int padding;
/**
* 文字框置信度门限,文字框没有正确框住所有文字时,减小此值
*/
private float boxScoreThresh;

private float boxThresh;
/**
* 单个文字框大小倍率,越大时单个文字框越大
*/
private float unClipRatio;
/**
* 启用(1)/禁用(0) 文字方向检测,只有图片倒置的情况下(旋转90~270度的图片),才需要启用文字方向检测
*/
private boolean doAngle;
/**
* 启用(1)/禁用(0) 角度投票(整张图片以最大可能文字方向来识别),当禁用文字方向检测时,此项也不起作用
*/
private boolean mostAngle;

public native boolean setNumThread(int numThread);

public native void initLogger(boolean isConsole, boolean isPartImg, boolean isResultImg);

public native void enableResultText(String imagePath);

public native boolean initModels(String modelsDir, String detName, String clsName, String recName, String keysName);

/**
* GPU0一般为默认GPU,参数选项:使用CPU(-1)/使用GPU0(0)/使用GPU1(1)/...
*/
public native void setGpuIndex(int gpuIndex);

public native String getVersion();

public native OcrResult detect(String input, int padding, int maxSideLen, float boxScoreThresh, float boxThresh, float unClipRatio, boolean doAngle, boolean mostAngle);

public OcrEngine() {
try {
StaticLog.info("java.library.path=" + System.getProperty("java.library.path"));
System.loadLibrary("RapidOcrNcnn");
} catch (Exception e) {
e.printStackTrace();
}
this.padding = 15;
this.boxScoreThresh = 0.25f;
this.boxThresh = 0.3f;
this.unClipRatio = 1.6f;
this.doAngle = true;
this.mostAngle = true;
}

public int getPadding() {
return this.padding;
}

public void setPadding(int i) {
this.padding = i;
}

public float getBoxScoreThresh() {
return this.boxScoreThresh;
}

public void setBoxScoreThresh(float f) {
this.boxScoreThresh = f;
}

public float getBoxThresh() {
return this.boxThresh;
}

public void setBoxThresh(float f) {
this.boxThresh = f;
}

public float getUnClipRatio() {
return this.unClipRatio;
}

public void setUnClipRatio(float f) {
this.unClipRatio = f;
}

public boolean getDoAngle() {
return this.doAngle;
}

public void setDoAngle(boolean z) {
this.doAngle = z;
}

public boolean getMostAngle() {
return this.mostAngle;
}

public void setMostAngle(boolean z) {
this.mostAngle = z;
}

public OcrResult detect(String input) {
return detect(input, 0);
}

public OcrResult detect(String input, int maxSideLen) {
return detect(input, this.padding, maxSideLen, this.boxScoreThresh, this.boxThresh, this.unClipRatio, this.doAngle, this.mostAngle);
}
}
9 changes: 9 additions & 0 deletions src/main/java/com/benjaminwan/ocrlibrary/OcrFailed.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.benjaminwan.ocrlibrary;

public final class OcrFailed extends OcrOutput {
public static final OcrFailed INSTANCE = new OcrFailed();

private OcrFailed() {
super();
}
}
5 changes: 5 additions & 0 deletions src/main/java/com/benjaminwan/ocrlibrary/OcrOutput.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.benjaminwan.ocrlibrary;

public abstract class OcrOutput {

}
54 changes: 54 additions & 0 deletions src/main/java/com/benjaminwan/ocrlibrary/OcrResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.benjaminwan.ocrlibrary;

import java.util.ArrayList;

public final class OcrResult extends OcrOutput {
private final double dbNetTime;

private final ArrayList<TextBlock> textBlocks;
private double detectTime;

private String strRes;

public OcrResult copy(double dbNetTime, ArrayList<TextBlock> textBlocks, double detectTime, String strRes) {
return new OcrResult(dbNetTime, textBlocks, detectTime, strRes);
}

public String toString() {
return "OcrResult(dbNetTime=" + this.dbNetTime + ", textBlocks=" + this.textBlocks + ", detectTime=" + this.detectTime + ", strRes=" + this.strRes + ')';
}

public double getDbNetTime() {
return this.dbNetTime;
}


public ArrayList<TextBlock> getTextBlocks() {
return this.textBlocks;
}

public double getDetectTime() {
return this.detectTime;
}

public void setDetectTime(double d) {
this.detectTime = d;
}


public String getStrRes() {
return this.strRes;
}

public void setStrRes(String str) {
this.strRes = str;
}

public OcrResult(double dbNetTime, ArrayList<TextBlock> textBlocks, double detectTime, String strRes) {
super();
this.dbNetTime = dbNetTime;
this.textBlocks = textBlocks;
this.detectTime = detectTime;
this.strRes = strRes;
}
}
9 changes: 9 additions & 0 deletions src/main/java/com/benjaminwan/ocrlibrary/OcrStop.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.benjaminwan.ocrlibrary;

public final class OcrStop extends OcrOutput {
public static final OcrStop INSTANCE = new OcrStop();

private OcrStop() {
super();
}
}
51 changes: 51 additions & 0 deletions src/main/java/com/benjaminwan/ocrlibrary/Point.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.benjaminwan.ocrlibrary;

public final class Point {
private int x;
private int y;

public Point copy(int x, int y) {
return new Point(x, y);
}

public String toString() {
return "Point(x=" + this.x + ", y=" + this.y + ')';
}

public int hashCode() {
int result = Integer.hashCode(this.x);
return (result * 31) + Integer.hashCode(this.y);
}

public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof Point)) {
return false;
}
Point point = (Point) other;
return this.x == point.x && this.y == point.y;
}

public Point(int x, int y) {
this.x = x;
this.y = y;
}

public int getX() {
return this.x;
}

public void setX(int i) {
this.x = i;
}

public int getY() {
return this.y;
}

public void setY(int i) {
this.y = i;
}
}
Loading

0 comments on commit f6cfa13

Please sign in to comment.