Native.cc 3.3 KB
Newer Older
G
Guanghua Yu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "Native.h"
#include "Pipeline.h"

#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     com_baidu_paddle_lite_demo_yolo_detection_Native
 * Method:    nativeInit
 * Signature:
 * (Ljava/lang/String;Ljava/lang/String;ILjava/lang/String;II[F[FF)J
 */
JNIEXPORT jlong JNICALL
Java_com_baidu_paddledetection_detection_Native_nativeInit(
W
wangguanzhong 已提交
29 30 31 32 33 34 35 36 37 38 39
    JNIEnv *env,
    jclass thiz,
    jstring jModelDir,
    jstring jLabelPath,
    jint cpuThreadNum,
    jstring jCPUPowerMode,
    jint inputWidth,
    jint inputHeight,
    jfloatArray jInputMean,
    jfloatArray jInputStd,
    jfloat scoreThreshold) {
G
Guanghua Yu 已提交
40 41 42 43 44
  std::string modelDir = jstring_to_cpp_string(env, jModelDir);
  std::string labelPath = jstring_to_cpp_string(env, jLabelPath);
  std::string cpuPowerMode = jstring_to_cpp_string(env, jCPUPowerMode);
  std::vector<float> inputMean = jfloatarray_to_float_vector(env, jInputMean);
  std::vector<float> inputStd = jfloatarray_to_float_vector(env, jInputStd);
W
wangguanzhong 已提交
45 46 47 48 49 50 51 52 53
  return reinterpret_cast<jlong>(new Pipeline(modelDir,
                                              labelPath,
                                              cpuThreadNum,
                                              cpuPowerMode,
                                              inputWidth,
                                              inputHeight,
                                              inputMean,
                                              inputStd,
                                              scoreThreshold));
G
Guanghua Yu 已提交
54 55 56 57 58 59 60 61
}

/*
 * Class:     com_baidu_paddle_lite_demo_yolo_detection_Native
 * Method:    nativeRelease
 * Signature: (J)Z
 */
JNIEXPORT jboolean JNICALL
W
wangguanzhong 已提交
62 63 64
Java_com_baidu_paddledetection_detection_Native_nativeRelease(JNIEnv *env,
                                                              jclass thiz,
                                                              jlong ctx) {
G
Guanghua Yu 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
  if (ctx == 0) {
    return JNI_FALSE;
  }
  Pipeline *pipeline = reinterpret_cast<Pipeline *>(ctx);
  delete pipeline;
  return JNI_TRUE;
}

/*
 * Class:     com_baidu_paddle_lite_demo_yolo_detection_Native
 * Method:    nativeProcess
 * Signature: (JIIIILjava/lang/String;)Z
 */
JNIEXPORT jboolean JNICALL
Java_com_baidu_paddledetection_detection_Native_nativeProcess(
W
wangguanzhong 已提交
80 81 82 83 84 85 86 87
    JNIEnv *env,
    jclass thiz,
    jlong ctx,
    jint inTextureId,
    jint outTextureId,
    jint textureWidth,
    jint textureHeight,
    jstring jsavedImagePath) {
G
Guanghua Yu 已提交
88 89 90 91 92
  if (ctx == 0) {
    return JNI_FALSE;
  }
  std::string savedImagePath = jstring_to_cpp_string(env, jsavedImagePath);
  Pipeline *pipeline = reinterpret_cast<Pipeline *>(ctx);
W
wangguanzhong 已提交
93 94
  return pipeline->Process(
      inTextureId, outTextureId, textureWidth, textureHeight, savedImagePath);
G
Guanghua Yu 已提交
95 96 97 98 99
}

#ifdef __cplusplus
}
#endif