Skip to content

Commit

Permalink
refactor some of: utils, jsfeat, Tracker
Browse files Browse the repository at this point in the history
  • Loading branch information
yofreke committed Sep 13, 2016
1 parent 9a9831e commit e4c9af3
Show file tree
Hide file tree
Showing 8 changed files with 198 additions and 86 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist/*
27 changes: 13 additions & 14 deletions js/Tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,29 @@ import EventEmitter from 'events';
import numeric from 'numeric';

// libs
import MosseFilter from './utils/mosse.js';
import MosseFilterResponses from './utils/mosseFilterResponses.js';
import MosseFilter from './utils/mosse';
import MosseFilterResponses from './utils/mosseFilterResponses';
import JsfeatFace from './jsfeat/JsfeatFace';
import webglFilter from './svmfilter_webgl.js';
import svmFilter from './svmfilter_fft.js';
import webglFilter from './svmfilter_webgl';
import svmFilter from './svmfilter_fft';

// filters
import entire_face_filter from './filters/entire_face_filter.json';
import left_eye_filter from './filters/left_eye_filter.json';
import right_eye_filter from './filters/right_eye_filter.json';
import nose_filter from './filters/nose_filter.json';
// import entireFaceFilter from './filters/entire_face_filter.json';
import leftEyeFilter from './filters/left_eye_filter.json';
import rightEyeFilter from './filters/right_eye_filter.json';
import noseFilter from './filters/nose_filter.json';


import {
requestAnimFrame,
cancelRequestAnimFrame
// drawData
} from './utils/canvasHelpers';
} from './utils/anim';
import { gpopt, gpopt2 } from './utils/meanshift';
import procrustes from './utils/procrustes';


const halfPI = Math.PI / 2;
const HAS_MOSSE_FILTERS = MosseFilter && left_eye_filter && right_eye_filter && nose_filter;
const HAS_MOSSE_FILTERS = MosseFilter && leftEyeFilter && rightEyeFilter && noseFilter;


export default class Tracker extends EventEmitter {
Expand Down Expand Up @@ -189,13 +188,13 @@ export default class Tracker extends EventEmitter {
if (this.model.hints && HAS_MOSSE_FILTERS) {
//var mossef_lefteye = new MosseFilter({drawResponse : document.getElementById('overlay2')});
this.mossef_lefteye = new MosseFilter();
this.mossef_lefteye.load(left_eye_filter);
this.mossef_lefteye.load(leftEyeFilter);
//var mossef_righteye = new MosseFilter({drawResponse : document.getElementById('overlay2')});
this.mossef_righteye = new MosseFilter();
this.mossef_righteye.load(right_eye_filter);
this.mossef_righteye.load(rightEyeFilter);
//var mossef_nose = new MosseFilter({drawResponse : document.getElementById('overlay2')});
this.mossef_nose = new MosseFilter();
this.mossef_nose.load(nose_filter);
this.mossef_nose.load(noseFilter);
} else {
console.log("MOSSE filters not found, using rough approximation for initialization.");
}
Expand Down
63 changes: 30 additions & 33 deletions js/jsfeat/findFace.worker.js

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions js/jsfeat/jsfeatWithFrontalface.js

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions js/utils/anim.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export const requestAnimFrame = (function () {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(/* function FrameRequestCallback */ callback, /* DOMElement Element */ element) {
return window.setTimeout(callback, 1000 / 60);
};
})();

export const cancelRequestAnimFrame = (function () {
return window.cancelAnimationFrame ||
window.webkitCancelRequestAnimationFrame ||
window.mozCancelRequestAnimationFrame ||
window.oCancelRequestAnimationFrame ||
window.msCancelRequestAnimationFrame ||
window.clearTimeout;
})();
20 changes: 20 additions & 0 deletions js/utils/canvas.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// function to draw pixeldata on some canvas, only used for debugging
export const drawData = (canvasContext, data, width, height, transposed, drawX, drawY) => {
const psci = canvasContext.createImageData(width, height);
const pscidata = psci.data;
for (let j = 0; j < width * height; j++) {
let val;
if (!transposed) {
val = data[(j % width) + ((j / width) >> 0) * width];
} else {
val = data[(j % height) * height + ((j / height) >> 0)];
}
val = val > 255 ? 255 : val;
val = val < 0 ? 0 : val;
pscidata[j * 4] = val;
pscidata[(j * 4) + 1] = val;
pscidata[(j * 4) + 2] = val;
pscidata[(j * 4) + 3] = 255;
}
canvasContext.putImageData(psci, drawX, drawY);
}
39 changes: 0 additions & 39 deletions js/utils/canvasHelpers.js

This file was deleted.

104 changes: 104 additions & 0 deletions js/utils/video.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
let VIDEO_EL;

const getVideoEl = () => {
if (!VIDEO_EL) {
VIDEO_EL = document.createElement('video');
}
return VIDEO_EL;
};


export const supportsVideo = () => {
return !!getVideoEl().canPlayType;
};


export const supportsH264BaselineVideo = () => {
if (!supportsVideo()) {
return false;
}
return getVideoEl().canPlayType('video/mp4; codecs="avc1.42E01E, mp4a.40.2"');
};


export const supportsOggTheoraVideo = () => {
if (!supportsVideo()) {
return false;
}
return getVideoEl().canPlayType('video/ogg; codecs="theora, vorbis"');
};


export const URL = (
window.URL || window.webkitURL || window.msURL || window.mozURL
);


export const getUserMedia = (
navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia ||
navigator.msGetUserMedia
);


export const supportsUserMedia = () => {
return !!getUserMedia;
};


export const supportsWebGL = () => {
let webGLContext;
let webGLTestCanvas = document.createElement('canvas');

if (!window.WebGLRenderingContext) {
return false;
}

webGLContext = (
webGLTestCanvas.getContext('webgl') ||
webGLTestCanvas.getContext('experimental-webgl')
);

if (!webGLContext || !webGLContext.getExtension('OES_texture_float')) {
webGLContext = null;
}

return webGLContext != null;
};


export const loadVideo = (cb) => {
if (!getUserMedia) {
cb(new Error('browser does not support getUserMedia'));
return;
}
// set up stream

let videoSelector = { video: true };
const appVersion = window.navigator.appVersion;
if (appVersion.match(/Chrome\/(.*?) /)) {
const chromeVersion = parseInt(
appVersion.match(/Chrome\/(\d+)\./)[1],
10
);
if (chromeVersion < 20) {
videoSelector = 'video';
}
};

getUserMedia(videoSelector, (stream) => {
cb(null, stream);
}, () => {
cb(new Error('problem trying to fetch video from your webcam'));
});
};


export const setVideoSrc = (videoEl, src) => {
if (videoEl.mozCaptureStream) {
videoEl.mozSrcObject = src;
} else {
videoEl.src = (URL && URL.createObjectURL(src)) || src;
}
}

0 comments on commit e4c9af3

Please sign in to comment.