Browse Source

add gpu button for squeezencnn sample, require api android-24 and ndk-r18b

tags/20190320
nihuini 7 years ago
parent
commit
c1c72ec1b7
7 changed files with 62 additions and 14 deletions
  1. +2
    -2
      examples/squeezencnn/jni/Android.mk
  2. +2
    -6
      examples/squeezencnn/jni/Application.mk
  3. +29
    -2
      examples/squeezencnn/jni/squeezencnn_jni.cpp
  4. +1
    -1
      examples/squeezencnn/project.properties
  5. +6
    -1
      examples/squeezencnn/res/layout/main.xml
  6. +21
    -1
      examples/squeezencnn/src/com/tencent/squeezencnn/MainActivity.java
  7. +1
    -1
      examples/squeezencnn/src/com/tencent/squeezencnn/SqueezeNcnn.java

+ 2
- 2
examples/squeezencnn/jni/Android.mk View File

@@ -1,7 +1,7 @@
LOCAL_PATH := $(call my-dir)

# change this folder path to yours
NCNN_INSTALL_PATH := /home/nihui/osd/ncnn/ncnn-android-lib
NCNN_INSTALL_PATH := /home/nihui/osd/ncnn-release/ncnn-android-vulkan-lib

include $(CLEAR_VARS)
LOCAL_MODULE := ncnn
@@ -25,6 +25,6 @@ LOCAL_CFLAGS += -fopenmp
LOCAL_CPPFLAGS += -fopenmp
LOCAL_LDFLAGS += -fopenmp

LOCAL_LDLIBS := -lz -llog -ljnigraphics
LOCAL_LDLIBS := -lz -llog -ljnigraphics -lvulkan

include $(BUILD_SHARED_LIBRARY)

+ 2
- 6
examples/squeezencnn/jni/Application.mk View File

@@ -1,7 +1,3 @@

# APP_STL := stlport_static
APP_STL := gnustl_static
# APP_ABI := armeabi armeabi-v7a
APP_STL := c++_static
APP_ABI := armeabi-v7a arm64-v8a
APP_PLATFORM := android-9
# NDK_TOOLCHAIN_VERSION := 4.9
APP_PLATFORM := android-24

+ 29
- 2
examples/squeezencnn/jni/squeezencnn_jni.cpp View File

@@ -73,9 +73,29 @@ static std::vector<std::string> split_string(const std::string& str, const std::

extern "C" {

JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved)
{
__android_log_print(ANDROID_LOG_DEBUG, "SqueezeNcnn", "JNI_OnLoad");

ncnn::create_gpu_instance();

return JNI_VERSION_1_4;
}

JNIEXPORT void JNI_OnUnload(JavaVM* vm, void* reserved)
{
__android_log_print(ANDROID_LOG_DEBUG, "SqueezeNcnn", "JNI_OnUnload");

ncnn::destroy_gpu_instance();
}

// public native boolean Init(byte[] param, byte[] bin, byte[] words);
JNIEXPORT jboolean JNICALL Java_com_tencent_squeezencnn_SqueezeNcnn_Init(JNIEnv* env, jobject thiz, jbyteArray param, jbyteArray bin, jbyteArray words)
{
// use vulkan compute
if (ncnn::get_gpu_count() != 0)
squeezenet.use_vulkan_compute = 1;

// init param
{
int len = env->GetArrayLength(param);
@@ -114,9 +134,14 @@ JNIEXPORT jboolean JNICALL Java_com_tencent_squeezencnn_SqueezeNcnn_Init(JNIEnv*
return JNI_TRUE;
}

// public native String Detect(Bitmap bitmap);
JNIEXPORT jstring JNICALL Java_com_tencent_squeezencnn_SqueezeNcnn_Detect(JNIEnv* env, jobject thiz, jobject bitmap)
// public native String Detect(Bitmap bitmap, boolean use_gpu);
JNIEXPORT jstring JNICALL Java_com_tencent_squeezencnn_SqueezeNcnn_Detect(JNIEnv* env, jobject thiz, jobject bitmap, jboolean use_gpu)
{
if (use_gpu == JNI_TRUE && ncnn::get_gpu_count() == 0)
{
return env->NewStringUTF("no vulkan capable gpu");
}

bench_start();

// ncnn from bitmap
@@ -147,6 +172,8 @@ JNIEXPORT jstring JNICALL Java_com_tencent_squeezencnn_SqueezeNcnn_Detect(JNIEnv

ncnn::Extractor ex = squeezenet.create_extractor();

ex.set_vulkan_compute(use_gpu);

ex.input(squeezenet_v1_1_param_id::BLOB_data, in);

ncnn::Mat out;


+ 1
- 1
examples/squeezencnn/project.properties View File

@@ -11,4 +11,4 @@
#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt

# Project target.
target=android-9
target=android-24

+ 6
- 1
examples/squeezencnn/res/layout/main.xml View File

@@ -18,7 +18,12 @@
android:id="@+id/buttonDetect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="识别" />
android:text="识别-cpu" />
<Button
android:id="@+id/buttonDetectGPU"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="识别-gpu" />
</LinearLayout>

<TextView


+ 21
- 1
examples/squeezencnn/src/com/tencent/squeezencnn/MainActivity.java View File

@@ -83,7 +83,27 @@ public class MainActivity extends Activity
if (yourSelectedImage == null)
return;

String result = squeezencnn.Detect(yourSelectedImage);
String result = squeezencnn.Detect(yourSelectedImage, false);

if (result == null)
{
infoResult.setText("detect failed");
}
else
{
infoResult.setText(result);
}
}
});

Button buttonDetectGPU = (Button) findViewById(R.id.buttonDetectGPU);
buttonDetectGPU.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if (yourSelectedImage == null)
return;

String result = squeezencnn.Detect(yourSelectedImage, true);

if (result == null)
{


+ 1
- 1
examples/squeezencnn/src/com/tencent/squeezencnn/SqueezeNcnn.java View File

@@ -21,7 +21,7 @@ public class SqueezeNcnn
{
public native boolean Init(byte[] param, byte[] bin, byte[] words);

public native String Detect(Bitmap bitmap);
public native String Detect(Bitmap bitmap, boolean use_gpu);

static {
System.loadLibrary("squeezencnn");


Loading…
Cancel
Save