OpenCV JS Load CascadeClassifier file

Using OpenCV Js and Node (not browser).

Trying to load haarcascade_frontalface_default.xml but always get: [ERROR:0@0.074] global /build/master-contrib_docs-lin64/opencv/modules/core/src/persistence.cpp (505) open Can't open file: 'lib/haarcascade_frontalface_default.xml' in read mode 6711208

I can open the file just fine regularly: readFileSync("lib/haarcascade_frontalface_default.xml") so I know the path is correct.

I’ve tried:

  • new cv.CascadeClassifier("lib/haarcascade_frontalface_default.xml");
  • new cv.CascadeClassifier(path.resolve("lib/haarcascade_frontalface_default.xml"));
  • new cv.CascadeClassifier().load("lib/haarcascade_frontalface_default.xml");

All with the same result. :frowning:

I see there’s: cv.FS_createLazyFile through which I think something is loading (though nothing is working further down the line); I wish this documented better somewhere. :frowning:

The thing that really throws me off is notes like this:

OpenCV: Using OpenCV.js In Node.js

where it says:

// Load pre-trained classifier files. Notice how we reference local files using relative paths just
// like we normally would do
faceCascade.load(‘./haarcascade_frontalface_default.xml’);

But it’s not pointing to the fact that this only works because of cv.FS.chdir(process.cwd()). Except, it works for them, but when I try it I get the cryptic:

[<generic error, no stack>] {
  node: undefined,
  setErrno: [Function (anonymous)],
  errno: 44,
  message: 'FS error'
}

Oh man. Finally made some progress:

  const emscriptenFs = cv.FS;
  const virtualMountFolder = "/local";
  emscriptenFs.mkdir(virtualMountFolder);
  emscriptenFs.mount(emscriptenFs.filesystems.NODEFS, { root: process.cwd() }, virtualMountFolder);
  emscriptenFs.chdir(virtualMountFolder);

And now I am able to just create the classifier: const faceClassifier = new cv.CascadeClassifier("lib/haarcascade_frontalface_default.xml"); and this succeeds.

2 Likes

Hey there. Got the same error, trying solution from your last comment, but got stuck at

ERROR abort(Assertion failed: undefined). Build with -s ASSERTIONS=1 for more info.

when calling

emscriptenFs.mount(emscriptenFs.filesystems.NODEFS, { root: process.cwd() }, virtualMountFolder);

I don’t understand whats going on, does it ring a bell to u?

My bad. I missed that u didn’t use browser. Which makes a ton of sense :sweat_smile: