Skip to content
Browse files
Import the original TensorFlow Android sample
  • Loading branch information
Nilhcem committed Mar 11, 2017
1 parent bac6aa1 commit 7c4664a55e33c62859e19ee10cfafe770bf299d8
Showing 96 changed files with 14,671 additions and 0 deletions.
@@ -0,0 +1,64 @@
<?xml version="1.0" encoding="UTF-8"?>
Copyright 2016 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
See the License for the specific language governing permissions and
limitations under the License.

<manifest xmlns:android=""

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="" />
<uses-feature android:name="" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

android:targetSdkVersion="23" />

<application android:allowBackup="true"

<activity android:name="org.tensorflow.demo.ClassifierActivity"
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />

<activity android:name="org.tensorflow.demo.DetectorActivity"
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />

<activity android:name="org.tensorflow.demo.StylizeActivity"
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />

@@ -0,0 +1,132 @@
# Description:
# TensorFlow camera demo app for Android.

package(default_visibility = ["//visibility:public"])

licenses(["notice"]) # Apache 2.0



LINKER_SCRIPT = "//tensorflow/contrib/android:jni/"

# contains the native code for image colorspace conversion
# and object tracking used by the demo. It does not require TF as a dependency
# to build if STANDALONE_DEMO_LIB is defined.
# TF support for the demo is provided separately by
name = "",
srcs = glob([
copts = tf_copts(),
defines = ["STANDALONE_DEMO_LIB"],
linkopts = [
"-z defs",
"-Wl,--version-script", # This line must be directly followed by LINKER_SCRIPT.
linkshared = 1,
linkstatic = 1,
tags = [
deps = [

name = "tensorflow_native_libs",
srcs = [
tags = [

name = "tensorflow_demo",
srcs = glob([
# Package assets from assets dir as well as all model targets. Remove undesired models
# (and corresponding Activities in source) to reduce APK size.
assets = [
assets_dir = "",
custom_package = "org.tensorflow.demo",
inline_constants = 1,
manifest = "AndroidManifest.xml",
resource_files = glob(["res/**"]),
tags = [
deps = [

name = "external_assets",
srcs = [

name = "all_files",
srcs = glob(
exclude = [
visibility = ["//tensorflow:__subpackages__"],

name = "java_files",
srcs = glob(["src/**/*.java"]),

name = "jni_files",
srcs = glob([

name = "resource_files",
srcs = glob(["res/**"]),

@@ -0,0 +1,174 @@
# TensorFlow Android Camera Demo

This folder contains an example application utilizing TensorFlow for Android

## Description

The demos in this folder are designed to give straightforward samples of using
TensorFlow in mobile applications.

Inference is done using the [TensorFlow Android Inference Interface](../../../tensorflow/contrib/android),
which may be built separately if you want a standalone library to drop into your
existing application. Object tracking and YUV -> RGB conversion is handled by

A device running Android 5.0 (API 21) or higher is required to run the demo due
to the use of the camera2 API, although the native libraries themselves can run
on API >= 14 devices.

## Current samples:

1. [TF Classify](
Uses the [Google Inception](
model to classify camera frames in real-time, displaying the top results
in an overlay on the camera image.
2. [TF Detect](
Demonstrates a model based on [Scalable Object Detection
using Deep Neural Networks]( to
localize and track people in the camera preview in real-time.
3. [TF Stylize](
Uses a model based on [A Learned Representation For Artistic Style]
( to restyle the camera preview image
to that of a number of different artists.

<img src="sample_images/classify1.jpg" width="30%">
<img src="sample_images/stylize1.jpg" width="30%">
<img src="sample_images/detect1.jpg" width="30%">

## Prebuilt APK:

If you just want the fastest path to trying the demo, you may download the
nightly build
[here]( Expand the
"View" and then the "out" folders under "Last Successful Artifacts" to find
tensorflow_demo.apk. Also available are precompiled native libraries that you
may drop into your own applications. See
for more details.

## Running the Demo

Once the app is installed it can be started via the "TF Classify", "TF Detect"
and "TF Stylize" icons, which have the orange TensorFlow logo as their icon.

While running the activities, pressing the volume keys on your device will
toggle debug visualizations on/off, rendering additional info to the screen
that may be useful for development purposes.

## Building the Demo from Source

Pick your preferred approach below. At the moment, we have full support for
Bazel, and partial support for gradle, cmake, make, and Android Studio.

As a first step for all build types, clone the TensorFlow repo with:

git clone --recurse-submodules

Note that `--recurse-submodules` is necessary to prevent some issues with
protobuf compilation.

### Bazel

NOTE: Bazel does not currently support building for Android on Windows. Full
support for gradle/cmake builds is coming soon, but in the meantime we suggest
that Windows users download the
[prebuilt binaries](

##### Install Bazel and Android Prerequisites

Bazel is the primary build system for TensorFlow. To build with Bazel,
it and the Android NDK and SDK must be installed on your system.

1. Get the recommended Bazel version listed in [os_setup.html](
2. The Android NDK is required to build the native (C/C++) TensorFlow code.
The current recommended version is 12b, which may be found
3. The Android SDK and build tools may be obtained
or alternatively as part of
[Android Studio]( Build
tools API >= 23 is required to build the TF Android demo (though it will
run on API >= 21 devices).

##### Edit WORKSPACE

The Android entries in [`<workspace_root>/WORKSPACE`](../../../WORKSPACE#L2-L13)
must be uncommented with the paths filled in appropriately depending on where
you installed the NDK and SDK. Otherwise an error such as:
"The external label '//external:android/sdk' is not bound to anything" will
be reported.

Also edit the API levels for the SDK in WORKSPACE to the highest level you
have installed in your SDK. This must be >= 23 (this is completely independent
of the API level of the demo, which is defined in AndroidManifest.xml).
The NDK API level may remain at 14.

##### Install Model Files (optional)

The TensorFlow `GraphDef`s that contain the model definitions and weights
are not packaged in the repo because of their size. They are downloaded
automatically and packaged with the APK by Bazel via a new_http_archive defined
in `WORKSPACE` during the build process.

**Optional**: If you wish to place the models in your assets manually (E.g. for
non-Bazel builds), remove all of the `model_files` entries from the `assets`
list in `tensorflow_demo` found in the `[BUILD](BUILD)` file. Then download
and extract the archives yourself to the `assets` directory in the source tree:

for MODEL_ZIP in
curl -L ${BASE_URL}/${MODEL_ZIP} -o /tmp/${MODEL_ZIP}
unzip /tmp/${MODEL_ZIP} -d tensorflow/examples/android/assets/

This will extract the models and their associated metadata files to the local
assets/ directory.

##### Build

After editing your WORKSPACE file to update the SDK/NDK configuration,
you may build the APK. Run this from your workspace root:

bazel build -c opt //tensorflow/examples/android:tensorflow_demo

If you get build errors about protocol buffers, run
`git submodule update --init` and make sure that you've modified your WORKSPACE
file as instructed, then try building again.

##### Install

Make sure that adb debugging is enabled on your Android 5.0 (API 21) or
later device, then after building use the following command from your workspace
root to install the APK:

adb install -r bazel-bin/tensorflow/examples/android/tensorflow_demo.apk

### Android Studio

Android Studio may be used to build the demo in conjunction with Bazel. First,
make sure that you can build with Bazel following the above directions. Then,
look at [build.gradle](build.gradle) and make sure that the path to Bazel
matches that of your system.

At this point you can add the tensorflow/examples/android directory as a new
Android Studio project. Click through installing all the Gradle extensions it
requests, and you should be able to have Android Studio build the demo like any
other application (it will call out to Bazel to build the native code with the

### CMake

Full CMake support for the demo is coming soon, but for now it is possible to
build the TensorFlow Android Inference library using
Empty file.
@@ -0,0 +1,15 @@
package(default_visibility = ["//visibility:public"])

licenses(["notice"]) # Apache 2.0

# It is necessary to use this filegroup rather than globbing the files in this
# folder directly the examples/android:tensorflow_demo target due to the fact
# that assets_dir is necessarily set to "" there (to allow using other
# arbitrary targets as assets).
name = "asset_files",
srcs = glob(
exclude = ["BUILD"],

0 comments on commit 7c4664a

Please sign in to comment.