habbes / xaval-issues

Feature requests and bug reports for Xaval
0 stars 0 forks source link

How to read xml file? #1

Open suren41977 opened 5 years ago

suren41977 commented 5 years ago

How to read haar cascade xml file to be used inside xaval playground?

habbes commented 5 years ago

Hey @suren41977 thanks for checking out Xaval.

You can read an XML file by importing it in the file manager at the bottom right and give it a name of your choice. Then you can load it in the code as follows:

const reader = xaval.io.files.getReader('xml_file_name');

With the reader, you can read the file as text, data url or buffer using the following methods:

reader.readText().then(text => console.log(text));
reader.readDataURL().then(dataUrl => console.log(dataUrl));
reader.readBuffer().then(buffer => console.log(buffer));

Not that each of those methods returns a promise. You can also get the blob url using reader.url property.

However I have not managed to get either of these urls to work with cv.CascadeClassifier().load() method. When I do I'll update this thread.

habbes commented 5 years ago

@suren41977 I've peeked into the source code of the samples on the OpenCV.js docs and figured out how to make it load the files from Xaval into OpenCV.js "file system".

Here are the steps:

So if you wanted to load both the face and features in order to perform face detection, you'd first import the xml file in the file manager, and maybe give it a name like face_features, and then you could run the following code:

const { io: files } = xaval;

const reader = files.getReader('face_features');
reader.readBuffer().then((buffer) => {
  const data = new Uint8Array(buffer);
  // store the file at a specified path in OpenCv.js
  cv.FS_createDataFile('/', 'face_features.xml', data, true, false, false);
  // now you can load it in a classifier using the same path passed to the previous function
  const faceCascade = cv.CascadeClassifier();
  faceCascade.load('face_features.xml');
  // ...proceed with face detection algorithm
});
habbes commented 5 years ago

Since this will probably be a common operation, I'll add a utility function in a future version to abstract all those steps.