From e35b23a9fff2ecb15dc2a436a677a68c3ffa5696 Mon Sep 17 00:00:00 2001 From: Anna Likholat Date: Thu, 20 Aug 2020 18:45:14 +0300 Subject: [PATCH] [JAVA] Changed blob costructor (using cArray) in samples (#1863) --- .../java/samples/face_detection_java_sample/Main.java | 7 +++---- .../java/samples/face_detection_sample_async/Main.java | 14 +++++--------- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/inference-engine/ie_bridges/java/samples/face_detection_java_sample/Main.java b/inference-engine/ie_bridges/java/samples/face_detection_java_sample/Main.java index 222b1dd..31bbbf3 100644 --- a/inference-engine/ie_bridges/java/samples/face_detection_java_sample/Main.java +++ b/inference-engine/ie_bridges/java/samples/face_detection_java_sample/Main.java @@ -52,14 +52,13 @@ public class Main { } Mat image = Imgcodecs.imread(imgPath); - - byte[] buff = new byte[(int) (image.total() * image.channels())]; - image.get(0, 0, buff); int[] dimsArr = {1, image.channels(), image.height(), image.width()}; TensorDesc tDesc = new TensorDesc(Precision.U8, dimsArr, Layout.NHWC); - Blob imgBlob = new Blob(tDesc, buff); + // The source image is also used at the end of the program to display the detection results, + // therefore the Mat object won't be destroyed by Garbage Collector while the network is running. + Blob imgBlob = new Blob(tDesc, image.dataAddr()); IECore core = new IECore(); diff --git a/inference-engine/ie_bridges/java/samples/face_detection_sample_async/Main.java b/inference-engine/ie_bridges/java/samples/face_detection_sample_async/Main.java index a29e1d6..0786f70 100644 --- a/inference-engine/ie_bridges/java/samples/face_detection_sample_async/Main.java +++ b/inference-engine/ie_bridges/java/samples/face_detection_sample_async/Main.java @@ -28,15 +28,10 @@ To get the list of command line parameters run the application with `--help` par public class Main { public static Blob imageToBlob(Mat image) { - if (buff == null) - buff = new byte[(int) (image.total() * image.channels())]; - - image.get(0, 0, buff); - int[] dimsArr = {1, image.channels(), image.height(), image.width()}; TensorDesc tDesc = new TensorDesc(Precision.U8, dimsArr, Layout.NHWC); - return new Blob(tDesc, buff); + return new Blob(tDesc, image.dataAddr()); } static void processInferRequets(WaitMode wait) { @@ -164,6 +159,9 @@ public class Main { asyncInferIsFree.setElementAt(false, i); processedFramesQueue.add(frame); // predictionsQueue is used in rendering + // The source frame is kept in processedFramesQueue, + // so the frame will be removed by java Garbage Collector only after completion of inference, + // and we can create Blob object using Mat object data address. Blob imgBlob = imageToBlob(frame); request.SetBlob(inputName, imgBlob); @@ -193,7 +191,7 @@ public class Main { if (detection == null) continue; - Mat img = processedFramesQueue.poll(waitingTime, TimeUnit.SECONDS); + Mat img = processedFramesQueue.poll(waitingTime, TimeUnit.SECONDS); int maxProposalCount = detection.length / 7; for (int curProposal = 0; curProposal < maxProposalCount; curProposal++) { @@ -257,8 +255,6 @@ public class Main { static Vector inferRequests = new Vector(); static Vector asyncInferIsFree; - static byte[] buff = null; - static int framesCounter = 0; static int resultCounter = 0; } -- 2.7.4