tree: eed8093cb755b2532aabf8f171bad63b0dbcf395 [path history] [tgz]
  1. app/
  2. dev_tools/
  3. models/
  4. .gitignore
  5. build.gradle
  6. gradle.properties
  7. README.md
  8. settings.gradle
apps/android_camera/README.md

Android Camera Demo Sample App

The Android Camera Demo Sample App provides a basic implementation of an Android app that uses the tvm runtime to perform image classification in real time.

You will need JDK, Android NDK and an Android device to use this.

Build and Installation

Prepare Models

The models/prepare_models.py script provides a example flow for dumping model parameter files for use by the app.

  1. Set path to the NDK CC: export TVM_NDK_CC=[Path to CC, e.g. /opt/android-toolchain-arm64/bin/aarch64-linux-android-g++]
  2. Switch to the script directory: cd models
  3. Run script: python3 prepare_model.py

Sample output

mobilenet_v2
getting model...
building...
dumping lib...
dumping graph...
dumping params...
dumping labels...
resnet18_v1
getting model...
building...
dumping lib...
dumping graph...
dumping params...
dumping labels...

Build APK

We use Gradle to build. Please follow the installation instruction for your operating system.

Before you build the Android application, please refer to TVM4J Installation Guide and install tvm4j-core to your local maven repository. You can find tvm4j dependency declare in app/build.gradle. Modify it if it is necessary.

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    androidTestImplementation('androidx.test.espresso:espresso-core:3.2.0', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    implementation 'androidx.appcompat:appcompat:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
    implementation 'com.google.android.material:material:1.5.0'
    implementation 'org.apache.tvm:tvm4j-core:0.0.1-SNAPSHOT'
    testImplementation 'junit:junit:4.13.2'

    implementation "androidx.concurrent:concurrent-futures:1.0.0"
    implementation "androidx.camera:camera-core:1.0.0-beta01"
    implementation "androidx.camera:camera-camera2:1.0.0-beta01"
    implementation "androidx.camera:camera-view:1.0.0-alpha08"
    implementation "androidx.camera:camera-extensions:1.0.0-alpha08"
    implementation "androidx.camera:camera-lifecycle:1.0.0-beta01"
}

Now use Gradle to compile JNI, resolve Java dependencies and build the Android application together with tvm4j. Run following script to generate the apk file.

export ANDROID_HOME=[Path to your Android SDK, e.g., ~/Android/sdk]
cd apps/android_camera
gradle clean build

In app/build/outputs/apk you'll find app-release-unsigned.apk, use dev_tools/gen_keystore.sh to generate a signature and use dev_tools/sign_apk.sh to get the signed apk file app/build/outputs/apk/release/tv8mdemo-release.apk.

Upload tv8mdemo-release.apk to your Android device and install it:

$ANDROID_HOME/platform-tools/adb install app/build/outputs/apk/release/tv8mdemo-release.apk

If you see error:

adb: failed to install app/build/outputs/apk/release/tv8mdemo-release.apk:
  Failure [INSTALL_FAILED_UPDATE_INCOMPATIBLE:
  Package ml.apache.tvm.android.androidcamerademo signatures do not match the previously installed version; ignoring!]

Run uninstall first:

$ANDROID_HOME/platform-tools/adb uninstall ml.apache.tvm.android.androidcamerademo