OpenCV.js does not work from a web worker if WASM threads are enabled

OpenCV.js does not work for me when built with WASM threads enabled (--threads option active) and used in a web worker.

The problem occurs when using OpenCV.js compiled with emscripten (3.1.57, or latest)

$ git clone
$ git checkout 4.10.0

Now build opencv.js with, eg. emscripten 3.1.57 (or latest emscripten version ; I tried a few versions, all produce the same result):

$ emcmake python3 ./opencv/platforms/js/ build_wasmthreads --build_wasm --threads

opencv.js has been generated in the build_wasmthreads/bin folder.

Next, make sure SharedArrayBuffer is enabled regardless of cross-origin header status, to be on the safe side ; for this, I use --enable-features=SharedArrayBuffer on Chrome.

Now try the following:

Case 1)

  • load opencv.js in a web page using a <script> tag
  • await cv; This line of code will succeed.

Case 2)

  • load opencv.js in a web worker using importScripts().
  • await cv; This line of code will fail with the following error:

Refused to execute script from ‘http://localhost/undefined’ because its MIME type (‘text/html’) is not executable, and strict MIME type checking is enabled

Note that OpenCV.js will always work fine when WASM threads are disabled (–threads option not used).

Shall a specific emscripten version be used for WASM threads to work in web workers?

Looks like you’ve got the problem pinned down.

I’d recommend filing an issue on OpenCV’s github, or adding your findings if such a one already exists.