justadudewhohacks / opencv4nodejs

Nodejs bindings to OpenCV 3 and OpenCV 4
MIT License
4.95k stars 826 forks source link

opencv4nodejs in MacOS catalina #775

Open karanvirkour22 opened 3 years ago

karanvirkour22 commented 3 years ago

Error Message

opencv4nodejs cannot be found in Appium-doctor

Manual Build - Environment Information

export OPENCV_DIR=/usr/local/Cellar/opencv/4.5.0_3 export OPENCV_INCLUDE_DIR=/usr/local/Cellar/opencv/4.5.0_3/include export OPENCV_LIB_DIR=/usr/local/Cellar/opencv/4.5.0_3/lib export OPENCV_BIN_DIR=/usr/local/Cellar/opencv/4.5.0._3/bin export OPENCV4NODEJS_DISABLE_AUTOBUILD=1

OpenCV version (example 3.4.1): ?.?.?4.5.0_3

@justadudewhohacks please if you can suggest what I am doing wrong

jlipps commented 3 years ago

did you run npm i -g opencv4nodejs at that point? did it install successfully?

aalhaimi commented 3 years ago

@karanvirkour22, try doing brew update and follow the steps mentioned here. Unlink tesseract, then you should be able to do npm i opencv4node

Pavithra24x7 commented 3 years ago

I'm facing same issue, not able to install opencv4nodejs. Tried multiple solutions. 1.Cleared npm cache & uninstalled node 2.unlinked tesseract 3.deleted package_lock.json But nothing works for me. Below is the error I am getting while hitting "npm install -g opencv4nodejs" command.

                            ^~~~

fatal error: too many errors emitted, stopping now [-ferror-limit=] 20 errors generated. make: *** [Release/obj.target/opencv4nodejs/cc/tracking/MultiTracker.o] Error 1 gyp ERR! build error gyp ERR! stack Error: make failed with exit code: 2 gyp ERR! stack at ChildProcess.onExit (/usr/local/Cellar/node@12/12.20.1/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:194:23) gyp ERR! stack at ChildProcess.emit (events.js:314:20) gyp ERR! stack at Process.ChildProcess._handle.onexit (internal/child_process.js:276:12) gyp ERR! System Darwin 19.2.0 gyp ERR! command "/usr/local/Cellar/node@12/12.20.1/bin/node" "/usr/local/Cellar/node@12/12.20.1/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild" "--jobs" "max" gyp ERR! cwd /usr/local/lib/node_modules/opencv4nodejs gyp ERR! node -v v12.20.1 gyp ERR! node-gyp -v v5.1.0 gyp ERR! not ok

onedotover commented 3 years ago

I'm hitting the same issue. In addition to the steps @Pavithra24x7 tried, I also tried:

  1. Upgrading node and node-gyp
  2. Installing with auto build enabled
  3. Installing with auto build disabled with opencv@4 and opencv@3
  4. Setting environment variables on command line and in package.json

Interestingly we do see the same issue with MultiTracker. I ran the following and got this output:

> cd node_modules/opencv4nodejs/build
> make

  CXX(target) Release/obj.target/opencv4nodejs/cc/tracking/tracking.o
In file included from ../cc/tracking/tracking.cc:6:
../cc/tracking/./Trackers/TrackerBoosting.h:8:14: error: no member named 'TrackerBoosting' in namespace 'cv'
        cv::Ptr<cv::TrackerBoosting> tracker;
                ~~~~^
In file included from ../cc/tracking/tracking.cc:7:
../cc/tracking/./Trackers/TrackerMedianFlow.h:8:14: error: no member named 'TrackerMedianFlow' in namespace 'cv'
        cv::Ptr<cv::TrackerMedianFlow> tracker;
                ~~~~^
In file included from ../cc/tracking/tracking.cc:9:
../cc/tracking/./Trackers/TrackerTLD.h:8:14: error: no member named 'TrackerTLD' in namespace 'cv'
        cv::Ptr<cv::TrackerTLD> tracker;
                ~~~~^
In file included from ../cc/tracking/tracking.cc:13:
../cc/tracking/MultiTracker.h:11:70: error: no member named 'MultiTracker' in namespace 'cv'
class MultiTracker : public FF::ObjectWrap<MultiTracker, cv::Ptr<cv::MultiTracker>> {
                                                                 ~~~~^
../cc/tracking/MultiTracker.h:30:2: error: expected a type
};
 ^
../cc/tracking/MultiTracker.h:30:2: error: expected '{' after base class list
In file included from ../cc/tracking/tracking.cc:21:
../cc/tracking/./Trackers/TrackerMOSSE.h:10:14: error: no member named 'TrackerMOSSE' in namespace 'cv'
        cv::Ptr<cv::TrackerMOSSE> tracker;
                ~~~~^
../cc/tracking/tracking.cc:35:16: error: no type named 'Init' in 'MultiTracker'
        MultiTracker::Init(target);
        ~~~~~~~~~~~~~~^
8 errors generated.
make: *** [Release/obj.target/opencv4nodejs/cc/tracking/tracking.o] Error 1

Environment: MacOS 11.2 System Darwin 20.3.0 node v15.7.0 (also tried on 12) node-gyp v7.1.2 npm v6.14.11

gcc --version Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1 Apple clang version 12.0.0 (clang-1200.0.32.29) Target: x86_64-apple-darwin20.3.0 Thread model: posix InstalledDir: /Library/Developer/CommandLineTools/usr/bin

KeitelDOG commented 3 years ago

Any update? I'm desperate, it's been 2 full days.

Harley-xk commented 3 years ago

same issue for weeks

anselanza commented 3 years ago

I get no errors about tesseract (I run brew unlink tesseract before installing/building opencv4nodejs for what it's worth) - but instead similar errors apparently relating to ceres (or is it multiview?):

In file included from /Users/stephen/repos/nike-move2-zero/nike-qrcode-reader/node_modules/opencv-build/opencv/opencv_contrib/modules/sfm/src/libmv_light/libmv/multiview/fundamental.cc:24:
In file included from /usr/local/include/ceres/ceres.h:37:
In file included from /usr/local/include/ceres/autodiff_cost_function.h:130:
In file included from /usr/local/include/ceres/internal/autodiff.h:148:
In file included from /usr/local/include/ceres/internal/array_selector.h:38:
/usr/local/include/ceres/internal/fixed_array.h:47:1: error: unknown type name 'constexpr'
constexpr static auto kFixedArrayUseDefault = static_cast<size_t>(-1);
^
/usr/local/include/ceres/internal/fixed_array.h:47:18: error: cannot combine with previous 'static' declaration specifier
constexpr static auto kFixedArrayUseDefault = static_cast<size_t>(-1);
                 ^
/usr/local/include/ceres/internal/fixed_array.h:59:57: error: a space is required between consecutive right angle brackets
      (use '> >')
                              Eigen::aligned_allocator<T>>::type;
                                                        ^
/usr/local/include/ceres/internal/fixed_array.h:86:52: error: a space is required between consecutive right angle brackets
      (use '> >')
          typename A = FixedArrayDefaultAllocator<T>>
                                                   ^
/usr/local/include/ceres/internal/fixed_array.h:91:10: error: unknown type name 'constexpr'
  static constexpr size_t kInlineBytesDefault = 256;
KeitelDOG commented 3 years ago

Macbook Pro mid-2012, Catalina 10.15.7 , OpenCV 4.5.0_5 with Brew. @Harley-xk , @anselanza I solved mine, it was not Appium-doctor though. But many other errors from either XCode, opencv4nodejs and specially node-gyp which was the most confused step. Here is the crucial steps that causes trouble and how to pass them:

Install XCode

Be sure to have XCode installed, a decent version, my XCode is 12.3 . Can install it with Terminal sudo xcode-select --install, or with XCode.app in App Store. Both will be under different directory, but both will do it. You can switch if you have both, but 1 is OK:

sudo xcode-select --switch /Library/Developer/CommandLineTools
sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer
xcode-select --print-path

Install Node-Gyp

Then you need to install package node-gyp. I installed it globally with npm install -g node-gyp

Unlink Tesseract

I unlink tesseract with brew unlink tesseract. Not sure if this is really a must.

Install OpenCV4Nodejs

Next step is to install this opencv4nodejs package. But before, to prevent autobuild to run after installation, you have to export variable in terminal 1st to OFF autobuild. Then after the package is installed you'll be able to set it off too in package.json:

export OPENCV4NODEJS_DISABLE_AUTOBUILD=1

I added it in my .bash_profile too just in case I'm redoing the process more times. Then in package.json add config so that the module codes load them in process.env variables (same as EXPORT ... in terminal), it depends on the directory you put your compiled OpenCV:

"opencv4nodejs": {
    "disableAutoBuild": 1,
    "opencvIncludeDir": "/usr/local/Cellar/opencv/4.5.0_5/include",
    "opencvLibDir": "/usr/local/Cellar/opencv/4.5.0_5/lib",
    "opencvBinDir": "/usr/local/Cellar/opencv/4.5.0_5/bin"
  }

Then install it:

npm install opencv4nodejs

At a certain time, it will show ... : Completed at the end, BUT WAIT till console display installation info and switch back to command input mode :

added 5 packages, and audited 658 packages in 5m
MyName-MacBook-Pro:project MyName$

You'll now see it appears in your package.json : "opencv4nodejs": "^5.6.0",.

Build OpenCV4NodeJS module with Node-Gyp

Now Last Step, NODE-GYP. READ CAREFULLY (take a rest if necessary, because at this point, my brain was exhausted and didn't catch half of it).

Understand Node-Gyp config binding.gyp

NODE-GYP DOES NOT CARE ABOUT OPENCV4NODEJS node-gyp is just a package to build any native add-on package for nodejs, but it's not tied with opencv4nodejs, therefore their example is universal and lack of details to use it with package.

Process Order for Node Gyp to build (BUT PLEASE DON'T DO IT):

  1. Create binding.gyp in root Project and add config details
    {
    "targets": [
    {
      "target_name": "binding",
      "sources": [ "src/binding.cc" ]
    }
    ]
    }
  2. run node-gyp configure. This will use the binding.gyp to generate a build/ directory with some files.
  3. run node-gyp build. This will build everything with a release directory with a .node file build/Release/....node

    If you do this for this package, then you'll get the error :

    make: *** No rule to make target Release/obj.target/binding/src/binding.o', needed by Release/binding.node'. Stop

    Then you've use their universal template to make it work. But Binding Gyp is used, not for internal config, but for Module Packages. So instead of keywords binding, it might be better to include the real module path like:

    {
    "targets": [
    {
      "target_name": "opencv4nodejs",
      "sources": [ "node_modules/cc/opencv4nodejs.cc" ]
    }
    ]
    }

    This would fully work if it was your own directory in your project with a single or couple of files to compile. OpenCV4NodeJS has a lot of them.

Proper way of building OpenCV4NodeJS

SOLUTION: You don't need to configure binding.gyp yourself OpenCV4NodeJS has its own node_modules/opencv4nodejs/binding.gyp. There is already thebuild and Release directories with files to used with the node-gyp build in node_modules/opencv4nodejs/build/Release/obj.target.

Go to module Directory:

cd node_modules/opencv4nodejs
node-gyp build
OR
node install/install.js

SO you don't have to use : node-gyp configure, but just node-gyp build.

You'll see a lot of Warnings printed in the console as it goes, but don't worry. If the Process succeed, then the built node file will be created:

node_modules/opencv4nodejs/build/Release/opencv4nodejs.node

OpenCV Contrib other modules: SIFT and SURF

You might also get Errors like I did on : SIFT and/or SURF not found. Those are algorithms used in OpenCV for xfeatures2d. But they have been moved to OpenCV Contrib modules https://github.com/opencv/opencv_contrib, and you need to compile your OpenCV along with the Contrib modules to have those 2 features. I don't know if I can skip them in opencv4nodejs modules with config options in package.json.

My brew install opencv@4 didn't seem to have added the contrib, although they say that you don't need to add brew install opencv@4 --with-contribs because it already add the contrib modules.

I saw is other Algorithms like FREAK and CUBA (maybe) that can be used as replacement of SIFT and SURF in OpenCV 4.5, but this package opencv4nodejs does not yet support them. Maybe a PR would be accepted for them.

And now I'm fighting with the Compilation of both opencv and opencv_contrib. The struggle continues 😁

Harley-xk commented 3 years ago

@KeitelDOG Thanks for your answer:) I'v struggled weeks for this library. There are too many complex issues and fewer documents. I've just gave up it and try to make a bridge between node and python 🙃

KeitelDOG commented 3 years ago

@Harley-xk My struggle is done. I'm just compiling OpenCV with Contrib right now, 50% progression. I use python 3.9.1 installed with Brew, along with pip3 that came with it. And then I use virtualenv and virtualenvwrapper to create a new environment for opencv 4.5 with python 3.9 , use the env and installed numpy with pip. And BOOM, compilation is done. I use this tutorial from Adrian, a specialist in this Machine Learning codes with OpenCV: https://www.pyimagesearch.com/2018/08/17/install-opencv-4-on-macos/

Anyway good luck with it. I was thinking of doing that too if I couldn't get this package to work. Like setup an API with python, and then make NodeJS call API endpoints for treating images and get the responses back. Like 2 API server in the same machine, 1 consuming the other.

KeitelDOG commented 3 years ago

I just got everything to work now. I use OpenCV 4.5.0 with Contrib which I compiled manually with cmake, python an make.

I dropped SIFT, SURF is still there and faster. Here's how I remove SIFT from node-gyp: https://github.com/justadudewhohacks/opencv4nodejs/issues/805#issuecomment-806314329

Screen Shot 2021-03-24 at 10 04 28 PM node_modules/opencv4nodejs/build/Release/opencv4nodejs.node built file.

KeitelDOG commented 3 years ago

@Harley-xk , my main goal was to implement this mini mobile document scanner: https://www.pyimagesearch.com/2014/09/01/build-kick-ass-mobile-document-scanner-just-5-minutes/ that takes an image with a rectangular document (might be in perspective view), detects the quadrilateral contours and stretches, warps it to become a rectangular bird eyed view.

Despite having opencv4nodejs to work, I couldn't understand yet how to convert it into JS, cause this package changes some opencv methods from their places, to be more Object Oriented. So I went and call node child_process instead and call a python script, waiting for response in base64 encoding. But Hey! a simple 1500 pixels height document took like 7 to 15 seconds. It was too much. Then I realized that it must be that when node is calling python script, a whole process has to be done to prepare the execution, and communicating back.

I just took a little time to figure out how to do the Transform part, like Ordering 4 Points (tricky but simple) and Perspective and Warp Transform. I did it 100% in Node JS now. Opening a file the results in my Macbook Pro mid-2012. are:

Input: image 3072 × 3851 pixels (phone camera), 1.1mb (converted to 1500 pixels height

Output: image 1197x1500 pixels

Elapsed Time: 169ms to 290ms

This is very fast now, and can be done many times in a second to feel like a good Mobile Scanner on a WebSocket.

So with you JS and Python bridge, like Node and Flask or Node and Django, if you can, keep me updated if possible about the performance on your image manipulation.

Harley-xk commented 3 years ago

@KeitelDOG , I've already done the python bridge by child_process. My task is to scan fillmarks and clip answer areas from an exam sheet, the image size is just the same as yours. It tooks about 600 to 700 ms, this is acceptable to me. Why it costs you 7 - 15 seconds?

KeitelDOG commented 3 years ago

@Harley-xk great, it's good that you found a solution that fits your case. I really don't understand why child process spawn is taking all this time on my Macbook. Probably running it was like a full npm start that load the codes first and then execute them when ready, and the loading would take the.

One thing I know is that when running it directly from python terminal, it's like a couple of seconds only. But in node, it's another story.

seba-murillo commented 3 years ago

I'm hitting the same issue. In addition to the steps @Pavithra24x7 tried, I also tried:

  1. Upgrading node and node-gyp
  2. Installing with auto build enabled
  3. Installing with auto build disabled with opencv@4 and opencv@3
  4. Setting environment variables on command line and in package.json

Interestingly we do see the same issue with MultiTracker. I ran the following and got this output:

> cd node_modules/opencv4nodejs/build
> make

  CXX(target) Release/obj.target/opencv4nodejs/cc/tracking/tracking.o
In file included from ../cc/tracking/tracking.cc:6:
../cc/tracking/./Trackers/TrackerBoosting.h:8:14: error: no member named 'TrackerBoosting' in namespace 'cv'
        cv::Ptr<cv::TrackerBoosting> tracker;
                ~~~~^
In file included from ../cc/tracking/tracking.cc:7:
../cc/tracking/./Trackers/TrackerMedianFlow.h:8:14: error: no member named 'TrackerMedianFlow' in namespace 'cv'
        cv::Ptr<cv::TrackerMedianFlow> tracker;
                ~~~~^
In file included from ../cc/tracking/tracking.cc:9:
../cc/tracking/./Trackers/TrackerTLD.h:8:14: error: no member named 'TrackerTLD' in namespace 'cv'
        cv::Ptr<cv::TrackerTLD> tracker;
                ~~~~^
In file included from ../cc/tracking/tracking.cc:13:
../cc/tracking/MultiTracker.h:11:70: error: no member named 'MultiTracker' in namespace 'cv'
class MultiTracker : public FF::ObjectWrap<MultiTracker, cv::Ptr<cv::MultiTracker>> {
                                                                 ~~~~^
../cc/tracking/MultiTracker.h:30:2: error: expected a type
};
 ^
../cc/tracking/MultiTracker.h:30:2: error: expected '{' after base class list
In file included from ../cc/tracking/tracking.cc:21:
../cc/tracking/./Trackers/TrackerMOSSE.h:10:14: error: no member named 'TrackerMOSSE' in namespace 'cv'
        cv::Ptr<cv::TrackerMOSSE> tracker;
                ~~~~^
../cc/tracking/tracking.cc:35:16: error: no type named 'Init' in 'MultiTracker'
        MultiTracker::Init(target);
        ~~~~~~~~~~~~~~^
8 errors generated.
make: *** [Release/obj.target/opencv4nodejs/cc/tracking/tracking.o] Error 1

Environment: MacOS 11.2 System Darwin 20.3.0 node v15.7.0 (also tried on 12) node-gyp v7.1.2 npm v6.14.11

gcc --version Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1 Apple clang version 12.0.0 (clang-1200.0.32.29) Target: x86_64-apple-darwin20.3.0 Thread model: posix InstalledDir: /Library/Developer/CommandLineTools/usr/bin

For the past week I've trying to get opencv4nodejs for appium image recognition, and finally managed to get it to work. Hoping it helps someone with the same issue, here's what I did:

NOTE: all steps might not be needed, use accordingly

1. uninstall opencv-build, opencv4nodejs and opencv:

also remember to unset OPENCV4NODEJS_DISABLE_AUTOBUILD

brew uninstall opecv
npm uninstall -g opencv-build
npm uninstall -g opencv4nodejs

2. install node v15.11. and npm v6.14.11

I recommend using n for changing node versions easily

npm i -g n
n 15.11.0
n (select version)
npm i -g npm@6.14.11

3. set env variable to select opencv modules to install WITH autobuild

for a list of modules check opencv.org in my case:

export OPENCV4NODEJS_AUTOBUILD_FLAGS=-DBUILD_LIST=core,imgproc,imgcodecs,videoio,highgui,video,calib3d,features2d,objdetect,dnn,ml,flann,photo,stitching,gapi

4. install

npm i -g opencv-build
npm i -g opencv4nodejs
ChuangYuMing commented 3 years ago

I'm hitting the same issue. In addition to the steps @Pavithra24x7 tried, I also tried:

  1. Upgrading node and node-gyp
  2. Installing with auto build enabled
  3. Installing with auto build disabled with opencv@4 and opencv@3
  4. Setting environment variables on command line and in package.json

Interestingly we do see the same issue with MultiTracker. I ran the following and got this output:

> cd node_modules/opencv4nodejs/build
> make

  CXX(target) Release/obj.target/opencv4nodejs/cc/tracking/tracking.o
In file included from ../cc/tracking/tracking.cc:6:
../cc/tracking/./Trackers/TrackerBoosting.h:8:14: error: no member named 'TrackerBoosting' in namespace 'cv'
        cv::Ptr<cv::TrackerBoosting> tracker;
                ~~~~^
In file included from ../cc/tracking/tracking.cc:7:
../cc/tracking/./Trackers/TrackerMedianFlow.h:8:14: error: no member named 'TrackerMedianFlow' in namespace 'cv'
        cv::Ptr<cv::TrackerMedianFlow> tracker;
                ~~~~^
In file included from ../cc/tracking/tracking.cc:9:
../cc/tracking/./Trackers/TrackerTLD.h:8:14: error: no member named 'TrackerTLD' in namespace 'cv'
        cv::Ptr<cv::TrackerTLD> tracker;
                ~~~~^
In file included from ../cc/tracking/tracking.cc:13:
../cc/tracking/MultiTracker.h:11:70: error: no member named 'MultiTracker' in namespace 'cv'
class MultiTracker : public FF::ObjectWrap<MultiTracker, cv::Ptr<cv::MultiTracker>> {
                                                                 ~~~~^
../cc/tracking/MultiTracker.h:30:2: error: expected a type
};
 ^
../cc/tracking/MultiTracker.h:30:2: error: expected '{' after base class list
In file included from ../cc/tracking/tracking.cc:21:
../cc/tracking/./Trackers/TrackerMOSSE.h:10:14: error: no member named 'TrackerMOSSE' in namespace 'cv'
        cv::Ptr<cv::TrackerMOSSE> tracker;
                ~~~~^
../cc/tracking/tracking.cc:35:16: error: no type named 'Init' in 'MultiTracker'
        MultiTracker::Init(target);
        ~~~~~~~~~~~~~~^
8 errors generated.
make: *** [Release/obj.target/opencv4nodejs/cc/tracking/tracking.o] Error 1

Environment: MacOS 11.2 System Darwin 20.3.0 node v15.7.0 (also tried on 12) node-gyp v7.1.2 npm v6.14.11 gcc --version Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1 Apple clang version 12.0.0 (clang-1200.0.32.29) Target: x86_64-apple-darwin20.3.0 Thread model: posix InstalledDir: /Library/Developer/CommandLineTools/usr/bin

For the past week I've trying to get opencv4nodejs for appium image recognition, and finally managed to get it to work. Hoping it helps someone with the same issue, here's what I did:

NOTE: all steps might not be needed, use accordingly

1. uninstall opencv-build, opencv4nodejs and opencv:

also remember to unset OPENCV4NODEJS_DISABLE_AUTOBUILD

brew uninstall opecv
npm uninstall -g opencv-build
npm uninstall -g opencv4nodejs

2. install node v15.11. and npm v6.14.11

I recommend using n for changing node versions easily

npm i -g n
n 15.11.0
n (select version)
npm i -g npm@6.14.11

3. set env variable to select opencv modules to install WITH autobuild

for a list of modules check opencv.org in my case:

export OPENCV4NODEJS_AUTOBUILD_FLAGS=-DBUILD_LIST=core,imgproc,imgcodecs,videoio,highgui,video,calib3d,features2d,objdetect,dnn,ml,flann,photo,stitching,gapi

4. install

npm i -g opencv-build
npm i -g opencv4nodejs

you are my god!

UrielCh commented 3 years ago

tested with nodejs 14 and it works:

npm version 6.14.13
node version v14.17.3
cmake version 3.21.0
brew uninstall opencv
npm uninstall -g opencv-build opencv4nodejs
npm uninstall opencv-build opencv4nodejs

in my case I install all modules locally:

npm i opencv-build
npm i opencv4nodejs
davebebo commented 2 years ago

Ok so I was able to get the full installation of opencv4nodejs working. I needed to downgrade node from version 16 to version 14 and do a little bit of compiler option digging!

Current Setup macOS Big Sur v11.6.1 Homebrew 3.3.2 opencv 4.5.3_2 node v14.18.1

Node installation (with Homebrew) brew install node@14

opencv installation (with Homebrew) brew install opencv

opencv4nodejs installtion (with node installer)

Georgin01 commented 2 years ago

@KeitelDOG

Macbook Pro mid-2012, Catalina 10.15.7 , OpenCV 4.5.0_5 with Brew. @Harley-xk , @anselanza I solved mine, it was not Appium-doctor though. But many other errors from either XCode, opencv4nodejs and specially node-gyp which was the most confused step. Here is the crucial steps that causes trouble and how to pass them:

Install XCode

Be sure to have XCode installed, a decent version, my XCode is 12.3 . Can install it with Terminal sudo xcode-select --install, or with XCode.app in App Store. Both will be under different directory, but both will do it. You can switch if you have both, but 1 is OK:

sudo xcode-select --switch /Library/Developer/CommandLineTools
sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer
xcode-select --print-path

Install Node-Gyp

Then you need to install package node-gyp. I installed it globally with npm install -g node-gyp

Unlink Tesseract

I unlink tesseract with brew unlink tesseract. Not sure if this is really a must.

Install OpenCV4Nodejs

Next step is to install this opencv4nodejs package. But before, to prevent autobuild to run after installation, you have to export variable in terminal 1st to OFF autobuild. Then after the package is installed you'll be able to set it off too in package.json:

export OPENCV4NODEJS_DISABLE_AUTOBUILD=1

I added it in my .bash_profile too just in case I'm redoing the process more times. Then in package.json add config so that the module codes load them in process.env variables (same as EXPORT ... in terminal), it depends on the directory you put your compiled OpenCV:

"opencv4nodejs": {
    "disableAutoBuild": 1,
    "opencvIncludeDir": "/usr/local/Cellar/opencv/4.5.0_5/include",
    "opencvLibDir": "/usr/local/Cellar/opencv/4.5.0_5/lib",
    "opencvBinDir": "/usr/local/Cellar/opencv/4.5.0_5/bin"
  }

Then install it:

npm install opencv4nodejs

At a certain time, it will show ... : Completed at the end, BUT WAIT till console display installation info and switch back to command input mode :

added 5 packages, and audited 658 packages in 5m
MyName-MacBook-Pro:project MyName$

You'll now see it appears in your package.json : "opencv4nodejs": "^5.6.0",.

Build OpenCV4NodeJS module with Node-Gyp

Now Last Step, NODE-GYP. READ CAREFULLY (take a rest if necessary, because at this point, my brain was exhausted and didn't catch half of it).

Understand Node-Gyp config binding.gyp

NODE-GYP DOES NOT CARE ABOUT OPENCV4NODEJS node-gyp is just a package to build any native add-on package for nodejs, but it's not tied with opencv4nodejs, therefore their example is universal and lack of details to use it with package.

Process Order for Node Gyp to build (BUT PLEASE DON'T DO IT):

  1. Create binding.gyp in root Project and add config details
{
  "targets": [
    {
      "target_name": "binding",
      "sources": [ "src/binding.cc" ]
    }
  ]
}
  1. run node-gyp configure. This will use the binding.gyp to generate a build/ directory with some files.
  2. run node-gyp build. This will build everything with a release directory with a .node file build/Release/....node

If you do this for this package, then you'll get the error :

make: *** No rule to make target Release/obj.target/binding/src/binding.o', needed by Release/binding.node'. Stop

Then you've use their universal template to make it work. But Binding Gyp is used, not for internal config, but for Module Packages. So instead of keywords binding, it might be better to include the real module path like:

{
  "targets": [
    {
      "target_name": "opencv4nodejs",
      "sources": [ "node_modules/cc/opencv4nodejs.cc" ]
    }
  ]
}

This would fully work if it was your own directory in your project with a single or couple of files to compile. OpenCV4NodeJS has a lot of them.

Proper way of building OpenCV4NodeJS

SOLUTION: You don't need to configure binding.gyp yourself OpenCV4NodeJS has its own node_modules/opencv4nodejs/binding.gyp. There is already thebuild and Release directories with files to used with the node-gyp build in node_modules/opencv4nodejs/build/Release/obj.target.

Go to module Directory:

cd node_modules/opencv4nodejs
node-gyp build
OR
node install/install.js

SO you don't have to use : node-gyp configure, but just node-gyp build.

You'll see a lot of Warnings printed in the console as it goes, but don't worry. If the Process succeed, then the built node file will be created:

node_modules/opencv4nodejs/build/Release/opencv4nodejs.node

OpenCV Contrib other modules: SIFT and SURF

You might also get Errors like I did on : SIFT and/or SURF not found. Those are algorithms used in OpenCV for xfeatures2d. But they have been moved to OpenCV Contrib modules https://github.com/opencv/opencv_contrib, and you need to compile your OpenCV along with the Contrib modules to have those 2 features. I don't know if I can skip them in opencv4nodejs modules with config options in package.json.

My brew install opencv@4 didn't seem to have added the contrib, although they say that you don't need to add brew install opencv@4 --with-contribs because it already add the contrib modules.

I saw is other Algorithms like FREAK and CUBA (maybe) that can be used as replacement of SIFT and SURF in OpenCV 4.5, but this package opencv4nodejs does not yet support them. Maybe a PR would be accepted for them.

And now I'm fighting with the Compilation of both opencv and opencv_contrib. The struggle continues 😁

That`s great. But have a problem on build opencv4nodejs.


CXX(target) Release/obj.target/opencv4nodejs/cc/tracking/tracking.o
In file included from ../cc/tracking/tracking.cc:6:
../cc/tracking/./Trackers/TrackerBoosting.h:8:14: error: no member named 'TrackerBoosting' in namespace 'cv'
        cv::Ptr<cv::TrackerBoosting> tracker;
                ~~~~^
In file included from ../cc/tracking/tracking.cc:7:
../cc/tracking/./Trackers/TrackerMedianFlow.h:8:14: error: no member named 'TrackerMedianFlow' in namespace 'cv'
        cv::Ptr<cv::TrackerMedianFlow> tracker;
                ~~~~^
In file included from ../cc/tracking/tracking.cc:9:
../cc/tracking/./Trackers/TrackerTLD.h:8:14: error: no member named 'TrackerTLD' in namespace 'cv'
        cv::Ptr<cv::TrackerTLD> tracker;
                ~~~~^
In file included from ../cc/tracking/tracking.cc:13:
../cc/tracking/MultiTracker.h:11:70: error: no member named 'MultiTracker' in namespace 'cv'
class MultiTracker : public FF::ObjectWrap<MultiTracker, cv::Ptr<cv::MultiTracker>> {
                                                                 ~~~~^
In file included from ../cc/tracking/tracking.cc:21:
../cc/tracking/./Trackers/TrackerMOSSE.h:10:14: error: no member named 'TrackerMOSSE' in namespace 'cv'
        cv::Ptr<cv::TrackerMOSSE> tracker;
                ~~~~^
5 errors generated.
make: *** [Release/obj.target/opencv4nodejs/cc/tracking/tracking.o] Error 1

Any ideas on how to fix this?

KeitelDOG commented 2 years ago

@Georgin01 if you have the Tracker error it's because you're using a higher version of OpenCV like 4.5.1 or higher which have Tracker codes moved in another structure. The last version of opencv4nodejs was out before SIFT, SURF and Tracker modification in OpenCV, which produced those probems. With 4.5.0 the Tracker is in the correct place, see this: https://github.com/justadudewhohacks/opencv4nodejs/issues/805

rrudiuk-devx commented 2 years ago

When I try to use the solution provided by @seba-murillo , during opencv4nodejs installation (after using the command npm i -g opencv4nodejs) I get this error:

Error: no OpenCV libraries found in lib dir: /usr/local/lib

As I understand, this happens because OpenCV was deleted here: brew uninstall opencv and never installed.

What am I missing? Can anyone please help?

zcmgyu commented 2 years ago

@rrudiuk-devx Do you have opencv in /usr/local/lib folder? If not, please install it.

rrudiuk-devx commented 2 years ago

@zcmgyu what installation process should I follow? As I understand, I shouldn't use installation via brew. So I need to just download opencv 4.5.0 and install it using cmake?

Georgin01 commented 2 years ago

@Georgin01 if you have the Tracker error it's because you're using a higher version of OpenCV like 4.5.1 or higher which have Tracker codes moved in another structure. The last version of opencv4nodejs was out before SIFT, SURF and Tracker modification in OpenCV, which produced those probems. With 4.5.0 the Tracker is in the correct place, see this: #805

Yes, I moved on. Specified paths in the package.json like this:

"opencv4nodejs": {
    "disableAutoBuild": 1,
    "opencvIncludeDir": "/usr/local/Cellar/opencv-4.5.0/build/include",
    "opencvLibDir": "/usr/local/Cellar/opencv-4.5.0/build/lib",
    "opencvBinDir": "/usr/local/Cellar/opencv-4.5.0/build/bin"
  },

Everything like in your comment here But now i have another errors, a lot of errors, on node install/install.js with native-node-utils, Xcode/C++, Trackers and others. Xcode 13.1 Build version 13A1030d node-gyp -v v8.4.1 opencv 4.5.0 node -v v14.18.2

Did you get along with this?

chetanJTH commented 2 years ago

I'm hitting the same issue. In addition to the steps @Pavithra24x7 tried, I also tried:

  1. Upgrading node and node-gyp
  2. Installing with auto build enabled
  3. Installing with auto build disabled with opencv@4 and opencv@3
  4. Setting environment variables on command line and in package.json

Interestingly we do see the same issue with MultiTracker. I ran the following and got this output:

> cd node_modules/opencv4nodejs/build
> make

  CXX(target) Release/obj.target/opencv4nodejs/cc/tracking/tracking.o
In file included from ../cc/tracking/tracking.cc:6:
../cc/tracking/./Trackers/TrackerBoosting.h:8:14: error: no member named 'TrackerBoosting' in namespace 'cv'
        cv::Ptr<cv::TrackerBoosting> tracker;
                ~~~~^
In file included from ../cc/tracking/tracking.cc:7:
../cc/tracking/./Trackers/TrackerMedianFlow.h:8:14: error: no member named 'TrackerMedianFlow' in namespace 'cv'
        cv::Ptr<cv::TrackerMedianFlow> tracker;
                ~~~~^
In file included from ../cc/tracking/tracking.cc:9:
../cc/tracking/./Trackers/TrackerTLD.h:8:14: error: no member named 'TrackerTLD' in namespace 'cv'
        cv::Ptr<cv::TrackerTLD> tracker;
                ~~~~^
In file included from ../cc/tracking/tracking.cc:13:
../cc/tracking/MultiTracker.h:11:70: error: no member named 'MultiTracker' in namespace 'cv'
class MultiTracker : public FF::ObjectWrap<MultiTracker, cv::Ptr<cv::MultiTracker>> {
                                                                 ~~~~^
../cc/tracking/MultiTracker.h:30:2: error: expected a type
};
 ^
../cc/tracking/MultiTracker.h:30:2: error: expected '{' after base class list
In file included from ../cc/tracking/tracking.cc:21:
../cc/tracking/./Trackers/TrackerMOSSE.h:10:14: error: no member named 'TrackerMOSSE' in namespace 'cv'
        cv::Ptr<cv::TrackerMOSSE> tracker;
                ~~~~^
../cc/tracking/tracking.cc:35:16: error: no type named 'Init' in 'MultiTracker'
        MultiTracker::Init(target);
        ~~~~~~~~~~~~~~^
8 errors generated.
make: *** [Release/obj.target/opencv4nodejs/cc/tracking/tracking.o] Error 1

Environment: MacOS 11.2 System Darwin 20.3.0 node v15.7.0 (also tried on 12) node-gyp v7.1.2 npm v6.14.11 gcc --version Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1 Apple clang version 12.0.0 (clang-1200.0.32.29) Target: x86_64-apple-darwin20.3.0 Thread model: posix InstalledDir: /Library/Developer/CommandLineTools/usr/bin

For the past week I've trying to get opencv4nodejs for appium image recognition, and finally managed to get it to work. Hoping it helps someone with the same issue, here's what I did:

NOTE: all steps might not be needed, use accordingly

1. uninstall opencv-build, opencv4nodejs and opencv:

also remember to unset OPENCV4NODEJS_DISABLE_AUTOBUILD

brew uninstall opecv
npm uninstall -g opencv-build
npm uninstall -g opencv4nodejs

2. install node v15.11. and npm v6.14.11

I recommend using n for changing node versions easily

npm i -g n
n 15.11.0
n (select version)
npm i -g npm@6.14.11

3. set env variable to select opencv modules to install WITH autobuild

for a list of modules check opencv.org in my case:

export OPENCV4NODEJS_AUTOBUILD_FLAGS=-DBUILD_LIST=core,imgproc,imgcodecs,videoio,highgui,video,calib3d,features2d,objdetect,dnn,ml,flann,photo,stitching,gapi

4. install

npm i -g opencv-build
npm i -g opencv4nodejs

you are the god of node

ProgrammingLife commented 2 years ago

For Ubuntu/Mint try this:

export OPENCV_LIB_DIR=/usr/lib/x86_64-linux-gnu/
npm i opencv4nodejs --save     

and double check you have opencv libs in /usr/lib/x86_64-linux-gnu/. If there are no libs then install opencv with apt. Cheers.

michaelrrrrrr commented 1 year ago

#

I'm hitting the same issue. In addition to the steps @Pavithra24x7 tried, I also tried:

  1. Upgrading node and node-gyp
  2. Installing with auto build enabled
  3. Installing with auto build disabled with opencv@4 and opencv@3
  4. Setting environment variables on command line and in package.json

Interestingly we do see the same issue with MultiTracker. I ran the following and got this output:

> cd node_modules/opencv4nodejs/build
> make

  CXX(target) Release/obj.target/opencv4nodejs/cc/tracking/tracking.o
In file included from ../cc/tracking/tracking.cc:6:
../cc/tracking/./Trackers/TrackerBoosting.h:8:14: error: no member named 'TrackerBoosting' in namespace 'cv'
        cv::Ptr<cv::TrackerBoosting> tracker;
                ~~~~^
In file included from ../cc/tracking/tracking.cc:7:
../cc/tracking/./Trackers/TrackerMedianFlow.h:8:14: error: no member named 'TrackerMedianFlow' in namespace 'cv'
        cv::Ptr<cv::TrackerMedianFlow> tracker;
                ~~~~^
In file included from ../cc/tracking/tracking.cc:9:
../cc/tracking/./Trackers/TrackerTLD.h:8:14: error: no member named 'TrackerTLD' in namespace 'cv'
        cv::Ptr<cv::TrackerTLD> tracker;
                ~~~~^
In file included from ../cc/tracking/tracking.cc:13:
../cc/tracking/MultiTracker.h:11:70: error: no member named 'MultiTracker' in namespace 'cv'
class MultiTracker : public FF::ObjectWrap<MultiTracker, cv::Ptr<cv::MultiTracker>> {
                                                                 ~~~~^
../cc/tracking/MultiTracker.h:30:2: error: expected a type
};
 ^
../cc/tracking/MultiTracker.h:30:2: error: expected '{' after base class list
In file included from ../cc/tracking/tracking.cc:21:
../cc/tracking/./Trackers/TrackerMOSSE.h:10:14: error: no member named 'TrackerMOSSE' in namespace 'cv'
        cv::Ptr<cv::TrackerMOSSE> tracker;
                ~~~~^
../cc/tracking/tracking.cc:35:16: error: no type named 'Init' in 'MultiTracker'
        MultiTracker::Init(target);
        ~~~~~~~~~~~~~~^
8 errors generated.
make: *** [Release/obj.target/opencv4nodejs/cc/tracking/tracking.o] Error 1

Environment: MacOS 11.2 System Darwin 20.3.0 node v15.7.0 (also tried on 12) node-gyp v7.1.2 npm v6.14.11 gcc --version Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1 Apple clang version 12.0.0 (clang-1200.0.32.29) Target: x86_64-apple-darwin20.3.0 Thread model: posix InstalledDir: /Library/Developer/CommandLineTools/usr/bin

For the past week I've trying to get opencv4nodejs for appium image recognition, and finally managed to get it to work. Hoping it helps someone with the same issue, here's what I did:

NOTE: all steps might not be needed, use accordingly

1. uninstall opencv-build, opencv4nodejs and opencv:

also remember to unset OPENCV4NODEJS_DISABLE_AUTOBUILD

brew uninstall opecv
npm uninstall -g opencv-build
npm uninstall -g opencv4nodejs

2. install node v15.11. and npm v6.14.11

I recommend using n for changing node versions easily

npm i -g n
n 15.11.0
n (select version)
npm i -g npm@6.14.11

3. set env variable to select opencv modules to install WITH autobuild

for a list of modules check opencv.org in my case:

export OPENCV4NODEJS_AUTOBUILD_FLAGS=-DBUILD_LIST=core,imgproc,imgcodecs,videoio,highgui,video,calib3d,features2d,objdetect,dnn,ml,flann,photo,stitching,gapi

4. install

npm i -g opencv-build
npm i -g opencv4nodejs

Following @seba-murillo's guide, I still got an error at the very last step of installing opencv4nodejs. The error log was so long that I could only find few useful ones such as "ERR! Error: Command failed: node-gyp rebuild --jobs max". Then I changed npm and nodejs back to their latest versions, and tried to install open4nodejs again, it just worked! The nodejs version is v18.12.1, and npm is 9.2.0.

dids-reyes commented 3 months ago

export OPENCV4NODEJS_AUTOBUILD_FLAGS=-DBUILD_LIST=core,imgproc,imgcodecs,videoio,highgui,video,calib3d,features2d,objdetect,dnn,ml,flann,photo,stitching,gapi

This got me pass 50% and failed at 91%

Screenshot 2024-07-07 at 2 14 38 PM

did anyone encounter this opencv_dnn error

i downgraded versions

npm i -g npm@6 nvm use v14