- JNIEXPORT jfloatArray JNICALL Java_MicroBrainFaceRec_RecFunctions_FaceRecFeature
- (JNIEnv *env,jobject obj, jint handle,jbyteArray bitmap, jint channels){
- uint8_t *rgba = NULL;
- // Get matrix pointer
- if (NULL != bitmap) {
- rgba = (uint8_t *)env->GetByteArrayElements(bitmap, 0);
- } else {
- LOGE("caffe-jni predict(): invalid args: jrgba(NULL)");
- return nullptr;
- }
- int const rgba_len = env->GetArrayLength(bitmap);
- LOGI("rgba_len= %d\n", rgba_len);
- float *inData = (float*)malloc(rgba_len* sizeof(float));
- //float inData[49152]={};
- size_t plane_size = 128*128;
- for (size_t i = 0; i < plane_size; i++) {
- inData[i] = static_cast<float>(rgba[i * 3 + 2]); // B
- inData[plane_size + i] = static_cast<float>(rgba[i * 3 + 1]); // G
- inData[2 * plane_size + i] = static_cast<float>(rgba[i * 3]); // R
- // Alpha is discarded
- inData[i] = (inData[i] - 127.5) * 0.0078125;
- inData[plane_size + i] = (inData[plane_size + i] - 127.5) * 0.0078125;
- inData[2 * plane_size + i] = (inData[2 * plane_size + i] - 127.5) * 0.0078125;
- }
- //float outFeature[512] = {};
- jfloatArray outFeature = env->NewFloatArray(512);
- jfloat* y0 = env->GetFloatArrayElements(outFeature,0);
- use_rknn::run_rknn(inData,y0);
- env->ReleaseFloatArrayElements(outFeature, y0, 0);
- free(inData);
- return outFeature;
- }
复制代码
其中用rknn设置如下:- bool run_rknn(float *inData, float *y0){
- if(!created) {
- LOGE("run: create hasn't successful!");
- return false;
- }
- rknn_input inputs[1];
- inputs[0].index = input_index;
- inputs[0].buf = inData;
- inputs[0].size = inputs_attr[0].n_elems* sizeof(float);
- //inputs[0].size = Size;
- inputs[0].pass_through = false;
- inputs[0].type = RKNN_TENSOR_FLOAT32;
- inputs[0].fmt = RKNN_TENSOR_NCHW;
- int ret = rknn_inputs_set(ctx, 1, inputs);
- if(ret < 0) {
- LOGE("rknn_input_set fail! ret=%d\n", ret);
- return false;
- }
- ret = rknn_run(ctx, nullptr);
- if(ret < 0) {
- LOGE("rknn_run fail! ret=%d\n", ret);
- return false;
- }
- rknn_output outputs[1];
- outputs[0].want_float = true;
- outputs[0].is_prealloc = true;
- outputs[0].index = output_index0;
- outputs[0].buf = y0;
- outputs[0].size = outputs_attr[0].n_elems * sizeof(float);;
- ret = rknn_outputs_get(ctx, 1, outputs, nullptr);
- if(ret < 0) {
- LOGE("rknn_outputs_get fail! ret=%d\n", ret);
- return false;
- }
- rknn_outputs_release(ctx, 1, outputs);
- return true;
- }
复制代码
运行的时候为什么第二个结果和第一个是一样的,我将两个分别注释以后,运行都可以得到正确的结果,但是同时运行的时候第二个结果总和第一个一样,这是为什么呢?jefferyzhang 发表于 2019-11-26 14:23
多取几次output看下,在async模式下你下一次的输出是这一次输入推理的结果。 ...
jefferyzhang 发表于 2019-11-26 15:15
参看文档:《Rockchip_User_Guide_RKNN_API_V1.2.0_CN》
第 : 3.1.1 rknn_init & rknn_init2
欢迎光临 Toybrick (https://t.rock-chips.com/) | Powered by Discuz! X3.3 |