Skip to content

Commit e929f98

Browse files
authored
Port Java bindings (opencv#2)
1 parent 5189cf8 commit e929f98

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+4200
-0
lines changed

.github/workflows/code_style.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: Code Style
2+
on: [push, pull_request]
3+
4+
jobs:
5+
Java:
6+
runs-on: ubuntu-18.04
7+
steps:
8+
- uses: actions/checkout@v2
9+
- uses: actions/setup-java@v1
10+
with:
11+
java-version: '11'
12+
13+
- name: Install dependencies
14+
run: |
15+
wget -nc https://github.com/google/google-java-format/releases/download/google-java-format-1.9/google-java-format-1.9-all-deps.jar
16+
17+
- name: Check code style
18+
run: |
19+
java -jar google-java-format-1.9-all-deps.jar --set-exit-if-changed -a -i $(find . -type f -name "*.java")
20+
21+
- name: Create code style diff
22+
if: failure()
23+
run: |
24+
git diff >java_code_style_diff.patch
25+
26+
- uses: actions/upload-artifact@v2
27+
if: failure()
28+
with:
29+
name: java_code_style_diff
30+
path: java_code_style_diff.patch

modules/java_api/CMakeLists.txt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Copyright (C) 2020 Intel Corporation
2+
3+
project(inference_engine_java_api)
4+
5+
# Find Java
6+
7+
find_package(Java REQUIRED)
8+
include(UseJava)
9+
10+
set(JAVA_AWT_INCLUDE_PATH NotNeeded)
11+
12+
find_package(JNI REQUIRED)
13+
14+
# Build native part
15+
16+
file(GLOB_RECURSE sources ${CMAKE_CURRENT_SOURCE_DIR}/cpp/*.cpp)
17+
18+
add_library(${PROJECT_NAME} SHARED ${sources})
19+
20+
target_link_libraries(${PROJECT_NAME} PRIVATE inference_engine)
21+
target_include_directories(${PROJECT_NAME} PRIVATE ${JNI_INCLUDE_DIRS})
22+
23+
# Build java part
24+
25+
file(GLOB_RECURSE java_source ${CMAKE_CURRENT_SOURCE_DIR}/org/intel/openvino/*.java)
26+
add_jar(inference_engine_jar ${java_source}
27+
OUTPUT_NAME ${PROJECT_NAME}
28+
OUTPUT_DIR ${CMAKE_LIBRARY_OUTPUT_DIRECTORY})
29+
30+
if(ENABLE_TESTS)
31+
add_subdirectory(tests)
32+
endif()
33+
34+
if(ENABLE_SAMPLES)
35+
add_subdirectory(samples)
36+
endif()

modules/java_api/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
## Software Requirements
2+
- OpenJDK (version depends on target OS)
3+
4+
### Linux
5+
To install OpenJDK:
6+
7+
* Ubuntu systems:
8+
```bash
9+
sudo apt-get install -y default-jdk
10+
```
11+
12+
## Building on Linux
13+
14+
To create Inference Engine Java API add ```-DENABLE_JAVA=ON``` flag in cmake command while building the Inference Engine.

modules/java_api/cpp/blob.cpp

Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
#include <inference_engine.hpp>
2+
3+
#include "openvino_java.hpp"
4+
#include "jni_common.hpp"
5+
6+
using namespace InferenceEngine;
7+
8+
JNIEXPORT jlong JNICALL Java_org_intel_openvino_Blob_GetTensorDesc(JNIEnv *env, jobject obj, jlong addr)
9+
{
10+
static const char method_name[] = "GetTensorDesc";
11+
try
12+
{
13+
Blob::Ptr *output = reinterpret_cast<Blob::Ptr *>(addr);
14+
TensorDesc *tDesc = new TensorDesc((*output)->getTensorDesc());
15+
16+
return (jlong)tDesc;
17+
}
18+
catch (const std::exception &e)
19+
{
20+
throwJavaException(env, &e, method_name);
21+
}
22+
catch (...)
23+
{
24+
throwJavaException(env, 0, method_name);
25+
}
26+
27+
return 0;
28+
}
29+
30+
JNIEXPORT jlong JNICALL Java_org_intel_openvino_Blob_GetBlob(JNIEnv *env, jobject obj, jlong tensorDescAddr)
31+
{
32+
static const char method_name[] = "GetBlob";
33+
try
34+
{
35+
TensorDesc *tDesc = (TensorDesc *)tensorDescAddr;
36+
37+
Blob::Ptr *blob = new Blob::Ptr();
38+
*blob = make_shared_blob<uint8_t>(*tDesc);
39+
40+
return (jlong)blob;
41+
}
42+
catch (const std::exception &e)
43+
{
44+
throwJavaException(env, &e, method_name);
45+
}
46+
catch (...)
47+
{
48+
throwJavaException(env, 0, method_name);
49+
}
50+
51+
return 0;
52+
}
53+
54+
JNIEXPORT jlong JNICALL Java_org_intel_openvino_Blob_BlobByte(JNIEnv *env, jobject obj, jlong tensorDescAddr, jbyteArray data)
55+
{
56+
static const char method_name[] = "BlobByte";
57+
try
58+
{
59+
TensorDesc *tDesc = (TensorDesc *)tensorDescAddr;
60+
61+
Blob::Ptr *blob = new Blob::Ptr();
62+
63+
*blob = make_shared_blob<uint8_t>((*tDesc));
64+
(*blob)->allocate();
65+
env->GetByteArrayRegion(data, 0, (*blob)->size(), (*blob)->buffer());
66+
67+
return (jlong)blob;
68+
}
69+
catch (const std::exception &e)
70+
{
71+
throwJavaException(env, &e, method_name);
72+
}
73+
catch (...)
74+
{
75+
throwJavaException(env, 0, method_name);
76+
}
77+
78+
return 0;
79+
}
80+
81+
JNIEXPORT jlong JNICALL Java_org_intel_openvino_Blob_BlobFloat(JNIEnv *env, jobject obj, jlong tensorDescAddr, jfloatArray data)
82+
{
83+
static const char method_name[] = "BlobFloat";
84+
try
85+
{
86+
TensorDesc *tDesc = (TensorDesc *)tensorDescAddr;
87+
88+
Blob::Ptr *blob = new Blob::Ptr();
89+
90+
*blob = make_shared_blob<float>((*tDesc));
91+
(*blob)->allocate();
92+
env->GetFloatArrayRegion(data, 0, (*blob)->size(), (*blob)->buffer());
93+
94+
return (jlong)blob;
95+
}
96+
catch (const std::exception &e)
97+
{
98+
throwJavaException(env, &e, method_name);
99+
}
100+
catch (...)
101+
{
102+
throwJavaException(env, 0, method_name);
103+
}
104+
105+
return 0;
106+
}
107+
108+
JNIEXPORT jlong JNICALL Java_org_intel_openvino_Blob_BlobCArray(JNIEnv *env, jobject obj, jlong tensorDescAddr, jlong matDataAddr)
109+
{
110+
static const char method_name[] = "BlobCArray";
111+
try
112+
{
113+
TensorDesc *tDesc = (TensorDesc *)tensorDescAddr;
114+
115+
auto precision = tDesc->getPrecision();
116+
117+
std::vector<size_t> dims = tDesc->getDims();
118+
Blob::Ptr *blob = new Blob::Ptr();
119+
120+
switch (precision) {
121+
case Precision::FP32:
122+
{
123+
float *data = (float *) matDataAddr;
124+
*blob = make_shared_blob<float>((*tDesc), data);
125+
break;
126+
}
127+
case Precision::Q78:
128+
case Precision::I16:
129+
case Precision::FP16:
130+
{
131+
short *data = (short *) matDataAddr;
132+
*blob = make_shared_blob<short>((*tDesc), data);
133+
break;
134+
}
135+
case Precision::U8:
136+
{
137+
uint8_t *data = (uint8_t *) matDataAddr;
138+
*blob = make_shared_blob<uint8_t>((*tDesc), data);
139+
break;
140+
}
141+
case Precision::I8:
142+
{
143+
int8_t *data = (int8_t *) matDataAddr;
144+
*blob = make_shared_blob<int8_t>((*tDesc), data);
145+
break;
146+
}
147+
case Precision::I32:
148+
{
149+
int32_t *data = (int32_t *) matDataAddr;
150+
*blob = make_shared_blob<int32_t>((*tDesc), data);
151+
break;
152+
}
153+
case Precision::BF16:
154+
{
155+
short *data = (short *) matDataAddr;
156+
*blob = make_shared_blob<short>((*tDesc), data);
157+
break;
158+
}
159+
default:
160+
throw std::runtime_error("Unsupported precision value!");
161+
}
162+
163+
return (jlong)blob;
164+
}
165+
catch (const std::exception &e)
166+
{
167+
throwJavaException(env, &e, method_name);
168+
}
169+
catch (...)
170+
{
171+
throwJavaException(env, 0, method_name);
172+
}
173+
174+
return 0;
175+
}
176+
177+
JNIEXPORT jint JNICALL Java_org_intel_openvino_Blob_size(JNIEnv *env, jobject obj, jlong addr)
178+
{
179+
static const char method_name[] = "size";
180+
try
181+
{
182+
Blob::Ptr *output = reinterpret_cast<Blob::Ptr *>(addr);
183+
int size = (*output)->size();
184+
185+
return size;
186+
}
187+
catch (const std::exception &e)
188+
{
189+
throwJavaException(env, &e, method_name);
190+
}
191+
catch (...)
192+
{
193+
throwJavaException(env, 0, method_name);
194+
}
195+
196+
return 0;
197+
}
198+
199+
JNIEXPORT jlong JNICALL Java_org_intel_openvino_Blob_rmap(JNIEnv *env, jobject obj, jlong addr)
200+
{
201+
static const char method_name[] = "rmap";
202+
try
203+
{
204+
Blob::Ptr *output = reinterpret_cast<Blob::Ptr *>(addr);
205+
206+
if ((*output)->is<MemoryBlob>()) {
207+
LockedMemory<const void> *lmem = new LockedMemory<const void> (as<MemoryBlob>(*output)->rmap());
208+
return (jlong)lmem;
209+
} else {
210+
throw std::runtime_error("Target Blob cannot be cast to the MemoryBlob!");
211+
}
212+
}
213+
catch (const std::exception &e)
214+
{
215+
throwJavaException(env, &e, method_name);
216+
}
217+
catch (...)
218+
{
219+
throwJavaException(env, 0, method_name);
220+
}
221+
222+
return 0;
223+
}
224+
225+
JNIEXPORT void JNICALL Java_org_intel_openvino_Blob_delete(JNIEnv *, jobject, jlong addr)
226+
{
227+
Blob::Ptr *output = reinterpret_cast<Blob::Ptr *>(addr);
228+
delete output;
229+
}

0 commit comments

Comments
 (0)