TensorFlow Lite Object Detection in Android App

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

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

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:

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:

Install

After building uses the following command from your workspace root to install the 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.

Leave a Reply

Your email address will not be published. Required fields are marked *