Skip to content

Commit

Permalink
Implements text_detection_ppocr java demo(opencv#251)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluehatch committed Jun 28, 2024
1 parent 2a2e544 commit 5962eb4
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 13 deletions.
11 changes: 11 additions & 0 deletions models/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<module>text_detection_ppocr</module>
</modules>


<dependencies>
<dependency>
<groupId>org.bytedeco</groupId>
Expand Down Expand Up @@ -67,6 +68,16 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>opencv-platform-gpu</artifactId>
<version>4.9.0-1.5.10</version>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>cuda-platform-redist</artifactId>
<version>12.3-8.9-1.5.10</version>
</dependency>
<dependency>
<groupId>com.beust</groupId>
<artifactId>jcommander</artifactId>
Expand Down
13 changes: 13 additions & 0 deletions models/text_detection_ppocr/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,19 @@ cmake --build build
./build/opencv_zoo_text_detection_ppocr -h
```

### Java

Install Maven to get started with:

```shell
# detect on camera input
mvn compile exec:java -q
# detect on an image
mvn compile exec:java -q -Dexec.args="--input /path/to/image -v"
# get help messages
mvn compile exec:java -q -Dexec.args="--help"
```

### Example outputs

![mask](./example_outputs/mask.jpg)
Expand Down
32 changes: 19 additions & 13 deletions models/text_detection_ppocr/demo.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ static class Args {
@Parameter(names = "--unclip_ratio", order = 8,
description = "The unclip ratio of the detected text region, which determines the output size.")
double unclipRatio = 2.0;
@Parameter(names = {"--save", "-s"}, order = 9, arity = 1,
@Parameter(names = {"--save", "-s"}, order = 9,
description = "Specify to save file with results (i.e. bounding box, confidence level). Invalid in case of camera input.")
boolean save = true;
@Parameter(names = {"--viz", "-v"}, order = 10, arity = 1,
boolean save;
@Parameter(names = {"--viz", "-v"}, order = 10,
description = "Specify to open a new window to show results. Invalid in case of camera input.")
boolean viz = true;
boolean viz;
@Parameter(names = {"--backend", "-bt"}, order = 11,
description = "Choose one of computation backends:" +
" 0: OpenCV implementation + CPU," +
Expand Down Expand Up @@ -93,8 +93,12 @@ public PPOCRDet(String modelPath, Size inputSize,
}

public Map.Entry<PointVectorVector, FloatPointer> infer(Mat image) {
assert image.rows() == inputSize.height() : "height of input image != net input size";
assert image.cols() == inputSize.width() : "width of input image != net input size";
if (image.rows() != inputSize.height()) {
throw new IllegalArgumentException("height of input image != net input size");
}
if (image.cols() != inputSize.width()) {
throw new IllegalArgumentException("width of input image != net input size");
}
final PointVectorVector pt = new PointVectorVector();
final FloatPointer confidences = new FloatPointer();
model.detect(image, pt, confidences);
Expand Down Expand Up @@ -125,8 +129,7 @@ static Mat visualize(Mat image, Map.Entry<PointVectorVector, FloatPointer> resul
}

/**
* Execute:
* mvn compile exec:java -Dexec.mainClass=demo -q -Dexec.args="--help"
* Execute: mvn compile exec:java -q -Dexec.args=""
*/
public static void main(String[] argv) {
final Args args = new Args();
Expand All @@ -140,7 +143,9 @@ public static void main(String[] argv) {
return;
}
final int[] backendTargetPair = backendTargetPairs[args.backend];
assert args.model != null && !args.model.isEmpty() : "Model name is empty";
if (args.model == null || args.model.isEmpty()) {
throw new IllegalArgumentException("Model name is empty");
}
final Size inpSize = new Size(args.width, args.height);

final PPOCRDet model = new PPOCRDet(args.model, inpSize,
Expand All @@ -153,7 +158,9 @@ public static void main(String[] argv) {
} else {
cap.open(0);
}
assert cap.isOpened() : "Cannot open video or file";
if (!cap.isOpened()) {
throw new IllegalArgumentException("Cannot open video or file");
}
Mat originalImage = new Mat();

final OpenCVFrameConverter.ToMat converter = new OpenCVFrameConverter.ToMat();
Expand All @@ -167,9 +174,8 @@ public static void main(String[] argv) {
final Scalar boxColor = new Scalar(0, 255, 0, 0);
final Scalar textColor = new Scalar(0, 0, 255, 0);
final TickMeter tm = new TickMeter();
while (cap.read(originalImage)) {
cap.read(originalImage);

while (cap.read(originalImage)) {
final int originalW = originalImage.cols();
final int originalH = originalImage.rows();
final double scaleHeight = originalH / (double) inpSize.height();
Expand All @@ -179,7 +185,7 @@ public static void main(String[] argv) {

// inference
tm.start();
Map.Entry<PointVectorVector, FloatPointer> results = model.infer(image);
final Map.Entry<PointVectorVector, FloatPointer> results = model.infer(image);
tm.stop();
// Scale the results bounding box
final PointVectorVector pvv = results.getKey();
Expand Down
11 changes: 11 additions & 0 deletions models/text_detection_ppocr/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@

<build>
<sourceDirectory>${project.basedir}</sourceDirectory>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<executable>java</executable>
<mainClass>demo</mainClass>
</configuration>
</plugin>
</plugins>
</build>

</project>

0 comments on commit 5962eb4

Please sign in to comment.