如何安装 opencv4nodejs 模块

Posted

技术标签:

【中文标题】如何安装 opencv4nodejs 模块【英文标题】:How to install opencv4nodejs module 【发布时间】:2019-09-07 06:00:14 【问题描述】:

您好,我正在尝试在我的 Node.js 项目中使用 opencv,所以我决定使用 opencv4nodejs。我尝试通过npm 安装此模块,我发现一些与我的计算机上缺少cmake 有关的错误。所以我决定通过brew 安装cmake。但是在这个过程之后,当我尝试安装这个模块时,会出现以下错误:

MacBook-Air-Micha:webcam-myidea michalkukielka$ npm install opencv4nodejs

> opencv-build@0.1.8 install /Users/michalkukielka/Documents/webcam-myidea/node_modules/opencv-build
> node ./install.js

info No package.json in folder. 
info install if you want to use an own OpenCV installation set OPENCV4NODEJS_DISABLE_AUTOBUILD
info readAutoBuildFile file does not exists: /Users/michalkukielka/Documents/webcam-myidea/node_modules/opencv-build/opencv/auto-build.json /Users/michalkukielka/Documents/webcam-myidea/node_modules/opencv-build/opencv/auto-build.json
info install failed to find auto-build.json: /Users/michalkukielka/Documents/webcam-myidea/node_modules/opencv-build/opencv/auto-build.json
info install 
info install running install script...
info install 
info install opencv version: 3.4.6
info install with opencv contrib: yes
info install custom build flags: 
info install 
info install executing: git --version
info install git --version: git version 2.20.1
info install 
info install executing: cmake --version
info install cmake --version: cmake version 3.15.3
info install 
info install CMake suite maintained and supported by Kitware (kitware.com/cmake).
info install 
info install installing opencv version 3.4.6 into directory: /Users/michalkukielka/Documents/webcam-myidea/node_modules/opencv-build/opencv
Cloning into 'opencv_contrib'...
remote: Enumerating objects: 2160, done.
remote: Counting objects: 100% (2160/2160), done.
remote: Compressing objects: 100% (1916/1916), done.
remote: Total 2160 (delta 365), reused 1101 (delta 134), pack-reused 0
Receiving objects: 100% (2160/2160), 50.82 MiB | 1.57 MiB/s, done.
Resolving deltas: 100% (365/365), done.
Note: checking out 'f26c98365da6af93cb5e49397b571190fca7bb55'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b <new-branch-name>

Checking out files: 100% (1780/1780), done.
Cloning into 'opencv'...
remote: Enumerating objects: 7328, done.
remote: Counting objects: 100% (7328/7328), done.
remote: Compressing objects: 100% (6260/6260), done.
remote: Total 7328 (delta 1166), reused 3907 (delta 692), pack-reused 0
Receiving objects: 100% (7328/7328), 81.33 MiB | 1.62 MiB/s, done.
Resolving deltas: 100% (1166/1166), done.
Note: checking out '33b765d7979fd8a6038026aa44f6ff1a9c082b7b'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b <new-branch-name>

Checking out files: 100% (6080/6080), done.
info install running cmake /Users/michalkukielka/Documents/webcam-myidea/node_modules/opencv-build/opencv/opencv,-DCMAKE_INSTALL_PREFIX=/Users/michalkukielka/Documents/webcam-myidea/node_modules/opencv-build/opencv/build,-DCMAKE_BUILD_TYPE=Release,-DBUILD_EXAMPLES=OFF,-DBUILD_DOCS=OFF,-DBUILD_TESTS=OFF,-DBUILD_PERF_TESTS=OFF,-DBUILD_JAVA=OFF,-DCUDA_NVCC_FLAGS=--expt-relaxed-constexpr,-DBUILD_opencv_apps=OFF,-DBUILD_opencv_aruco=OFF,-DBUILD_opencv_bgsegm=OFF,-DBUILD_opencv_bioinspired=OFF,-DBUILD_opencv_ccalib=OFF,-DBUILD_opencv_datasets=OFF,-DBUILD_opencv_dnn_objdetect=OFF,-DBUILD_opencv_dpm=OFF,-DBUILD_opencv_fuzzy=OFF,-DBUILD_opencv_hfs=OFF,-DBUILD_opencv_java_bindings_generator=OFF,-DBUILD_opencv_js=OFF,-DBUILD_opencv_img_hash=OFF,-DBUILD_opencv_line_descriptor=OFF,-DBUILD_opencv_optflow=OFF,-DBUILD_opencv_phase_unwrapping=OFF,-DBUILD_opencv_python3=OFF,-DBUILD_opencv_python_bindings_generator=OFF,-DBUILD_opencv_reg=OFF,-DBUILD_opencv_rgbd=OFF,-DBUILD_opencv_saliency=OFF,-DBUILD_opencv_shape=OFF,-DBUILD_opencv_stereo=OFF,-DBUILD_opencv_stitching=OFF,-DBUILD_opencv_structured_light=OFF,-DBUILD_opencv_superres=OFF,-DBUILD_opencv_surface_matching=OFF,-DBUILD_opencv_ts=OFF,-DBUILD_opencv_xobjdetect=OFF,-DBUILD_opencv_xphoto=OFF,-DWITH_VTK=OFF,-DOPENCV_ENABLE_NONFREE=ON,-DOPENCV_EXTRA_MODULES_PATH=/Users/michalkukielka/Documents/webcam-myidea/node_modules/opencv-build/opencv/opencv_contrib/modules
-- The CXX compiler identification is unknown
-- The C compiler identification is unknown
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - failed
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - failed
CMake Error at CMakeLists.txt:153 (message):
  CMake fails to determine the bitness of the target platform.

    Please check your CMake and compiler installation. If you are cross-compiling then ensure that your CMake toolchain file correctly sets the compiler details.


-- Configuring incomplete, errors occurred!
See also "/Users/michalkukielka/Documents/webcam-myidea/node_modules/opencv-build/opencv/build/CMakeFiles/CMakeOutput.log".
See also "/Users/michalkukielka/Documents/webcam-myidea/node_modules/opencv-build/opencv/build/CMakeFiles/CMakeError.log".
ERR! child process exited with code 1 (for more info, set '--loglevel silly') 

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! opencv-build@0.1.8 install: `node ./install.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the opencv-build@0.1.8 install script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/michalkukielka/.npm/_logs/2019-09-07T05_38_02_479Z-debug.log

据我了解,cmake 存在一些新问题,但我找不到解决方案。

你能帮我完成安装过程吗opencv4nodejs

【问题讨论】:

【参考方案1】:

首先:原生节点模块是通过node-gyp 构建的,npm 默认已经自带。但是,node-gyp 要求您安装 python。如果您遇到 node-gyp 特定问题,请先查看 node-gyp 的已知问题。

重要提示: node-gyp 不会正确处理空格,因此请确保项目目录的路径不包含任何空格。在“C:\Program Files\some_dir”或类似目录下安装 opencv4nodejs 将无法正常工作,并且会失败,并显示:“fatal error C1083: Cannot open include file: 'opencv2/core.hpp'”!**

在 Windows 上,您还需要 Windows 构建工具来编译 OpenCV 和 opencv4nodejs。如果您没有安装 Visual Studio 或 Windows 构建工具,您可以轻松安装 VS2015 构建工具:

npm install --global windows-build-tools

第二点:你应该检查 CMake 和编译器的安装。如果您正在交叉编译,请确保您的 CMake 工具链文件正确设置了编译器详细信息。

CMake 不(总是)听 CC 和 CXX。而是使用 CMAKE_C_COMPILER 和 CMAKE_CXX_COMPILER:

cmake -DCMAKE_C_COMPILER=/usr/bin/gcc -DCMAKE_CXX_COMPILER=/usr/bin/g++ ...

另请参阅documentation。

或者,您可以提供toolchain file,但在这种情况下,这可能有点过头了。

最后:在 OSX 下,我们可以简单地通过 brew 安装 OpenCV:

brew update
brew install opencv@4
brew link --force opencv@4

【讨论】:

好的,我明白了你的意思,但这个提议并没有解决我安装节点的 opencv4nodejs 模块的问题,我必须安装它才能运行收到的一些遗留代码。 ERR! child process exited with code 1 (for more info, set '--loglevel silly') npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! opencv-build@0.1.8 install: `node ./install.js` npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the opencv-build@0.1.8 install script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above. npm ERR! A complete log of this run can be found in: npm ERR! /Users/michalkukielka/.npm/_logs/2019-09-07T10_40_14_132Z-debug.log 查看此链接:***.com/questions/42308879/npm-err-code-elifecycle

以上是关于如何安装 opencv4nodejs 模块的主要内容,如果未能解决你的问题,请参考以下文章

未能在 Mac(Apple M1)上安装 opencv4nodejs 会引发找不到路径错误

npm install --save opencv4nodejs

appium---MacAppium-Doctor提示WARN:“ opencv4nodejs cannot be found”解决方案

无法在 Vue + 电子应用程序中编译 opencv4nodejs

转译《opencv4nodejs人脸识别》

python中如何安装SciPY模块