Skip to content

Commit

Permalink
JsfeatFace extends EventEmitter
Browse files Browse the repository at this point in the history
  • Loading branch information
yofreke committed Sep 14, 2016
1 parent e4c9af3 commit 3cb409f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 35 deletions.
13 changes: 8 additions & 5 deletions js/Tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -972,8 +972,7 @@ export default class Tracker extends EventEmitter {
return positions;
}

_faceDetected (e, callback) {
var comp = e.data.comp;
_faceDetected (comp, callback) {
if (comp && comp.length > 0) {
this.candidate = comp[0];
} else {
Expand All @@ -1000,9 +999,13 @@ export default class Tracker extends EventEmitter {
cc.drawImage(el, 0, 0, el.width, el.height);

var jf = new JsfeatFace(canvas);
jf.faceDetected = this._faceDetected.bind(this);
//TODO Allow option that limit simultaneous trigger of WebWorkers
var comp = jf.findFace(callback);
// jf.faceDetected = this._faceDetected.bind(this);
jf.on('faceDetected', (comp) => {
this._faceDetected(comp, callback);
});

// TODO Allow option that limit simultaneous trigger of WebWorkers
jf.findFace();
}

// calculate score of current fit
Expand Down
28 changes: 14 additions & 14 deletions js/jsfeat/JsfeatFace.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
import EventEmitter from 'events';
// simple wrapper for jsfeat face detector
import findFaceWorker from './findFace.worker';

/**
* this cascade is derived from https://github.com/mtschirs/js-objectdetect implementation
* @author Martin Tschirsich / http://www.tu-darmstadt.de/~m_t
*/
import frontalface from '../filters/frontalface.json';
// import frontalface from '../filters/frontalface.json';

export default class JsfeatFace {

export default class JsfeatFace extends EventEmitter {
/**
* @param {Canvas|Image|Video} image
*/
constructor (image) {
super();
this.work_canvas = undefined;
this.work_ctx = undefined;

this.image = image;
this.w = this.image.width;
this.h = this.image.height;

if (this.image.tagName == 'VIDEO' || this.image.tagName == 'IMG') {
if (this.image.tagName === 'VIDEO' || this.image.tagName === 'IMG') {
this.work_canvas = document.createElement('canvas');
this.work_canvas.height = h;
this.work_canvas.width = w;
this.work_canvas.height = this.image.width;
this.work_canvas.width = this.image.height;
this.work_ctx = this.work_canvas.getContext('2d');
} else if (this.image.tagName == 'CANVAS') {
} else if (this.image.tagName === 'CANVAS') {
this.work_ctx = this.image.getContext('2d');
}

Expand All @@ -36,23 +35,24 @@ export default class JsfeatFace {
// var classifier = frontalface;

this.worker = findFaceWorker();
}

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

this.faceDetected(e, callback);
this.emit('faceDetected', e.data.comp);
}, false);
}

if (this.image.tagName == 'VIDEO' || this.image.tagName == 'IMG') {
findFace () {
if (this.image.tagName === 'VIDEO' || this.image.tagName === 'IMG') {
this.work_ctx.drawImage(this.image, 0, 0);
}
const imageData = this.work_ctx.getImageData(0, 0, this.w, this.h);

// console.time('findFace');
this.worker.postMessage({
w: this.w,
h: this.h,
Expand Down
30 changes: 14 additions & 16 deletions js/jsfeat/findFace.worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,22 @@ onmessage = function (e) {
const rl = rects.length;

// console.timeEnd('findFace');
if (rl > 0) {
let best = rects[0];
for (let i = 1; i < rl; i++) {
if (rects[i].neighbors > best.neighbors) {
best = rects[i]
} else if (rects[i].neighbors == best.neighbors) {
if (rects[i].confidence > best.confidence) {
best = rects[i];
}
if (rl <= 0) {
self.postMessage({ comp: false });
return;
}

let best = rects[0];
for (let i = 1; i < rl; i++) {
if (rects[i].neighbors > best.neighbors) {
best = rects[i]
} else if (rects[i].neighbors === best.neighbors) {
if (rects[i].confidence > best.confidence) {
best = rects[i];
}
}
// return [best];
self.postMessage({
comp: [best]
});
} else {
self.postMessage(imageData);
//return false;
}

self.postMessage({ comp: [best] });
// END Old findFace
};

0 comments on commit 3cb409f

Please sign in to comment.