Skip to content

Instantly share code, notes, and snippets.

@timothyclemans
Created April 28, 2014 04:15
Show Gist options
  • Save timothyclemans/11361630 to your computer and use it in GitHub Desktop.
Save timothyclemans/11361630 to your computer and use it in GitHub Desktop.
[wearscript] facical
<html style="width:100%; height:100%; overflow:hidden">
<body style="width:100%; height:100%; overflow:hidden; margin:0" bgcolor="#000">
<script type="text/javascript" src="http://yourjavascript.com/27943214444/ccv.js"></script>
<script type="text/javascript" src="http://yourjavascript.com/47473214320/face.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<p id="data" style="color: red; font-size: 30px; width: 100px"></p>
<canvas id="output"></canvas>
<script>
WS.log("starting");
image = document.createElement('img');
var canvas = document.getElementById("output");
var ctx = canvas.getContext("2d");
function tap() {
WS.say('scanning');
ctx.clear();
$("#data").html("");
scan();
}
WS.gestureCallback('onGestureTAP', function () {
tap();
});
WS.gestureCallback('onEyeGesture', function () {
tap();
});
function scan(){
WS.cameraOn(1.0, 10, 10, function (x) {
WS.log("got picture")
image.src = 'data:image/jpg;base64,' + x;
ctx.drawImage(image, 0, 0);
var faces = detect_faces();
if(faces.length == 0 ) { /*ctx.clear()*/ }
else{
WS.log(faces.length + " faces detected");
WS.say("face detected")
WS.cameraOff();
var confidence = faces[0].confidence;
WS.log(JSON.stringify(confidence))
WS.log(x);
sendImage(x);
}
});
}
function detect_faces(){
return ccv.detect_objects({ "canvas" : ccv.grayscale(ccv.pre(image)),
"cascade" : cascade,
"interval" : 5,
"min_neighbors" : 1 });
}
function sendImage(x){
$.ajax({
type: "POST",
url: "http://rekognition.com/func/api/",
data: {api_key: "puzGW5VLbIupO0GD", api_secret: "6tGeeSLM55hgyTWE", jobs: "face_recognize", base64: x},
success: function(data){
try {
var matches = data.face_detection[0].matches;
}
catch(e) {
addName(x);
}
if (matches != undefined) {
WS.log(JSON.stringify(matches));
if (parseFloat(matches[0].score) < 0.7){ addName(x) };
var stringData = JSON.stringify(matches);
WS.log(stringData);
$("#data").html(stringData);
} else {
addName(x);
}
}
}).fail(function(){ WS.log("failure")});
}
function trainFaces(){
$.ajax({
type: "POST",
url: "http://rekognition.com/func/api/",
data: {api_key: "puzGW5VLbIupO0GD", api_secret: "6tGeeSLM55hgyTWE", jobs: "face_train"},
success: function(data){ WS.say("face trained") }
});
}
function addName(x){
WS.say("add name");
WS.speechRecognize('Add name', function speech(data) {
addFace(data.replace(/ /g, "_").toLowerCase(), x);
});
}
function addFace(tag, x){
$.ajax({
type: "POST",
url: "http://rekognition.com/func/api/",
data: {api_key: "puzGW5VLbIupO0GD", api_secret: "6tGeeSLM55hgyTWE", jobs: "face_add", base64: x, tag: tag},
success: function(data){ WS.say("name added"); trainFaces() }
});
}
CanvasRenderingContext2D.prototype.clear =
CanvasRenderingContext2D.prototype.clear || function (preserveTransform) {
if (preserveTransform) {
this.save();
this.setTransform(1, 0, 0, 1, 0, 0);
}
this.clearRect(0, 0, this.canvas.width, this.canvas.height);
if (preserveTransform) {
this.restore();
}
};
</script>
</body>
</html>
{"name":"Example"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment