Apple Silicon M1: OpenVINO-2021.3, OpenCV-4.5.2-openvino

## Introduction
2021-03-26時点の内容。

Apple Silicon M1でビルドしよう。OpenVINOもNeural Compute Stick 2なら動きます。

![ncs2-openvino.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/154910/6429bf78-097b-38db-cec5-9c32b57a4d93.jpeg)


## Environment

- Apple Silicon (M1) MacBook Air
- Intel Compute Stick 2 (NCS2)
- OpenVINO 2021.3 (No support CPU:arm)
- OpenCV 4.5.3-openvino
- [MacPorts](https://www.macports.org/)
  - sudo port install cmake gflags autoconf automake libtool libusb git-lfs
- [Miniforge3](https://github.com/conda-forge/miniforge)
  - conda install numpy matplotlib cython pandas scipy scikit-learn

# build OpenCV 4.5.2-openvino
## source tree
```
git clone --depth 1 -b 4.5.2-openvino https://github.com/opencv/opencv.git opencv-4.5.2-openvino
```

### build
Install dir /opt/intel/openvino/opencv. Same as OpenVINO Distribution.

```
mkdir opencv-4.5.2-openvino/build
cd opencv-4.5.2-openvino/build

cmake \
    -DCMAKE_INSTALL_PREFIX=/opt/intel/openvino/opencv \
    -DCMAKE_BUILD_TYPE=Release \
    -DWITH_INF_ENGINE=ON \
    -DENABLE_CXX11=ON \
    -DWITH_TBB=ON \
    -DPYTHON3_EXECUTABLE=$HOME/miniforge3/bin/python3.9 \
    -DPYTHON3_LIBRARY=$HOME/miniforge3/lib/libpython3.9.dylib \
    -DPYTHON3_INCLUDE_DIR=$HOME/miniforge3/include/python3.9 ..

make -j 4
sudo make install
```
## build OpenVINO 2021.3
### source tree
```
git clone -b 2021.2 https://github.com/openvinotoolkit/openvino.git openvino-2021.3
cd openvino-2021.3
git submodule update --init --recursive
```

### build

Install dir /opt/intel/openvino. Same as OpenVINO Distribution.

```
mkdir openvino-2021.3/build
cd openvino-2021.3/build

cmake \
    -DCMAKE_INSTALL_PREFIX=/opt/intel/openvino \
    -DCMAKE_BUILD_TYPE=Release \
    -DENABLE_PYTHON=ON \
    -DPYTHON_EXECUTABLE=$HOME/miniforge3/bin/python3.9 \
    -DPYTHON_LIBRARY=$HOME/miniforge3/lib/libpython3.9.dylib \
    -DPYTHON_INCLUDE_DIR=$HOME/miniforge3/include/python3.9 ..

make -j 4
sudo make install
```

patch this.

```
*** setupvars.sh.orig	2020-12-17 12:28:59.000000000 +0900
--- setupvars.sh	2020-12-17 12:29:34.000000000 +0900
***************
*** 18,23 ****
--- 18,24 ----
  BASE_DIR="$( dirname "$SCRIPT_DIR" )"
  
  INSTALLDIR="${BASE_DIR}"
+ INSTALLDIR=/opt/intel/openvino
```  

environment valiable

```
source /opt/intel/openvino/bin/setupvars.sh
export ngraph_DIR=/opt/intel/openvino/deployment_tools/ngraph/cmake
export OpenCV_DIR=/opt/intel/openvino/opencv 
export PYTHONPATH=$OpenCV_DIR/lib/python3.9/site-packages:$PYTHONPATH
```
※ お互いに依存しているので OpenCV → OpenVINO →OpenCVでビルド&インストールすること。

# Run with Intel Neural Compurte Stick 2

```
python opencv-4.5.2-openvino/samples/python/video_threaded.py
```

```
python /opt/arm64/openvino/deployment_tools/inference_engine/samples/python/classification_sample_async/classification_sample_async.py -m alexnet.xml -d MYRIAD -i image.jpg
```
- [OpenCV DNN backend Inference Engine (OpenVINO: Python, C++)](https://qiita.com/kose3/items/bb7f4aa0d6a4e4ab26e8)

Run Success!!  pythonでも動作しています。


## Reference

（最小ステップしか書いていないので）ここに書いてあるままでうまく行かなかったり、環境を多少変更したい場合は以下に目を通すとできるんじゃないかな。

- [M1 MacBookでの開発環境構築](https://qiita.com/hjmr/items/b81de997d4c2b64ef140)
- OpenVINO wiki: [BuildingForMacOS](https://github.com/openvinotoolkit/openvino/wiki/BuildingForMacOS)

## TODO

- CoreMLも試したい、、、けどねぇ。OpenCV ML backend でCoreMLサポートしないですかね。