NodeJS 后台服务使用opencv4nodejs 安装的过程
环境
系统:deepin v23
node版本:v21
opencv4nodejs:@u4/opencv4nodejs": "^7.1.1
1.检查工具
在此之前我先检查一下安装需要的工具,因为我是获取的源代码到本地进行打包安装的,所以需要安装工具
sudo apt install -y g++
sudo apt install -y cmake
sudo apt install -y make
sudo apt install -y wget unzip
2.安装打包需要的依赖库
sudo apt-get install build-essential libgtk2.0-dev libgtk-3-dev libavcodec-dev libavformat-dev libjpeg-dev libswscale-dev libtiff5-dev
3.下载源码
wget -O opencv.zip https://github.com/opencv/opencv/archive/4.10.0.zip
将源码进行解压 移动到合适的目录下,我这里解压后得到一个opencv-master文件,然后将他移动到opencv文件夹中
unzip opencv.zip
mv opencv-master opencv
所以目录结构如下,一边进行下一步在build文件内执行cmake命令
├── build
└── opencv-master
├── 3rdparty
├── apps
├── build
├── cmake
├── CMakeLists.txt
├── CONTRIBUTING.md
├── COPYRIGHT
├── data
├── doc
├── include
├── LICENSE
├── modules
├── platforms
├── README.md
├── samples
└── SECURITY.md
然后我们开始进入opencv目录下的build文件夹,开始编译openCV
cd opencv
mkdir build
cd build
进入opencv的文件夹build后,执行cmake指令(OpenCV4以上默认不使用pkg-config,该编译选项开启生成opencv4.pc文件)
cmake -D CMAKE_BUILD_TYPE=Release -D OPENCV_GENERATE_PKGCONFIG=YES ..
然后使用make进行编译
make -j8
经过一段时间等待编译后,我们就可以对编译好的东西进行安装了,直接在当前目录下进行install
sudo make install
接下来就到node项目中安装这个npm包了(@u4/opencv4nodejs),当然大家会发现安装不上去,比如终端提示这样的信息,虽然官方有提醒我们在pagekage.json中配置
"opencv4nodejs": {
"disableAutoBuild": 1,
"opencvIncludeDir": "/usr/local/include/opencv4",
"opencvLibDir": "/usr/local/lib",
"opencvBinDir": "/usr/local/bin",
"autoBuildOpencvVersion": "4.10.0"
}
但并不影响效果,所以依旧在执行下一步的时候炸了。
npm i @u4/opencv4nodejs
然后boom!!!
npm ERR! gyp: Call to 'node ./bin/install.js OPENCV4NODEJS_DEFINES' returned exit status 1 while in binding.gyp. while trying to load binding.gyp
npm ERR! gyp ERR! configure error
npm ERR! gyp ERR! stack Error: gyp failed with exit code: 1
npm ERR! gyp ERR! stack at ChildProcess.<anonymous> (/media/Mashiro/inst_47/Project/WebstormProjects/openCV/node_modules/node-gyp/lib/configure.js:317:18)
npm ERR! gyp ERR! stack at ChildProcess.emit (node:events:519:28)
npm ERR! gyp ERR! stack at ChildProcess._handle.onexit (node:internal/child_process:294:12)
npm ERR! gyp ERR! System Linux 6.1.32-amd64-desktop-hwe
npm ERR! gyp ERR! command "/home/Mashiro/.config/nvm/versions/node/v21.7.3/bin/node" "/media/Mashiro/inst_47/Project/WebstormProjects/openCV/node_modules/.bin/node-gyp" "rebuild"
npm ERR! gyp ERR! cwd /media/Mashiro/inst_47/Project/WebstormProjects/openCV/node_modules/@u4/opencv4nodejs
npm ERR! gyp ERR! node -v v21.7.3
npm ERR! gyp ERR! node-gyp -v v10.2.0
npm ERR! gyp ERR! not ok
我也检查了一下,但是貌似和node的版本关系不大,毕竟选择的库要求node>=12,和gyp的版本也没关系。
最后我发现需要安装opencv-build之后
npm i @u4/opencv-build
并执行
npm link
build-opencv --incDir /usr/include/opencv4/ --libDir /lib/x86_64-linux-gnu/ --binDir=/usr/bin/ --nobuild rebuild
才能够正确的引用并安装上opencv4nodejs(苦笑.jpg)