Deployment option for android device

Question/Issue: I want to deploy a model to an android device. I don’t know which library I should use for deployment. Please suggest, which deployment library I should select.

Hi @ashokp

For deploying a model to an Android device, you can go a couple of routes, Naitive Android - utilize tflite for android, Cross Platform JS - tflite plus cross platform framework like ionic, or Cross Platform JS - wasm.

WASM may be the easiest and you can start with our browser guide - Through WebAssembly (browser) | Edge Impulse Documentation

I don’t have any docs for the tflite for android, but you can find their getting started guide here, and ask us questions / comment on your progress: Android 빠른 시작  |  TensorFlow Lite

It provides tools to convert your trained models into a format compatible with Android devices and APIs to integrate them into your Android applications seamlessly.

If you still decide to go naitive android you can follow these high level steps to deploy your Edge Impulse model to an Android device using TensorFlow Lite:

  1. Export your trained model in TensorFlow Lite format.
  2. Integrate the TensorFlow Lite model into your Android project.
  3. Use the TensorFlow Lite Android Support Library to load and run the model on the device.
  4. Implement the necessary logic in your Android application to process input data and interpret model outputs. (here is where you can give some feedback to us on your progress and hopefully we can get you)

I’ve made some progress with the ionic cross platform approac, but havent had time to complete it.

Feel free to reach out if you need further assistance or have any more questions!

Best

Eoin

If the exported Tflite model contains DSP blocks from Edge impulse, this would mean that these blocks would also need to be there. Our use case is to run the Tflite model. with the DSP blocks in Android and in Unity3D engine. Are the source code for the eported DSP blocks available? and how and where are the DSP blocks being called and run from?

1 Like

Thanks for the reply @Eoin ,

I have tried the demo project on audio classification. It used a preprocessing block ‘mfe’. After exporting the model, I get a c++ package. I want to understand where and how the sequence of the pipeline is defined. I couldn’t locate the code where the pipeline is defined, for example the input signal is preprocessed, features are extracted and then model is used for inference.

@Eoin, Do you have updates on these questions. This is quite a big blocker for us to move forward.

Very cool @BlueLotus!

You can review out MFE code and Docs in the public git repo:

Also see Custom Processing Blocks:

This level of usage isn’t something I’m able to help with unfortunately but you should be able to engage our ml solutions via our sales team if you need further assistance with it - Contact

Best

Eoin

Thanks @Eoin for sharing the docs links. I am trying to integrate the cpp package of edge impulse with unity using android studio, used the following videos as reference:

  1. Deploy an ML Model to Any Target with the Edge Impulse C++ Library (youtube.com)
  2. Creating a C++ Android Plugin for Unity with Android Studio (youtube.com)
    I am getting some errors while making the module in android studio. May you please check if the CMakeLists.txt file is correct? or please share any suggestion if you have.

Following is the content of the CMakeLists.txt file:

Sets the minimum CMake version required for this project.

cmake_minimum_required(VERSION 3.22.1)

include(edge-impulse-sdk/cmake/utils.cmake)
add_subdirectory(edge-impulse-sdk/cmake/zephyr)

RECURSIVE_FIND_FILE_APPEND(MODEL_SOURCE “tflite-model” “*.cpp”)
target_include_directories(cppLib PRIVATE .)

add all sources to the project

target_sources(cppLib PRIVATE ${MODEL_SOURCE})

project(“cppLib”)

add_library(cppLib
SHARED
# List C/C++ source files with relative paths to this CMakeLists.txt.
main.cpp)

find_library( # sets the name of the path variable
log-lib
# Specify the name of the NDK lib that you want CMake to locate

        log

)

target_link_libraries(cppLib
# List libraries link to the target library
android
log)

Hi @ashokp

Nice work but try to get both working independently first, the android SDK / NDK doesn’t appear to be set up correctly. If you can get both working on their own you will have more hope of integrating.

For Edge Impulse get C++ working on your desktop, I see some reference to Zephyr for example you would be better off to start over and work along this guide:

Best

Eoin

Thanks, I will follow this guide and get back to you.

1 Like

We got success in running a simple cpp project in unity-android. Here is how we did it:
We built the .so file using the android-ndk (we listed all the files in android.mk and built with sh build_plugin.sh ) . Created a C# script in Unity to interface with the C++ library using the [DllImport] attribute.
But while compiling the edge-impulse sdk cpp package + example-standalone-inferencing-master we got an issue that the android ndk does not compile .hpp files.

This is the content of the build_plugin.sh file:

#!/bin/sh
echo ""
echo "Compiling NativeCode.c..."
C:/Users/USER/AppData/Local/Android/Sdk/ndk/27.0.11718014/ndk-build.cmd NDK_PROJECT_PATH=. NDK_APPLICATION_MK=Application.mk $*
# mv libs/armeabi/libnative.so ..

# echo ""
# echo "Cleaning up / removing build folders..."  #optional..
# rm -rf libs
# rm -rf obj

# echo ""
# echo "Done!"

Hi, you can compile the edge impulse library with the main.cpp as android static library .so file. download AndroidNativePlugin.unitypackage zip, extract it and import the unitypackage in unity, then you will get a src there which will contain android.mk, application.mk and build_plugin.sh. You can use that to compile edge impulse library. Just add

APP_SHORT_COMMANDS := true
APP_STL := c++_static    # or c++_shared
NDK_TOOLCHAIN_VERSION := clang
APP_CPPFLAGS := -std=c++17 -fexceptions

these commands in application.mk and add all files in the android.mk

1 Like

Thanks for reporting back your results folks, this is exciting!

Hi @Eoin,

As you can see from this chat, I have created a so file using the android native and then used it inside the Unity game engine. This is working fine but the issue is we need to attach a new so file for every new task (model) inside the unity. We want to access the DSP blocks inside the Unity game engine or an android studio project and attach our model with the blocks. We found that with the so file it will not be possible to access the dsp blocks from the sdk as the parameters related to the blocks and model are defined inside different header files. I am still searching for the solution. If you have any suggestion, it will be a great help.

1 Like

Awesome work @ashokp!

I’m working on an Android starter guide this week, and will take your work as the initial starting point. Hopefully I’ll be able to figure that part out…

Will update over the next few days.

Best

Eoin

1 Like

Hi @Eoin

I’m glad to know that you found my work useful. Is there any update on Android starter guide?

Best
Ashok