From d740fdd37d95d4eede1e532f1abc858d0a6bd6df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A1udio=20Paulo?= Date: Tue, 15 Oct 2024 10:51:39 +0100 Subject: [PATCH] [JAVA_API] Add as_long() to Tensor --- .../java_api/src/main/cpp/openvino_java.hpp | 1 + modules/java_api/src/main/cpp/tensor.cpp | 23 +++++++++++++++++++ .../main/java/org/intel/openvino/Tensor.java | 7 ++++++ .../java/org/intel/openvino/TensorTests.java | 1 + 4 files changed, 32 insertions(+) diff --git a/modules/java_api/src/main/cpp/openvino_java.hpp b/modules/java_api/src/main/cpp/openvino_java.hpp index 42b2a9755d..2138f0b8d7 100644 --- a/modules/java_api/src/main/cpp/openvino_java.hpp +++ b/modules/java_api/src/main/cpp/openvino_java.hpp @@ -66,6 +66,7 @@ extern "C" JNIEXPORT jintArray JNICALL Java_org_intel_openvino_Tensor_GetShape(JNIEnv *, jobject, jlong); JNIEXPORT jfloatArray JNICALL Java_org_intel_openvino_Tensor_asFloat(JNIEnv *, jobject, jlong); JNIEXPORT jintArray JNICALL Java_org_intel_openvino_Tensor_asInt(JNIEnv *, jobject, jlong); + JNIEXPORT jlongArray JNICALL Java_org_intel_openvino_Tensor_asLong(JNIEnv *, jobject, jlong); JNIEXPORT void JNICALL Java_org_intel_openvino_Tensor_delete(JNIEnv *, jobject, jlong); // ov::PrePostProcessor diff --git a/modules/java_api/src/main/cpp/tensor.cpp b/modules/java_api/src/main/cpp/tensor.cpp index 87e430382c..34e21aadd7 100644 --- a/modules/java_api/src/main/cpp/tensor.cpp +++ b/modules/java_api/src/main/cpp/tensor.cpp @@ -160,6 +160,29 @@ JNIEXPORT jintArray JNICALL Java_org_intel_openvino_Tensor_asInt(JNIEnv *env, jo return 0; } +JNIEXPORT jlongArray JNICALL Java_org_intel_openvino_Tensor_asLong(JNIEnv *env, jobject, jlong addr) +{ + JNI_METHOD( + "asLong", + Tensor *ov_tensor = (Tensor *)addr; + + size_t size = ov_tensor->get_size(); + const long *data = ov_tensor->data(); + + jlongArray result = env->NewLongArray(size); + if (!result) { + throw std::runtime_error("Out of memory!"); + } jlong *arr = env->GetLongArrayElements(result, nullptr); + + for (size_t i = 0; i < size; ++i) + arr[i] = data[i]; + + env->ReleaseLongArrayElements(result, arr, 0); + return result; + ) + return 0; +} + JNIEXPORT void JNICALL Java_org_intel_openvino_Tensor_delete(JNIEnv *, jobject, jlong addr) { Tensor *tensor = (Tensor *)addr; diff --git a/modules/java_api/src/main/java/org/intel/openvino/Tensor.java b/modules/java_api/src/main/java/org/intel/openvino/Tensor.java index b3236e26a1..b91d827e1d 100644 --- a/modules/java_api/src/main/java/org/intel/openvino/Tensor.java +++ b/modules/java_api/src/main/java/org/intel/openvino/Tensor.java @@ -66,6 +66,11 @@ public int[] as_int() { return asInt(nativeObj); } + /** Returns the tensor data as a long array. */ + public long[] as_long() { + return asLong(nativeObj); + } + /*----------------------------------- native methods -----------------------------------*/ private static native long TensorCArray(int type, int[] shape, long cArray); @@ -81,6 +86,8 @@ public int[] as_int() { private static native int[] asInt(long addr); + private static native long[] asLong(long addr); + private static native int GetSize(long addr); @Override diff --git a/modules/java_api/src/test/java/org/intel/openvino/TensorTests.java b/modules/java_api/src/test/java/org/intel/openvino/TensorTests.java index 45a0f3cd8f..5cbb9c9f5f 100644 --- a/modules/java_api/src/test/java/org/intel/openvino/TensorTests.java +++ b/modules/java_api/src/test/java/org/intel/openvino/TensorTests.java @@ -40,6 +40,7 @@ public void testGetTensorFromLong() { Tensor tensor = new Tensor(dimsArr, inputData); assertArrayEquals(dimsArr, tensor.get_shape()); + assertArrayEquals(inputData, tensor.as_long()); assertEquals(size, tensor.get_size()); } }