Created
January 13, 2018 13:27
-
-
Save ArvinH/f10ea345904d09f51aebb3f1e34e95e5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.log('popup ready!'); | |
let startPredicting = false; | |
let times = 0; | |
let y = 0; | |
document.addEventListener('DOMContentLoaded', function() { | |
let video = document.getElementById('video'); | |
let still = document.getElementById('still'); | |
let up = document.getElementById('up'); | |
let down = document.getElementById('down'); | |
let turnoffButton = document.getElementById('turnoff'); | |
let predict = document.getElementById('predict'); | |
let knn = new p5ml.KNNImageClassifier(modelLoaded); | |
// Still | |
still.addEventListener('click', function() { | |
knn.addImage(video, 1); | |
times++; | |
}) | |
// Up | |
up.addEventListener('click', function() { | |
knn.addImage(video, 2); | |
times++; | |
}) | |
// Down | |
down.addEventListener('click', function() { | |
knn.addImage(video, 3); | |
times++; | |
}) | |
// turnoffButton | |
turnoffButton.addEventListener('click', function () { | |
knn.addImage(video, 4); | |
times++; | |
}) | |
setInterval(function() { | |
if (times > 10) { | |
knn.predict(video, function(data) { | |
if (data.classIndex == 1) { | |
console.log('response', 'still'); | |
} else if (data.classIndex == 2) { | |
chrome.runtime.sendMessage({ direction: "up" }, function (response) { | |
console.log('response', 'up'); | |
}); | |
} else if (data.classIndex == 3) { | |
chrome.runtime.sendMessage({ direction: "down" }, function (response) { | |
console.log('response', 'down'); | |
}); | |
} else if (data.classIndex == 4) { | |
chrome.runtime.sendMessage({ direction: "turn off" }, function (response) { | |
console.log('response', 'turn off'); | |
}); | |
} | |
}); | |
} | |
}, 1500); | |
navigator.getUserMedia = navigator.getUserMedia; | |
if (navigator.getUserMedia) { | |
navigator.getUserMedia({ audio: false, video: true }, | |
function(stream) { | |
video.srcObject = stream; | |
video.onloadedmetadata = function(e) { | |
video.play(); | |
}; | |
}, | |
function(err) { | |
console.log("The following error occurred: " + err.name); | |
} | |
); | |
} else { | |
console.log("getUserMedia not supported"); | |
} | |
}) | |
function modelLoaded() { | |
console.log('Model Loaded') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment