Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made reference changes, and import changes in files, Added a parameter to check if the face is in the frame #322

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
224 changes: 112 additions & 112 deletions src/index.mjs

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/params.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const params = {
saveDataAcrossSessions: true,
// Whether or not to store accuracy eigenValues, used by the calibration example file
storingPoints: false,
faceInFrame: true,

trackEye: 'both',
};
Expand Down
16 changes: 8 additions & 8 deletions src/ridgeReg.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import util from './util';
import util_regression from './util_regression';
import params from './params';
import util from './util.mjs';
import util_regression from './util_regression.mjs';
import params from './params.mjs';

const reg = {};

Expand All @@ -9,7 +9,7 @@ const reg = {};
* this object allow to perform ridge regression
* @constructor
*/
reg.RidgeReg = function() {
reg.RidgeReg = function () {
this.init();
};

Expand All @@ -32,7 +32,7 @@ reg.RidgeReg.prototype.addData = util_regression.addData
* @param {Object} eyesObj - The current user eyes object
* @returns {Object}
*/
reg.RidgeReg.prototype.predict = function(eyesObj) {
reg.RidgeReg.prototype.predict = function (eyesObj) {
if (!eyesObj || this.eyeFeaturesClicks.length === 0) {
return null;
}
Expand All @@ -57,11 +57,11 @@ reg.RidgeReg.prototype.predict = function(eyesObj) {

var eyeFeats = util.getEyeFeats(eyesObj);
var predictedX = 0;
for(var i=0; i< eyeFeats.length; i++){
for (var i = 0; i < eyeFeats.length; i++) {
predictedX += eyeFeats[i] * coefficientsX[i];
}
var predictedY = 0;
for(var i=0; i< eyeFeats.length; i++){
for (var i = 0; i < eyeFeats.length; i++) {
predictedY += eyeFeats[i] * coefficientsY[i];
}

Expand Down Expand Up @@ -91,7 +91,7 @@ reg.RidgeReg.prototype.setData = util_regression.setData;
* Return the data
* @returns {Array.<Object>|*}
*/
reg.RidgeReg.prototype.getData = function() {
reg.RidgeReg.prototype.getData = function () {
return this.dataClicks.data;
}

Expand Down
96 changes: 48 additions & 48 deletions src/ridgeRegThreaded.mjs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import util from './util';
import util_regression from './util_regression';
import params from './params';
import mat from './mat';
import util from './util.mjs';
import util_regression from './util_regression.mjs';
import params from './params.mjs';
import mat from './mat.mjs';

const reg = {};

var ridgeParameter = Math.pow(10,-5);
var ridgeParameter = Math.pow(10, -5);
var dataWindow = 700;
var weights = {'X':[0],'Y':[0]};
var weights = { 'X': [0], 'Y': [0] };
var trailDataWindow = 10;


Expand All @@ -17,83 +17,83 @@ var trailDataWindow = 10;
* this object allow to perform threaded ridge regression
* @constructor
*/
reg.RidgeRegThreaded = function() {
reg.RidgeRegThreaded = function () {
this.init();
};

/**
* Initialize new arrays and initialize Kalman filter.
*/
reg.RidgeRegThreaded.prototype.init = function() {
this.screenXClicksArray = new util.DataWindow(dataWindow);
this.screenYClicksArray = new util.DataWindow(dataWindow);
this.eyeFeaturesClicks = new util.DataWindow(dataWindow);
reg.RidgeRegThreaded.prototype.init = function () {
this.screenXClicksArray = new util.DataWindow(dataWindow);
this.screenYClicksArray = new util.DataWindow(dataWindow);
this.eyeFeaturesClicks = new util.DataWindow(dataWindow);

this.screenXTrailArray = new util.DataWindow(trailDataWindow);
this.screenYTrailArray = new util.DataWindow(trailDataWindow);
this.eyeFeaturesTrail = new util.DataWindow(trailDataWindow);
this.screenXTrailArray = new util.DataWindow(trailDataWindow);
this.screenYTrailArray = new util.DataWindow(trailDataWindow);
this.eyeFeaturesTrail = new util.DataWindow(trailDataWindow);

this.dataClicks = new util.DataWindow(dataWindow);
this.dataTrail = new util.DataWindow(dataWindow);
this.dataClicks = new util.DataWindow(dataWindow);
this.dataTrail = new util.DataWindow(dataWindow);

// Place the src/ridgeworker.js file into the same directory as your html file.
if (!this.worker) {
if (!this.worker) {
this.worker = new Worker('ridgeWorker.mjs'); // [20200708] TODO: Figure out how to make this inline
this.worker.onerror = function(err) { console.log(err.message); };
this.worker.onmessage = function(evt) {
weights.X = evt.data.X;
weights.Y = evt.data.Y;
};
console.log('initialized worker');
}
this.worker.onerror = function (err) { console.log(err.message); };
this.worker.onmessage = function (evt) {
weights.X = evt.data.X;
weights.Y = evt.data.Y;
};
console.log('initialized worker');
}

// Initialize Kalman filter [20200608 xk] what do we do about parameters?
// [20200611 xk] unsure what to do w.r.t. dimensionality of these matrices. So far at least
// by my own anecdotal observation a 4x1 x vector seems to work alright
var F = [ [1, 0, 1, 0],
[0, 1, 0, 1],
[0, 0, 1, 0],
[0, 0, 0, 1]];
var F = [[1, 0, 1, 0],
[0, 1, 0, 1],
[0, 0, 1, 0],
[0, 0, 0, 1]];

//Parameters Q and R may require some fine tuning
var Q = [ [1/4, 0, 1/2, 0],
[0, 1/4, 0, 1/2],
[1/2, 0, 1, 0],
[0, 1/2, 0, 1]];// * delta_t
var delta_t = 1/10; // The amount of time between frames
Q = mat.multScalar(Q, delta_t);

var H = [ [1, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0],
[0, 0, 0, 1, 0, 0]];
var H = [ [1, 0, 0, 0],
[0, 1, 0, 0]];
var Q = [[1 / 4, 0, 1 / 2, 0],
[0, 1 / 4, 0, 1 / 2],
[1 / 2, 0, 1, 0],
[0, 1 / 2, 0, 1]];// * delta_t
var delta_t = 1 / 10; // The amount of time between frames
Q = mat.multScalar(Q, delta_t);

var H = [[1, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0],
[0, 0, 0, 1, 0, 0]];
var H = [[1, 0, 0, 0],
[0, 1, 0, 0]];
var pixel_error = 47; //We will need to fine tune this value [20200611 xk] I just put a random value here

//This matrix represents the expected measurement error
var R = mat.multScalar(mat.identity(2), pixel_error);
var R = mat.multScalar(mat.identity(2), pixel_error);

var P_initial = mat.multScalar(mat.identity(4), 0.0001); //Initial covariance matrix
var x_initial = [[500], [500], [0], [0]]; // Initial measurement matrix

this.kalman = new util_regression.KalmanFilter(F, H, Q, R, P_initial, x_initial);
this.kalman = new util_regression.KalmanFilter(F, H, Q, R, P_initial, x_initial);
}
/**
* Add given data from eyes
* @param {Object} eyes - eyes where extract data to add
* @param {Object} screenPos - The current screen point
* @param {Object} type - The type of performed action
*/
reg.RidgeRegThreaded.prototype.addData = function(eyes, screenPos, type) {
reg.RidgeRegThreaded.prototype.addData = function (eyes, screenPos, type) {
if (!eyes) {
return;
}
//not doing anything with blink at present
// if (eyes.left.blink || eyes.right.blink) {
// return;
// }
this.worker.postMessage({'eyes':util.getEyeFeats(eyes), 'screenPos':screenPos, 'type':type});
this.worker.postMessage({ 'eyes': util.getEyeFeats(eyes), 'screenPos': screenPos, 'type': type });
};

/**
Expand All @@ -102,7 +102,7 @@ reg.RidgeRegThreaded.prototype.addData = function(eyes, screenPos, type) {
* @param {Object} eyesObj - The current user eyes object
* @returns {Object}
*/
reg.RidgeRegThreaded.prototype.predict = function(eyesObj) {
reg.RidgeRegThreaded.prototype.predict = function (eyesObj) {
// console.log('LOGGING..');
if (!eyesObj) {
return null;
Expand All @@ -112,7 +112,7 @@ reg.RidgeRegThreaded.prototype.predict = function(eyesObj) {

var eyeFeats = util.getEyeFeats(eyesObj);
var predictedX = 0, predictedY = 0;
for(var i=0; i< eyeFeats.length; i++){
for (var i = 0; i < eyeFeats.length; i++) {
predictedX += eyeFeats[i] * coefficientsX[i];
predictedY += eyeFeats[i] * coefficientsY[i];
}
Expand Down Expand Up @@ -148,7 +148,7 @@ reg.RidgeRegThreaded.prototype.setData = util_regression.setData
* Return the data
* @returns {Array.<Object>|*}
*/
reg.RidgeRegThreaded.prototype.getData = function() {
reg.RidgeRegThreaded.prototype.getData = function () {
return this.dataClicks.data;
};

Expand Down
20 changes: 10 additions & 10 deletions src/ridgeWeightedReg.mjs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import util from './util';
import util_regression from './util_regression';
import params from './params'
import util from './util.mjs';
import util_regression from './util_regression.mjs';
import params from './params.mjs';

const reg = {};

/**
* Constructor of RidgeWeightedReg object
* @constructor
*/
reg.RidgeWeightedReg = function() {
reg.RidgeWeightedReg = function () {
this.init();
};

Expand All @@ -31,7 +31,7 @@ reg.RidgeWeightedReg.prototype.addData = util_regression.addData
* @param {Object} eyesObj - The current user eyes object
* @returns {Object}
*/
reg.RidgeWeightedReg.prototype.predict = function(eyesObj) {
reg.RidgeWeightedReg.prototype.predict = function (eyesObj) {
if (!eyesObj || this.eyeFeaturesClicks.length === 0) {
return null;
}
Expand All @@ -52,12 +52,12 @@ reg.RidgeWeightedReg.prototype.predict = function(eyesObj) {
var weightedXArray = Array(len);
var weightedYArray = Array(len);
for (var i = 0; i < len; i++) {
var weight = Math.sqrt( 1 / (len - i) ); // access from oldest to newest so should start with low weight and increase steadily
var weight = Math.sqrt(1 / (len - i)); // access from oldest to newest so should start with low weight and increase steadily
//abstraction is leaking...
var trueIndex = this.eyeFeaturesClicks.getTrueIndex(i);
for (var j = 0; j < this.eyeFeaturesClicks.data[trueIndex].length; j++) {
var val = this.eyeFeaturesClicks.data[trueIndex][j] * weight;
if (weightedEyeFeats[trueIndex] !== undefined){
if (weightedEyeFeats[trueIndex] !== undefined) {
weightedEyeFeats[trueIndex].push(val);
} else {
weightedEyeFeats[trueIndex] = [val];
Expand All @@ -78,11 +78,11 @@ reg.RidgeWeightedReg.prototype.predict = function(eyesObj) {

var eyeFeats = util.getEyeFeats(eyesObj);
var predictedX = 0;
for(var i=0; i< eyeFeats.length; i++){
for (var i = 0; i < eyeFeats.length; i++) {
predictedX += eyeFeats[i] * coefficientsX[i];
}
var predictedY = 0;
for(var i=0; i< eyeFeats.length; i++){
for (var i = 0; i < eyeFeats.length; i++) {
predictedY += eyeFeats[i] * coefficientsY[i];
}

Expand Down Expand Up @@ -112,7 +112,7 @@ reg.RidgeWeightedReg.prototype.setData = util_regression.setData;
* Return the data
* @returns {Array.<Object>|*}
*/
reg.RidgeWeightedReg.prototype.getData = function() {
reg.RidgeWeightedReg.prototype.getData = function () {
return this.dataClicks.data;
};

Expand Down
Loading