Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JAVA] API 2.0 face detection demo #406

Merged
merged 3 commits into from
Sep 2, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions modules/java_api/samples/benchmark_app/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import java.util.Map;
import java.util.Vector;

// This demo uses DEPRICATED OpenVINO Java API!

public class Main {

static boolean adjustShapesBatch(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Copyright (C) 2020 Intel Corporation
# Copyright (C) 2020-2022 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

get_filename_component(sample_name "${CMAKE_CURRENT_SOURCE_DIR}" NAME)

ie_add_java_sample(NAME ${sample_name} OPENCV_DEPENDENCIES)

73 changes: 32 additions & 41 deletions modules/java_api/samples/face_detection_java_sample/Main.java
Original file line number Diff line number Diff line change
@@ -1,37 +1,31 @@
import org.intel.openvino.compatibility.*;
// Copyright (C) 2020-2022 Intel Corporation
// SPDX-License-Identifier: Apache-2.0

import org.intel.openvino.*;
import org.intel.openvino.Core;
import org.opencv.core.*;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.*;
import org.opencv.imgproc.Imgproc;

import java.util.ArrayList;
import java.util.Map;

/*
This is face detection java sample.

This is face detection Java sample (for OpenVINO Java API 2.0).
Upon the start-up the sample application reads command line parameters and loads a network
and an image to the Inference Engine device. When inference is done, the application will show
the image with detected objects enclosed in rectangles in new window.It also outputs the
confidence value and the coordinates of the rectangle to the standard output stream.

To get the list of command line parameters run the application with `--help` paramether.
*/
public class Main {
public static void main(String[] args) {
final double THRESHOLD = 0.7;
try {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
System.loadLibrary(org.opencv.core.Core.NATIVE_LIBRARY_NAME);
} catch (UnsatisfiedLinkError e) {
System.err.println("Failed to load OpenCV library\n" + e);
System.exit(1);
}
try {
System.loadLibrary(IECore.NATIVE_LIBRARY_NAME);
} catch (UnsatisfiedLinkError e) {
System.err.println("Failed to load Inference Engine library\n" + e);
System.exit(1);
}
Core.loadNativeLibs();

ArgumentParser parser = new ArgumentParser("This is face detection sample");
parser.addArgument("-i", "path to image");
Expand All @@ -52,41 +46,38 @@ public static void main(String[] args) {

Mat image = Imgcodecs.imread(imgPath);

int[] dimsArr = {1, image.channels(), image.height(), image.width()};
TensorDesc tDesc = new TensorDesc(Precision.U8, dimsArr, Layout.NHWC);

// 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());
Core core = new Core();
Model net = core.read_model(xmlPath);

IECore core = new IECore();
/* 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. */
int[] dimsArr = {1, image.rows(), image.cols(), 3};
Tensor input_tensor = new Tensor(ElementType.u8, dimsArr, image.dataAddr());

CNNNetwork net = core.ReadNetwork(xmlPath);
PrePostProcessor p = new PrePostProcessor(net);
p.input()
.tensor()
.set_element_type(ElementType.u8)
.set_layout(new Layout("NHWC"))
.set_spatial_static_shape(image.rows(), image.cols());

Map<String, InputInfo> inputsInfo = net.getInputsInfo();
String inputName = new ArrayList<String>(inputsInfo.keySet()).get(0);
InputInfo inputInfo = inputsInfo.get(inputName);
p.input().preprocess().resize(ResizeAlgorithm.RESIZE_LINEAR);
p.input().model().set_layout(new Layout("NCHW"));
p.build();

inputInfo.getPreProcess().setResizeAlgorithm(ResizeAlgorithm.RESIZE_BILINEAR);
inputInfo.setLayout(Layout.NHWC);
inputInfo.setPrecision(Precision.U8);
CompiledModel compiledModel = core.compile_model(net, "CPU");
InferRequest inferRequest = compiledModel.create_infer_request();

String outputName = new ArrayList<String>(net.getOutputsInfo().keySet()).get(0);
inferRequest.set_input_tensor(input_tensor);
inferRequest.infer();

ExecutableNetwork executableNetwork = core.LoadNetwork(net, "CPU");
InferRequest inferRequest = executableNetwork.CreateInferRequest();
Tensor output_tensor = inferRequest.get_output_tensor();
float detection[] = output_tensor.data();

inferRequest.SetBlob(inputName, imgBlob);
inferRequest.Infer();

Blob output = inferRequest.GetBlob(outputName);
int dims[] = output.getTensorDesc().getDims();
int dims[] = output_tensor.get_shape();
int maxProposalCount = dims[2];

float detection[] = new float[output.size()];
output.rmap().get(detection);

for (int curProposal = 0; curProposal < maxProposalCount; curProposal++) {
int image_id = (int) detection[curProposal * 7];
if (image_id < 0) break;
Expand All @@ -105,7 +96,7 @@ public static void main(String[] args) {
String result = "[" + curProposal + "," + label + "] element, prob = " + confidence;
result += " (" + xmin + "," + ymin + ")-(" + xmax + "," + ymax + ")";

System.out.println(result);
System.out.print(result);
System.out.println(" - WILL BE PRINTED!");

// Draw rectangle around detected object.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import java.util.concurrent.TimeUnit;

/*
This demo uses DEPRICATED OpenVINO Java API!

This is async face detection java sample.

Upon the start-up the sample application reads command line parameters and loads a network
Expand Down