generated from project-mirai/mirai-console-plugin-template
-
-
Notifications
You must be signed in to change notification settings - Fork 8
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
Showing
25 changed files
with
1,138 additions
and
14 deletions.
There are no files selected for viewing
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
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
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,70 @@ | ||
package com.benjaminwan.ocrlibrary; | ||
|
||
import io.github.mymonstercat.Model; | ||
import io.github.mymonstercat.ocr.config.HardwareConfig; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.util.Objects; | ||
|
||
/** | ||
* OCR engine object that interacts with library files through JNI. | ||
*/ | ||
@Slf4j | ||
public class OcrEngine { | ||
|
||
public void initEngine(Model model, HardwareConfig hardwareConfig) { | ||
if (!isInit) { | ||
synchronized (this) { | ||
if (!isInit) { | ||
initLogger(false, false, false); | ||
setNumThread(hardwareConfig.getNumThread()); | ||
if (hardwareConfig.getGpuIndex() != -1) { | ||
setGpuIndex(hardwareConfig.getGpuIndex()); | ||
} | ||
if (!initModels(model.getTempDirPath(), model.getDetName(), model.getClsName(), model.getRecName(), model.getKeysName())) { | ||
log.error("Model initialization error, please check the models path! Model: {}", model); | ||
throw new IllegalArgumentException("Model initialization error, please check the models/keys path!"); | ||
} | ||
inferType = model.getModelType(); | ||
log.info("Inference engine initialized successfully, currently using inference engine: {}-v{}", inferType, getVersion()); | ||
log.info("Model configuration during initialization: {}, Hardware configuration: {}", model, hardwareConfig); | ||
isInit = true; | ||
} | ||
} | ||
} else { | ||
if (!Objects.equals(model.getModelType(), inferType)) { | ||
log.warn("Engine has been initialized already; switching engines post-initialization is not supported, continuing to use {} inference engine", inferType); | ||
} else { | ||
log.info("Currently using inference engine: {}-v{}", inferType, getVersion()); | ||
} | ||
} | ||
} | ||
|
||
private volatile boolean isInit = false; | ||
private String inferType; | ||
|
||
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); | ||
|
||
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 native OcrResult detectInput( | ||
OcrInput input, int padding, int maxSideLen, | ||
float boxScoreThresh, float boxThresh, | ||
float unClipRatio, boolean doAngle, boolean 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,61 @@ | ||
package com.benjaminwan.ocrlibrary; | ||
|
||
/** | ||
* 支持传入bitmap二进制数据和传输的字节数据图片 | ||
*/ | ||
public class OcrInput { | ||
/** | ||
* 文件或者bitmap字节数据 | ||
*/ | ||
private final byte[] data; | ||
/** | ||
* 0 bitmap 1 bytes | ||
*/ | ||
private final int type; | ||
/** | ||
* 通道数 图片通道bitmap需要传递具体的通道数,file bytes 只需要传入1或者>1表示灰度或者彩色 | ||
*/ | ||
private final int channels; | ||
/** | ||
* 图片宽度 | ||
*/ | ||
private int width; | ||
/** | ||
* 图片高度 | ||
*/ | ||
private int height; | ||
|
||
public OcrInput(byte[] data, int channels, int width, int height) { | ||
this.data = data; | ||
this.type = 0; | ||
this.channels = channels; | ||
this.width = width; | ||
this.height = height; | ||
} | ||
|
||
public OcrInput(byte[] data) { | ||
this.data = data; | ||
this.type = 1; | ||
this.channels = 4; | ||
} | ||
|
||
public byte[] getData() { | ||
return data; | ||
} | ||
|
||
public int getType() { | ||
return type; | ||
} | ||
|
||
public int getChannels() { | ||
return channels; | ||
} | ||
|
||
public int getWidth() { | ||
return width; | ||
} | ||
|
||
public int getHeight() { | ||
return height; | ||
} | ||
} |
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 interface 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,51 @@ | ||
package com.benjaminwan.ocrlibrary; | ||
|
||
import java.util.ArrayList; | ||
|
||
public class OcrResult implements OcrOutput { | ||
private final double dbNetTime; | ||
private final ArrayList<TextBlock> textBlocks; | ||
private double detectTime; | ||
private String strRes; | ||
|
||
public OcrResult(double dbNetTime, ArrayList<TextBlock> textBlocks, double detectTime, String strRes) { | ||
this.dbNetTime = dbNetTime; | ||
this.textBlocks = textBlocks; | ||
this.detectTime = detectTime; | ||
this.strRes = strRes; | ||
} | ||
|
||
public double getDbNetTime() { | ||
return dbNetTime; | ||
} | ||
|
||
public ArrayList<TextBlock> getTextBlocks() { | ||
return textBlocks; | ||
} | ||
|
||
public double getDetectTime() { | ||
return detectTime; | ||
} | ||
|
||
public void setDetectTime(double detectTime) { | ||
this.detectTime = detectTime; | ||
} | ||
|
||
public String getStrRes() { | ||
return strRes; | ||
} | ||
|
||
public void setStrRes(String strRes) { | ||
this.strRes = strRes; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "OcrResult{" + | ||
"dbNetTime=" + dbNetTime + | ||
", textBlocks=" + textBlocks + | ||
", detectTime=" + detectTime + | ||
", 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,50 @@ | ||
package com.benjaminwan.ocrlibrary; | ||
|
||
import java.util.Objects; | ||
|
||
public class Point { | ||
private int x; | ||
private int y; | ||
|
||
public Point(int x, int y) { | ||
this.x = x; | ||
this.y = y; | ||
} | ||
|
||
public int getX() { | ||
return x; | ||
} | ||
|
||
public void setX(int x) { | ||
this.x = x; | ||
} | ||
|
||
public int getY() { | ||
return y; | ||
} | ||
|
||
public void setY(int y) { | ||
this.y = y; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
Point point = (Point) o; | ||
return x == point.x && y == point.y; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(x, y); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Point{" + | ||
"x=" + x + | ||
", y=" + y + | ||
'}'; | ||
} | ||
} |
Oops, something went wrong.