Skip to content

Commit

Permalink
Merge pull request #69 from litongjava/master
Browse files Browse the repository at this point in the history
release to v2.2.9
  • Loading branch information
litongjava authored Dec 10, 2023
2 parents 2e3820d + 6d57eba commit 235a6cd
Show file tree
Hide file tree
Showing 17 changed files with 379 additions and 222 deletions.
2 changes: 1 addition & 1 deletion docs/cn/1 快速上手/1.0 快速上手.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ mvn clean package -DskipTests
以下是一个基本的 `jpackage` 命令示例,用于将 JavaFX JAR 打包为 `.exe` 文件:

```bash
jpackage --type exe --input target/ --main-jar tool-ocr-1.2.6.jar --name tree-hole-ocr --main-class com.luooqi.ocr.MainFm
jpackage --type exe --input target/ --main-jar tool-ocr-1.2.6.jar --name tree-hole-ocr --main-class com.luooqi.ocr.OcrApp
```

其中:
Expand Down
40 changes: 23 additions & 17 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

<groupId>com.luooqi</groupId>
<artifactId>tools-ocr</artifactId>
<version>2.2.8</version>
<version>2.2.9</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<main.class>com.luooqi.ocr.MainFm</main.class>
<main.class>com.luooqi.ocr.OcrApp</main.class>
<lombok.version>1.18.20</lombok.version>
<logback.version>1.2.3</logback.version>
<djl.version>0.25.0</djl.version>
Expand All @@ -39,6 +39,12 @@
<version>4.2</version>
</dependency>

<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.24</version>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
Expand Down Expand Up @@ -96,21 +102,21 @@
</dependency>
<!-- GraalVM - Java Fx -->
<!-- https://mvnrepository.com/artifact/org.openjfx/javafx-controls -->
<!-- <dependency>-->
<!-- <groupId>org.openjfx</groupId>-->
<!-- <artifactId>javafx-controls</artifactId>-->
<!-- <version>17</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.openjfx</groupId>-->
<!-- <artifactId>javafx-swing</artifactId>-->
<!-- <version>17</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.openjfx</groupId>-->
<!-- <artifactId>javafx-fxml</artifactId>-->
<!-- <version>17</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.openjfx</groupId>-->
<!-- <artifactId>javafx-controls</artifactId>-->
<!-- <version>17</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.openjfx</groupId>-->
<!-- <artifactId>javafx-swing</artifactId>-->
<!-- <version>17</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.openjfx</groupId>-->
<!-- <artifactId>javafx-fxml</artifactId>-->
<!-- <version>17</version>-->
<!-- </dependency>-->


<!--windows gpu-->
Expand Down
4 changes: 4 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
- onnx
- paddle ocr
- opencv

## 开源地址
[gitee](https://gitee.com/ppnt/tools-ocr) | [github](https://github.com/litongjava/tools-ocr)
## 安装
> - **安装路径请勿包含中文字符**
> - 本程序使用 JavaFX 开发,提供的安装包中已经包含了Java
Expand Down Expand Up @@ -49,6 +52,7 @@ mvn jfx:native -DskipTests -f pom.xml
![2](readme_files/2.jpg)

## TODO
- [x] PDF识别
- [x] 图片文字识别
- [x] 识别结果文本对齐(暂未实现多分栏)
- [x] 全屏模式下截图
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,18 +104,20 @@ public NDList processOutput(TranslatorContext ctx, NDList list) {
srcMat.release();
}

NDList dt_boxes = null;
NDArray boxes = boxes_from_bitmap(manager, pred, newMask);
if (boxes != null) {
//boxes[:, :, 0] = boxes[:, :, 0] / ratio_w
NDArray boxes1 = boxes.get(":, :, 0").div(ratio_w);
boxes.set(new NDIndex(":, :, 0"), boxes1);
//boxes[:, :, 1] = boxes[:, :, 1] / ratio_h
NDArray boxes2 = boxes.get(":, :, 1").div(ratio_h);
boxes.set(new NDIndex(":, :, 1"), boxes2);

//boxes[:, :, 0] = boxes[:, :, 0] / ratio_w
NDArray boxes1 = boxes.get(":, :, 0").div(ratio_w);
boxes.set(new NDIndex(":, :, 0"), boxes1);
//boxes[:, :, 1] = boxes[:, :, 1] / ratio_h
NDArray boxes2 = boxes.get(":, :, 1").div(ratio_h);
boxes.set(new NDIndex(":, :, 1"), boxes2);
dt_boxes = this.filter_tag_det_res(boxes);

NDList dt_boxes = this.filter_tag_det_res(boxes);

dt_boxes.detach();
dt_boxes.detach();
}

// release Mat
newMask.release();
Expand Down Expand Up @@ -255,12 +257,18 @@ private NDArray boxes_from_bitmap(NDManager manager, NDArray pred, Mat bitmap) {
newContour.release();
}

NDArray boxes = NDArrays.stack(boxList);

// release
hierarchy.release();

NDArray boxes = null;
if (boxList.size() > 0) {
boxes = NDArrays.stack(boxList);
return boxes;
}

return boxes;


}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ public List<RotatedBox> predict(NDManager manager,
Image image, Predictor<Image, NDList> detector, Predictor<Image, String> recognizer)
throws TranslateException {
NDList boxes = detector.predict(image);
if (boxes == null) {
return null;
}
// 交给 NDManager自动管理内存
// attach to manager for automatic memory management
boxes.attach(manager);
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/com/luooqi/ocr/OcrApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.luooqi.ocr;

import cn.hutool.core.thread.GlobalThreadPool;
import com.luooqi.ocr.config.InitConfig;
import com.luooqi.ocr.local.PaddlePaddleOCRV4;
import com.luooqi.ocr.windows.MainForm;
import javafx.application.Application;
import javafx.stage.Stage;
import lombok.extern.slf4j.Slf4j;
import org.jnativehook.GlobalScreen;

@Slf4j
public class OcrApp extends Application {

public static void main(String[] args) {
launch(args);
}

@Override
public void init() throws Exception {
super.init();
InitConfig.init();
}


@Override
public void start(Stage primaryStage) {
MainForm mainForm = new MainForm();
mainForm.init(primaryStage);
primaryStage.show();
}

@Override
public void stop() throws Exception {
log.info("close");
GlobalScreen.unregisterNativeHook();
PaddlePaddleOCRV4.INSTANCE.close();
GlobalThreadPool.shutdown(true);
}
}
8 changes: 8 additions & 0 deletions src/main/java/com/luooqi/ocr/constants/ImagesConstants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.luooqi.ocr.constants;

/**
* Created by [email protected] on 12/9/2023_7:14 PM
*/
public class ImagesConstants {
public static final String LOGO = "img/logo.png";
}
49 changes: 0 additions & 49 deletions src/main/java/com/luooqi/ocr/local/LocalOCR.java

This file was deleted.

13 changes: 13 additions & 0 deletions src/main/java/com/luooqi/ocr/local/PaddlePaddleOCRV4.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import ai.djl.repository.zoo.ModelNotFoundException;
import ai.djl.repository.zoo.ModelZoo;
import ai.djl.repository.zoo.ZooModel;
import ai.djl.translate.TranslateException;
import com.litongjava.djl.paddle.ocr.v4.common.RotatedBox;
import com.litongjava.djl.paddle.ocr.v4.common.RotatedBoxCompX;
import com.litongjava.djl.paddle.ocr.v4.detection.OcrV4Detection;
Expand Down Expand Up @@ -62,7 +63,14 @@ public void init() {
public String ocr(File imageFile) throws Exception {
Path path = imageFile.toPath();
Image image = OpenCVImageFactory.getInstance().fromFile(path);
return ocr(image);
}

public String ocr(Image image) throws Exception {
List<RotatedBox> detections = recognition.predict(manager, image, detector, recognizer);
if (detections == null) {
return null;
}

List<RotatedBox> initList = new ArrayList<>();
for (RotatedBox result : detections) {
Expand Down Expand Up @@ -105,4 +113,9 @@ public String ocr(File imageFile) throws Exception {
}
return fullText.toString();
}

public void close() {
detector.close();
recognizer.close();
}
}
36 changes: 19 additions & 17 deletions src/main/java/com/luooqi/ocr/snap/ScreenCapture.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

import cn.hutool.core.swing.ScreenUtil;
import cn.hutool.log.StaticLog;
import com.luooqi.ocr.MainFm;
import com.luooqi.ocr.model.CaptureInfo;
import com.luooqi.ocr.utils.CommUtils;
import com.luooqi.ocr.windows.MainForm;
import javafx.animation.AnimationTimer;
import javafx.application.Platform;
import javafx.embed.swing.SwingFXUtils;
Expand Down Expand Up @@ -180,8 +180,7 @@ public ScreenCapture(Stage mainStage) {

mainCanvas.setOnMouseDragged(m -> {
if (m.getButton() == MouseButton.PRIMARY) {
if (m.getScreenX() >= CaptureInfo.ScreenMinX &&
m.getScreenX() <= CaptureInfo.ScreenMaxX) {
if (m.getScreenX() >= CaptureInfo.ScreenMinX && m.getScreenX() <= CaptureInfo.ScreenMaxX) {
data.mouseXNow = (int) m.getX();
} else if (m.getScreenX() > CaptureInfo.ScreenMaxX) {
data.mouseXNow = CaptureInfo.ScreenWidth;
Expand Down Expand Up @@ -311,8 +310,7 @@ private void addKeyHandlers() {
}
});

data.anyPressed.addListener((obs, wasPressed, isNowPressed) ->
{
data.anyPressed.addListener((obs, wasPressed, isNowPressed) -> {
if (isNowPressed) {
yPressedAnimation.start();
} else {
Expand Down Expand Up @@ -370,15 +368,17 @@ private void repaintCanvas() {
: data.mouseYNow // UP
;

gc.strokeRect(data.rectUpperLeftX - 1.00, data.rectUpperLeftY - 1.00, data.rectWidth + 2.00, data.rectHeight + 2.00);
gc.strokeRect(data.rectUpperLeftX - 1.00, data.rectUpperLeftY - 1.00, data.rectWidth + 2.00,
data.rectHeight + 2.00);
gc.clearRect(data.rectUpperLeftX, data.rectUpperLeftY, data.rectWidth, data.rectHeight);

// draw the text
if (!data.hideExtraFeatures.getValue() && (data.rectWidth > 0 || data.rectHeight > 0)) {
double middle = data.rectUpperLeftX + data.rectWidth / 2.00;
gc.setLineWidth(1);
gc.setFill(Color.FIREBRICK);
gc.fillRect(middle - 77, data.rectUpperLeftY < 50 ? data.rectUpperLeftY + 2 : data.rectUpperLeftY - 18.00, 100, 18);
gc.fillRect(middle - 77, data.rectUpperLeftY < 50 ? data.rectUpperLeftY + 2 : data.rectUpperLeftY - 18.00, 100,
18);
gc.setFill(Color.WHITE);
gc.fillText(data.rectWidth + " * " + data.rectHeight, middle - 77 + 9,
data.rectUpperLeftY < 50 ? data.rectUpperLeftY + 17.00 : data.rectUpperLeftY - 4.00);
Expand All @@ -398,16 +398,16 @@ private void selectWholeScreen() {

public void prepareForCapture() {
isSnapping = true;
MainFm.stage.setOpacity(0.0f);
MainForm.stage.setOpacity(0.0f);
Platform.runLater(() -> {
Rectangle rectangle = CommUtils.getDisplayScreen(MainFm.stage);
Rectangle rectangle = CommUtils.getDisplayScreen(MainForm.stage);
data.reset();
CaptureInfo.ScreenMinX = rectangle.x;
CaptureInfo.ScreenMaxX = rectangle.x + rectangle.width;
CaptureInfo.ScreenWidth = rectangle.width;
CaptureInfo.ScreenHeight = rectangle.height;
BufferedImage bufferedImage = ScreenUtil.captureScreen(rectangle);
//bufferedImage = Scalr.resize(bufferedImage, Scalr.Method.QUALITY, Scalr.Mode.AUTOMATIC, CaptureInfo.ScreenWidth * 2, CaptureInfo.ScreenHeight * 2);
// bufferedImage = Scalr.resize(bufferedImage, Scalr.Method.QUALITY, Scalr.Mode.AUTOMATIC, CaptureInfo.ScreenWidth * 2, CaptureInfo.ScreenHeight * 2);
WritableImage fxImage = SwingFXUtils.toFXImage(bufferedImage, null);
deActivateAllKeys();
scene.setRoot(new Pane());
Expand All @@ -417,9 +417,9 @@ public void prepareForCapture() {
mainCanvas.setHeight(CaptureInfo.ScreenHeight);
mainCanvas.setCursor(Cursor.CROSSHAIR);
initGraphContent();
rootPane.setBackground(new Background(new BackgroundImage(fxImage,
BackgroundRepeat.NO_REPEAT, BackgroundRepeat.NO_REPEAT,
BackgroundPosition.CENTER, new BackgroundSize(CaptureInfo.ScreenWidth, CaptureInfo.ScreenHeight, false, false, true, true))));
rootPane.setBackground(new Background(new BackgroundImage(fxImage, BackgroundRepeat.NO_REPEAT,
BackgroundRepeat.NO_REPEAT, BackgroundPosition.CENTER,
new BackgroundSize(CaptureInfo.ScreenWidth, CaptureInfo.ScreenHeight, false, false, true, true))));
repaintCanvas();
stage.setScene(scene);
stage.setFullScreenExitHint("");
Expand All @@ -438,19 +438,21 @@ private void prepareImage() {
BufferedImage image;
try {
mainCanvas.setDisable(true);
image = new Robot().createScreenCapture(new Rectangle(data.rectUpperLeftX + CaptureInfo.ScreenMinX, data.rectUpperLeftY + (int) CommUtils.getCrtScreen(stage).getVisualBounds().getMinY(), data.rectWidth, data.rectHeight));
image = new Robot().createScreenCapture(new Rectangle(data.rectUpperLeftX + CaptureInfo.ScreenMinX,
data.rectUpperLeftY + (int) CommUtils.getCrtScreen(stage).getVisualBounds().getMinY(), data.rectWidth,
data.rectHeight));
} catch (AWTException ex) {
StaticLog.error(ex);
return;
} finally {
mainCanvas.setDisable(false);
MainFm.restore(false);
MainForm.restore(false);
}
MainFm.doOcr(image);
MainForm.doOcr(image);
}

public void cancelSnap() {
deActivateAllKeys();
MainFm.restore(true);
MainForm.restore(true);
}
}
Loading

0 comments on commit 235a6cd

Please sign in to comment.