Skip to content

Commit 5962eb4

Browse files
committed
Implements text_detection_ppocr java demo(opencv#251)
1 parent 2a2e544 commit 5962eb4

File tree

4 files changed

+54
-13
lines changed

4 files changed

+54
-13
lines changed

models/pom.xml

+11
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
<module>text_detection_ppocr</module>
2020
</modules>
2121

22+
2223
<dependencies>
2324
<dependency>
2425
<groupId>org.bytedeco</groupId>
@@ -67,6 +68,16 @@
6768
</exclusion>
6869
</exclusions>
6970
</dependency>
71+
<dependency>
72+
<groupId>org.bytedeco</groupId>
73+
<artifactId>opencv-platform-gpu</artifactId>
74+
<version>4.9.0-1.5.10</version>
75+
</dependency>
76+
<dependency>
77+
<groupId>org.bytedeco</groupId>
78+
<artifactId>cuda-platform-redist</artifactId>
79+
<version>12.3-8.9-1.5.10</version>
80+
</dependency>
7081
<dependency>
7182
<groupId>com.beust</groupId>
7283
<artifactId>jcommander</artifactId>

models/text_detection_ppocr/README.md

+13
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,19 @@ cmake --build build
4343
./build/opencv_zoo_text_detection_ppocr -h
4444
```
4545

46+
### Java
47+
48+
Install Maven to get started with:
49+
50+
```shell
51+
# detect on camera input
52+
mvn compile exec:java -q
53+
# detect on an image
54+
mvn compile exec:java -q -Dexec.args="--input /path/to/image -v"
55+
# get help messages
56+
mvn compile exec:java -q -Dexec.args="--help"
57+
```
58+
4659
### Example outputs
4760

4861
![mask](./example_outputs/mask.jpg)

models/text_detection_ppocr/demo.java

+19-13
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ static class Args {
5454
@Parameter(names = "--unclip_ratio", order = 8,
5555
description = "The unclip ratio of the detected text region, which determines the output size.")
5656
double unclipRatio = 2.0;
57-
@Parameter(names = {"--save", "-s"}, order = 9, arity = 1,
57+
@Parameter(names = {"--save", "-s"}, order = 9,
5858
description = "Specify to save file with results (i.e. bounding box, confidence level). Invalid in case of camera input.")
59-
boolean save = true;
60-
@Parameter(names = {"--viz", "-v"}, order = 10, arity = 1,
59+
boolean save;
60+
@Parameter(names = {"--viz", "-v"}, order = 10,
6161
description = "Specify to open a new window to show results. Invalid in case of camera input.")
62-
boolean viz = true;
62+
boolean viz;
6363
@Parameter(names = {"--backend", "-bt"}, order = 11,
6464
description = "Choose one of computation backends:" +
6565
" 0: OpenCV implementation + CPU," +
@@ -93,8 +93,12 @@ public PPOCRDet(String modelPath, Size inputSize,
9393
}
9494

9595
public Map.Entry<PointVectorVector, FloatPointer> infer(Mat image) {
96-
assert image.rows() == inputSize.height() : "height of input image != net input size";
97-
assert image.cols() == inputSize.width() : "width of input image != net input size";
96+
if (image.rows() != inputSize.height()) {
97+
throw new IllegalArgumentException("height of input image != net input size");
98+
}
99+
if (image.cols() != inputSize.width()) {
100+
throw new IllegalArgumentException("width of input image != net input size");
101+
}
98102
final PointVectorVector pt = new PointVectorVector();
99103
final FloatPointer confidences = new FloatPointer();
100104
model.detect(image, pt, confidences);
@@ -125,8 +129,7 @@ static Mat visualize(Mat image, Map.Entry<PointVectorVector, FloatPointer> resul
125129
}
126130

127131
/**
128-
* Execute:
129-
* mvn compile exec:java -Dexec.mainClass=demo -q -Dexec.args="--help"
132+
* Execute: mvn compile exec:java -q -Dexec.args=""
130133
*/
131134
public static void main(String[] argv) {
132135
final Args args = new Args();
@@ -140,7 +143,9 @@ public static void main(String[] argv) {
140143
return;
141144
}
142145
final int[] backendTargetPair = backendTargetPairs[args.backend];
143-
assert args.model != null && !args.model.isEmpty() : "Model name is empty";
146+
if (args.model == null || args.model.isEmpty()) {
147+
throw new IllegalArgumentException("Model name is empty");
148+
}
144149
final Size inpSize = new Size(args.width, args.height);
145150

146151
final PPOCRDet model = new PPOCRDet(args.model, inpSize,
@@ -153,7 +158,9 @@ public static void main(String[] argv) {
153158
} else {
154159
cap.open(0);
155160
}
156-
assert cap.isOpened() : "Cannot open video or file";
161+
if (!cap.isOpened()) {
162+
throw new IllegalArgumentException("Cannot open video or file");
163+
}
157164
Mat originalImage = new Mat();
158165

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

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

180186
// inference
181187
tm.start();
182-
Map.Entry<PointVectorVector, FloatPointer> results = model.infer(image);
188+
final Map.Entry<PointVectorVector, FloatPointer> results = model.infer(image);
183189
tm.stop();
184190
// Scale the results bounding box
185191
final PointVectorVector pvv = results.getKey();

models/text_detection_ppocr/pom.xml

+11
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,17 @@
1515

1616
<build>
1717
<sourceDirectory>${project.basedir}</sourceDirectory>
18+
<plugins>
19+
<plugin>
20+
<groupId>org.codehaus.mojo</groupId>
21+
<artifactId>exec-maven-plugin</artifactId>
22+
<version>3.3.0</version>
23+
<configuration>
24+
<executable>java</executable>
25+
<mainClass>demo</mainClass>
26+
</configuration>
27+
</plugin>
28+
</plugins>
1829
</build>
1930

2031
</project>

0 commit comments

Comments
 (0)