From: Jaeyun Date: Wed, 25 Sep 2019 07:31:43 +0000 (+0900) Subject: [Android/Sample] move sample app to nnstreamere-example repo X-Git-Tag: submit/tizen/20190930.043252~7 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=e13e6dd85e0d05a9c88501585921dda5692f146f;p=platform%2Fupstream%2Fnnstreamer.git [Android/Sample] move sample app to nnstreamere-example repo Remove sample app in this repo. I newly uploaded the sample code to use Android API in example repository. Signed-off-by: Jaeyun Jung --- diff --git a/api/android/sample/build.gradle b/api/android/sample/build.gradle deleted file mode 100644 index 6e3321b..0000000 --- a/api/android/sample/build.gradle +++ /dev/null @@ -1,27 +0,0 @@ -apply plugin: 'com.android.application' - -android { - compileSdkVersion 28 - defaultConfig { - applicationId "com.samsung.android.nnstreamer.sample" - minSdkVersion 24 - targetSdkVersion 28 - versionCode 1 - versionName "1.0" - } - buildTypes { - release { - minifyEnabled false - proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' - } - } - buildToolsVersion '28.0.3' -} - -dependencies { - implementation fileTree(include: ['*.aar'], dir: 'libs') - implementation 'com.android.support:appcompat-v7:28.0.0' - implementation 'com.android.support.constraint:constraint-layout:1.1.3' - // Set latest version of NNStreamer API - // implementation 'org.nnsuite:nnstreamer:0.0.1' -} diff --git a/api/android/sample/libs/Readme.txt b/api/android/sample/libs/Readme.txt deleted file mode 100644 index 8ac7b1e..0000000 --- a/api/android/sample/libs/Readme.txt +++ /dev/null @@ -1 +0,0 @@ -To run this sample, copy nnstreamer-api library file into 'libs' directory. diff --git a/api/android/sample/proguard-rules.pro b/api/android/sample/proguard-rules.pro deleted file mode 100644 index f1b4245..0000000 --- a/api/android/sample/proguard-rules.pro +++ /dev/null @@ -1,21 +0,0 @@ -# Add project specific ProGuard rules here. -# You can control the set of applied configuration files using the -# proguardFiles setting in build.gradle. -# -# For more details, see -# http://developer.android.com/guide/developing/tools/proguard.html - -# If your project uses WebView with JS, uncomment the following -# and specify the fully qualified class name to the JavaScript interface -# class: -#-keepclassmembers class fqcn.of.javascript.interface.for.webview { -# public *; -#} - -# Uncomment this to preserve the line number information for -# debugging stack traces. -#-keepattributes SourceFile,LineNumberTable - -# If you keep the line number information, uncomment this to -# hide the original source file name. -#-renamesourcefileattribute SourceFile diff --git a/api/android/sample/src/main/AndroidManifest.xml b/api/android/sample/src/main/AndroidManifest.xml deleted file mode 100644 index d2e3dec..0000000 --- a/api/android/sample/src/main/AndroidManifest.xml +++ /dev/null @@ -1,20 +0,0 @@ - - - - - - - - - - - - - - diff --git a/api/android/sample/src/main/java/org/nnsuite/nnstreamer/sample/MainActivity.java b/api/android/sample/src/main/java/org/nnsuite/nnstreamer/sample/MainActivity.java deleted file mode 100644 index 6750f9a..0000000 --- a/api/android/sample/src/main/java/org/nnsuite/nnstreamer/sample/MainActivity.java +++ /dev/null @@ -1,652 +0,0 @@ -package org.nnsuite.nnstreamer.sample; - -import android.Manifest; -import android.app.Activity; -import android.content.pm.PackageManager; -import android.os.Bundle; -import android.os.CountDownTimer; -import android.os.Environment; -import android.support.v4.app.ActivityCompat; -import android.support.v4.content.ContextCompat; -import android.util.Log; - -import org.nnsuite.nnstreamer.CustomFilter; -import org.nnsuite.nnstreamer.NNStreamer; -import org.nnsuite.nnstreamer.Pipeline; -import org.nnsuite.nnstreamer.SingleShot; -import org.nnsuite.nnstreamer.TensorsData; -import org.nnsuite.nnstreamer.TensorsInfo; - -import java.io.File; -import java.nio.ByteBuffer; - -/** - * Sample code to run the application with nnstreamer-api. - * Before building this sample, copy nnstreamer-api library file into 'libs' directory. - */ -public class MainActivity extends Activity { - private static final String TAG = "NNStreamer-Sample"; - - private static final int PERMISSION_REQUEST_CODE = 3; - private static final String[] requiredPermissions = new String[] { - Manifest.permission.READ_EXTERNAL_STORAGE - }; - - private boolean initialized = false; - private boolean isFailed = false; - private CountDownTimer exampleTimer = null; - private int exampleRun = 0; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - setContentView(R.layout.activity_main); - - /* check permissions */ - for (String permission : requiredPermissions) { - if (!checkPermission(permission)) { - ActivityCompat.requestPermissions(this, - requiredPermissions, PERMISSION_REQUEST_CODE); - return; - } - } - - initNNStreamer(); - } - - @Override - public void onResume() { - super.onResume(); - - if (initialized) { - /* set timer to run examples */ - exampleRun = 0; - isFailed = false; - setExampleTimer(200); - } - } - - @Override - public void onPause() { - super.onPause(); - - stopExampleTimer(); - } - - /** - * Check the permission is granted. - */ - private boolean checkPermission(final String permission) { - return (ContextCompat.checkSelfPermission(this, permission) - == PackageManager.PERMISSION_GRANTED); - } - - @Override - public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { - if (requestCode == PERMISSION_REQUEST_CODE) { - for (int grant : grantResults) { - if (grant != PackageManager.PERMISSION_GRANTED) { - Log.i(TAG, "Permission denied, close app."); - finish(); - return; - } - } - - initNNStreamer(); - return; - } - - finish(); - } - - /** - * Initialize NNStreamer. - */ - private void initNNStreamer() { - if (initialized) { - return; - } - - try { - initialized = NNStreamer.initialize(this); - } catch(Exception e) { - e.printStackTrace(); - Log.e(TAG, e.getMessage()); - } finally { - if (initialized) { - Log.i(TAG, "Version: " + NNStreamer.getVersion()); - } else { - Log.e(TAG, "Failed to initialize NNStreamer"); - finish(); - } - } - } - - /** - * Set timer to run the examples. - */ - private void setExampleTimer(long time) { - stopExampleTimer(); - - exampleTimer = new CountDownTimer(time, time) { - @Override - public void onTick(long millisUntilFinished) { - } - - @Override - public void onFinish() { - /* run the examples repeatedly */ - if (exampleRun > 5) { - Log.d(TAG, "Stop timer to run example"); - - if (isFailed) { - Log.d(TAG, "Error occurs while running the examples"); - } - - return; - } - - int option = (exampleRun % 6); - - if (option == 1) { - Log.d(TAG, "==== Run pipeline example with state callback ===="); - runPipe(true); - } else if (option == 2) { - Log.d(TAG, "==== Run pipeline example ===="); - runPipe(false); - } else if (option == 3) { - Log.d(TAG, "==== Run pipeline example with valve ===="); - runPipeValve(); - } else if (option == 4) { - Log.d(TAG, "==== Run pipeline example with switch ===="); - runPipeSwitch(); - } else if (option == 5) { - Log.d(TAG, "==== Run pipeline example with custom filter ===="); - runPipeCustomFilter(); - } else { - Log.d(TAG, "==== Run single-shot example ===="); - runSingle(); - } - - exampleRun++; - setExampleTimer(500); - } - }; - - exampleTimer.start(); - } - - /** - * Cancel example timer. - */ - private void stopExampleTimer() { - if (exampleTimer != null) { - exampleTimer.cancel(); - exampleTimer = null; - } - } - - /** - * Print tensors info. - * - * The data type of tensor in NNStreamer: - * {@link NNStreamer#TENSOR_TYPE_INT32} - * {@link NNStreamer#TENSOR_TYPE_UINT32} - * {@link NNStreamer#TENSOR_TYPE_INT16} - * {@link NNStreamer#TENSOR_TYPE_UINT16} - * {@link NNStreamer#TENSOR_TYPE_INT8} - * {@link NNStreamer#TENSOR_TYPE_UINT8} - * {@link NNStreamer#TENSOR_TYPE_FLOAT64} - * {@link NNStreamer#TENSOR_TYPE_FLOAT32} - * {@link NNStreamer#TENSOR_TYPE_UNKNOWN} - * - * The maximum rank that NNStreamer supports. - * {@link NNStreamer#TENSOR_RANK_LIMIT} - * - * The maximum number of tensor instances that tensors may have. - * {@link NNStreamer#TENSOR_SIZE_LIMIT} - */ - private void printTensorsInfo(TensorsInfo info) { - int num = info.getTensorsCount(); - - Log.d(TAG, "The number of tensors in info: " + num); - for (int i = 0; i < num; i++) { - int[] dim = info.getTensorDimension(i); - - Log.d(TAG, "Info index " + i + - " name: " + info.getTensorName(0) + - " type: " + info.getTensorType(0) + - " dim: " + dim[0] + ":" + dim[1] + ":" + dim[2] + ":" + dim[3]); - } - } - - /** - * Print tensors data. - * - * The maximum number of tensor instances that tensors may have. - * {@link NNStreamer#TENSOR_SIZE_LIMIT} - */ - private void printTensorsData(TensorsData data) { - int num = data.getTensorsCount(); - - Log.d(TAG, "The number of tensors in data: " + num); - for (int i = 0; i < num; i++) { - ByteBuffer buffer = data.getTensorData(i); - - Log.d(TAG, "Data index " + i + " received " + buffer.capacity()); - } - } - - /** - * Example to run single-shot. - */ - private void runSingle() { - /* example with image classification tf-lite model */ - String root = Environment.getExternalStorageDirectory().getAbsolutePath(); - File model = new File(root + "/nnstreamer/tflite_model_img/mobilenet_v1_1.0_224_quant.tflite"); - - if (!model.exists()) { - Log.w(TAG, "Cannot find the model file"); - return; - } - - try { - SingleShot single = new SingleShot(model); - - Log.d(TAG, "Get input tensors info"); - TensorsInfo inInfo = single.getInputInfo(); - printTensorsInfo(inInfo); - - Log.d(TAG, "Get output tensors info"); - TensorsInfo outInfo = single.getOutputInfo(); - printTensorsInfo(outInfo); - - /* set timeout (1 second) */ - single.setTimeout(1000); - - /* single-shot invoke */ - for (int i = 0; i < 15; i++) { - /* dummy input */ - TensorsData in = TensorsData.allocate(inInfo); - - Log.d(TAG, "Try to invoke data " + (i + 1)); - - TensorsData out = single.invoke(in); - printTensorsData(out); - - Thread.sleep(50); - } - - single.close(); - } catch (Exception e) { - e.printStackTrace(); - Log.e(TAG, e.getMessage()); - isFailed = true; - } - } - - /** - * Example to run pipeline. - * - * The state of pipeline: - * {@link NNStreamer#PIPELINE_STATE_UNKNOWN} - * {@link NNStreamer#PIPELINE_STATE_NULL} - * {@link NNStreamer#PIPELINE_STATE_READY} - * {@link NNStreamer#PIPELINE_STATE_PAUSED} - * {@link NNStreamer#PIPELINE_STATE_PLAYING} - */ - private void runPipe(boolean addStateCb) { - /* example with image classification tf-lite model */ - String root = Environment.getExternalStorageDirectory().getAbsolutePath(); - File model = new File(root + "/nnstreamer/tflite_model_img/mobilenet_v1_1.0_224_quant.tflite"); - - if (!model.exists()) { - Log.w(TAG, "Cannot find the model file"); - return; - } - - try { - String desc = "appsrc name=srcx ! other/tensor,dimension=(string)3:224:224:1,type=(string)uint8,framerate=(fraction)0/1 ! " + - "tensor_filter framework=tensorflow-lite model=" + model.getAbsolutePath() + " ! " + - "tensor_sink name=sinkx"; - - /* pipeline state callback */ - Pipeline.StateChangeCallback stateCb = null; - - if (addStateCb) { - stateCb = new Pipeline.StateChangeCallback() { - @Override - public void onStateChanged(int state) { - Log.d(TAG, "The pipeline state changed to " + state); - } - }; - } - - Pipeline pipe = new Pipeline(desc, stateCb); - - /* register sink callback */ - pipe.setSinkCallback("sinkx", new Pipeline.NewDataCallback() { - int received = 0; - - @Override - public void onNewDataReceived(TensorsData data, TensorsInfo info) { - Log.d(TAG, "Received new data callback " + (++received)); - - printTensorsInfo(info); - printTensorsData(data); - } - }); - - Log.d(TAG, "Current state is " + pipe.getState()); - - /* start pipeline */ - pipe.start(); - - /* push input buffer */ - for (int i = 0; i < 15; i++) { - /* dummy input */ - TensorsData in = new TensorsData(); - in.addTensorData(TensorsData.allocateByteBuffer(3 * 224 * 224)); - - Log.d(TAG, "Push input data " + (i + 1)); - - pipe.inputData("srcx", in); - Thread.sleep(50); - } - - Log.d(TAG, "Current state is " + pipe.getState()); - - pipe.close(); - } catch (Exception e) { - e.printStackTrace(); - Log.e(TAG, e.getMessage()); - isFailed = true; - } - } - - /** - * Example to run pipeline with valve. - */ - private void runPipeValve() { - try { - String desc = "appsrc name=srcx ! other/tensor,dimension=(string)3:100:100:1,type=(string)uint8,framerate=(fraction)0/1 ! " + - "tee name=t " + - "t. ! queue ! tensor_sink name=sink1 " + - "t. ! queue ! valve name=valvex ! tensor_sink name=sink2"; - - Pipeline pipe = new Pipeline(desc); - - /* register sink callback */ - pipe.setSinkCallback("sink1", new Pipeline.NewDataCallback() { - int received = 0; - - @Override - public void onNewDataReceived(TensorsData data, TensorsInfo info) { - Log.d(TAG, "Received new data callback at sink1 " + (++received)); - - printTensorsInfo(info); - printTensorsData(data); - } - }); - - pipe.setSinkCallback("sink2", new Pipeline.NewDataCallback() { - int received = 0; - - @Override - public void onNewDataReceived(TensorsData data, TensorsInfo info) { - Log.d(TAG, "Received new data callback at sink2 " + (++received)); - - printTensorsInfo(info); - printTensorsData(data); - } - }); - - /* start pipeline */ - pipe.start(); - - /* push input buffer */ - for (int i = 0; i < 15; i++) { - /* dummy input */ - TensorsData in = new TensorsData(); - in.addTensorData(TensorsData.allocateByteBuffer(3 * 100 * 100)); - - Log.d(TAG, "Push input data " + (i + 1)); - - pipe.inputData("srcx", in); - Thread.sleep(50); - - if (i == 10) { - /* close valve */ - pipe.controlValve("valvex", false); - } - } - - pipe.close(); - } catch (Exception e) { - e.printStackTrace(); - Log.e(TAG, e.getMessage()); - isFailed = true; - } - } - - /** - * Example to run pipeline with output-selector. - */ - private void runPipeSwitch() { - try { - /* Note that the sink element needs option 'async=false' - * - * Prerolling problem - * For running the pipeline, set async=false in the sink element when using an output selector. - * The pipeline state can be changed to paused after all sink element receive buffer. - */ - String desc = "appsrc name=srcx ! other/tensor,dimension=(string)3:100:100:1,type=(string)uint8,framerate=(fraction)0/1 ! " + - "output-selector name=outs " + - "outs.src_0 ! tensor_sink name=sink1 async=false " + - "outs.src_1 ! tensor_sink name=sink2 async=false"; - - Pipeline pipe = new Pipeline(desc); - - /* register sink callback */ - pipe.setSinkCallback("sink1", new Pipeline.NewDataCallback() { - int received = 0; - - @Override - public void onNewDataReceived(TensorsData data, TensorsInfo info) { - Log.d(TAG, "Received new data callback at sink1 " + (++received)); - - printTensorsInfo(info); - printTensorsData(data); - } - }); - - pipe.setSinkCallback("sink2", new Pipeline.NewDataCallback() { - int received = 0; - - @Override - public void onNewDataReceived(TensorsData data, TensorsInfo info) { - Log.d(TAG, "Received new data callback at sink2 " + (++received)); - - printTensorsInfo(info); - printTensorsData(data); - } - }); - - /* start pipeline */ - pipe.start(); - - /* push input buffer */ - for (int i = 0; i < 15; i++) { - /* dummy input */ - TensorsData in = new TensorsData(); - in.addTensorData(TensorsData.allocateByteBuffer(3 * 100 * 100)); - - Log.d(TAG, "Push input data " + (i + 1)); - - pipe.inputData("srcx", in); - Thread.sleep(50); - - if (i == 10) { - /* select pad */ - pipe.selectSwitchPad("outs", "src_1"); - } - } - - /* get pad list of output-selector */ - String[] pads = pipe.getSwitchPads("outs"); - Log.d(TAG, "Total pad in output-selector: " + pads.length); - for (String pad : pads) { - Log.d(TAG, "Pad name: " + pad); - } - - pipe.close(); - } catch (Exception e) { - e.printStackTrace(); - Log.e(TAG, e.getMessage()); - isFailed = true; - } - } - - /** - * Example to run pipeline with custom filter. - */ - private void runPipeCustomFilter() { - try { - /* register custom-filter (passthrough) */ - CustomFilter customPassthrough = CustomFilter.registerCustomFilter("custom-passthrough", - new CustomFilter.CustomFilterCallback() { - @Override - public TensorsInfo getOutputInfo(TensorsInfo inInfo) { - Log.d(TAG, "Received info callback in custom-passthrough"); - return inInfo; - } - - @Override - public TensorsData invoke(TensorsData inData, TensorsInfo inInfo, TensorsInfo outInfo) { - Log.d(TAG, "Received invoke callback in custom-passthrough"); - return inData; - } - }); - - /* register custom-filter (convert data type to float) */ - CustomFilter customConvert = CustomFilter.registerCustomFilter("custom-convert", - new CustomFilter.CustomFilterCallback() { - @Override - public TensorsInfo getOutputInfo(TensorsInfo inInfo) { - Log.d(TAG, "Received info callback in custom-convert"); - - TensorsInfo out = inInfo; - out.setTensorType(0, NNStreamer.TENSOR_TYPE_FLOAT32); - - return out; - } - - @Override - public TensorsData invoke(TensorsData inData, TensorsInfo inInfo, TensorsInfo outInfo) { - Log.d(TAG, "Received invoke callback in custom-convert"); - - ByteBuffer input = inData.getTensorData(0); - ByteBuffer output = TensorsData.allocateByteBuffer(4 * 10); - - for (int i = 0; i < 10; i++) { - float value = (float) input.getInt(i * 4); - output.putFloat(i * 4, value); - } - - TensorsData out = new TensorsData(); - out.addTensorData(output); - - return out; - } - }); - - /* register custom-filter (add constant) */ - CustomFilter customAdd = CustomFilter.registerCustomFilter("custom-add", - new CustomFilter.CustomFilterCallback() { - @Override - public TensorsInfo getOutputInfo(TensorsInfo inInfo) { - Log.d(TAG, "Received info callback in custom-add"); - return inInfo; - } - - @Override - public TensorsData invoke(TensorsData inData, TensorsInfo inInfo, TensorsInfo outInfo) { - Log.d(TAG, "Received invoke callback in custom-add"); - - ByteBuffer input = inData.getTensorData(0); - ByteBuffer output = TensorsData.allocateByteBuffer(4 * 10); - - for (int i = 0; i < 10; i++) { - float value = input.getFloat(i * 4); - - /* add constant */ - value += 1.5; - output.putFloat(i * 4, value); - } - - TensorsData out = new TensorsData(); - out.addTensorData(output); - - return out; - } - }); - - String desc = "appsrc name=srcx ! other/tensor,dimension=(string)10:1:1:1,type=(string)int32,framerate=(fraction)0/1 ! " + - "tensor_filter framework=" + customPassthrough.getName() + " ! " + - "tensor_filter framework=" + customConvert.getName() + " ! " + - "tensor_filter framework=" + customAdd.getName() + " ! " + - "tensor_sink name=sinkx"; - - Pipeline pipe = new Pipeline(desc); - - /* register sink callback */ - pipe.setSinkCallback("sinkx", new Pipeline.NewDataCallback() { - int received = 0; - - @Override - public void onNewDataReceived(TensorsData data, TensorsInfo info) { - Log.d(TAG, "Received new data callback at sinkx " + (++received)); - - printTensorsInfo(info); - printTensorsData(data); - - ByteBuffer output = data.getTensorData(0); - - for (int i = 0; i < 10; i++) { - Log.d(TAG, "Received data: index " + i + " value " + output.getFloat(i * 4)); - } - } - }); - - /* start pipeline */ - pipe.start(); - - /* push input buffer */ - for (int i = 0; i < 15; i++) { - ByteBuffer input = TensorsData.allocateByteBuffer(4 * 10); - - for (int j = 0; j < 10; j++) { - input.putInt(j * 4, j); - } - - TensorsData in = new TensorsData(); - in.addTensorData(input); - - pipe.inputData("srcx", in); - Thread.sleep(50); - } - - pipe.close(); - - /* close custom-filter */ - customPassthrough.close(); - customConvert.close(); - customAdd.close(); - } catch (Exception e) { - e.printStackTrace(); - Log.e(TAG, e.getMessage()); - isFailed = true; - } - } -} diff --git a/api/android/sample/src/main/res/drawable/nnsuite_logo.png b/api/android/sample/src/main/res/drawable/nnsuite_logo.png deleted file mode 100644 index 723f278..0000000 Binary files a/api/android/sample/src/main/res/drawable/nnsuite_logo.png and /dev/null differ diff --git a/api/android/sample/src/main/res/layout/activity_main.xml b/api/android/sample/src/main/res/layout/activity_main.xml deleted file mode 100644 index 6682bf8..0000000 --- a/api/android/sample/src/main/res/layout/activity_main.xml +++ /dev/null @@ -1,18 +0,0 @@ - - - - - - diff --git a/api/android/sample/src/main/res/values/colors.xml b/api/android/sample/src/main/res/values/colors.xml deleted file mode 100644 index 69b2233..0000000 --- a/api/android/sample/src/main/res/values/colors.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - #008577 - #00574B - #D81B60 - diff --git a/api/android/sample/src/main/res/values/strings.xml b/api/android/sample/src/main/res/values/strings.xml deleted file mode 100644 index eddf1b7..0000000 --- a/api/android/sample/src/main/res/values/strings.xml +++ /dev/null @@ -1,3 +0,0 @@ - - NNStreamer API Sample - diff --git a/api/android/sample/src/main/res/values/styles.xml b/api/android/sample/src/main/res/values/styles.xml deleted file mode 100644 index 5885930..0000000 --- a/api/android/sample/src/main/res/values/styles.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - -