-
Notifications
You must be signed in to change notification settings - Fork 480
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
洛千
committed
Feb 8, 2023
1 parent
ae49a2d
commit f6cfa13
Showing
31 changed files
with
477 additions
and
2,094 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
src/main/java/com/benjaminwan/ocrlibrary/OcrEngine.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.benjaminwan.ocrlibrary; | ||
|
||
public abstract class OcrOutput { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.