Compile OpenCV with CANN backend

Prepare environment

Simply you can use docker container, Dockerfile is here.

Or install manually:

driver

1
2
3
Download driver here. https://www.hiascend.com/hardware/firmware-drivers
chmod +x A300-3010-npu-driver_6.0.0_linux-x86_64.run
./A300-3010-npu-driver_6.0.0_linux-x86_64.run --install

toolkit

1
2
3
Download ascend_toolkit here. https://www.hiascend.com/en/software/cann/community
chmod +x Ascend-cann-toolkit_7.0.RC1.alpha002_linux-x86_64.run
./Ascend-cann-toolkit_7.0.RC1.alpha002_linux-x86_64.run --install

Clone repository

Clone opencv repositories, In order to facilitate code reading, make sure opencv_contrib is inside opencv dir.

1
2
3
git clone git@github.com:opencv/opencv.git
cd opencv
git clone git@github.com:opencv/opencv_contrib.git

Build

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
cmake 
-DCMAKE_INSTALL_PREFIX=/home/hua/code/opencv/build/install
-DWITH_DEBUG=1
-DBUILD_WITH_DEBUG_INFO=1
-DOPENCV_EXTRA_MODULES_PATH=/home/hua/code/opencv/opencv_contrib/modules
-DWITH_CUDA=0
-DWITH_CANN=1
-DPYTHON3_EXECUTABLE=/home/hua/anaconda3/envs/py39/bin/python
-DPYTHON_LIBRARY=/home/hua/anaconda3/envs/py39
-DPYTHON_INCLUDE_DIR=/home/hua/anaconda3/envs/py39/include/python3.9
-DBUILD_opencv_wechat_qrcode=OFF
-DBUILD_opencv_xfeatures2d=OFF
-DBUILD_opencv_face=OFF
-DBUILD_opencv_dnn=OFF
-DBUILD_opencv_features2d=OFF
-DWITH_CAROTENE=OFF
-DWITH_IPP=OFF
-DBUILD_DOCS=ON

make -j
make install

Make sure CANN and python-dev is detected:

1
2
3
4
5
6
7
8
9
[cmake] --   CANN:                          YES
[cmake] -- Include path /home/hua/Ascend/ascend-toolkit/latest/include /home/hua/Ascend/ascend-toolkit/latest/opp
[cmake] -- Link libraries: /home/hua/Ascend/ascend-toolkit/latest/acllib/lib64/libascendcl.so /home/hua/Ascend/ascend-toolkit/latest/lib64/libacl_op_compiler.so /home/hua/Ascend/ascend-toolkit/latest/opp/built-in/op_proto/lib/linux/x86_64/libopsproto.so /home/hua/Ascend/ascend-toolkit/latest/compiler/lib64/libgraph.so /home/hua/Ascend/ascend-toolkit/latest/compiler/lib64/libge_compiler.so /home/hua/Ascend/ascend-toolkit/latest/compiler/lib64/libgraph_base.so
[cmake] --
[cmake] -- Python 3:
[cmake] -- Interpreter: /home/hua/anaconda3/envs/py39/bin/python (ver 3.9.17)
[cmake] -- Libraries: /home/hua/anaconda3/envs/py39/lib/libpython3.9.so (ver 3.9.17)
[cmake] -- numpy: /home/hua/anaconda3/envs/py39/lib/python3.9/site-packages/numpy/core/include (ver 1.25.2)
[cmake] -- install path: lib/python3.9/site-packages/cv2/python-3.9

Run Test

1
./bin/opencv_test_cannops