Skip to content

Commit e0207f4

Browse files
authored
Merge pull request #1 from microsoft/yguo/sample-application
Add initial sample android application
2 parents 749448f + 38ad78f commit e0207f4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1514
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Built application files
2+
*.apk
3+
*.aar
4+
*.ap_
5+
*.aab
6+
7+
# Files for the ART/Dalvik VM
8+
*.dex
9+
10+
# Java class files
11+
*.class
12+
13+
# Generated files
14+
bin/
15+
gen/
16+
out/
17+
# Uncomment the following line in case you need and you don't have the release build type files in your app
18+
# release/
19+
20+
# Gradle files
21+
.gradle/
22+
build/
23+
24+
# Local configuration file (sdk path, etc)
25+
local.properties
26+
27+
# Proguard folder generated by Eclipse
28+
proguard/
29+
30+
# Log Files
31+
*.log
32+
33+
# Android Studio Navigation editor temp files
34+
.navigation/
35+
36+
# Android Studio captures folder
37+
captures/
38+
39+
# IntelliJ
40+
*.iml
41+
.idea/workspace.xml
42+
.idea/tasks.xml
43+
.idea/gradle.xml
44+
.idea/assetWizardSettings.xml
45+
.idea/dictionaries
46+
.idea/libraries
47+
# Android Studio 3 in .gitignore file.
48+
.idea/caches
49+
.idea/modules.xml
50+
# Comment next line if keeping position of elements in Navigation Editor is relevant for you
51+
.idea/navEditor.xml
52+
53+
# Keystore files
54+
# Uncomment the following lines if you do not want to check your keystore files in.
55+
#*.jks
56+
#*.keystore
57+
58+
# External native build folder generated in Android Studio 2.2 and later
59+
.externalNativeBuild
60+
.cxx/
61+
62+
# Google Services (e.g. APIs or Firebase)
63+
# google-services.json
64+
65+
# Freeline
66+
freeline.py
67+
freeline/
68+
freeline_project_description.json
69+
70+
# fastlane
71+
fastlane/report.xml
72+
fastlane/Preview.html
73+
fastlane/screenshots
74+
fastlane/test_output
75+
fastlane/readme.md
76+
77+
# Version control
78+
vcs.xml
79+
80+
# lint
81+
lint/intermediates/
82+
lint/generated/
83+
lint/outputs/
84+
lint/tmp/
85+
# lint/reports/
86+
87+
.idea/
88+
.DS_Store
89+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# ONNX Runtime Mobile image classification Android sample application
2+
3+
## Overview
4+
This is an example application for [ONNX Runtime](https://github.com/microsoft/onnxruntime) on Android. The demo app uses image classfication which is able to continuously classify the objects it sees from the device's camera in real-time and displays the most probable inference result on the screen.
5+
6+
This example is loosely based on [Google CodeLabs - Getting Started with CameraX](https://codelabs.developers.google.com/codelabs/camerax-getting-started)
7+
8+
### Model
9+
We use classic MobileNetV2(float) model and MobileNetV2 (uint8) in this sample app.
10+
11+
## Requirements
12+
- Android Studio 4.1+ (installed on Mac/Windows/Linux)
13+
- Android SDK 29+
14+
- Android NDK r21+
15+
- Android device in developer mode and enable USB debugging
16+
17+
## Build And Run
18+
### Prerequisites
19+
- MobileNetV2 ort format model
20+
- labels text file (used for image classification)
21+
- Prebuilt ONNX Runtime arm64 Android Archive(AAR) files, which can be directly imported in Android Studio
22+
23+
The above three files are provided and can be downloaded [here](https://1drv.ms/u/s!Auaxv_56eyubgQX-S_kTP0AP66Km?e=e8YMX1).
24+
25+
[Optional] You can also build your own ONNX Runtime arm64 AAR files for Android. (See [build instructions here](https://www.onnxruntime.ai/docs/how-to/build.html#android) and [Build Android Archive(AAR)](https://www.onnxruntime.ai/docs/how-to/build.html#build-android-archive-aar)).
26+
27+
28+
### Step 1. Clone the ONNX Runtime Mobile examples source code and download required model files
29+
Clone this ORT Mobile examples GitHub repository to your computer to get the sample application.
30+
31+
Download the packages provided in `Prerequisites`.
32+
33+
- Copy MobileNetV1 onnx model and the labels file to `example/image_classification/android/app/src/main/res/raw/`
34+
35+
- Create `/libs` directory under `app/` and copy the AAR file `onnxruntime-release.aar` to `app/libs`
36+
37+
Then open the sample application in Android Studio. To do this, open Android Studio and select `Open an existing project`, browse folders and open the folder `examples/image_classification/android/`.
38+
39+
<img width=60% src="images/screenshot_1.png"/>
40+
41+
### Step 2. Build the sample application in Android Studio
42+
43+
Select `Build-Make Project` in the top toolbar in Android Studio and check the projects has built successfully.
44+
45+
<img width=60% src="images/screenshot_3.png" alt="App Screenshot"/>
46+
47+
<img width=60% src="images/screenshot_4.png" alt="App Screenshot"/>
48+
49+
### Step 3. Connect your Android Device and run the app
50+
51+
Connect your Android Device to the computer and select your device in the top-down device bar.
52+
53+
<img width=60% src="images/screenshot_5.png" alt="App Screenshot"/>
54+
55+
<img width=60% src="images/screenshot_6.png" alt="App Screenshot"/>
56+
57+
Then Select `Run-Run app` and this will prompt the app to be installed on your device.
58+
59+
Now you can test and try by opening the app `ort_image_classifier` on your device. The app may request your permission for using the camera.
60+
61+
62+
#
63+
Here's an example screenshot of the app.
64+
65+
<img width=20% src="images/screenshot_2.jpg" alt="App Screenshot" />
66+
67+
68+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
plugins {
2+
id 'com.android.application'
3+
id 'kotlin-android'
4+
id 'kotlin-android-extensions'
5+
}
6+
7+
android {
8+
compileSdkVersion 30
9+
buildToolsVersion "30.0.2"
10+
11+
defaultConfig {
12+
applicationId "ai.onnxruntime.example.imageclassifier"
13+
minSdkVersion 24
14+
targetSdkVersion 30
15+
versionCode 1
16+
versionName "1.0"
17+
18+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
19+
}
20+
21+
buildTypes {
22+
release {
23+
minifyEnabled false
24+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
25+
}
26+
}
27+
28+
compileOptions {
29+
sourceCompatibility JavaVersion.VERSION_1_8
30+
targetCompatibility JavaVersion.VERSION_1_8
31+
}
32+
33+
kotlinOptions {
34+
jvmTarget = '1.8'
35+
}
36+
}
37+
38+
dependencies {
39+
// CameraX core library using the camera2 implementation
40+
def camerax_version = "1.0.0-rc03"
41+
// The following line is optional, as the core library is included indirectly by camera-camera2
42+
implementation "androidx.camera:camera-camera2:${camerax_version}"
43+
// If you want to additionally use the CameraX Lifecycle library
44+
implementation "androidx.camera:camera-lifecycle:${camerax_version}"
45+
// If you want to additionally use the CameraX View class
46+
implementation "androidx.camera:camera-view:1.0.0-alpha22"
47+
48+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
49+
implementation 'androidx.core:core-ktx:1.3.2'
50+
implementation 'androidx.appcompat:appcompat:1.2.0'
51+
implementation 'com.google.android.material:material:1.3.0'
52+
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
53+
testImplementation 'junit:junit:4.+'
54+
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
55+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
56+
57+
implementation(name: "onnxruntime-release", ext: "aar")
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package ai.onnxruntime.example.imageclassifier
2+
3+
import androidx.test.ext.junit.runners.AndroidJUnit4
4+
import androidx.test.platform.app.InstrumentationRegistry
5+
import org.junit.Assert.assertEquals
6+
import org.junit.Test
7+
import org.junit.runner.RunWith
8+
9+
/**
10+
* Instrumented test, which will execute on an Android device.
11+
*
12+
* See [testing documentation](http://d.android.com/tools/testing).
13+
*/
14+
@RunWith(AndroidJUnit4::class)
15+
class ExampleInstrumentedTest {
16+
@Test
17+
fun useAppContext() {
18+
// Context of the app under test.
19+
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
20+
assertEquals("ai.onnxruntime.example.imageclassifier", appContext.packageName)
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="ai.onnxruntime.example.imageclassifier">
4+
5+
<uses-feature android:name="android.hardware.camera.any" />
6+
<uses-permission android:name="android.permission.CAMERA" />
7+
8+
<application
9+
android:allowBackup="true"
10+
android:icon="@mipmap/ic_launcher"
11+
android:label="@string/app_name"
12+
android:roundIcon="@mipmap/ic_launcher_round"
13+
android:supportsRtl="true"
14+
android:theme="@style/Theme.Ort_image_classifier">
15+
<activity android:name=".MainActivity">
16+
<intent-filter>
17+
<action android:name="android.intent.action.MAIN" />
18+
19+
<category android:name="android.intent.category.LAUNCHER" />
20+
</intent-filter>
21+
</activity>
22+
</application>
23+
24+
</manifest>

0 commit comments

Comments
 (0)