|
| 1 | +# Coco Detection Android Demo |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +This is a demo for ARM processors Android devices. Using object detection model to reach the coco datasets' infomation. We use pre-trained models from [Open Model Zoo](https://github.com/openvinotoolkit/open_model_zoo): [ssdlite_mobilenet_v2](https://github.com/openvinotoolkit/open_model_zoo/tree/master/models/public/ssdlite_mobilenet_v2) for object detection in coco dataset, [efficientdet-d0-tf](https://github.com/openvinotoolkit/open_model_zoo/tree/master/models/public/efficientdet-d0-tf), [pelee-coco](https://github.com/openvinotoolkit/open_model_zoo/tree/master/models/public/pelee-coco). |
| 6 | + |
| 7 | +The application reads frames from your device's camera or emulator's camera, and processes the network to detect the coco objects' locations and attributes. Then draw it on the image. |
| 8 | + |
| 9 | +The current openvino engine does not currently support models in INT8 MIXed format for reasoning on ARM devices, but will support models in this format in the near future and will achieve better performance. |
| 10 | + |
| 11 | +Otherwise, there is no difference between FP16 and FP32 for CPU, the plugin will convert it automatically to FP32. |
| 12 | + |
| 13 | +## How to run it |
| 14 | + |
| 15 | +### Build the OpenVINO libraries for Android |
| 16 | + |
| 17 | +Before we run the demo on ARM Android phones, we need to prepare the libraries for ARM plugin and java bingings for OpenVINO. These libraries are built from Ubuntu 18.04, and it |
| 18 | + |
| 19 | +- Set java environment |
| 20 | + |
| 21 | +```bash |
| 22 | +sudo apt-get install -y openjdk-8-jdk |
| 23 | +export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64 |
| 24 | +``` |
| 25 | + |
| 26 | +- Create work directory |
| 27 | + |
| 28 | +```bash |
| 29 | +mkdir openvino_android |
| 30 | +export WORK_DIR="$(pwd)/openvino_android" |
| 31 | +cd $WORK_DIR |
| 32 | +``` |
| 33 | + |
| 34 | +- Clone `OpenVINO` and `OpenVINO Contrib` repositories (Use master branch for Java API 2.0). |
| 35 | + |
| 36 | +```bash |
| 37 | +git clone --recurse https://github.com/openvinotoolkit/openvino.git "$WORK_DIR/openvino" |
| 38 | +git clone --recurse https://github.com/openvinotoolkit/openvino_contrib.git "$WORK_DIR/openvino_contrib" |
| 39 | +``` |
| 40 | + |
| 41 | +- Download Android NDK and set environment for it. (If you need proxy, you need to set specific url to XXX, or just remove `--no_https --proxy=http --proxy_host=XXX --proxy_port=XXX`) |
| 42 | + |
| 43 | +```bash |
| 44 | +mkdir "$WORK_DIR/android-tools" |
| 45 | +wget https://dl.google.com/android/repository/commandlinetools-linux-7583922_latest.zip |
| 46 | +unzip commandlinetools-linux-7583922_latest.zip |
| 47 | +yes | ./cmdline-tools/bin/sdkmanager --sdk_root="$WORK_DIR/android-tools" --licenses --no_https --proxy=http --proxy_host=XXX --proxy_port=XXX |
| 48 | +./cmdline-tools/bin/sdkmanager --sdk_root="$WORK_DIR/android-tools" --install "ndk-bundle" --no_https --proxy=http --proxy_host=XXX --proxy_port=XXX |
| 49 | +``` |
| 50 | + |
| 51 | +- Build OpenVINO and ARM plugin for ARM64 |
| 52 | + |
| 53 | +```bash |
| 54 | +mkdir "$WORK_DIR/openvino_build" "$WORK_DIR/openvino_install" |
| 55 | +cmake -GNinja \ |
| 56 | + -DVERBOSE_BUILD=ON \ |
| 57 | + -DCMAKE_BUILD_TYPE=Release \ |
| 58 | + -DCMAKE_TOOLCHAIN_FILE="$WORK_DIR/android-tools/ndk-bundle/build/cmake/android.toolchain.cmake" \ |
| 59 | + -DANDROID_ABI=arm64-v8a \ |
| 60 | + -DANDROID_STL=c++_shared \ |
| 61 | + -DANDROID_PLATFORM=29 \ |
| 62 | + -DENABLE_SAMPLES=ON \ |
| 63 | + -DENABLE_INTEL_MYRIAD=OFF \ |
| 64 | + -DENABLE_INTEL_MYRIAD_COMMON=OFF \ |
| 65 | + -DBUILD_java_api=ON \ |
| 66 | + -DTHREADING=SEQ \ |
| 67 | + -DIE_EXTRA_MODULES="$WORK_DIR/openvino_contrib/modules" \ |
| 68 | + -DARM_COMPUTE_SCONS_JOBS=$(nproc) \ |
| 69 | + -DCMAKE_INSTALL_PREFIX="$WORK_DIR/openvino_install" \ |
| 70 | + -B "$WORK_DIR/openvino_build" -S "$WORK_DIR/openvino" |
| 71 | +ninja -C "$WORK_DIR/openvino_build" |
| 72 | +ninja -C "$WORK_DIR/openvino_build" install |
| 73 | +``` |
| 74 | + |
| 75 | +The built results are in `$WORK_DIR/openvino_install/runtime/lib/aarch64`. We will use them later. |
| 76 | + |
| 77 | +> Please confirm that your `plugins.xml` in `$WORK_DIR/openvino_install/runtime/lib/aarch64` contains plugin name `"CPU"`. |
| 78 | +
|
| 79 | +- Build Java API 2.0 for Android |
| 80 | + |
| 81 | +```bash |
| 82 | +source $WORK_DIR/openvino_install/setupvars.sh |
| 83 | +cd $WORK_DIR/openvino_contrib/modules/java_api |
| 84 | +gradle build |
| 85 | +``` |
| 86 | + |
| 87 | +- Download and convert model "ssdlite_mobilenet_v2" [or pelee-coco, efficientdet-d0-tf] with Open Model Zoo |
| 88 | + |
| 89 | +```bash |
| 90 | +git clone --depth 1 https://github.com/openvinotoolkit/open_model_zoo "$WORK_DIR/open_model_zoo" |
| 91 | +cd "$WORK_DIR/open_model_zoo/tools/downloader" |
| 92 | +python3 -m pip install -r requirements.in |
| 93 | +omz_downloader --name ssdlite_mobilenet_v2 --output_dir $WORK_DIR/open_model_zoo/tools/downloader |
| 94 | +omz_converter --name ssdlite_mobilenet_v2 --download_dir $WORK_DIR/open_model_zoo/tools/downloader --precision FP16 |
| 95 | +``` |
| 96 | + |
| 97 | +### Import demo project on Android Studio |
| 98 | + |
| 99 | +In this step, we will import demo project to infer object detection. |
| 100 | + |
| 101 | +- Choose and download [Android Studio](https://developer.android.com/studio) on your PC. |
| 102 | + |
| 103 | +- Clone latest branch of OpenVINO Contrib. |
| 104 | + |
| 105 | +```bash |
| 106 | +git clone https://github.com/openvinotoolkit/openvino_contrib.git "$WORK_DIR/demo" |
| 107 | +``` |
| 108 | + |
| 109 | +- Select "File -> Open", and import demo project in `"$WORK_DIR/demo/openvino_contrib/modules/java_api/demos/coco_detection_android_demo"`. |
| 110 | + |
| 111 | +- Copy libraries and model files to the corresponding folder. |
| 112 | + |
| 113 | + 1. Clone `"$WORK_DIR/openvino_contrib/modules/java_api/build/libs/java_api.jar"` to `app/libs` folder, and add it as library. |
| 114 | + 2. Clone `"$WORK_DIR/openvino_install/runtime/lib/aarch64/*.so"` and `"$WORK_DIR/android-tools/ndk-bundle/sources/cxx-stl/llvm-libc++/libs/arm64-v8a/libc++_shared.so"` to `"app/src/main/jniLibs/arm64-v8a"` |
| 115 | + 3. Clone `"$WORK_DIR/openvino_install/runtime/lib/aarch64/plugins.xml"` to `"app/src/main/assets"` |
| 116 | + 4. Copy `"$WORK_DIR/open_model_zoo/tools/downloader/intel/ssdlite_mobilenet_v2/FP16/ssdlite_mobilenet_v2.xml"`, `"$WORK_DIR/open_model_zoo/tools/downloader/intel/ssdlite_mobilenet_v2/FP16/ssdlite_mobilenet_v2.bin"` to `"app/src/main/assets"` |
| 117 | + |
| 118 | +- Add OpenCV dependency to project |
| 119 | + |
| 120 | + 1. Download [OpenCV SDK for Android](https://github.com/opencv/opencv/releases/download/4.5.0/opencv-4.5.0-android-sdk.zip) and unpack it. |
| 121 | + 2. Import OpenCV module: select "File -> New -> ImportModule", and sepcify a path to unpacked SDK and set module name to "ocv". |
| 122 | + 3. Replace `compileSdkVersion 26`, `targetSdkVersion 26` to `compileSdkVersion 32`, `targetSdkVersion 32` in `"$WORK_DIR/coco_detection_android_demo/ocv/build.gradle"` |
| 123 | + |
| 124 | +- Start a ARM-based Android Emulator. |
| 125 | + |
| 126 | + 1. Using `AVD Manager -> Create Virtual Device`, and choose one virtual device. |
| 127 | + 2. Select a system image with `arm64-v8a`. |
| 128 | + |
| 129 | +- Run it! |
| 130 | + |
| 131 | +> The first time when you run the demo application on your device, your need to grant camera permission. Then run it again. |
0 commit comments