Object detection in the image is an important task for applications including self-driving, face detection, video surveillance, count objects in the image. You can implement the CNN based object detection algorithm on the mobile app.

TensorFlow Lite is a great solution for object detection with high accuracy. The SSD Model is create using TensorFlow Object Detection API to get image feature maps and a convolutional layer to find bounding boxes for recognized objects.

Android Demo App


The demo app available on GitHub. It is a simple camera app that Demonstrates an SSD-Mobilenet model trained using the TensorFlow Object Detection API to localize and track objects in the camera preview in real-time. To run the demo, a device running Android 5.0 ( API 21) or higher is required.

Building TensorFlow Lite on Android


In this tutorial we will use Bazel to build our TensorFlow Lite mobile demos APK and deploying with ADB on the command line.

Prerequisites.

  1. Install least SDK version(26.1.1).
  2. Install latest version of Bazel(0.13.0+).
  3. TensorFlow Lite required NDK(16b+) to build the native (C/C++)
  4. Bazel requires Android Build Tools 27.0.3+
  5. You also need to install the Android Support Repository, available through Android Studio under Android SDK Manager -> SDK Tools -> Android Support Repository.

 

Clone the TensorFlow repo

$ git clone https://github.com/tensorflow/tensorflow

In the root of the TensorFlow repository, update the tensorflow/WORKSPACE file with the api level and location of the SDK and NDK.

android_sdk_repository (
name = "androidsdk",
api_level = 23,
build_tools_version = "27.0.3",
path = "/home/brijesh/Android/Sdk",
)
android_ndk_repository(
name = "androidndk",
path = "/home/brijesh/Android/Sdk/ndk-bundle",
api_level = 19,
)

Create a Bazel WORKSPACE

Every workspace must have a text file named WORKSPACE located in the top-level workspace directory. Enter the following at the command line to create a workspace:

touch $WORKSPACE/WORKSPACE

Download pre-trained model

Download the quantized mobilenet_ssd TensorFlow Lite model and unzip and copy mobilenet_ssd.tflite to the assets: tensorflow/contrib/lite/examples/android/assets/

Build the source code

To build the demo app,  run following command in command prompt:

 bazel build --cxxopt=--std=c++11 //tensorflow/contrib/lite/examples/android:tflite_demo

Install

After building uses the following command from your workspace root to install the APK:

adb install -r bazel-bin/tensorflow/contrib/lite/examples/android/tflite_demo.apk

Tensorflow lite object detection App uses a multi-box model to try to draw bounding boxes around the locations of people in the camera. These boxes are annotated with the confidence for each detection result.