开发者问题收集

如何安装 opencv4nodejs 模块

2019-09-07
11476

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

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

> [email protected] 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! [email protected] install: `node ./install.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] 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 的安装过程吗?

2个回答

首先:本机节点模块是通过 node-gyp 构建的,默认情况下它已随 npm 一起提供。但是, node-gyp 要求您安装 python。如果您遇到特定于 node-gyp 的问题,请先查看 node-gyp 的已知问题。

重要说明: node-gyp 无法正确处理空格,因此请确保项目目录的路径不包含任何空格。在“C:\Program Files\some_dir”或类似位置下安装 opencv4nodejs 将不起作用,并将失败:“致命错误 C1083:无法打开包含文件:'opencv2/core.hpp'”!**

在 Windows 上,您还需要 Windows Build Tools 来编译 OpenCV 和 opencv4nodejs。如果您没有安装 Visual Studio 或 Windows Build Tools,您可以轻松安装 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++ ...

另请参阅 文档

或者,您可以提供 工具链文件 ,但在这种情况下这可能有点小题大做。

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

brew update
brew install opencv@4
brew link --force opencv@4
mohammad javad ahmadi
2019-09-07

在尝试了许多其他方法后,这个对我有用:

sudo npm install -g opencv4nodejs --unsafe-perm=true --allow-root

感谢 https://stackoverflow.com/a/64567717/

Ravinda Dissanayake
2023-05-03