diff --git a/models/pom.xml b/models/pom.xml index cac848e0..0fb6f07c 100644 --- a/models/pom.xml +++ b/models/pom.xml @@ -19,6 +19,7 @@ text_detection_ppocr + org.bytedeco @@ -67,6 +68,16 @@ + + org.bytedeco + opencv-platform-gpu + 4.9.0-1.5.10 + + + org.bytedeco + cuda-platform-redist + 12.3-8.9-1.5.10 + com.beust jcommander diff --git a/models/text_detection_ppocr/README.md b/models/text_detection_ppocr/README.md index 1a875d1c..21367f77 100644 --- a/models/text_detection_ppocr/README.md +++ b/models/text_detection_ppocr/README.md @@ -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) diff --git a/models/text_detection_ppocr/demo.java b/models/text_detection_ppocr/demo.java index 866fc1a4..b65a02a5 100644 --- a/models/text_detection_ppocr/demo.java +++ b/models/text_detection_ppocr/demo.java @@ -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," + @@ -93,8 +93,12 @@ public PPOCRDet(String modelPath, Size inputSize, } public Map.Entry 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); @@ -125,8 +129,7 @@ static Mat visualize(Mat image, Map.Entry 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(); @@ -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, @@ -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(); @@ -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(); @@ -179,7 +185,7 @@ public static void main(String[] argv) { // inference tm.start(); - Map.Entry results = model.infer(image); + final Map.Entry results = model.infer(image); tm.stop(); // Scale the results bounding box final PointVectorVector pvv = results.getKey(); diff --git a/models/text_detection_ppocr/pom.xml b/models/text_detection_ppocr/pom.xml index 79a0b8e8..657f7974 100644 --- a/models/text_detection_ppocr/pom.xml +++ b/models/text_detection_ppocr/pom.xml @@ -15,6 +15,17 @@ ${project.basedir} + + + org.codehaus.mojo + exec-maven-plugin + 3.3.0 + + java + demo + + + \ No newline at end of file