Ask Your Question

Revision history [back]

Opencv.js in web worker not loading

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-js-webworker/raw/master/public/js/opencv_3_4_custom_O3.js Which is used in demo here: https://vinissimus.github.io/opencv-js-webworker/

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 ?