Opencv.js 4.4 in web worker is not loading

asked 2020-06-27 12:04:15 -0600

Hello, I'm using opencv 4.4 and compiling it to javascript using following command docker

run --rm --workdir /code -v "$PWD":/code "trzeci/emscripten:latest" python ./platforms/js/build_js.py build

taken from https://docs.opencv.org/master/d4/da1/tutorial_js_setup.html, everythin compiles just fine, it produces opencv.js file. I'm able to load this file via <script> tag and use it.

let opencvScript = document.createElement("script");                             
opencvScript.setAttribute("src", "js/lib/opencv.js");                            
opencvScript.onload = function () {                                              
    cv["onRuntimeInitialized"] = () => {                                             
        console.log("#opencv loaded !");  // Works                                           
};                                                                   
};

But I want to use opencv in java script web workers. I use following code to import opencv.js in web worker (worker.js file)

importScripts("./opencv.js");                                                    
cv["onRuntimeInitialized"] = () => {                                             
    console.log("#opencv loaded !");// Doesn't work, it is never executed
};

and create worker like this:

w = new Worker("worker.js")

The problem is nothing happens. No error is thrown, script finishes execution but library is not loaded. I found already complied opencv.js here: https://github.com/vinissimus/opencv-... Which is used in demo here: https://vinissimus.github.io/opencv-j...

And this file is loaded in my worker.js but unfortunately it doesn't have CascadeClassifier which I want to use in js web worker. Anyone struggled which such problem and know if it's possible to load opencv.js in web worker ?

edit retag flag offensive close merge delete

Comments

I think in recent versions it is properly loaded in Utils using .loadOpenCv() and .createFileFromUrl(). There are many books and classes available, as well as CodePen examples.

Should you load OpenCV into a webworker, or does it use them itself if compatible? Wasm?

kpachinger gravatar imagekpachinger ( 2020-07-03 14:01:49 -0600 )edit

I fix with copy first lines of code from https://raw.githubusercontent.com/vin... and paste it in my opencv.js

Druzh gravatar imageDruzh ( 2020-10-14 08:40:36 -0600 )edit

Congratulations! There's no telling what you will integrate Next.js! 👍

kpachinger gravatar imagekpachinger ( 2020-10-15 10:58:27 -0600 )edit