1- import org .intel .openvino .compatibility .*;
1+ // Copyright (C) 2020-2022 Intel Corporation
2+ // SPDX-License-Identifier: Apache-2.0
3+
4+ import org .intel .openvino .*;
5+ import org .intel .openvino .Core ;
26import org .opencv .core .*;
37import org .opencv .highgui .HighGui ;
48import org .opencv .imgcodecs .*;
59import org .opencv .imgproc .Imgproc ;
610
7- import java .util .ArrayList ;
8- import java .util .Map ;
9-
1011/*
11- This is face detection java sample.
12-
12+ This is face detection Java sample (for OpenVINO Java API 2.0).
1313Upon the start-up the sample application reads command line parameters and loads a network
1414and an image to the Inference Engine device. When inference is done, the application will show
1515the image with detected objects enclosed in rectangles in new window.It also outputs the
1616confidence value and the coordinates of the rectangle to the standard output stream.
17-
1817To get the list of command line parameters run the application with `--help` paramether.
1918*/
2019public class Main {
2120 public static void main (String [] args ) {
2221 final double THRESHOLD = 0.7 ;
2322 try {
24- System .loadLibrary (Core .NATIVE_LIBRARY_NAME );
23+ System .loadLibrary (org . opencv . core . Core .NATIVE_LIBRARY_NAME );
2524 } catch (UnsatisfiedLinkError e ) {
2625 System .err .println ("Failed to load OpenCV library\n " + e );
2726 System .exit (1 );
2827 }
29- try {
30- System .loadLibrary (IECore .NATIVE_LIBRARY_NAME );
31- } catch (UnsatisfiedLinkError e ) {
32- System .err .println ("Failed to load Inference Engine library\n " + e );
33- System .exit (1 );
34- }
28+ Core .loadNativeLibs ();
3529
3630 ArgumentParser parser = new ArgumentParser ("This is face detection sample" );
3731 parser .addArgument ("-i" , "path to image" );
@@ -52,41 +46,38 @@ public static void main(String[] args) {
5246
5347 Mat image = Imgcodecs .imread (imgPath );
5448
55- int [] dimsArr = {1 , image .channels (), image .height (), image .width ()};
56- TensorDesc tDesc = new TensorDesc (Precision .U8 , dimsArr , Layout .NHWC );
57-
58- // The source image is also used at the end of the program to display the detection results,
59- // therefore the Mat object won't be destroyed by Garbage Collector while the network is
60- // running.
61- Blob imgBlob = new Blob (tDesc , image .dataAddr ());
49+ Core core = new Core ();
50+ Model net = core .read_model (xmlPath );
6251
63- IECore core = new IECore ();
52+ /* The source image is also used at the end of the program to display the detection results,
53+ therefore the Mat object won't be destroyed by Garbage Collector while the network is
54+ running. */
55+ int [] dimsArr = {1 , image .rows (), image .cols (), 3 };
56+ Tensor input_tensor = new Tensor (ElementType .u8 , dimsArr , image .dataAddr ());
6457
65- CNNNetwork net = core .ReadNetwork (xmlPath );
58+ PrePostProcessor p = new PrePostProcessor (net );
59+ p .input ()
60+ .tensor ()
61+ .set_element_type (ElementType .u8 )
62+ .set_layout (new Layout ("NHWC" ))
63+ .set_spatial_static_shape (image .rows (), image .cols ());
6664
67- Map < String , InputInfo > inputsInfo = net . getInputsInfo ( );
68- String inputName = new ArrayList < String >( inputsInfo . keySet ()). get ( 0 );
69- InputInfo inputInfo = inputsInfo . get ( inputName );
65+ p . input (). preprocess (). resize ( ResizeAlgorithm . RESIZE_LINEAR );
66+ p . input (). model (). set_layout ( new Layout ( "NCHW" ) );
67+ p . build ( );
7068
71- inputInfo .getPreProcess ().setResizeAlgorithm (ResizeAlgorithm .RESIZE_BILINEAR );
72- inputInfo .setLayout (Layout .NHWC );
73- inputInfo .setPrecision (Precision .U8 );
69+ CompiledModel compiledModel = core .compile_model (net , "CPU" );
70+ InferRequest inferRequest = compiledModel .create_infer_request ();
7471
75- String outputName = new ArrayList <String >(net .getOutputsInfo ().keySet ()).get (0 );
72+ inferRequest .set_input_tensor (input_tensor );
73+ inferRequest .infer ();
7674
77- ExecutableNetwork executableNetwork = core . LoadNetwork ( net , "CPU" );
78- InferRequest inferRequest = executableNetwork . CreateInferRequest ();
75+ Tensor output_tensor = inferRequest . get_output_tensor ( );
76+ float detection [] = output_tensor . data ();
7977
80- inferRequest .SetBlob (inputName , imgBlob );
81- inferRequest .Infer ();
82-
83- Blob output = inferRequest .GetBlob (outputName );
84- int dims [] = output .getTensorDesc ().getDims ();
78+ int dims [] = output_tensor .get_shape ();
8579 int maxProposalCount = dims [2 ];
8680
87- float detection [] = new float [output .size ()];
88- output .rmap ().get (detection );
89-
9081 for (int curProposal = 0 ; curProposal < maxProposalCount ; curProposal ++) {
9182 int image_id = (int ) detection [curProposal * 7 ];
9283 if (image_id < 0 ) break ;
@@ -105,7 +96,7 @@ public static void main(String[] args) {
10596 String result = "[" + curProposal + "," + label + "] element, prob = " + confidence ;
10697 result += " (" + xmin + "," + ymin + ")-(" + xmax + "," + ymax + ")" ;
10798
108- System .out .println (result );
99+ System .out .print (result );
109100 System .out .println (" - WILL BE PRINTED!" );
110101
111102 // Draw rectangle around detected object.
0 commit comments