从零开始编译OpenCV NPU教程
本文介绍如何从零开始,成功编译OpenCV NPU项目。
本示例使用的机器为华为云昇腾310虚拟机。
1 2 3
| 实例类型:AI加速型Ai1s 操作系统:Ubuntu 18.04 IDE:VSCode
|
1. 环境准备
1.1 安装驱动及依赖
按照昇腾虚拟机固件与驱动安装指引__卸载旧版驱动__和__安装Ascend 310驱动__(此处仅摘抄了需要执行的命令),此时先不要安装CANN,待准备好依赖的python环境后再安装。
1 2 3 4 5 6 7 8 9 10 11 12
| cd /usr/local/Ascend
./A300-3010-npu-driver_21.0.4_linux-x86_64.run --full
npu-smi info
usermod -a -G HwHiAiUser/gid usrName/uid
apt-get install -y gcc g++ make cmake zlib1g zlib1g-dev openssl libsqlite3-dev libssl-dev libffi-dev unzip pciutils ne
|
1.2 Anaconda安装及配置
从Anaconda官网下载x86_64对应的Anaconda,使用bash命令安装:
1 2 3
| bash Anaconda3-2023.07-2-Linux-x86_64.sh
|
由于国内下载conda资源过慢,建议配置清华源anaconda镜像提高下载速度。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| vim ~/.condarc
channels: - defaults show_channel_urls: true default_channels: - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2 custom_channels: conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud pytorch-lts: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud deepmodeling: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/
|
1.3 python虚拟环境安装
新建python版本为3.X的conda虚拟环境___【CANN目前只支持python3.7/3.8/3.9版本】___
1 2 3 4 5 6 7 8 9 10 11 12 13
| conda create -n YOUR_ENV_NAME python=3.9
conda activate YOUR_ENV_NAME
pip3 install --upgrade pip
pip3 install attrs numpy decorator sympy cffi pyyaml pathlib2 psutil protobuf scipy requests
vim ~/.bashrc
source ~/.bashrc
|
1.4 CANN安装
注意 开发套件安装,从 CANN 社区版下载-昇腾社区下载__6.3.RC2.alpha003__对应的CANN。
1 2 3 4 5 6 7 8 9
| ./Ascend-cann-toolkit_6.3.RC2.alpha003_linux-x86_64.run --install
vim ~/.bashrc
source ~/Ascend/ascend-toolkit/set_env.sh
source ~/.bashrc
|
安装好驱动之后,将系统用户添加到组里,保证在usrName用户下可以正常使用驱动。
1
| usermod -a -G HwHiAiUser/gid usrName/uid
|
2. OpenCV下载及编译
2.1 OpenCV项目下载
OpenCV为保证官方发行版的稳定性,使用另外的仓opencv_contrib接收贡献的功能,因此opencv和opencv_contrib均需下载。
1 2 3 4 5 6
| git clone https://github.com/opencv/opencv.git
cd opencv
git clone https://github.com/opencv/opencv_contrib.git
|
2.2 项目配置
在opencv项目路径下新建.vscode
文件夹,新建调试配置文件launch.json
,写入以下内容(注释为解析,不需要写入):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| { "version": "0.2.0", "configurations": [ { "name": "ascend", "type": "cppdbg", "request": "launch", "cwd": "${workspaceFolder}", "program": "~/anaconda3/envs/py39/bin/python", "args": [ "~/workspace/opencv/test.py", ], "MIMode": "gdb", "targetArchitecture": "x86_64", "stopAtEntry": false, "externalConsole": false, "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ] } ] }
|
同样地,.vscode
文件夹下新建配置文件setting.json
,写入cmake配置项和文件关联配置项如下(注释为解析,不需要写入):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| { "cmake.configureArgs": [ "-DCMAKE_INSTALL_PREFIX=~/workspace/opencv/build/install", "-DWITH_DEBUG=0", "-DBUILD_WITH_DEBUG_INFO=0", "-DOPENCV_EXTRA_MODULES_PATH=~/workspace/opencv/opencv_contrib/modules", "-DWITH_CUDA=0", "-DWITH_CANN=1", "-DPYTHON3_EXECUTABLE=~/anaconda3/envs/$YOUR_ENV_NAME$/bin/python", "-DPYTHON_LIBRARY=~/anaconda3/envs/$YOUR_ENV_NAME$", "-DPYTHON_INCLUDE_DIR=~/anaconda3/envs/$YOUR_ENV_NAME$/include/python3.9", "-DBUILD_opencv_wechat_qrcode=OFF", "-DBUILD_opencv_xfeatures2d=OFF", "-DBUILD_opencv_face=OFF", "-DBUILD_opencv_dnn=OFF", "-DBUILD_opencv_features2d=OFF", "-DBUILD_opencv_bioinspired=OFF", "-DBUILD_opencv_flann=OFF", "-DBUILD_opencv_hdf=OFF", "-DBUILD_opencv_hfs=OFF", "-DBUILD_opencv_fuzzy=OFF", "-DBUILD_opencv_intensity_transform=OFF", "-DBUILD_opencv_line_descriptor=OFF", "-DBUILD_opencv_ml=OFF", "-DBUILD_opencv_phase_unwrapping=OFF", "-DBUILD_opencv_photo=OFF", "-DBUILD_opencv_quality=OFF", "-DBUILD_opencv_tracking=OFF", "-DWITH_CAROTENE=OFF", "-DWITH_IPP=OFF", "-DBUILD_DOCS=ON", ], }
|
2.3 项目编译
通过vscode安装C/C++、CMake、CMake Tools、Python拓展,由于项目配置项较多,通过2.2 项目配置+CMake Tools对项目进行编译。
利用CMake Tool进行编译示例界面。configure根据2.2 项目配置中指定的编译选项进行配置。build则实现项目编译。
完成cmake configure之后,请检查是否包含CANN(下图黄色高亮),python是否指向YOUR_ENV_NAME虚拟环境(下图绿色高亮),如果这些答案均为“是”,则关键配置正确。
build成功的标志则为[build] Build finished with exit code 0
。
项目编译完成后,在build
目录下会出现python_loader
文件夹,在终端中执行python_loader
路径下的setup.py
,即可将opencv安装到python虚拟环境中,从而实现python调用opencv。
1 2 3 4 5 6 7 8 9 10
| cd ./build/python_loader
python setup.py develop
python >>>import cv2 >>>
|