|
| 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). |
0 commit comments