Skip to content

Commit 7c4664a

Browse files
committed
Import the original TensorFlow Android sample
1 parent bac6aa1 commit 7c4664a

96 files changed

Lines changed: 14671 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

android/AndroidManifest.xml

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright 2016 The TensorFlow Authors. All Rights Reserved.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
18+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
19+
package="org.tensorflow.demo">
20+
21+
<uses-permission android:name="android.permission.CAMERA" />
22+
<uses-feature android:name="android.hardware.camera" />
23+
<uses-feature android:name="android.hardware.camera.autofocus" />
24+
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
25+
26+
<uses-sdk
27+
android:minSdkVersion="21"
28+
android:targetSdkVersion="23" />
29+
30+
<application android:allowBackup="true"
31+
android:debuggable="true"
32+
android:label="@string/app_name"
33+
android:icon="@drawable/ic_launcher"
34+
android:theme="@style/MaterialTheme">
35+
36+
<activity android:name="org.tensorflow.demo.ClassifierActivity"
37+
android:screenOrientation="portrait"
38+
android:label="@string/activity_name_classification">
39+
<intent-filter>
40+
<action android:name="android.intent.action.MAIN" />
41+
<category android:name="android.intent.category.LAUNCHER" />
42+
</intent-filter>
43+
</activity>
44+
45+
<activity android:name="org.tensorflow.demo.DetectorActivity"
46+
android:screenOrientation="portrait"
47+
android:label="@string/activity_name_detection">
48+
<intent-filter>
49+
<action android:name="android.intent.action.MAIN" />
50+
<category android:name="android.intent.category.LAUNCHER" />
51+
</intent-filter>
52+
</activity>
53+
54+
<activity android:name="org.tensorflow.demo.StylizeActivity"
55+
android:screenOrientation="portrait"
56+
android:label="@string/activity_name_stylize">
57+
<intent-filter>
58+
<action android:name="android.intent.action.MAIN" />
59+
<category android:name="android.intent.category.LAUNCHER" />
60+
</intent-filter>
61+
</activity>
62+
</application>
63+
64+
</manifest>

android/BUILD

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
# Description:
2+
# TensorFlow camera demo app for Android.
3+
4+
package(default_visibility = ["//visibility:public"])
5+
6+
licenses(["notice"]) # Apache 2.0
7+
8+
load(
9+
"//tensorflow:tensorflow.bzl",
10+
"tf_copts",
11+
"tf_opts_nortti_if_android",
12+
)
13+
14+
exports_files(["LICENSE"])
15+
16+
LINKER_SCRIPT = "//tensorflow/contrib/android:jni/version_script.lds"
17+
18+
# libtensorflow_demo.so contains the native code for image colorspace conversion
19+
# and object tracking used by the demo. It does not require TF as a dependency
20+
# to build if STANDALONE_DEMO_LIB is defined.
21+
# TF support for the demo is provided separately by libtensorflow_inference.so.
22+
cc_binary(
23+
name = "libtensorflow_demo.so",
24+
srcs = glob([
25+
"jni/**/*.cc",
26+
"jni/**/*.h",
27+
]),
28+
copts = tf_copts(),
29+
defines = ["STANDALONE_DEMO_LIB"],
30+
linkopts = [
31+
"-landroid",
32+
"-ljnigraphics",
33+
"-llog",
34+
"-lm",
35+
"-z defs",
36+
"-s",
37+
"-Wl,--version-script", # This line must be directly followed by LINKER_SCRIPT.
38+
LINKER_SCRIPT,
39+
],
40+
linkshared = 1,
41+
linkstatic = 1,
42+
tags = [
43+
"manual",
44+
"notap",
45+
],
46+
deps = [
47+
LINKER_SCRIPT,
48+
],
49+
)
50+
51+
cc_library(
52+
name = "tensorflow_native_libs",
53+
srcs = [
54+
":libtensorflow_demo.so",
55+
"//tensorflow/contrib/android:libtensorflow_inference.so",
56+
],
57+
tags = [
58+
"manual",
59+
"notap",
60+
],
61+
)
62+
63+
android_binary(
64+
name = "tensorflow_demo",
65+
srcs = glob([
66+
"src/**/*.java",
67+
]),
68+
# Package assets from assets dir as well as all model targets. Remove undesired models
69+
# (and corresponding Activities in source) to reduce APK size.
70+
assets = [
71+
"//tensorflow/examples/android/assets:asset_files",
72+
":external_assets",
73+
],
74+
assets_dir = "",
75+
custom_package = "org.tensorflow.demo",
76+
inline_constants = 1,
77+
manifest = "AndroidManifest.xml",
78+
resource_files = glob(["res/**"]),
79+
tags = [
80+
"manual",
81+
"notap",
82+
],
83+
deps = [
84+
":tensorflow_native_libs",
85+
"//tensorflow/contrib/android:android_tensorflow_inference_java",
86+
],
87+
)
88+
89+
filegroup(
90+
name = "external_assets",
91+
srcs = [
92+
"@inception5h//:model_files",
93+
"@mobile_multibox//:model_files",
94+
"@stylize//:model_files",
95+
],
96+
)
97+
98+
filegroup(
99+
name = "all_files",
100+
srcs = glob(
101+
["**/*"],
102+
exclude = [
103+
"**/METADATA",
104+
"**/OWNERS",
105+
"bin/**",
106+
"gen/**",
107+
"gradleBuild/**",
108+
"libs/**",
109+
],
110+
),
111+
visibility = ["//tensorflow:__subpackages__"],
112+
)
113+
114+
filegroup(
115+
name = "java_files",
116+
srcs = glob(["src/**/*.java"]),
117+
)
118+
119+
filegroup(
120+
name = "jni_files",
121+
srcs = glob([
122+
"jni/**/*.cc",
123+
"jni/**/*.h",
124+
]),
125+
)
126+
127+
filegroup(
128+
name = "resource_files",
129+
srcs = glob(["res/**"]),
130+
)
131+
132+
exports_files(["AndroidManifest.xml"])

android/README.md

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
# TensorFlow Android Camera Demo
2+
3+
This folder contains an example application utilizing TensorFlow for Android
4+
devices.
5+
6+
## Description
7+
8+
The demos in this folder are designed to give straightforward samples of using
9+
TensorFlow in mobile applications.
10+
11+
Inference is done using the [TensorFlow Android Inference Interface](../../../tensorflow/contrib/android),
12+
which may be built separately if you want a standalone library to drop into your
13+
existing application. Object tracking and YUV -> RGB conversion is handled by
14+
libtensorflow_demo.so.
15+
16+
A device running Android 5.0 (API 21) or higher is required to run the demo due
17+
to the use of the camera2 API, although the native libraries themselves can run
18+
on API >= 14 devices.
19+
20+
## Current samples:
21+
22+
1. [TF Classify](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/android/src/org/tensorflow/demo/ClassifierActivity.java):
23+
Uses the [Google Inception](https://arxiv.org/abs/1409.4842)
24+
model to classify camera frames in real-time, displaying the top results
25+
in an overlay on the camera image.
26+
2. [TF Detect](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/android/src/org/tensorflow/demo/DetectorActivity.java):
27+
Demonstrates a model based on [Scalable Object Detection
28+
using Deep Neural Networks](https://arxiv.org/abs/1312.2249) to
29+
localize and track people in the camera preview in real-time.
30+
3. [TF Stylize](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/android/src/org/tensorflow/demo/StylizeActivity.java):
31+
Uses a model based on [A Learned Representation For Artistic Style]
32+
(https://arxiv.org/abs/1610.07629) to restyle the camera preview image
33+
to that of a number of different artists.
34+
35+
<img src="sample_images/classify1.jpg" width="30%">
36+
<img src="sample_images/stylize1.jpg" width="30%">
37+
<img src="sample_images/detect1.jpg" width="30%">
38+
39+
## Prebuilt APK:
40+
41+
If you just want the fastest path to trying the demo, you may download the
42+
nightly build
43+
[here](https://ci.tensorflow.org/view/Nightly/job/nightly-android/). Expand the
44+
"View" and then the "out" folders under "Last Successful Artifacts" to find
45+
tensorflow_demo.apk. Also available are precompiled native libraries that you
46+
may drop into your own applications. See
47+
[tensorflow/contrib/android/README.md](../../../tensorflow/contrib/android/README.md)
48+
for more details.
49+
50+
## Running the Demo
51+
52+
Once the app is installed it can be started via the "TF Classify", "TF Detect"
53+
and "TF Stylize" icons, which have the orange TensorFlow logo as their icon.
54+
55+
While running the activities, pressing the volume keys on your device will
56+
toggle debug visualizations on/off, rendering additional info to the screen
57+
that may be useful for development purposes.
58+
59+
## Building the Demo from Source
60+
61+
Pick your preferred approach below. At the moment, we have full support for
62+
Bazel, and partial support for gradle, cmake, make, and Android Studio.
63+
64+
As a first step for all build types, clone the TensorFlow repo with:
65+
66+
```
67+
git clone --recurse-submodules https://github.com/tensorflow/tensorflow.git
68+
```
69+
70+
Note that `--recurse-submodules` is necessary to prevent some issues with
71+
protobuf compilation.
72+
73+
### Bazel
74+
75+
NOTE: Bazel does not currently support building for Android on Windows. Full
76+
support for gradle/cmake builds is coming soon, but in the meantime we suggest
77+
that Windows users download the
78+
[prebuilt binaries](https://ci.tensorflow.org/view/Nightly/job/nightly-android/)
79+
instead.
80+
81+
##### Install Bazel and Android Prerequisites
82+
83+
Bazel is the primary build system for TensorFlow. To build with Bazel,
84+
it and the Android NDK and SDK must be installed on your system.
85+
86+
1. Get the recommended Bazel version listed in [os_setup.html](https://www.tensorflow.org/versions/master/get_started/os_setup.html#source)
87+
2. The Android NDK is required to build the native (C/C++) TensorFlow code.
88+
The current recommended version is 12b, which may be found
89+
[here](https://developer.android.com/ndk/downloads/older_releases.html#ndk-12b-downloads).
90+
3. The Android SDK and build tools may be obtained
91+
[here](https://developer.android.com/tools/revisions/build-tools.html),
92+
or alternatively as part of
93+
[Android Studio](https://developer.android.com/studio/index.html). Build
94+
tools API >= 23 is required to build the TF Android demo (though it will
95+
run on API >= 21 devices).
96+
97+
##### Edit WORKSPACE
98+
99+
The Android entries in [`<workspace_root>/WORKSPACE`](../../../WORKSPACE#L2-L13)
100+
must be uncommented with the paths filled in appropriately depending on where
101+
you installed the NDK and SDK. Otherwise an error such as:
102+
"The external label '//external:android/sdk' is not bound to anything" will
103+
be reported.
104+
105+
Also edit the API levels for the SDK in WORKSPACE to the highest level you
106+
have installed in your SDK. This must be >= 23 (this is completely independent
107+
of the API level of the demo, which is defined in AndroidManifest.xml).
108+
The NDK API level may remain at 14.
109+
110+
##### Install Model Files (optional)
111+
112+
The TensorFlow `GraphDef`s that contain the model definitions and weights
113+
are not packaged in the repo because of their size. They are downloaded
114+
automatically and packaged with the APK by Bazel via a new_http_archive defined
115+
in `WORKSPACE` during the build process.
116+
117+
**Optional**: If you wish to place the models in your assets manually (E.g. for
118+
non-Bazel builds), remove all of the `model_files` entries from the `assets`
119+
list in `tensorflow_demo` found in the `[BUILD](BUILD)` file. Then download
120+
and extract the archives yourself to the `assets` directory in the source tree:
121+
122+
```bash
123+
BASE_URL=https://storage.googleapis.com/download.tensorflow.org/models
124+
for MODEL_ZIP in inception5h.zip mobile_multibox_v1a.zip stylize_v1.zip
125+
do
126+
curl -L ${BASE_URL}/${MODEL_ZIP} -o /tmp/${MODEL_ZIP}
127+
unzip /tmp/${MODEL_ZIP} -d tensorflow/examples/android/assets/
128+
done
129+
```
130+
131+
This will extract the models and their associated metadata files to the local
132+
assets/ directory.
133+
134+
##### Build
135+
136+
After editing your WORKSPACE file to update the SDK/NDK configuration,
137+
you may build the APK. Run this from your workspace root:
138+
139+
```bash
140+
bazel build -c opt //tensorflow/examples/android:tensorflow_demo
141+
```
142+
143+
If you get build errors about protocol buffers, run
144+
`git submodule update --init` and make sure that you've modified your WORKSPACE
145+
file as instructed, then try building again.
146+
147+
##### Install
148+
149+
Make sure that adb debugging is enabled on your Android 5.0 (API 21) or
150+
later device, then after building use the following command from your workspace
151+
root to install the APK:
152+
153+
```bash
154+
adb install -r bazel-bin/tensorflow/examples/android/tensorflow_demo.apk
155+
```
156+
157+
### Android Studio
158+
159+
Android Studio may be used to build the demo in conjunction with Bazel. First,
160+
make sure that you can build with Bazel following the above directions. Then,
161+
look at [build.gradle](build.gradle) and make sure that the path to Bazel
162+
matches that of your system.
163+
164+
At this point you can add the tensorflow/examples/android directory as a new
165+
Android Studio project. Click through installing all the Gradle extensions it
166+
requests, and you should be able to have Android Studio build the demo like any
167+
other application (it will call out to Bazel to build the native code with the
168+
NDK).
169+
170+
### CMake
171+
172+
Full CMake support for the demo is coming soon, but for now it is possible to
173+
build the TensorFlow Android Inference library using
174+
[tensorflow/contrib/android/cmake](../../../tensorflow/contrib/android/cmake).

android/__init__.py

Whitespace-only changes.

android/assets/BUILD

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
licenses(["notice"]) # Apache 2.0
4+
5+
# It is necessary to use this filegroup rather than globbing the files in this
6+
# folder directly the examples/android:tensorflow_demo target due to the fact
7+
# that assets_dir is necessarily set to "" there (to allow using other
8+
# arbitrary targets as assets).
9+
filegroup(
10+
name = "asset_files",
11+
srcs = glob(
12+
["**/*"],
13+
exclude = ["BUILD"],
14+
),
15+
)

0 commit comments

Comments
 (0)