From cdc87140a6f75ae90019180b3b4c842465dd58b3 Mon Sep 17 00:00:00 2001 From: wangliu Date: Thu, 12 Jul 2018 15:17:17 +0800 Subject: [PATCH] modify code style --- src/jni/paddle_mobile_jni.cpp | 170 +++++++++++++++++----------------- src/jni/paddle_mobile_jni.h | 9 +- 2 files changed, 91 insertions(+), 88 deletions(-) diff --git a/src/jni/paddle_mobile_jni.cpp b/src/jni/paddle_mobile_jni.cpp index 47dbc08392..b28244c791 100644 --- a/src/jni/paddle_mobile_jni.cpp +++ b/src/jni/paddle_mobile_jni.cpp @@ -69,15 +69,16 @@ JNIEXPORT jboolean JNICALL Java_com_baidu_paddle_PML_loadCombined( optimize); } -JNIEXPORT jfloatArray JNICALL -Java_com_baidu_paddle_PML_predictImage(JNIEnv *env, jclass thiz, jfloatArray buf, jintArray ddims) { +JNIEXPORT jfloatArray JNICALL Java_com_baidu_paddle_PML_predictImage( + JNIEnv *env, jclass thiz, jfloatArray buf, jintArray ddims) { ANDROIDLOGI("predictImage invoked"); jsize ddim_size = env->GetArrayLength(ddims); - if(ddim_size != 4) { - ANDROIDLOGE("ddims size not equal to 4"); + if (ddim_size != 4) { + ANDROIDLOGE("ddims size not equal to 4"); } jint *ddim_ptr = env->GetIntArrayElements(ddims, NULL); - framework::DDim ddim = framework::make_ddim({ddim_ptr[0], ddim_ptr[1], ddim_ptr[2], ddim_ptr[3]}); + framework::DDim ddim = framework::make_ddim( + {ddim_ptr[0], ddim_ptr[1], ddim_ptr[2], ddim_ptr[3]}); int length = framework::product(ddim); jfloatArray result = NULL; int count = 0; @@ -95,93 +96,94 @@ Java_com_baidu_paddle_PML_predictImage(JNIEnv *env, jclass thiz, jfloatArray buf count = output->numel(); result = env->NewFloatArray(count); env->SetFloatArrayRegion(result, 0, count, output->data()); - env->ReleaseIntArrayElements(ddims,ddim_ptr, 0); + env->ReleaseIntArrayElements(ddims, ddim_ptr, 0); ANDROIDLOGI("predictImage finished"); return result; } -inline int yuv_to_rgb(int y, int u, int v, float *r, float *g, float *b) - { - int r1 = (int)(y + 1.370705 * (v - 128)); - int g1 = (int)(y - 0.698001 * (u - 128) - 0.703125 * (v - 128)); - int b1 = (int)(y + 1.732446 * (u - 128)); +inline int yuv_to_rgb(int y, int u, int v, float *r, float *g, float *b) { + int r1 = (int)(y + 1.370705 * (v - 128)); + int g1 = (int)(y - 0.698001 * (u - 128) - 0.703125 * (v - 128)); + int b1 = (int)(y + 1.732446 * (u - 128)); - r1 = (int) fminf(255, fmaxf(0, r1)); - g1 = (int) fminf(255, fmaxf(0, g1)); - b1 = (int) fminf(255, fmaxf(0, b1)); - *r = r1; - *g = g1; - *b = b1; + r1 = (int)fminf(255, fmaxf(0, r1)); + g1 = (int)fminf(255, fmaxf(0, g1)); + b1 = (int)fminf(255, fmaxf(0, b1)); + *r = r1; + *g = g1; + *b = b1; - return 0; - } -void convert_nv21_to_matrix(uint8_t *nv21, float *matrix, int width, int height, int targetWidth, int targetHeight, float *means) { - const uint8_t *yData = nv21; - const uint8_t *vuData = nv21 + width * height; - - const int yRowStride = width; - const int vuRowStride = width; - - float scale_x = width * 1.0 / targetWidth; - float scale_y = height * 1.0 / targetHeight; - - for (int j = 0; j < targetHeight; ++j) { - int y = j * scale_y; - const uint8_t *pY = yData + y * yRowStride; - const uint8_t *pVU = vuData + (y >> 1) * vuRowStride; - for (int i = 0; i < targetWidth; ++i) { - int x = i * scale_x; - const int offset = ((x >> 1) << 1); - float r = 0; - float g = 0; - float b = 0; - yuv_to_rgb(pY[x], pVU[offset + 1], pVU[offset], &r, &g, &b); - int r_index = j * targetWidth + i; - int g_index = r_index + targetWidth * targetHeight; - int b_index = g_index + targetWidth * targetHeight; - matrix[r_index] = r - means[0]; - matrix[g_index] = g - means[1]; - matrix[b_index] = b - means[2]; - } - } - } - -JNIEXPORT jfloatArray JNICALL -Java_com_baidu_paddle_PML_predictYuv(JNIEnv *env, jclass thiz, jbyteArray yuv_, jint imgwidth, jint imgHeight, jintArray ddims, -jfloatArray meanValues) { - ANDROIDLOGI("predictYuv invoked"); - jsize ddim_size = env->GetArrayLength(ddims); - if(ddim_size != 4) { - ANDROIDLOGE("ddims size not equal to 4"); - } - jint *ddim_ptr = env->GetIntArrayElements(ddims, NULL); - framework::DDim ddim = framework::make_ddim({ddim_ptr[0], ddim_ptr[1], ddim_ptr[2], ddim_ptr[3]}); - int length = framework::product(ddim); - float matrix[length]; - jbyte *yuv = env->GetByteArrayElements(yuv_, NULL); - float *meansPointer = nullptr; - if (nullptr != meanValues) { - meansPointer = env->GetFloatArrayElements(meanValues, NULL); - } - convert_nv21_to_matrix((uint8_t *)yuv,matrix, imgwidth, imgHeight, ddim[3], ddim[2], meansPointer ); - jfloatArray result = NULL; - int count = 0; - framework::Tensor input; - input.Resize(ddim); - auto input_ptr = input.mutable_data(); - for (int i = 0; i < length; i++) { - input_ptr[i] = matrix[i]; + return 0; +} +void convert_nv21_to_matrix(uint8_t *nv21, float *matrix, int width, int height, + int targetWidth, int targetHeight, float *means) { + const uint8_t *yData = nv21; + const uint8_t *vuData = nv21 + width * height; + + const int yRowStride = width; + const int vuRowStride = width; + + float scale_x = width * 1.0 / targetWidth; + float scale_y = height * 1.0 / targetHeight; + + for (int j = 0; j < targetHeight; ++j) { + int y = j * scale_y; + const uint8_t *pY = yData + y * yRowStride; + const uint8_t *pVU = vuData + (y >> 1) * vuRowStride; + for (int i = 0; i < targetWidth; ++i) { + int x = i * scale_x; + const int offset = ((x >> 1) << 1); + float r = 0; + float g = 0; + float b = 0; + yuv_to_rgb(pY[x], pVU[offset + 1], pVU[offset], &r, &g, &b); + int r_index = j * targetWidth + i; + int g_index = r_index + targetWidth * targetHeight; + int b_index = g_index + targetWidth * targetHeight; + matrix[r_index] = r - means[0]; + matrix[g_index] = g - means[1]; + matrix[b_index] = b - means[2]; } - auto output = shared_paddle_mobile_instance->Predict(input); - count = output->numel(); - result = env->NewFloatArray(count); - env->SetFloatArrayRegion(result, 0, count, output->data()); - env->ReleaseByteArrayElements(yuv_, yuv, 0); - env->ReleaseIntArrayElements(ddims, ddim_ptr, 0); - env->ReleaseFloatArrayElements(meanValues, meansPointer, 0); - ANDROIDLOGI("predictYuv finished"); - return result; + } +} +JNIEXPORT jfloatArray JNICALL Java_com_baidu_paddle_PML_predictYuv( + JNIEnv *env, jclass thiz, jbyteArray yuv_, jint imgwidth, jint imgHeight, + jintArray ddims, jfloatArray meanValues) { + ANDROIDLOGI("predictYuv invoked"); + jsize ddim_size = env->GetArrayLength(ddims); + if (ddim_size != 4) { + ANDROIDLOGE("ddims size not equal to 4"); + } + jint *ddim_ptr = env->GetIntArrayElements(ddims, NULL); + framework::DDim ddim = framework::make_ddim( + {ddim_ptr[0], ddim_ptr[1], ddim_ptr[2], ddim_ptr[3]}); + int length = framework::product(ddim); + float matrix[length]; + jbyte *yuv = env->GetByteArrayElements(yuv_, NULL); + float *meansPointer = nullptr; + if (nullptr != meanValues) { + meansPointer = env->GetFloatArrayElements(meanValues, NULL); + } + convert_nv21_to_matrix((uint8_t *)yuv, matrix, imgwidth, imgHeight, ddim[3], + ddim[2], meansPointer); + jfloatArray result = NULL; + int count = 0; + framework::Tensor input; + input.Resize(ddim); + auto input_ptr = input.mutable_data(); + for (int i = 0; i < length; i++) { + input_ptr[i] = matrix[i]; + } + auto output = shared_paddle_mobile_instance->Predict(input); + count = output->numel(); + result = env->NewFloatArray(count); + env->SetFloatArrayRegion(result, 0, count, output->data()); + env->ReleaseByteArrayElements(yuv_, yuv, 0); + env->ReleaseIntArrayElements(ddims, ddim_ptr, 0); + env->ReleaseFloatArrayElements(meanValues, meansPointer, 0); + ANDROIDLOGI("predictYuv finished"); + return result; } JNIEXPORT void JNICALL Java_com_baidu_paddle_PML_clear(JNIEnv *env, diff --git a/src/jni/paddle_mobile_jni.h b/src/jni/paddle_mobile_jni.h index 92a0e40590..a830ab43c8 100644 --- a/src/jni/paddle_mobile_jni.h +++ b/src/jni/paddle_mobile_jni.h @@ -36,14 +36,15 @@ JNIEXPORT jboolean JNICALL Java_com_baidu_paddle_PML_loadCombined( /** * object detection for anroid */ -JNIEXPORT jfloatArray JNICALL -Java_com_baidu_paddle_PML_predictImage(JNIEnv *env, jclass thiz, jfloatArray buf, jintArray ddims); +JNIEXPORT jfloatArray JNICALL Java_com_baidu_paddle_PML_predictImage( + JNIEnv *env, jclass thiz, jfloatArray buf, jintArray ddims); /** * object detection for anroid */ -JNIEXPORT jfloatArray JNICALL -Java_com_baidu_paddle_PML_predictYuv(JNIEnv *env, jclass thiz, jbyteArray yuv, jint imgwidth, jint imgHeight, jintArray ddims, jfloatArray meanValues); +JNIEXPORT jfloatArray JNICALL Java_com_baidu_paddle_PML_predictYuv( + JNIEnv *env, jclass thiz, jbyteArray yuv, jint imgwidth, jint imgHeight, + jintArray ddims, jfloatArray meanValues); /** * object detection for anroid -- GitLab