Skip to content

Instantly share code, notes, and snippets.

@marcelcaraciolo
Last active August 29, 2015 14:00
Show Gist options
  • Save marcelcaraciolo/a922a259b23470fce171 to your computer and use it in GitHub Desktop.
Save marcelcaraciolo/a922a259b23470fce171 to your computer and use it in GitHub Desktop.
var express = require('express')
, routes = require('./routes')
, http = require('http')
, path = require('path')
, fs = require('fs')
, uuid = require('uuid')
, yhat = require('yhat');
var app = express();
var yh = yhat.init("your username", "your apikey");
$("#predict").click(function(e) {
// convert canvas to an image url
var img = canvas.toDataURL();
// make request to server
$.post("/predict", {img: img, n: n}, function(d) {
// remove any pre-existing results
$("#results").children().remove()
// add the results to the page
var rows = ["<tr><td>Digit</td><td>Probability</td></tr>"]
for (var i = 0; i < 10; i++) {
row = "<tr><td>" + i + "</td>" + "<td>" + d.prediction.probs['prob_' + i] + "</td></tr>";
if (i==d.prediction.label & n==i) {
row = "<tr style='background-color: green;'><td>" + i + "</td>" + "<td>" + d.prediction.probs['prob_' + i] + "</td></tr>"
} else if (i==n) {
row = "<tr style='background-color: red;'><td>" + i + "</td>" + "<td>" + d.prediction.probs['prob_' + i] + "</td></tr>"
} else {
row = "<tr><td>" + i + "</td>" + "<td>" + d.prediction.probs['prob_' + i] + "</td></tr>"
}
rows.push(row);
}
});
return false;
});
app.post('/predict', function(req, res) {
// grab image from request
var img = req.body.img;
// convert into a string
var data = img.replace(/^data:image\/\w+;base64,/, "");
// make request to Yhat
yh.predict("DigitRecognizer", 6, {data: data}, function(data) {
// respond w/ JSON
res.send(data);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment