'Weird "Uncaught 6645968" Error when detecting face in OpenCV

I am trying to detect a face using opencv.js. My code works perfectly, reading an image and converting it to grayscale. Here's my code:

Note: OpenCV v4.5.2 used, https://docs.opencv.org/4.5.2/opencv.js

// Reading the Image
let imgElement = document.getElementById("imageSrc");
let mat = cv.imread(imgElement);
let dst = new cv.Mat();

// Converting image to grayscale
cv.cvtColor(mat, dst, cv.COLOR_RGBA2GRAY);
cv.imshow("canvasOutput", dst);

// Haar Casscade Data
const trainedFaceData = new cv.CascadeClassifier("haarcascade_frontalface_default.xml");
console.log(trainedFaceData);

// Getting the coordinates of the face
let faces = new cv.RectVector();
let msize = new cv.Size(0, 0);
let coords = trainedFaceData.detectMultiScale(
dst,
faces,
1.1,
3,
0,
msize,
msize
);
console.log(coords);

// Free memory
mat.delete();
dst.delete();

The error occurs when I try to detect the face, here is the error:

Uncaught 6645968 Error

The 6645968 in the error is also the value of the pointer (ptr) property of the new CascadeClassifier at line 11 (something I noted).

BONUS RESEARCH: I tried OpenCV.js v3.4.0, https://docs.opencv.org/3.4.0/opencv.js and got this error:

Cannot construct CascadeClassifier due to unbound types: N2cv6StringE Error

Check this error if it helps. Thanks!



Solution 1:[1]

You need to load the cascade classifier using load():

let classifier = new cv.CascadeClassifier();
classifier.load('haarcascade_frontalface_default.xml');

see the docs

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 David Strahm