pre: OpenCV 4.1.2 (version++)
[platform/upstream/opencv.git] / doc / tutorials / dnn / dnn_android / dnn_android.markdown
1 # How to run deep networks on Android device {#tutorial_dnn_android}
2
3 ## Introduction
4 In this tutorial you'll know how to run deep learning networks on Android device
5 using OpenCV deep learning module.
6
7 Tutorial was written for the following versions of corresponding software:
8 - Android Studio 2.3.3
9 - OpenCV 3.3.0+
10
11 ## Requirements
12
13 - Download and install Android Studio from https://developer.android.com/studio.
14
15 - Get the latest pre-built OpenCV for Android release from https://github.com/opencv/opencv/releases and unpack it (for example, `opencv-4.1.2-android-sdk.zip`).
16
17 - Download MobileNet object detection model from https://github.com/chuanqi305/MobileNet-SSD. We need a configuration file `MobileNetSSD_deploy.prototxt` and weights `MobileNetSSD_deploy.caffemodel`.
18
19 ## Create an empty Android Studio project
20 - Open Android Studio. Start a new project. Let's call it `opencv_mobilenet`.
21 ![](1_start_new_project.png)
22
23 - Keep default target settings.
24 ![](2_start_new_project.png)
25
26 - Use "Empty Activity" template. Name activity as `MainActivity` with a
27 corresponding layout `activity_main`.
28 ![](3_start_new_project.png)
29
30   ![](4_start_new_project.png)
31
32 - Wait until a project was created. Go to `Run->Edit Configurations`.
33 Choose `USB Device` as target device for runs.
34 ![](5_setup.png)
35 Plug in your device and run the project. It should be installed and launched
36 successfully before we'll go next.
37 @note Read @ref tutorial_android_dev_intro in case of problems.
38
39 ![](6_run_empty_project.png)
40
41 ## Add OpenCV dependency
42
43 - Go to `File->New->Import module` and provide a path to `unpacked_OpenCV_package/sdk/java`. The name of module detects automatically.
44 Disable all features that Android Studio will suggest you on the next window.
45 ![](7_import_module.png)
46
47   ![](8_import_module.png)
48
49 - Open two files:
50
51   1. `AndroidStudioProjects/opencv_mobilenet/app/build.gradle`
52
53   2. `AndroidStudioProjects/opencv_mobilenet/openCVLibrary330/build.gradle`
54
55   Copy both `compileSdkVersion` and `buildToolsVersion` from the first file to
56   the second one.
57
58   `compileSdkVersion 14` -> `compileSdkVersion 26`
59
60   `buildToolsVersion "25.0.0"` -> `buildToolsVersion "26.0.1"`
61
62 - Make the project. There is no errors should be at this point.
63
64 - Go to `File->Project Structure`. Add OpenCV module dependency.
65 ![](9_opencv_dependency.png)
66
67   ![](10_opencv_dependency.png)
68
69 - Install once an appropriate OpenCV manager from `unpacked_OpenCV_package/apk`
70 to target device.
71 @code
72 adb install OpenCV_3.3.0_Manager_3.30_armeabi-v7a.apk
73 @endcode
74
75 - Congratulations! We're ready now to make a sample using OpenCV.
76
77 ## Make a sample
78 Our sample will takes pictures from a camera, forwards it into a deep network and
79 receives a set of rectangles, class identifiers and confidence values in `[0, 1]`
80 range.
81
82 - First of all, we need to add a necessary widget which displays processed
83 frames. Modify `app/src/main/res/layout/activity_main.xml`:
84 @include android/mobilenet-objdetect/res/layout/activity_main.xml
85
86 - Put downloaded `MobileNetSSD_deploy.prototxt` and `MobileNetSSD_deploy.caffemodel`
87 into `app/build/intermediates/assets/debug` folder.
88
89 - Modify `/app/src/main/AndroidManifest.xml` to enable full-screen mode, set up
90 a correct screen orientation and allow to use a camera.
91 @include android/mobilenet-objdetect/AndroidManifest.xml
92
93 - Replace content of `app/src/main/java/org/opencv/samples/opencv_mobilenet/MainActivity.java`:
94 @include android/mobilenet-objdetect/src/org/opencv/samples/opencv_mobilenet/MainActivity.java
95
96 - Launch an application and make a fun!
97 ![](11_demo.jpg)