justadudewhohacks / opencv4nodejs

Nodejs bindings to OpenCV 3 and OpenCV 4
MIT License
4.97k stars 827 forks source link

macOS Big Sur Install ALMOST THERE!!! (but fails because of Tracker / MultiTracker) #805

Open frudman opened 3 years ago

frudman commented 3 years ago

Trying the manual install, as per below (because the easier npm install opencv4nodejs fails in all sorts of ways!)

[march 23 notation: see next comment on this thread for possible culprit (was api changed to a legacy api by opencv.org?)]

[But now this seems important] brew-installed OpenCV4 (version 4.5.1_3)

But first, some info:

ProductName:    macOS
ProductVersion: 11.2.3 (Big Sur) [so, up to date]
BuildVersion:   20D91

uname -a: Darwin xxxxx.local 20.3.0 Darwin Kernel Version 20.3.0: xxxxxxnu-7195.81.3~1/RELEASE_X86_64 x86_64 node -v: v15.12.0 node-gyp -v: v7.1.2 python --version: Python 3.9.2

Added to ~/.zshrc for command line:

export OPENCV4NODEJS_DISABLE_AUTOBUILD=1
export OPENCV4NODEJS_DISABLE_EXTERNAL_MEM_TRACKING=1
export OPENCV_INCLUDE_DIR=/usr/local/Cellar/opencv/4.5.1_3/include/opencv4
export OPENCV_LIB_DIR=/usr/local/Cellar/opencv/4.5.1_3/lib
export OPENCV_BIN_DIR=/usr/local/Cellar/opencv/4.5.1_3/bin
export PATH=/usr/local/Cellar/opencv/4.5.1_3/bin:$PATH
set | grep OPEN
echo "\nall set for opencv4nodejs\nPATH=$PATH\n"

WITH OUTPUT (for new shell):

HELLO frederico, you b in ZSH mode, NOT bash

OPENCV4NODEJS_DISABLE_AUTOBUILD=1
OPENCV4NODEJS_DISABLE_EXTERNAL_MEM_TRACKING=1
OPENCV_BIN_DIR=/usr/local/Cellar/opencv/4.5.1_3/bin
OPENCV_INCLUDE_DIR=/usr/local/Cellar/opencv/4.5.1_3/include/opencv4
OPENCV_LIB_DIR=/usr/local/Cellar/opencv/4.5.1_3/lib

all set for opencv4nodejs

PATH=/usr/local/Cellar/opencv/4.5.1_3/bin:/Users/freddy/.pyenv/shims:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/share/dotnet:~/.dotnet/tools:/Library/Apple/usr/bin:/Library/Frameworks/Mono.framework/Versions/Current/Commands

THEN, npm install opencv4nodejs RESULTS:

added 30 packages, and audited 33 packages in 44s
found 0 vulnerabilities

THEN cd node_modules/opencv4nodejs/build and make V=1 RESULTS:

c++ -o Release/obj.target/opencv4nodejs/cc/tracking/tracking.o ../cc/tracking/tracking.cc '-DNODE_GYP_MODULE_NAME=opencv4nodejs' '-DUSING_UV_SHARED=1' '-DUSING_V8_SHARED=1' '-DV8_DEPRECATION_WARNINGS=1' '-DV8_DEPRECATION_WARNINGS' '-DV8_IMMINENT_DEPRECATION_WARNINGS' '-D_DARWIN_USE_64_BIT_INODE=1' '-D_LARGEFILE_SOURCE' '-D_FILE_OFFSET_BITS=64' '-DOPENSSL_NO_PINSHARED' '-DOPENSSL_THREADS' '-DOPENCV4NODEJS_FOUND_LIBRARY_CORE' '-DOPENCV4NODEJS_FOUND_LIBRARY_HIGHGUI' '-DOPENCV4NODEJS_FOUND_LIBRARY_IMGCODECS' '-DOPENCV4NODEJS_FOUND_LIBRARY_IMGPROC' '-DOPENCV4NODEJS_FOUND_LIBRARY_FEATURES2D' '-DOPENCV4NODEJS_FOUND_LIBRARY_CALIB3D' '-DOPENCV4NODEJS_FOUND_LIBRARY_PHOTO' '-DOPENCV4NODEJS_FOUND_LIBRARY_OBJDETECT' '-DOPENCV4NODEJS_FOUND_LIBRARY_ML' '-DOPENCV4NODEJS_FOUND_LIBRARY_VIDEO' '-DOPENCV4NODEJS_FOUND_LIBRARY_VIDEOIO' '-DOPENCV4NODEJS_FOUND_LIBRARY_VIDEOSTAB' '-DOPENCV4NODEJS_FOUND_LIBRARY_DNN' '-DOPENCV4NODEJS_FOUND_LIBRARY_FACE' '-DOPENCV4NODEJS_FOUND_LIBRARY_TEXT' '-DOPENCV4NODEJS_FOUND_LIBRARY_TRACKING' '-DOPENCV4NODEJS_FOUND_LIBRARY_XFEATURES2D' '-DOPENCV4NODEJS_FOUND_LIBRARY_XIMGPROC' '-DBUILDING_NODE_EXTENSION' -I/Users/freddy/Library/Caches/node-gyp/15.12.0/include/node -I/Users/freddy/Library/Caches/node-gyp/15.12.0/src -I/Users/freddy/Library/Caches/node-gyp/15.12.0/deps/openssl/config -I/Users/freddy/Library/Caches/node-gyp/15.12.0/deps/openssl/openssl/include -I/Users/freddy/Library/Caches/node-gyp/15.12.0/deps/uv/include -I/Users/freddy/Library/Caches/node-gyp/15.12.0/deps/zlib -I/Users/freddy/Library/Caches/node-gyp/15.12.0/deps/v8/include -I/usr/local/Cellar/opencv/4.5.1_3/include/opencv4 -I../cc -I../cc/core -I../../nan -I/Users/freddy/devx/test-python-video-app1/node_modules/native-node-utils/src  -O3 -gdwarf-2 -mmacosx-version-min=10.9 -arch x86_64 -Wall -Wendif-labels -W -Wno-unused-parameter -std=gnu++1y -stdlib=libc++ -fno-rtti -fno-strict-aliasing -std=c++11 -stdlib=libc++ -MMD -MF ./Release/.deps/Release/obj.target/opencv4nodejs/cc/tracking/tracking.o.d.raw   -c
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
frudman commented 3 years ago

So I'm looking at the code on OpenCV (https://docs.opencv.org/) for this TrackerBoosting and notice something odd:

So, I'm wondering if there was a naming changes on the classes that is not (yet?) reflected in opencv4nodejs?

Anyone?

(ps: I'm new to their documentation so it's possible I'm just missing something. I'm not sure there'd be such a big naming change for classes on a minor version update from 4.5.0 to 4.5.1, right?)

KeitelDOG commented 3 years ago

@frudman I got the same issue this morning, OpenCV 4.5.1 put MultiTracker in a legacy directory of opencv2/tracking directory 4 months ago, which means opencv4nodejs package that has been updated 15 months ago, didn't know about legacy of MultiTracker.

OpenCV 4.5.0 doesn't have it in legacy, but whole version was updated 6 months ago.

Now I'm trying to find a version of OpenCV, between 4.1 (as example in the README) and 4.5.0 that is recent enough to compile correctly and old enough to be built with opencv4nodejs.

I first tried compiled with Brew brew installed opencv@4 (4.5.0_5), but wasn't built in opencv4nodejs because CONTRIB modules SIFT and SURF were not found

I then tried compile manually OpenCV 4.5.1, success. But failed to be built in opencv4nodejs because of MultiTracker not found, which is now in Legacy.

Then I tried OpenCV 4.2.0, couldn't compile, due to DNN module, Deep Neural Network for integration of external models like Tensorflow, Yolo, etc.

Now I'm gonna try version 4.5.0, because with Brew, those DNN compilation passed. I'm taking a chance with it.

If you find any version that fit, please tell me.

frudman commented 3 years ago

@KeitelDOG, Thanks for this update. A few questions (if you have a chance):

[please note: i'm not proficient enough in c/c++ to compile opencv/opencv4nodejs from source. my investigation was just from looking at the code and their documentation. I might be willing to push that a little, for everyone's sake, but looking for an easier path first! :-) ]

1- 15 months from last update? does this mean opencv4nodejs is no longer maintained? Is there any other project that does this (i.e. openCV with nodejs) that you might be aware?

2- Is there a simple-enough way to disable tracker/multi-tracker in order to finish the compilation? 2b- ...or will this just create more problems? 2c- ...or is tracker/multitracker needed to use opencv with node?

3- I'm not a C/C++ coder but if there's a simple way to re-specify openCV's API so that it can properly compile for opencv4nodejs (i.e.1- with the latest version of openCV; i.e.2- and if you can give me some quick pointers as to how to do this), I may be willing/able to do some of that work to move things along. I ask because sometimes converting from c/c++ to node is simply re-entering the api/class-definitions in a format that allows nodejs to ingest the lib code (i.e. in order to marshal calls properly). (and yes, I realize that that might be way over simplifying the effort). Just a though. 3b- e.g. for what I mean: https://koistya.medium.com/how-to-call-c-c-code-from-node-js-86a773033892 (this is just a 5-second google search example); 3c- Does opencv4nodejs depend on something like that to compile?

4- Does the openCV API change a lot like this (feels like a big change to me from 4.5.0 to 4.5.1)? And did it actually change a large amount (from std to legacy) in a minor version??? Is this typical of the openCV api updates?

Just curious.

Thanks. Freddy.

KeitelDOG commented 3 years ago

@frudman I found a solution for myself by removing SIFT 🎉, as I understand everything now on the 3 sides: OpenCV - OpenCV4NodeJS - Node-Gyp. I stick with OpenCV 4.5.0, only SIFT is the problem with this version, I build it without SIFT because SURF is an alternative and faster.

I compile opencv and opencv_contrib 4.5.0 manually following this tutorial: https://www.pyimagesearch.com/2018/08/17/install-opencv-4-on-macos/ . But I think brew install opencv@4 should work, if not installing 4.5.1 which causes MultiTracker problem.

1- Seems like there is a pause. 2 years is like abandon for me. I think 95% of what you want in Deep Learning can be achieved by this. It's just that they did not update the codes to match lastest OpenCV structure changing, specially SIFT, SURF that are now patented (Non Commercial use only unless obtaining the right), MultiTracker algorithms that is moved, and FAST, FREAK that are alternative to SIFT and SURF.

2- I think there is a way. But easy or medium might depend on how the feature is called on other files. I just built it by disabling SIFT. I go to node_modules/opencv4nodejs/binding.gyp, and remove or comment the line: #"cc/xfeatures2d/SIFTDetector.cc",. And then:

cd node_modules/opencv4nodejs
node-gyp configure

then your file: node_modules/opencv4nodejs/build/opencv4nodejs.target.mk will go from:

$(obj).target/$(TARGET)/cc/xfeatures2d/xfeatures2d.o \
$(obj).target/$(TARGET)/cc/xfeatures2d/SIFTDetector.o
$(obj).target/$(TARGET)/cc/xfeatures2d/SURFDetector.o

to

$(obj).target/$(TARGET)/cc/xfeatures2d/xfeatures2d.o \
$(obj).target/$(TARGET)/cc/xfeatures2d/SURFDetector.o

But 1 other file in the code include SIFT file as hard coded, because they are still bound in this package. I commented out 2 lines, and node_modules/opencv4nodejs/cc/xfeatures2d/xfeatures2d.cc became:

#include "opencv_modules.h"

#ifdef HAVE_OPENCV_XFEATURES2D

#include "xfeatures2d.h"
// #include "SIFTDetector.h"
#include "SURFDetector.h"

NAN_MODULE_INIT(XFeatures2d::Init) {
    // SIFTDetector::Init(target);
    SURFDetector::Init(target);
};

#endif // HAVE_OPENCV_XFEATURES2D

And now I just install it with:

node install/install.js

And BOOM (you can see the generated : node_modules/opencv4nodejs/build/Release/opencv4nodejs.node file Screen Shot 2021-03-24 at 10 04 28 PM

3- I know some but I'm no expert in c/c++, but instead Javascript. As it appears, there is 2 places to handle it, either you add missing Multitracker back in it's place, which can be cumbersome the way I see it in OpenCV, or you just change opencv4nodejs codes to adapt it to new emplacement, which is even harder the way Node-Gyp work to build native code into JS. OpenCV 4.5.0 and 4.5.1 has just been out, so if you don't really have the obligation to use 4.5.1, I would suggest you to go with 4.5.0 instead. I was up for version 4.2 or even 3.4.6 if 4.5.0 couldn't work, because old versions would match better with this package. What I've seen done with OpenCV 2.4 and 3 since 2016 is just incredible.

opencv4nodejs is tied to OpenCV classes and methods. It uses internally https://github.com/justadudewhohacks/native-node-utils, from the same hacker dude, and depends like you said from package node-gyp to build native code with native-code-utils codes implemented in opencv4nodejs.

4- OpenCV seems to be from a bunch of super genius guys that are more like Algorithm experts, System Engineers, Mathematicians, than normal Software Engineers. If it's the case, that would explain those kind of quick changes that keep breaking tied packages from minor versions.

KeitelDOG commented 3 years ago

Those are others algorithms for image that are not implemented yet with opencv4nodejs: https://stackoverflow.com/a/33670318/5565544

You can try ORB (Oriented FAST and Rotated BRIEF) as an alternate to SURF in open cv. It almost works as good as SURF and SIFT and it's free unlike SIFT and SURF which are patented and can't be used commercially.

There is many of them.

SURF Feature Dectector example in case you decide like me to drop SIFT (it's not that I had a choice, lol): https://docs.opencv.org/3.4/d7/d66/tutorial_feature_detection.html

frudman commented 3 years ago

@KeitelDOG Holy WOW! That's a LOT of info! Thanks for that!

I will spend today "digesting" all your information and see if I can get it working here on my system.

Here we go...

(ps: using 4.5.0 is definitely good enough for me, so no problem there, and glad you've shown me the way)

frudman commented 3 years ago

So I tried to set it up and failed.

Your instructions above are great but depend on loading 4.5.0 version. Turns out, you can't do that using brew (no formulae for that specific version: goes from latest 4.5.1 to 3.4 or something like that). [unless I missed something?]

So, in order for me to set it up, I'd have to compile opencv from source (git) and select somehow (branch) v4.5.0. This starts getting complicated for me (also setting up the whole tool chain and correct versions of those [e.g. python 3.6 instead of 3.9 and such]).

[ps: i tried looking into creating a brew formulae just for 4.5.0; this seemed doable but again, a lot of work for an outdated version, of a possibly no-longer-supported product, so...]

[ps2: for anyone coming in later, I found this link that shows how to easily build v4.5.0 (or any other version that's been git-tagged, i guess): https://docs.opencv.org/4.5.0/d0/db2/tutorial_macos_install.html ...and these are the sha sigs and such: https://github.com/opencv/opencv/releases/tag/4.5.0 ...and yes, it's been tagged as 4.5.0]

So, I'll put opencv4nodejs on hold for now because...

...I found an alternative to nodejs: https://emgu.com/wiki/index.php/Main_Page

For me C# works just as well, so I'm going to try to use that instead, for now anyway (nodejs would have been better). But the app I'm working on is self contained so C# or nodejs both work (and I'm comfortable with both).

The advantage is that EMGU is up to date and seems well maintained (at least for now, and looking at its history, kept up to date fairly quickly).

While opencv4nodejs is [seems to be] unmaintained [possibly abandoned?] I don't want to invest too much time to run an older version, that will then not keep up to newer releases.

I'll circle back to opencv4nodejs if I can't get the emgu platform to work (or if opencv4nodejs starts moving forward again, which would be great).

Sorry to bail on you, and thanks for your pointers. Freddy.

KeitelDOG commented 3 years ago

@frudman too bad that it still causes you problem. Brew has version 4.5.0_5, when I installed it like 1 week ago, it did installed. Maybe they just replace the formulae with version 4.5.1, there should be a way to get old formulae.

Anyway I did mine with manual compilation, with virtualenv, virtualenvwrapper, python 3.9, pip, numpy, cmake, make, opencv and opencv_contrib zip files in releases page, etc. , from the tutorial of Andrian listed above.

Alos there is other packages of opencv that just fork opencv repos and add the necessary js codes to built it, so it's 100% compatible with that specific opencv version. I saw one for version 2.4, one for 3.3, not sure if others support version 4.

The manual compilation process is straight forward. I found complication on compiling version 4.2, 4.3, but 4.5.0 and 4.5.1 compiled nicely with no problem. But I don't know how Big Sure chip would react with 4.5.0, but I suppose if 4.5.1 compiled on your machine, then so should 4.5.0.

kelvin-lima commented 3 years ago

@KeitelDOG Thanks for your information. Works fine and it's a good workaround for those who not need SIFT detectors. I've used OpenCV version 3.4.14 and it works well.

KeitelDOG commented 3 years ago

@kelvin-lima u're welcome. I was able to port into OpenCV4NodeJS the full algorithms of Adrian Mobile Scanner https://www.pyimagesearch.com/2014/09/01/build-kick-ass-mobile-document-scanner-just-5-minutes/ made with Python and OpenCV. It's even faster in Node than in Python environment, treating a 4000x3000 pixel document in just 140ms to 200ms in a Macbook Pro mid-2012. Some OpenCV code flow a restructured by this package, and most basic Python Numpy matrix algo are easily reproducible for basic calculations. I don't even get to use SURF for now.

kelvin-lima commented 3 years ago

Just to clarify a little bit more. It Will work as expected if you use Node.js version 14.16 and OpenCV version less than 4.5.1. I'm currently using opencv3 because I only need cascade recognizers. :)

KeitelDOG commented 3 years ago

@kelvin-lima I'm not sure for all versions of OpenCV. My node version is v15.4.0, npm version is 7.0.15. And I'm using OpenCV 4.5.0. I tried using between 4.1 and 4.3 , but they wouldn't even compile with my cmake and python setup. But I think if you can compile it, then it should work. But 4.5.0 is guaranteed.

seba-murillo commented 3 years ago

I managed to get it working in my case (tracking error). See https://github.com/justadudewhohacks/opencv4nodejs/issues/775#issuecomment-878141077

KeitelDOG commented 3 years ago

@seba-murillo it's a good solution, passing the features build list instead of hacking package code directly. However, wouldn't we loose the Tracker feature? Not everyone might need it though, but they're gonna lose it, they gotta be aware of that.

davebebo commented 3 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)

p-gintare commented 2 years ago

for Monterey, there were the same issues and more but finally have managed to solve opencv4nodejs installation issues and successfully installed it. (Xcode 13.2.1) it seems that cmake version can be the latest What have worked for me:

unset OPENCV4NODEJS_DISABLE_AUTOBUILD=1

npm -g uninstall opencv4nodejs
brew uninstall opencv
brew uninstall ffmpeg

if you have n:
n 14.18.1
or just have a node version of 14.18.1

brew install ffmpeg@4
npm i -g npm@6.14.11
brew unlink tesseract
export CXXFLAGS="-std=c++14 -Wno-c++11-narrowing"
npm -g install opencv4nodejs
brew link tesseract
Falconiere commented 2 years ago

for Monterey, there were the same issues and more but finally have managed to solve opencv4nodejs installation issues and successfully installed it. (Xcode 13.2.1) it seems that cmake version can be the latest What have worked for me:

unset OPENCV4NODEJS_DISABLE_AUTOBUILD=1

npm -g uninstall opencv4nodejs
brew uninstall opencv
brew uninstall ffmpeg

if you have n:
n 14.18.1
or just have a node version of 14.18.1

brew install ffmpeg@4
npm i -g npm@6.14.11
brew unlink tesseract
export CXXFLAGS="-std=c++14 -Wno-c++11-narrowing"
npm -g install opencv4nodejs
brew link tesseract

Did work for me, thank you so much!

uniquejava commented 2 years ago

@p-gintare Brilliant. The only error I met is command not found: cmake And I have to install cmake first brew install cmake. And then everything works like charm!

$ sw_vers 
ProductName:    macOS
ProductVersion: 12.3.1
BuildVersion:   21E258

$ node -v
v14.19.1

$ npm -v
6.14.16
EdoTrotta commented 2 years ago
  • export CXXFLAGS="-std=c++14 -Wno-c++11-narrowing"

This line -> export CXXFLAGS="-std=c++14 -Wno-c++11-narrowing"

Solved the issues I was facing. BIG UP!