diff --git a/Cargo.lock b/Cargo.lock index d09aed2c51..f7df333f52 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -80,6 +80,23 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0" +[[package]] +name = "android_log-sys" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ecc8056bf6ab9892dcd53216c83d1597487d7dacac16c8df6b877d127df9937" + +[[package]] +name = "android_logger" +version = "0.14.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05b07e8e73d720a1f2e4b6014766e6039fd2e96a4fa44e2a78d0e1fa2ff49826" +dependencies = [ + "android_log-sys", + "env_filter", + "log", +] + [[package]] name = "android_system_properties" version = "0.1.5" @@ -911,6 +928,12 @@ dependencies = [ "shlex", ] +[[package]] +name = "cesu8" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d43a04d8753f35258c91f8ec639f792891f748a1edbd759cf1dcea3382ad83c" + [[package]] name = "cfg-expr" version = "0.15.8" @@ -1145,6 +1168,16 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "combine" +version = "4.6.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba5a308b75df32fe02788e748662718f03fde005016435c444eea572398219fd" +dependencies = [ + "bytes", + "memchr", +] + [[package]] name = "comfy-table" version = "7.1.1" @@ -3191,6 +3224,22 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9028f49264629065d057f340a86acb84867925865f73bbf8d47b4d149a7e88b8" +[[package]] +name = "jni" +version = "0.21.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a87aa2bb7d2af34197c04845522473242e1aa17c12f4935d5856491a7fb8c97" +dependencies = [ + "cesu8", + "cfg-if", + "combine", + "jni-sys", + "log", + "thiserror", + "walkdir", + "windows-sys 0.45.0", +] + [[package]] name = "jni-sys" version = "0.3.0" @@ -3568,6 +3617,17 @@ dependencies = [ "serde", ] +[[package]] +name = "mnist-inference-android" +version = "0.15.0" +dependencies = [ + "android_logger", + "burn", + "burn-import", + "jni", + "log", +] + [[package]] name = "mnist-inference-web" version = "0.15.0" @@ -7320,6 +7380,15 @@ dependencies = [ "windows_x86_64_msvc 0.36.1", ] +[[package]] +name = "windows-sys" +version = "0.45.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75283be5efb2831d37ea142365f009c02ec203cd29a3ebecbc093d52315b66d0" +dependencies = [ + "windows-targets 0.42.2", +] + [[package]] name = "windows-sys" version = "0.48.0" @@ -7347,6 +7416,21 @@ dependencies = [ "windows-targets 0.52.6", ] +[[package]] +name = "windows-targets" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e5180c00cd44c9b1c88adb3693291f1cd93605ded80c250a75d472756b4d071" +dependencies = [ + "windows_aarch64_gnullvm 0.42.2", + "windows_aarch64_msvc 0.42.2", + "windows_i686_gnu 0.42.2", + "windows_i686_msvc 0.42.2", + "windows_x86_64_gnu 0.42.2", + "windows_x86_64_gnullvm 0.42.2", + "windows_x86_64_msvc 0.42.2", +] + [[package]] name = "windows-targets" version = "0.48.5" @@ -7378,6 +7462,12 @@ dependencies = [ "windows_x86_64_msvc 0.52.6", ] +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8" + [[package]] name = "windows_aarch64_gnullvm" version = "0.48.5" @@ -7396,6 +7486,12 @@ version = "0.36.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9bb8c3fd39ade2d67e9874ac4f3db21f0d710bee00fe7cab16949ec184eeaa47" +[[package]] +name = "windows_aarch64_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43" + [[package]] name = "windows_aarch64_msvc" version = "0.48.5" @@ -7414,6 +7510,12 @@ version = "0.36.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "180e6ccf01daf4c426b846dfc66db1fc518f074baa793aa7d9b9aaeffad6a3b6" +[[package]] +name = "windows_i686_gnu" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f" + [[package]] name = "windows_i686_gnu" version = "0.48.5" @@ -7438,6 +7540,12 @@ version = "0.36.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2e7917148b2812d1eeafaeb22a97e4813dfa60a3f8f78ebe204bcc88f12f024" +[[package]] +name = "windows_i686_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060" + [[package]] name = "windows_i686_msvc" version = "0.48.5" @@ -7456,6 +7564,12 @@ version = "0.36.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4dcd171b8776c41b97521e5da127a2d86ad280114807d0b2ab1e462bc764d9e1" +[[package]] +name = "windows_x86_64_gnu" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36" + [[package]] name = "windows_x86_64_gnu" version = "0.48.5" @@ -7468,6 +7582,12 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3" + [[package]] name = "windows_x86_64_gnullvm" version = "0.48.5" @@ -7486,6 +7606,12 @@ version = "0.36.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c811ca4a8c853ef420abd8592ba53ddbbac90410fab6903b3e79972a631f7680" +[[package]] +name = "windows_x86_64_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0" + [[package]] name = "windows_x86_64_msvc" version = "0.48.5" diff --git a/Cargo.toml b/Cargo.toml index 27c376ecd3..5e15acfff9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,12 +11,14 @@ members = [ "crates/burn-import/onnx-tests", "examples/*", "examples/pytorch-import/model", + "examples/mnist-inference-android/app/src/main/rust", "xtask", ] exclude = [ "examples/notebook", - "examples/raspberry-pi-pico", # will cause dependency building issues otherwise + "examples/mnist-inference-android", + "examples/raspberry-pi-pico", # will cause dependency building issues otherwise # "crates/burn-cuda", # comment this line to work on burn-cuda ] diff --git a/_typos.toml b/_typos.toml index 54a3645698..7ea989be1e 100644 --- a/_typos.toml +++ b/_typos.toml @@ -6,8 +6,9 @@ extend-exclude = [ "*.onnx", "assets/ModuleSerialization.xml", "examples/image-classification-web/src/model/label.txt", + "examples/mnist-inference-android/gradle/*", ] [default.extend-words] # Don't correct "arange" which is intentional -arange = "arange" \ No newline at end of file +arange = "arange" diff --git a/examples/mnist-inference-android/.gitignore b/examples/mnist-inference-android/.gitignore new file mode 100644 index 0000000000..aa724b7707 --- /dev/null +++ b/examples/mnist-inference-android/.gitignore @@ -0,0 +1,15 @@ +*.iml +.gradle +/local.properties +/.idea/caches +/.idea/libraries +/.idea/modules.xml +/.idea/workspace.xml +/.idea/navEditor.xml +/.idea/assetWizardSettings.xml +.DS_Store +/build +/captures +.externalNativeBuild +.cxx +local.properties diff --git a/examples/mnist-inference-android/README.md b/examples/mnist-inference-android/README.md new file mode 100644 index 0000000000..dbdd5b9faa --- /dev/null +++ b/examples/mnist-inference-android/README.md @@ -0,0 +1,105 @@ +# MNIST number detector Android App + +This project is a sample Android application that demonstrates how to integrate a `Burn` into an android app using the JNI (Java Native Interface). + +## Table of Contents + +- [Workflow](#workflow) +- [Prerequisites](#prerequisites) +- [Setup](#setup) +- [How To make your own](#how-to-make-your-own) +- [License](#license) + +## Workflow +1. **Image Input:** The user provides an image input through the app's interface. +2. **Image Processing:** The image is converted to a grayscale `byteArray` in Kotlin. +3. **JNI Bridge:** The grayscale `byteArray` is passed to a Rust function via JNI. +4. **Rust Processing:** The Rust function calls the `forward` method from the `burn` library, using a pretrained MNIST ONNX model to perform inference. +5. **Result Handling:** The result, an integer representing the predicted digit, is logged to the android console and returned from Rust to Kotlin. +6. **Output Display:** The predicted digit is displayed on the screen + +## Prerequisites +- Android Studio (latest version recommended) +- Rust (installed and configured) +- Android NDK (Native Development Kit) + +## Setup +1. **Install Rust dependencies:** + + Ensure Rust is installed and the `cargo` command is available: + + ```bash + rustup update + ``` + And that you have installed all the rustup toolchains required: + ```bash + rustup target add \ + aarch64-linux-android \ + armv7-linux-androideabi \ + i686-linux-android \ + x86_64-linux-android + ``` + +2. **Configure the Android NDK:** + + Ensure that the Android NDK is installed. You can install it via Android Studio's SDK Manager. + +3. **Build the android app:** + + Running the android app should automatically build the rust libraries due to the gradle tasks configured at the app level. (More on that later) + + +## How To make your own +1. There are a few ways to compile a rust library for android - + - Add targets in `.cargo/config.toml` and build with them. Then we can add the `.so` files generated to the jni directory in `app/src/main/jniLibs` + - Add gradle plugins (like [rust-android-gradle](https://github.com/mozilla/rust-android-gradle) or [cargo-ndk-android](https://github.com/willir/cargo-ndk-android-gradle) using `rust-android-gradle` in this project) to do the work for you, so that the rust library is built on each app build. (Might want to change for expensive library builds) +2. To interface with Kotlin(Java) you can either use an interface generator (like [flapigen-rs](https://github.com/Dushistov/flapigen-rs)) or make them by yourself. This sample function doesn't use flapigen. +3. Now the function to be called from android (`infer()` here) needs to follow the [JNI naming conventions](https://docs.oracle.com/javase/1.5.0/docs/guide/jni/spec/design.html) (The correct name is also shown in the call error if it doesn't exist). +4. **Important** The first 2 arguments of the jni interfacing function will be the `env` variable (for interface functions) and the `this` object. The data you pass will start from the 3rd argument. +5. Next for converting the data from java to rust data types, there are multiple functions in the env variable passed to the function. Use as required... +6. Then in the app's `build.gradle` we add the part to run the cargo build before building the app and the also the cargo build details: +```kotlin +// Cargo build details +cargo { + module = "./src/main/rust" // Or whatever directory contains your Cargo.toml + libname = "mnist_inference_android" // Or whatever matches Cargo.toml's [package] name. + targets = listOf( + "arm", "arm64", + "x86", + "x86_64" + ) + prebuiltToolchains = true +} + +// Used to build cargo before the android build task is run +// See more options here: https://github.com/mozilla/rust-android-gradle/issues/133 +project.afterEvaluate { + tasks.withType(com.nishtahir.CargoBuildTask::class) + .forEach { buildTask -> + tasks.withType(com.android.build.gradle.tasks.MergeSourceSetFolders::class) + .configureEach { + this.inputs.dir( + layout.buildDirectory.dir("rustJniLibs" + File.separatorChar + buildTask.toolchain!!.folder) + ) + this.dependsOn(buildTask) + } + } +} +``` +(In the example we have also added the target directory in `config.toml` since otherwise it will build into the workspace target, which we do not want) +7. Here the library's name is `mnist-android` so we will initialize it in our app: +```kotlin +class MainActivity : ComponentActivity() { + init { + System.loadLibrary("mnist_android") // Note: '-' is changed to '_' + } + ... +} +``` +8. Finally use it by declaring it as an external function first +```kotlin +external fun infer(inputImage: ByteArray): Int; + +... +infer(byteArray) +``` \ No newline at end of file diff --git a/examples/mnist-inference-android/app/.gitignore b/examples/mnist-inference-android/app/.gitignore new file mode 100644 index 0000000000..42afabfd2a --- /dev/null +++ b/examples/mnist-inference-android/app/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/examples/mnist-inference-android/app/build.gradle.kts b/examples/mnist-inference-android/app/build.gradle.kts new file mode 100644 index 0000000000..33a1f02d98 --- /dev/null +++ b/examples/mnist-inference-android/app/build.gradle.kts @@ -0,0 +1,91 @@ +plugins { + alias(libs.plugins.android.application) + alias(libs.plugins.jetbrains.kotlin.android) + alias(libs.plugins.mozilla.rust.android) +} + +android { + namespace = "com.example.mnistinferenceandroid" + compileSdk = 34 + + defaultConfig { + applicationId = "com.example.mnistinferenceandroid" + minSdk = 24 + targetSdk = 34 + versionCode = 1 + versionName = "1.0" + + testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" + vectorDrawables { + useSupportLibrary = true + } + } + + buildTypes { + release { + isMinifyEnabled = false + proguardFiles( + getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro" + ) + } + } + compileOptions { + sourceCompatibility = JavaVersion.VERSION_1_8 + targetCompatibility = JavaVersion.VERSION_1_8 + } + kotlinOptions { + jvmTarget = "1.8" + } + buildFeatures { + compose = true + } + composeOptions { + kotlinCompilerExtensionVersion = "1.5.1" + } + packaging { + resources { + excludes += "/META-INF/{AL2.0,LGPL2.1}" + } + } + ndkVersion = "27.0.12077973" +} + +dependencies { + implementation(libs.androidx.core.ktx) + implementation(libs.androidx.lifecycle.runtime.ktx) + implementation(libs.androidx.activity.compose) + implementation(platform(libs.androidx.compose.bom)) + implementation(libs.androidx.ui) + implementation(libs.androidx.ui.graphics) + implementation(libs.androidx.ui.tooling.preview) + implementation(libs.androidx.material3) + testImplementation(libs.junit) + androidTestImplementation(libs.androidx.junit) + androidTestImplementation(libs.androidx.espresso.core) + androidTestImplementation(platform(libs.androidx.compose.bom)) + androidTestImplementation(libs.androidx.ui.test.junit4) + debugImplementation(libs.androidx.ui.tooling) + debugImplementation(libs.androidx.ui.test.manifest) +} + +cargo { + module = "./src/main/rust" // Or whatever directory contains your Cargo.toml + libname = "mnist_inference_android" // Or whatever matches Cargo.toml's [package] name. + targets = listOf( + "arm", "arm64", "x86", "x86_64" + ) + prebuiltToolchains = true +} + +// Used to build cargo before the android build task is run +project.afterEvaluate { + tasks.withType(com.nishtahir.CargoBuildTask::class).forEach { buildTask -> + tasks.withType(com.android.build.gradle.tasks.MergeSourceSetFolders::class) + .configureEach { + this.inputs.dir( + layout.buildDirectory.dir("rustJniLibs" + File.separatorChar + buildTask.toolchain!!.folder) + ) + this.dependsOn(buildTask) + } + } +} \ No newline at end of file diff --git a/examples/mnist-inference-android/app/proguard-rules.pro b/examples/mnist-inference-android/app/proguard-rules.pro new file mode 100644 index 0000000000..481bb43481 --- /dev/null +++ b/examples/mnist-inference-android/app/proguard-rules.pro @@ -0,0 +1,21 @@ +# 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 \ No newline at end of file diff --git a/examples/mnist-inference-android/app/src/androidTest/java/com/example/mnistinferenceandroid/ExampleInstrumentedTest.kt b/examples/mnist-inference-android/app/src/androidTest/java/com/example/mnistinferenceandroid/ExampleInstrumentedTest.kt new file mode 100644 index 0000000000..44f832b984 --- /dev/null +++ b/examples/mnist-inference-android/app/src/androidTest/java/com/example/mnistinferenceandroid/ExampleInstrumentedTest.kt @@ -0,0 +1,24 @@ +package com.example.mnistinferenceandroid + +import androidx.test.platform.app.InstrumentationRegistry +import androidx.test.ext.junit.runners.AndroidJUnit4 + +import org.junit.Test +import org.junit.runner.RunWith + +import org.junit.Assert.* + +/** + * Instrumented test, which will execute on an Android device. + * + * See [testing documentation](http://d.android.com/tools/testing). + */ +@RunWith(AndroidJUnit4::class) +class ExampleInstrumentedTest { + @Test + fun useAppContext() { + // Context of the app under test. + val appContext = InstrumentationRegistry.getInstrumentation().targetContext + assertEquals("com.example.mnistinferenceandroid", appContext.packageName) + } +} \ No newline at end of file diff --git a/examples/mnist-inference-android/app/src/main/AndroidManifest.xml b/examples/mnist-inference-android/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000000..8405077d8b --- /dev/null +++ b/examples/mnist-inference-android/app/src/main/AndroidManifest.xml @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/examples/mnist-inference-android/app/src/main/java/com/example/mnistinferenceandroid/MainActivity.kt b/examples/mnist-inference-android/app/src/main/java/com/example/mnistinferenceandroid/MainActivity.kt new file mode 100644 index 0000000000..a4f5c52404 --- /dev/null +++ b/examples/mnist-inference-android/app/src/main/java/com/example/mnistinferenceandroid/MainActivity.kt @@ -0,0 +1,28 @@ +package com.example.mnistinferenceandroid + +import android.os.Bundle +import androidx.activity.ComponentActivity +import androidx.activity.compose.setContent +import androidx.activity.enableEdgeToEdge +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.material3.Scaffold +import androidx.compose.ui.Modifier +import com.example.mnistinferenceandroid.ui.theme.MnistInferenceAndroidTheme + +class MainActivity : ComponentActivity() { + init { + System.loadLibrary("mnist_inference_android") + } + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + enableEdgeToEdge() + setContent { + MnistInferenceAndroidTheme { + Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding -> + MnistRecognizePage() + } + } + } + } +} diff --git a/examples/mnist-inference-android/app/src/main/java/com/example/mnistinferenceandroid/MnistInferPage.kt b/examples/mnist-inference-android/app/src/main/java/com/example/mnistinferenceandroid/MnistInferPage.kt new file mode 100644 index 0000000000..8c1a6faaac --- /dev/null +++ b/examples/mnist-inference-android/app/src/main/java/com/example/mnistinferenceandroid/MnistInferPage.kt @@ -0,0 +1,53 @@ +package com.example.mnistinferenceandroid + +import android.net.Uri +import androidx.activity.compose.rememberLauncherForActivityResult +import androidx.activity.result.contract.ActivityResultContracts +import androidx.compose.foundation.layout.* +import androidx.compose.material3.Button +import androidx.compose.material3.Text +import androidx.compose.runtime.* +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.unit.dp + +external fun infer(inputImage: ByteArray): Int; +@Composable +fun MnistRecognizePage() { + var result by remember { mutableStateOf(null) } + val context = LocalContext.current + + // Launcher to pick an image from the file explorer + val pickImageLauncher = rememberLauncherForActivityResult( + contract = ActivityResultContracts.GetContent() + ) { uri: Uri? -> + uri?.let { + val byteArray = uriToByteArray(context, it); + if(byteArray != null){ + result = infer(byteArray) + } + } + } + + Column( + modifier = Modifier + .fillMaxSize() + .padding(16.dp), + horizontalAlignment = Alignment.CenterHorizontally, + verticalArrangement = Arrangement.Center + ) { + Button(onClick = { pickImageLauncher.launch("image/*") }) { + Text("Select Image") + } + + Spacer(modifier = Modifier.height(16.dp)) + + result?.let { + if (it == -1) + Text(text = "Model prediction error") + else + Text("The number is: $it") + } + } +} \ No newline at end of file diff --git a/examples/mnist-inference-android/app/src/main/java/com/example/mnistinferenceandroid/ui/theme/Color.kt b/examples/mnist-inference-android/app/src/main/java/com/example/mnistinferenceandroid/ui/theme/Color.kt new file mode 100644 index 0000000000..a0bca2a41d --- /dev/null +++ b/examples/mnist-inference-android/app/src/main/java/com/example/mnistinferenceandroid/ui/theme/Color.kt @@ -0,0 +1,11 @@ +package com.example.mnistinferenceandroid.ui.theme + +import androidx.compose.ui.graphics.Color + +val Purple80 = Color(0xFFD0BCFF) +val PurpleGrey80 = Color(0xFFCCC2DC) +val Pink80 = Color(0xFFEFB8C8) + +val Purple40 = Color(0xFF6650a4) +val PurpleGrey40 = Color(0xFF625b71) +val Pink40 = Color(0xFF7D5260) \ No newline at end of file diff --git a/examples/mnist-inference-android/app/src/main/java/com/example/mnistinferenceandroid/ui/theme/Theme.kt b/examples/mnist-inference-android/app/src/main/java/com/example/mnistinferenceandroid/ui/theme/Theme.kt new file mode 100644 index 0000000000..305550545d --- /dev/null +++ b/examples/mnist-inference-android/app/src/main/java/com/example/mnistinferenceandroid/ui/theme/Theme.kt @@ -0,0 +1,58 @@ +package com.example.mnistinferenceandroid.ui.theme + +import android.app.Activity +import android.os.Build +import androidx.compose.foundation.isSystemInDarkTheme +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.darkColorScheme +import androidx.compose.material3.dynamicDarkColorScheme +import androidx.compose.material3.dynamicLightColorScheme +import androidx.compose.material3.lightColorScheme +import androidx.compose.runtime.Composable +import androidx.compose.ui.platform.LocalContext + +private val DarkColorScheme = darkColorScheme( + primary = Purple80, + secondary = PurpleGrey80, + tertiary = Pink80 +) + +private val LightColorScheme = lightColorScheme( + primary = Purple40, + secondary = PurpleGrey40, + tertiary = Pink40 + + /* Other default colors to override + background = Color(0xFFFFFBFE), + surface = Color(0xFFFFFBFE), + onPrimary = Color.White, + onSecondary = Color.White, + onTertiary = Color.White, + onBackground = Color(0xFF1C1B1F), + onSurface = Color(0xFF1C1B1F), + */ +) + +@Composable +fun MnistInferenceAndroidTheme( + darkTheme: Boolean = isSystemInDarkTheme(), + // Dynamic color is available on Android 12+ + dynamicColor: Boolean = true, + content: @Composable () -> Unit +) { + val colorScheme = when { + dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> { + val context = LocalContext.current + if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context) + } + + darkTheme -> DarkColorScheme + else -> LightColorScheme + } + + MaterialTheme( + colorScheme = colorScheme, + typography = Typography, + content = content + ) +} \ No newline at end of file diff --git a/examples/mnist-inference-android/app/src/main/java/com/example/mnistinferenceandroid/ui/theme/Type.kt b/examples/mnist-inference-android/app/src/main/java/com/example/mnistinferenceandroid/ui/theme/Type.kt new file mode 100644 index 0000000000..b906ab59b1 --- /dev/null +++ b/examples/mnist-inference-android/app/src/main/java/com/example/mnistinferenceandroid/ui/theme/Type.kt @@ -0,0 +1,17 @@ +package com.example.mnistinferenceandroid.ui.theme + +import androidx.compose.material3.Typography +import androidx.compose.ui.text.TextStyle +import androidx.compose.ui.text.font.FontFamily +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.unit.sp + +val Typography = Typography( + bodyLarge = TextStyle( + fontFamily = FontFamily.Default, + fontWeight = FontWeight.Normal, + fontSize = 16.sp, + lineHeight = 24.sp, + letterSpacing = 0.5.sp + ) +) \ No newline at end of file diff --git a/examples/mnist-inference-android/app/src/main/java/com/example/mnistinferenceandroid/utils.kt b/examples/mnist-inference-android/app/src/main/java/com/example/mnistinferenceandroid/utils.kt new file mode 100644 index 0000000000..ed4dde1c28 --- /dev/null +++ b/examples/mnist-inference-android/app/src/main/java/com/example/mnistinferenceandroid/utils.kt @@ -0,0 +1,42 @@ +package com.example.mnistinferenceandroid + +import android.content.Context +import android.graphics.Bitmap +import android.graphics.BitmapFactory +import android.graphics.Color +import android.net.Uri + +fun uriToByteArray(context: Context, uri: Uri): ByteArray? { + val inputStream = context.contentResolver.openInputStream(uri) ?: return null + val byteArray = inputStream.readBytes() + val imageMap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.size) + + // The model takes 28x28 images as input so reduce size before grayscale conversion + val reducedMap = Bitmap.createScaledBitmap(imageMap, 28, 28, false) + + val pixelArray = convertToGrayscaleArray(reducedMap) + return pixelArray +} + +fun convertToGrayscaleArray(bmp: Bitmap): ByteArray { + // Create a mutable bitmap with the same dimensions as the original + val width = bmp.width + val height = bmp.height + + val grayscaleArray = ByteArray(width * height) + // Iterate over each pixel in the original bitmap + for (y in 0 until height) { + for (x in 0 until width) { + // Get the pixel color at (x, y) + val pixel = bmp.getPixel(x, y) + + val r = Color.red(pixel) + val g = Color.green(pixel) + val b = Color.blue(pixel) + // Converting to grayscale using the NTSC formula + val gray = (0.299 * r + 0.587 * g + 0.114 * b).toInt() + grayscaleArray[x + y * width] = gray.toByte() // Can also use the int array directly + } + } + return grayscaleArray +} diff --git a/examples/mnist-inference-android/app/src/main/res/drawable/ic_launcher_background.xml b/examples/mnist-inference-android/app/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 0000000000..07d5da9cbf --- /dev/null +++ b/examples/mnist-inference-android/app/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/mnist-inference-android/app/src/main/res/drawable/ic_launcher_foreground.xml b/examples/mnist-inference-android/app/src/main/res/drawable/ic_launcher_foreground.xml new file mode 100644 index 0000000000..2b068d1146 --- /dev/null +++ b/examples/mnist-inference-android/app/src/main/res/drawable/ic_launcher_foreground.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/examples/mnist-inference-android/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml b/examples/mnist-inference-android/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml new file mode 100644 index 0000000000..6f3b755bf5 --- /dev/null +++ b/examples/mnist-inference-android/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/examples/mnist-inference-android/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml b/examples/mnist-inference-android/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml new file mode 100644 index 0000000000..6f3b755bf5 --- /dev/null +++ b/examples/mnist-inference-android/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/examples/mnist-inference-android/app/src/main/res/mipmap-hdpi/ic_launcher.webp b/examples/mnist-inference-android/app/src/main/res/mipmap-hdpi/ic_launcher.webp new file mode 100644 index 0000000000..c209e78ecd Binary files /dev/null and b/examples/mnist-inference-android/app/src/main/res/mipmap-hdpi/ic_launcher.webp differ diff --git a/examples/mnist-inference-android/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp b/examples/mnist-inference-android/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp new file mode 100644 index 0000000000..b2dfe3d1ba Binary files /dev/null and b/examples/mnist-inference-android/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp differ diff --git a/examples/mnist-inference-android/app/src/main/res/mipmap-mdpi/ic_launcher.webp b/examples/mnist-inference-android/app/src/main/res/mipmap-mdpi/ic_launcher.webp new file mode 100644 index 0000000000..4f0f1d64e5 Binary files /dev/null and b/examples/mnist-inference-android/app/src/main/res/mipmap-mdpi/ic_launcher.webp differ diff --git a/examples/mnist-inference-android/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp b/examples/mnist-inference-android/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp new file mode 100644 index 0000000000..62b611da08 Binary files /dev/null and b/examples/mnist-inference-android/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp differ diff --git a/examples/mnist-inference-android/app/src/main/res/mipmap-xhdpi/ic_launcher.webp b/examples/mnist-inference-android/app/src/main/res/mipmap-xhdpi/ic_launcher.webp new file mode 100644 index 0000000000..948a3070fe Binary files /dev/null and b/examples/mnist-inference-android/app/src/main/res/mipmap-xhdpi/ic_launcher.webp differ diff --git a/examples/mnist-inference-android/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp b/examples/mnist-inference-android/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp new file mode 100644 index 0000000000..1b9a6956b3 Binary files /dev/null and b/examples/mnist-inference-android/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp differ diff --git a/examples/mnist-inference-android/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp b/examples/mnist-inference-android/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp new file mode 100644 index 0000000000..28d4b77f9f Binary files /dev/null and b/examples/mnist-inference-android/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp differ diff --git a/examples/mnist-inference-android/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp b/examples/mnist-inference-android/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp new file mode 100644 index 0000000000..9287f50836 Binary files /dev/null and b/examples/mnist-inference-android/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp differ diff --git a/examples/mnist-inference-android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp b/examples/mnist-inference-android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp new file mode 100644 index 0000000000..aa7d6427e6 Binary files /dev/null and b/examples/mnist-inference-android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp differ diff --git a/examples/mnist-inference-android/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp b/examples/mnist-inference-android/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp new file mode 100644 index 0000000000..9126ae37cb Binary files /dev/null and b/examples/mnist-inference-android/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp differ diff --git a/examples/mnist-inference-android/app/src/main/res/values/colors.xml b/examples/mnist-inference-android/app/src/main/res/values/colors.xml new file mode 100644 index 0000000000..f8c6127d32 --- /dev/null +++ b/examples/mnist-inference-android/app/src/main/res/values/colors.xml @@ -0,0 +1,10 @@ + + + #FFBB86FC + #FF6200EE + #FF3700B3 + #FF03DAC5 + #FF018786 + #FF000000 + #FFFFFFFF + \ No newline at end of file diff --git a/examples/mnist-inference-android/app/src/main/res/values/strings.xml b/examples/mnist-inference-android/app/src/main/res/values/strings.xml new file mode 100644 index 0000000000..1270390b3b --- /dev/null +++ b/examples/mnist-inference-android/app/src/main/res/values/strings.xml @@ -0,0 +1,3 @@ + + mnist inference android + \ No newline at end of file diff --git a/examples/mnist-inference-android/app/src/main/res/values/themes.xml b/examples/mnist-inference-android/app/src/main/res/values/themes.xml new file mode 100644 index 0000000000..e64bae40c7 --- /dev/null +++ b/examples/mnist-inference-android/app/src/main/res/values/themes.xml @@ -0,0 +1,5 @@ + + + +