Run OpenVINO-2021.2, OpenCV-4.5.1-openvino on M1 MacBook Air with Intel Neural Compute Stick 2 (conda-forge/Miniforge)

# Introduction

2020-12-19 時点の記録。

conda-forge/Miniforge では[Apple Silicon(M1)対応(osx-arm64)](https://conda-forge.org/blog/posts/2020-10-29-macos-arm64/)が先んじています。
numpy, scipy, matplotlib などが対応されています。

Work!! Intel Neural Compute Stick 2 (MYRIAD X) on Apple Silicon (M1) MacBook Air !!
![MBA-NCS2.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/154910/04f8117f-ad42-33d3-092d-d6a83eb9a980.jpeg)

# Environment
- MacBook Air 2020 (M1, 2020)
- Intel Compute Stick 2 (NCS2)
- OpenVINO 2021.2 (No support CPU:arm)
- OpenCV 4.5.1-openvino
- [Miniforge3](https://github.com/conda-forge/miniforge): cython
- MacPorts: cmake automake autoconf wget libusb libtool

# build OpenVINO 2021.2
## source tree

NOTE: Don't work MacPorts's(Homebrew's) git-lfs yet. (2020-12-19)

Download git-lfs from https://git-lfs.github.com/
cp /usr/local/bin/git-lfs (x86_64)

```
git clone -b 2021.2 https://github.com/openvinotoolkit/openvino.git openvino-2021.2
cd openvino-2021.2
git submodule update --init --recursive
```

## build

Install dir /opt/arm64/openvino. Same as OpenVINO Distribution.
(/usr/localで色々混じるのが嫌だし、Intel Ubuntuではaptで入れるので場所を揃えたいので)

```
mkdir openvino-2021.2/build
cd openvino-2021.2/build

cmake \
    -DCMAKE_INSTALL_PREFIX=/opt/arm64/openvino_2021.2 \
    -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 8
sudo make install
sudo ln -s /opt/arm64/openvino_2021.2 /opt/arm64/openvino
```

if use zsh, patch it.

```
*** 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=/usr/local
```  

environment valiable

```
source /opt/arm64/openvino/bin/setupvars.sh
export ngraph_DIR=/opt/arm64/openvino/cmake
export OpenCV_DIR=/opt/arm64/openvino/opencv
export PYTHONPATH=/opt/arm64/openvino/opencv/lib/python3.9/site-packages:$PYTHONPATH
export LD_LIBRARY_PATH=/opt/arm64/openvino/lib:$LD_LIBRARY_PATH
```

# build OpenCV

## source tree

```
git clone --depth 1 -b 4.5.1-openvino https://github.com/opencv/opencv.git opencv-4.5.1-openvino
```

## build

Install dir /opt/arm64/openvino/opencv. Same as OpenVINO Distribution.

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

cmake \
    -DCMAKE_INSTALL_PREFIX=/opt/arm64/openvino_2021.2/opencv \
    -DCMAKE_BUILD_TYPE=Release \
    -DWITH_INF_ENGINE=ON \
    -DENABLE_CXX11=ON \
    -DWITH_TBB=ON \
    -DENABLE_PYTHON=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 8
sudo make install
```

# Run with Intel Neural Compurte Stick 2

```
python opencv-4.5.1-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
```

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


# Reference
- OpenVINO wiki: [BuildingForMacOS](https://github.com/openvinotoolkit/openvino/wiki/BuildingForMacOS)

# Note

pytorch が早くApple Silicon(M1)対応されますように！