Global persistence.cpp:505 open Can't open file in read mode

OK, the problem is that you need to get the XML file into the cv filesystem. The answer is in this stackoverflow post:

Here is the solution:
Ok, so 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