{"id":36822273,"url":"https://github.com/edgeimpulse/example-android-inferencing","last_synced_at":"2026-01-12T14:01:51.780Z","repository":{"id":281251823,"uuid":"926678597","full_name":"edgeimpulse/example-android-inferencing","owner":"edgeimpulse","description":"sample android app templates","archived":false,"fork":false,"pushed_at":"2025-11-12T19:37:03.000Z","size":24300,"stargazers_count":3,"open_issues_count":2,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-11-12T21:20:11.625Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause-clear","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/edgeimpulse.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.3-clause-bsd-clear","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-02-03T17:17:40.000Z","updated_at":"2025-11-12T19:37:07.000Z","dependencies_parsed_at":"2025-03-07T21:33:55.310Z","dependency_job_id":null,"html_url":"https://github.com/edgeimpulse/example-android-inferencing","commit_stats":null,"previous_names":["edgeimpulse/example-android-inferencing"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/edgeimpulse/example-android-inferencing","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edgeimpulse%2Fexample-android-inferencing","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edgeimpulse%2Fexample-android-inferencing/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edgeimpulse%2Fexample-android-inferencing/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edgeimpulse%2Fexample-android-inferencing/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/edgeimpulse","download_url":"https://codeload.github.com/edgeimpulse/example-android-inferencing/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edgeimpulse%2Fexample-android-inferencing/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28340211,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-12T12:22:26.515Z","status":"ssl_error","status_checked_at":"2026-01-12T12:22:10.856Z","response_time":98,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2026-01-12T14:01:13.569Z","updated_at":"2026-01-12T14:01:51.666Z","avatar_url":"https://github.com/edgeimpulse.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n# example-android-inferencing\n\n## Edge Impulse Inferencing on Android\nThis repository contains a minimal example for running an **Edge Impulse** machine learning model on an **Android** device using **Android NDK** and **TensorFlow Lite**.\n\nFully tested on Vision for: FOMO AD, Object Detection, and on sensor data for KWS, and accelerometer with WearOS\n\n\n\nSee the [Android Documentation](https://docs.edgeimpulse.com/docs/run-inference/cpp-library/running-your-impulse-android).\n\n## Prerequisites\n\n- https://edgeimpulse.com/signup\n  \n### Edge Impulse:\n- Ensure you have followed the **[on Android guide](https://docs.edgeimpulse.com/docs/run-inference/cpp-library/running-your-impulse-android)** and have a trained model.\n- Export your model as a **C++ library** from **Edge Impulse Studio**.\n\n### Workshop\n- 1. Join: https://edgeimpulse.com/signup\n- 2. Follow one of the tutorial guides for beginners [here](https://docs.edgeimpulse.com/docs/readme/for-beginners#tutorials-and-resources-for-beginners)\n- 3. Export the C++ Binary [Visual Anomaly](https://docs.edgeimpulse.com/docs/edge-impulse-studio/learning-blocks/visual-anomaly-detection) or download the prebuilt GMM Cracks Demo on the workshop download the C++ export [here](https://drive.google.com/file/d/1oXP83vHUDs7iS6uuAlZilmrWyDYsBc9t/view?usp=sharing)\n- 4. Follow the rest of this repo\n- 5. Now knowledgable Android Developers can make a change to the [Kotlin](https://developer.android.com/get-started/codelabs) appliction logic to build your own app around the runInference function, e.g. count instances of detections, add thresholding to only detect beyond 70% of confidence, change the UI.\n\n### Android Development:\n- Install **Android Studio**.\n- Install **Android NDK** and **CMake** via the **Android Studio SDK Manager**.\nThe example is tested to work with Android Studio Ladybug Feature Drop | 2024.2.2, Android API 35, Android SDK Build-Tools 35.0.1, NDK 27.0.12077973, CMake 3.22.1.\n---\n\n## Cloning the Base Repository\nWe created an example repository that contains an **Android Studio project with C++ support**.\nClone or download this repository:\n\n```sh\ngit clone https://github.com/edgeimpulse/example-android-inferencing.git\ncd example-android-inferencing\n```\n\n## Run the Windows / Linux / OSX script to fetch resources\n\n```sh\ncd example-android-inferencing/example_static_buffer/app/src/main/cpp/tflite\nsh download_tflite_libs.bat # download_tflite_libs.sh for OSX and Linux\n```\n\n## Import the Project to Android Studio\n\nChoose the project to import\n\n- [WearOS](example_motion_WearOS)\n- [Android](example_camera_inference)\n- [Static Buffer](example_static_buffer)\n\n1. Open **Android Studio**.\n2. Select **Open an existing Android Studio project**.\n3. Navigate to the cloned repository and select it.\n\n## Download CPP Project from Edge Impulse\n1. Go to **Edge Impulse Studio**.\n2. Export your trained model as a **C++ library**.\n3. Download the exported model.\n\n## Integrate the Model with the Project\n1. Extract the downloaded **C++ library**.\n2. Copy the extracted files into the `example-android-inferencing/example_static_buffer/app/src/main/cpp` directory, dont copy the CMake.txt file.\n\n## Paste in the Test Feature Set for the CPP Test\n1. Obtain the test feature set from **Edge Impulse Studio** test impulse tab.\n2. Paste the test feature set into the raw_features array the native_lib.cpp licated in the cpp directory.\n\n```cpp\nstd::vector\u003cfloat\u003e raw_features = {\n    // Copy raw features here (e.g. from the 'Model testing' page)\n};\n```\n\n## Build and Run the Project\n1. In **Android Studio**, click on **Build** \u003e **Make Project**.\n2. Once the build is successful, run the project on an Android device or emulator.\n\n\n## Adding New Sensors to the WearOS example\nIf you want to integrate additional sensors, such as a Gyroscope or Heart Rate Sensor, follow these steps:\n\n1. Enable the Sensor in the Code\nIn MainActivity.kt, locate the sensor initialization section and uncomment the corresponding lines:\n\n```kotlin\n// Uncomment to add Gyroscope support\nprivate var gyroscope: Sensor? = null\n\n// Uncomment to add Heart Rate sensor support\nprivate var heartRateSensor: Sensor? = null\n```\n\n2. Initialize the Sensor in onCreate\nInside onCreate(), uncomment and initialize the sensor:\n\n```kotlin\n gyroscope = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE)\n// heartRateSensor = sensorManager.getDefaultSensor(Sensor.TYPE_HEART_RATE)\n```\n3. Register the Sensor in onResume\nTo start collecting sensor data when the app is active, uncomment the registration logic:\n\n```kotlin\n gyroscope?.also {\n     sensorManager.registerListener(this, it, SensorManager.SENSOR_DELAY_NORMAL)\n }\n\n// heartRateSensor?.also {\n//     sensorManager.registerListener(this, it, SensorManager.SENSOR_DELAY_NORMAL)\n// }\n```\n4. Handle Sensor Data in onSensorChanged\nModify the onSensorChanged() function to collect new sensor data:\n\n```kotlin\n Gyroscope data\n Sensor.TYPE_GYROSCOPE -\u003e {\n     ringBuffer[ringBufferIndex++] = event.values[0] // X rotation\n     ringBuffer[ringBufferIndex++] = event.values[1] // Y rotation\n     ringBuffer[ringBufferIndex++] = event.values[2] // Z rotation\n }\n\n// Heart Rate data\n// Sensor.TYPE_HEART_RATE -\u003e {\n//     ringBuffer[ringBufferIndex++] = event.values[0] // Heart rate BPM\n// }\n```\n5. Unregister the Sensor in onPause\nTo save battery and improve performance, ensure sensors stop when the app is paused:\n\n```kotlin\nsensorManager.unregisterListener(this)\n```\n6. Run the App and Verify\nBuild and deploy the app on a WearOS device.\nCheck logs for new sensor data.\nEnsure inference runs correctly with the additional inputs.\n\n\n\n### Troubleshooting, and other deployment hardware\n\nTesting on devices without ready access to a camera, like Device Cloud or VR headsets that dont allow you to use the passthrough camera:\n\n```\noverride fun onResume() {\n    super.onResume()\n\n    //Read the asset\n    val bmp = assets.open(\"test.jpg\").use { BitmapFactory.decodeStream(it) }\n\n    //Resize to the model’s input — helper does this for you\n    val resized = EIImageHelper.resizeBitmap(bmp)\n\n    //Run inference (synchronous, one-shot)\n    val result = EIClassifierImage.run(resized)\n\n    //Show scores in the existing TextView\n    runOnUiThread { resultText.text = result.format() }\n}\n```\n\n\n\n## Running on 32bit\n\n\nIf you want to run 32bit libs you will also need to change the build type from 64bit to 32bit\n`example-android-inferencing-main/example_motion_WearOS/app/build.gradle.kts`\n\nThis is the flag to add:\n\n```\n\nndk {\n            abiFilters += \"arm64-v8a\" //-\u003e armeabi-v7a\n        }\n```\n\n### Also for 32bit CMakeLists will need some modification for 2.19 version of tflite\n\nOur original CMakeLists.txt used these two commands to link the libraries:\nlink_directories(${CMAKE_SOURCE_DIR}/tflite/android32)\ntarget_link_libraries(test_cpp ... tensorflow-lite XNNPACK ...)\n\nThis needs to change to:\n\n```\n# For more information about using CMake with Android Studio, read the\n# documentation: https://d.android.com/studio/projects/add-native-code.html.\n# For more examples on how to use CMake, see https://github.com/android/ndk-samples.\n\ncmake_minimum_required(VERSION 3.22.1)\n\nproject(\"test_cpp\")\n\nset(CMAKE_VERBOSE_MAKEFILE TRUE)\nset(CMAKE_CXX_FLAGS \"${CMAKE_CXX_FLAGS} -v -stdlib=libc++\")\n\ninclude(edge-impulse-sdk/cmake/utils.cmake)\n\nset(CMAKE_CXX_STANDARD 17)\nset(CMAKE_CXX_STANDARD_REQUIRED ON)\n\nset(EI_SDK_FOLDER edge-impulse-sdk)\n\nadd_definitions(-DEI_CLASSIFIER_ENABLE_DETECTION_POSTPROCESS_OP=1\n    -DEI_CLASSIFIER_USE_FULL_TFLITE=1\n    -DNDEBUG\n)\n\n# Define the directory where the pre-built libraries are located\nset(TFLITE_LIB_DIR ${CMAKE_SOURCE_DIR}/tflite/android32)\n\n# Create IMPORTED targets for each pre-built static library\nadd_library(tensorflow-lite STATIC IMPORTED)\nset_target_properties(tensorflow-lite PROPERTIES IMPORTED_LOCATION ${TFLITE_LIB_DIR}/libtensorflow-lite.a)\n\nadd_library(xnnpack-delegate STATIC IMPORTED)\nset_target_properties(xnnpack-delegate PROPERTIES IMPORTED_LOCATION ${TFLITE_LIB_DIR}/libxnnpack-delegate.a)\n\nadd_library(XNNPACK STATIC IMPORTED)\nset_target_properties(XNNPACK PROPERTIES IMPORTED_LOCATION ${TFLITE_LIB_DIR}/libXNNPACK.a)\n\nadd_library(pthreadpool STATIC IMPORTED)\nset_target_properties(pthreadpool PROPERTIES IMPORTED_LOCATION ${TFLITE_LIB_DIR}/libpthreadpool.a)\n\nadd_library(cpuinfo STATIC IMPORTED)\nset_target_properties(cpuinfo PROPERTIES IMPORTED_LOCATION ${TFLITE_LIB_DIR}/libcpuinfo.a)\n\nadd_library(ruy STATIC IMPORTED)\nset_target_properties(ruy PROPERTIES IMPORTED_LOCATION ${TFLITE_LIB_DIR}/libruy.a)\n\nadd_library(farmhash STATIC IMPORTED)\nset_target_properties(farmhash PROPERTIES IMPORTED_LOCATION ${TFLITE_LIB_DIR}/libfarmhash.a)\n\nadd_library(fft2d_fftsg STATIC IMPORTED)\nset_target_properties(fft2d_fftsg PROPERTIES IMPORTED_LOCATION ${TFLITE_LIB_DIR}/libfft2d_fftsg.a)\n\nadd_library(fft2d_fftsg2d STATIC IMPORTED)\nset_target_properties(fft2d_fftsg2d PROPERTIES IMPORTED_LOCATION ${TFLITE_LIB_DIR}/libfft2d_fftsg2d.a)\n\nadd_library(flatbuffers STATIC IMPORTED)\nset_target_properties(flatbuffers PROPERTIES IMPORTED_LOCATION ${TFLITE_LIB_DIR}/libflatbuffers.a)\n\nadd_library(absl STATIC IMPORTED)\nset_target_properties(absl PROPERTIES IMPORTED_LOCATION ${TFLITE_LIB_DIR}/libabsl.a)\n\nadd_library(microkernels-prod STATIC IMPORTED)\nset_target_properties(microkernels-prod PROPERTIES IMPORTED_LOCATION ${TFLITE_LIB_DIR}/libmicrokernels-prod.a)\n\n# Define the main shared library for the app\nadd_library(${CMAKE_PROJECT_NAME} SHARED\n        native-lib.cpp)\n\ntarget_include_directories(${CMAKE_PROJECT_NAME} PRIVATE .)\n\n# Collect other Edge Impulse SDK source files\nfile(GLOB EI_SOURCE_FILES\n        \"${CMAKE_SOURCE_DIR}/edge-impulse-sdk/CMSIS/DSP/Source/TransformFunctions/*.c\"\n        \"${CMAKE_SOURCE_DIR}/edge-impulse-sdk/CMSIS/DSP/Source/CommonTables/*.c\"\n        \"${CMAKE_SOURCE_DIR}/edge-impulse-sdk/CMSIS/DSP/Source/BasicMathFunctions/*.c\"\n        \"${CMAKE_SOURCE_DIR}/edge-impulse-sdk/CMSIS/DSP/Source/ComplexMathFunctions/*.c\"\n        \"${CMAKE_SOURCE_DIR}/edge-impulse-sdk/CMSIS/DSP/Source/FastMathFunctions/*.c\"\n        \"${CMAKE_SOURCE_DIR}/edge-impulse-sdk/CMSIS/DSP/Source/SupportFunctions/*.c\"\n        \"${CMAKE_SOURCE_DIR}/edge-impulse-sdk/CMSIS/DSP/Source/MatrixFunctions/*.c\"\n        \"${CMAKE_SOURCE_DIR}/edge-impulse-sdk/CMSIS/DSP/Source/StatisticsFunctions/*.c\"\n        \"${CMAKE_SOURCE_DIR}/tflite-model/*.cpp\"\n        \"${CMAKE_SOURCE_DIR}/edge-impulse-sdk/dsp/kissfft/*.cpp\"\n        \"${CMAKE_SOURCE_DIR}/edge-impulse-sdk/dsp/dct/*.cpp\"\n        \"${CMAKE_SOURCE_DIR}/edge-impulse-sdk/dsp/memory.cpp\"\n        \"${CMAKE_SOURCE_DIR}/edge-impulse-sdk/porting/posix/*.c*\"\n        \"${CMAKE_SOURCE_DIR}/edge-impulse-sdk/porting/mingw32/*.c*\"\n)\n\ntarget_sources(${CMAKE_PROJECT_NAME} PRIVATE ${EI_SOURCE_FILES})\n\n# Link the main library to Android system libraries and the TFLite libraries.\n# Use a linker group (--start-group and --end-group) to resolve circular\n# dependencies between the static libraries.\ntarget_link_libraries(${CMAKE_PROJECT_NAME}\n        android\n        log\n        m\n        atomic\n        -Wl,--start-group\n        tensorflow-lite\n        xnnpack-delegate\n        XNNPACK\n        pthreadpool\n        cpuinfo\n        ruy\n        farmhash\n        fft2d_fftsg\n        fft2d_fftsg2d\n        flatbuffers\n        absl\n        microkernels-prod\n        -Wl,--end-group\n)\n\n\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fedgeimpulse%2Fexample-android-inferencing","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fedgeimpulse%2Fexample-android-inferencing","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fedgeimpulse%2Fexample-android-inferencing/lists"}