[JAVA] Changed blob costructor (using cArray) in samples (#1863)
authorAnna Likholat <anna.likholat@intel.com>
Thu, 20 Aug 2020 15:45:14 +0000 (18:45 +0300)
committerGitHub <noreply@github.com>
Thu, 20 Aug 2020 15:45:14 +0000 (18:45 +0300)
inference-engine/ie_bridges/java/samples/face_detection_java_sample/Main.java
inference-engine/ie_bridges/java/samples/face_detection_sample_async/Main.java

index 222b1dd..31bbbf3 100644 (file)
@@ -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();
 
index a29e1d6..0786f70 100644 (file)
@@ -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<InferRequest> inferRequests = new Vector<InferRequest>();
     static Vector<Boolean> asyncInferIsFree;
 
-    static byte[] buff = null;
-
     static int framesCounter = 0;
     static int resultCounter = 0;
 }