Skip to content

Commit

Permalink
fix web worker
Browse files Browse the repository at this point in the history
- add worker-loader, use inline option
- format jsfeat_detect.worker.js to match worker-loader expectations
- use correct jsfeat version (package.json was using a version that is too new)
- ignore intermediate `dist/*.worker.js` in gitignore
  • Loading branch information
yofreke committed Sep 12, 2016
1 parent 0901691 commit 45f5f43
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 25 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules/
dist/*.worker.js
23 changes: 8 additions & 15 deletions js/jsfeat_detect.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
// simple wrapper for jsfeat face detector
var findFaceWorker = require('./jsfeat_detect_worker.js');

// Curtousy of stackoverflow this function
Worker.createURL = function(func_or_string){
var str = (typeof func_or_string === 'function')?func_or_string.toString():func_or_string;
var blob = new Blob(['\'use strict\';\nself.onmessage ='+str], { type: 'text/javascript' });
return window.URL.createObjectURL(blob);
};

Worker.create = function(func_or_string){
return new Worker(Worker.createURL(func_or_string));
};

var findFaceWorker = require('./jsfeat_detect.worker');

/**
* this cascade is derived from https://github.com/mtschirs/js-objectdetect implementation
Expand Down Expand Up @@ -48,16 +36,21 @@ var jsfeat_face = function(image) {
}
var imageData = work_ctx.getImageData(0, 0, w, h);

var worker = Worker.create(findFaceWorker);
var worker = findFaceWorker();

worker.addEventListener('message', function (e) {
if (e.data.type === 'console') {
console[e.data.func].apply(window, e.data.args);
return;
}

this.faceDetected(e, callback);
}.bind(this), false);

worker.postMessage({
w: w,
h: h,
imageData:imageData
imageData: imageData
});
}

Expand Down
9 changes: 2 additions & 7 deletions js/jsfeat_detect_worker.js → js/jsfeat_detect.worker.js

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"scripts": {
"build": "PROD_ENV=1 webpack --optimize-minimize --optimize-dedupe",
"watch": "webpack --debug --devtool eval-cheap-module-source-map --output-pathinfo --progress --colors --watch",
"serve": "webpack-dev-server --debug --devtool eval-cheap-module-source-map --output-pathinfo --inline --hot --host localhost"
"serve": "webpack-dev-server --debug --devtool eval-cheap-module-source-map --output-pathinfo --inline --hot --host localhost",
"clean": "rm -rf dist"
},
"main": "clmtracker.js",
"repository": {
Expand All @@ -22,10 +23,11 @@
"stats.js": "0.0.14-master",
"uglify": "^0.1.5",
"webpack": "^1.13.2",
"webpack-dev-server": "^1.15.1"
"webpack-dev-server": "^1.15.1",
"worker-loader": "^0.7.1"
},
"dependencies": {
"jsfeat": "0.0.8",
"jsfeat": "git+https://github.com/inspirit/jsfeat#f584f93c78b5085ef141eb1dc66d9d06a7123ec3",
"numeric": "^1.2.6"
}
}
4 changes: 4 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ module.exports = {
{
test: /\.json$/,
loader: 'json-loader'
},
{
test: /\.worker\.js$/,
loader: 'worker-loader?inline=true'
}
]
}
Expand Down

0 comments on commit 45f5f43

Please sign in to comment.