On Android
Last updated
Was this helpful?
Last updated
Was this helpful?
Impulses deployed as a C++ library, can be included in an Android application to run the impulse as an Android distributable binary (APK). This tutorial will demonstrate some examples of building an Android application around image or sensor data.
This is made possible by using the Android NDK, which allows you to write native code in C/C++ and include it in your Android application. The C++ library generated by Edge Impulse can be included in your Android application to run the impulse on device.
To try out an example, we have created an application that you can download and run on your Android device. The APK contains our GMM Cracks demo project to detect cracks in concrete.
Or continue reading to build your own project as an Android application. This document will guide you through the high level process of building an Android application. See the example-android-inferencing README for more details, and any latest updates.
Make sure you followed the Visual anomaly detection (FOMO-AD) tutorial, and have a trained impulse, or clone Visual GMM cracks.
Also install the following software:
We created an example repository which contains a sample application for Android, and wearOS which you can use to build on, and experiment using your own impulse. Download the application as a .zip, or import this repository using Git:
WearOS - example_motion_WearOS
Android - example_camera_inference
Static Buffer - example_static_buffer Open Android Studio.
Make sure you have exported your impulse as a C++ library. If you haven't done this yet, follow the steps in the C++ Library documentation. Ensure that TensorFlow lite is selected, before the C++ library is generated.
Depending on the example you want to use, import the project into Android Studio:
The Static Buffer example is a simple application that uses a static buffer to run the impulse on the device. The application will show the result of the inference on the screen.
To get inference to work, we need to add raw data from one of our samples to native-lib.cpp. Head back to the studio and click on Live classification. Then load a validation sample, and click on a row under 'Detailed result'. Make a note of the classification results, as we want our local application to produce the same numbers from inference.
Here we replace the raw_features array in native-lib.cpp with the raw data from the sample.
The application will show the result of the inference on the screen.
The Android example is a simple application that uses the camera to collect data, and run the impulse on the device. The application will show the result of the inference on the screen.
The WearOS example is a simple application that uses the accelerometer sensor to collect data, and run the impulse on the device. The application will show the result of the inference on the screen.
Unzip your Edge Impulse C++ Library export. Copy these folders into your project's app/src/main/cpp/
directory:
edge-impulse-sdk/
model-parameters/
tflite-model/
Do not copy the CMakeList.txt
file.
The CMakeList.txt
that is included in the repo was modified in the for Android applications.
Run the Windows / Linux / OSX script to fetch resources
Now you can build the application.
To build the application, open the project in Android Studio, and click on the 'Run' button. This will build the application and deploy it to your Android device.
If you want to integrate additional sensors, such as a Gyroscope or Heart Rate Sensor, follow these steps:
Enable the Sensor in the Code In MainActivity.kt, locate the sensor initialization section and uncomment the corresponding lines:
Initialize the Sensor in onCreate Inside onCreate(), uncomment and initialize the sensor:
Register the Sensor in onResume To start collecting sensor data when the app is active, uncomment the registration logic:
Handle Sensor Data in onSensorChanged Modify the onSensorChanged() function to collect new sensor data:
Unregister the Sensor in onPause To save battery and improve performance, ensure sensors stop when the app is paused:
This is the where you can add additional libraries or options to the CMakeLists.txt file. For example, to enable the full TFLite library, you can add the following line to the CMakeLists.txt file:
To further optimize inference on Android, future updates will include:
GPU acceleration with LiteRT delegate: Improves performance for TFLite models.
For now, refer to Google's LiteRT documentation for details, or contact sales for more information.
You now have a working Android application that runs your impulse on the device. You can use this as a starting point to build your own application, or integrate it into an existing application.
Android opens up the ability to distribute Android APKs for a wide range of platforms, including WearOS, Automotive, Television, Unity, and eXtended Reality (XR). This ease of distribution is a powerful tool for deploying your impulse on a wide range of devices.
We hope this tutorial has been helpful. If you have any questions, or need further assistance, please reach out to us on the Edge Impulse forum.