Improve cascade file loading and error handling for js tutorials

This commit is contained in:
Ningxin Hu 2017-09-26 13:01:45 +08:00
parent f57106b00a
commit ea2c13c91e
3 changed files with 29 additions and 20 deletions

View File

@ -77,18 +77,14 @@ tryIt.addEventListener('click', () => {
});
utils.loadOpenCv(() => {
let eyeCascadeFile = 'haarcascade_eye.xml';
utils.createFileFromUrl(eyeCascadeFile, eyeCascadeFile, () => {
let faceCascadeFile = 'haarcascade_frontalface_default.xml';
utils.createFileFromUrl(faceCascadeFile, faceCascadeFile, () => {
tryIt.removeAttribute('disabled');
});
});
});
let Module = {
preRun: [function() {
Module.FS_createPreloadedFile('/', 'haarcascade_eye.xml',
'haarcascade_eye.xml', true, false);
let frontalfaceXML = 'haarcascade_frontalface_default.xml';
let frontalfaceDefaultXML = 'haarcascade_frontalface_default.xml';
Module.FS_createPreloadedFile('/', frontalfaceXML,
frontalfaceDefaultXML, true, false);
}],
};
</script>
</body>
</html>

View File

@ -131,16 +131,11 @@ function onVideoStopped() {
startAndStop.innerText = 'Start';
}
let Module = {
preRun: [function() {
Module.FS_createPreloadedFile(
'/', 'haarcascade_frontalface_default.xml',
'haarcascade_frontalface_default.xml', true, false);
}],
};
utils.loadOpenCv(() => {
let faceCascadeFile = 'haarcascade_frontalface_default.xml';
utils.createFileFromUrl(faceCascadeFile, faceCascadeFile, () => {
startAndStop.removeAttribute('disabled');
});
});
</script>
</body>

View File

@ -19,6 +19,24 @@ function Utils(errorOutputId) { // eslint-disable-line no-unused-vars
node.parentNode.insertBefore(script, node);
};
this.createFileFromUrl = function(path, url, callback) {
let request = new XMLHttpRequest();
request.open('GET', url, true);
request.responseType = 'arraybuffer';
request.onload = function(ev) {
if (request.readyState === 4) {
if (request.status === 200) {
let data = new Uint8Array(request.response);
cv.FS_createDataFile('/', path, data, true, false, false);
callback();
} else {
self.printError('Failed to load ' + url + ' status: ' + request.status);
}
}
};
request.send();
};
this.loadImageToCanvas = function(url, cavansId) {
let canvas = document.getElementById(cavansId);
let ctx = canvas.getContext('2d');