Skip to content

Commit

Permalink
feat: ocr image filter
Browse files Browse the repository at this point in the history
  • Loading branch information
guimc233 committed Aug 1, 2024
1 parent b402452 commit 82db532
Show file tree
Hide file tree
Showing 25 changed files with 1,138 additions and 14 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
* **Thanks for everyone!!**
* Online demo: `1324248744` (QQ)
* Sponsor @ [Afdian](https://afdian.net/a/lgz-bot)
* Due to some reason, this project only support windows/linux x86_64 now.

## About LGZ bot
* 这是[Mirai](https://github.com/mamoe/mirai)框架的插件,依赖于[Mirai Console](https://github.com/mamoe/mirai-console)运行.
* 本插件可以为使用者提供群消息合规性检查,并作出相应的处罚.
* 目前建议在[Overflow](https://github.com/MrXiaoM/Overflow/)下继续运行此插件, 本插件已经完成了一些对Overflow的兼容
* 本插件可以为使用者提供群消息合规性检查功能.
* 还有一些有趣的小东西 \>_\<
* Thanks to [LL4J](https://github.com/LL4J/), [ADDetector4J](https://github.com/siuank/ADDetector4J)
* Thanks to [LL4J](https://github.com/LL4J/), [ADDetector4J](https://github.com/siuank/ADDetector4J),
[RapidOCR-Java](https://github.com/MyMonsterCat/RapidOcr-Java)
and [恶臭数字论证器](https://github.com/itorr/homo)

## License
Expand Down
9 changes: 7 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ plugins {
id("net.mamoe.mirai-console") version "2.16.0"
id("org.jetbrains.kotlin.jvm") version "1.9.0"
id("com.github.johnrengelman.shadow") version "8.0.0"
id("io.freefair.lombok") version "8.6"
id("maven-publish")
}

Expand All @@ -14,17 +15,21 @@ dependencies {
implementation(kotlin("stdlib"))
implementation("io.ktor:ktor-server-core:$ktor_version")
implementation("io.ktor:ktor-server-netty:$ktor_version")
implementation("ch.qos.logback:logback-classic:1.2.11")
implementation("ch.qos.logback:logback-classic:1.4.12")
implementation("com.github.promeg:tinypinyin:2.0.3")
implementation("com.huaban:jieba-analysis:+")
implementation("com.github.promeg:tinypinyin-lexicons-java-cncity:2.0.3")
implementation("org.json:json:20230227")
implementation("org.json:json:20231013")
implementation("org.apache.httpcomponents:httpclient:4.5.14")
implementation("org.jsoup:jsoup:1.15.3")
implementation("io.github.mymonstercat:rapidocr-onnx-windows-x86_64:1.2.2")
implementation("io.github.mymonstercat:rapidocr-onnx-linux-x86_64:1.2.2")
implementation("org.xerial:sqlite-jdbc:3.46.0.0")

compileOnly("top.mrxiaom:overflow-core-api:$overflow_version")
compileOnly("xyz.cssxsh.mirai:mirai-hibernate-plugin:2.8.0")
compileOnly("top.mrxiaom:overflow-core:$overflow_version")
compileOnly("org.projectlombok:lombok:1.18.34")
testImplementation("xyz.cssxsh.mirai:mirai-hibernate-plugin:2.8.0")

compileOnly(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))
Expand Down
70 changes: 70 additions & 0 deletions src/main/java/com/benjaminwan/ocrlibrary/OcrEngine.java
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
);

}
61 changes: 61 additions & 0 deletions src/main/java/com/benjaminwan/ocrlibrary/OcrInput.java
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;
}
}
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 interface OcrOutput {
}
51 changes: 51 additions & 0 deletions src/main/java/com/benjaminwan/ocrlibrary/OcrResult.java
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 + '\'' +
'}';
}
}
50 changes: 50 additions & 0 deletions src/main/java/com/benjaminwan/ocrlibrary/Point.java
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 +
'}';
}
}
Loading

0 comments on commit 82db532

Please sign in to comment.