Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

OpenCV.js in Web Worker problem

Hi everyone!
In my application I have to use multiple haar cascades so I thought that using parallel computing would be a good idea.
To achieve this I'm trying to use OpenCV's classifiers in Web Workers but I'm having some problems with them.
My worker code is as follows:

importScripts("./opencv.js");
importScripts("./utils.js");
let utils = new Utils('errorMessage');
let cascadeFile = 'HAAR_19Stages.xml';
let num = new cv.RectVector();
let result = false;
var classifier = new cv.CascadeClassifier();

init = function () {

    utils.createFileFromUrl(cascadeFile, cascadeFile, () => {
        classifier.load(cascadeFile);
    })
}

self.addEventListener('message', function (e) {

    let frame = e.data.grayScaleframe;
    classifier.detectMultiScale(frame, num, 1.1, 3, 0, new cv.Size(10, 10), new cv.Size(300, 300));
    if (num.size() > 0) {
        result = true;
    } else {
        result = false;
    }
    self.postMessage(result);
}, false)

init();

First I load the classifier with the "init" function, then, from the main thread, I call the code sending a message to the event listener. I pass a frame (grayScaleframe) and I want to analyze it with detectMultiScale.
The problem is that it gives me the error "Uncaught TypeError: Cannot read property '$$' of undefined" when using the "detectMultiScale" function. It's like if it forgets of the classifier declaration and initialization.
Since I'm new to Web Workers and OpenCV.js, I was asking if some of you has a solution to this.
Thank you