I am encountering an error while attempting to load the 'haarcascade_eye.xml' file using OpenCV.js. The error message is as follows

I am encountering an error while attempting to load the ‘haarcascade_eye.xml’ file using OpenCV.js. The error message is as follows
A:[ERROR:0@1.713] global persistence.cpp:512 open
Can’t open file: :‘haarcascade_frontalface_default.xml’
B:[ERROR:0@1.714] global persistence.cpp:512 open Can’t open file: ‘haarcascade_eye.xml’
in read mode

  1. “Although this model is available in our file path, we’ve made several attempts. We even looked on the internet and copied your example code, but it still didn’t work.”
  2. “On the internet, there are many people facing similar issues, but it seems that no one has resolved this problem.”
  3. “OpenCV.js doesn’t have a strong community for JavaScript. We hope that JavaScript developers can also benefit from this library. Thank you.”

From this stackoverflow post:

Ok, this is for the face detector but the same approach will work for you. You need to make the file accessible to cv by reading it into the opencv file system. To do this, you need to use an XMLHttpRequest() to get the XML file using its URL and then call the cv.CascadeClassifier as follows:

const xml_model_url = 'https://raw.githubusercontent.com/opencv/opencv/master/data/haarcascades/haarcascade_frontalface_default.xml';
const xml_path = "haarcascade_frontalface_default.xml";
let faceCascade = null;

function createFileFromUrl(path, url, callback) {
// path: string to access loaded file trough cv
// url: path of the actual file on your FS
// callback: what to do when file is loaded

  let request = new XMLHttpRequest();
  request.open('GET', url, true);
  request.responseType = 'arraybuffer';
  request.onload = function(ev) {
      request = this;
      if (request.readyState === 4) {
          if (request.status === 200) {
              let data = new Uint8Array(request.response);
              cv.FS_createDataFile('/', path, data, true, false, false);
              callback();
          } else {
              console.error('Failed to load ' + url + ' status: ' + request.status);
          }
      }
  };
  request.send();
}
function createCascade() {
  faceCascade = new cv.CascadeClassifier(xml_path);
}
createFileFromUrl(xml_path, xml_model_url,createCascade);
1 Like