Published
Edited
Jun 13, 2018
9 stars
Insert cell
Insert cell
ml5 = require("ml5")
Insert cell
Insert cell
Insert cell
d3 = require("d3")
Insert cell
import {text, file} from "@jashkenas/inputs"
Insert cell
Insert cell
classifier = ml5.imageClassifier('Mobilenet')
Insert cell
Insert cell
Insert cell
image = new Promise(function(resolve, reject) {
var image = new Image();
image.src = URL.createObjectURL(theFile);
image.crossOrigin = "anonymous";
image.onload = () => resolve(image);
image.onerror = reject;
})
Insert cell
Insert cell
output = new Promise(resolve => classifier.predict(image, resolve))
Insert cell
Insert cell
result = output[0].className
Insert cell
probability = d3.format(".1%")(output[0].probability)
Insert cell
md`## I guess this is a ${result}. My confidence is ${probability}.`
Insert cell
Insert cell
table = {
// Set up the header, using the keys from above
let header = "Object" + "|" + "Probability" + "\n-|-\n"
// Create an empty body, ready to be filled by the loop
let body = ""
// Loop over all the things in the output object
for (let item of output) {
let line = ""; // empty line, ready to be added to
line += item[Object.keys(output[0])[0]]; // Add the object
line += "|"; // Add a pipe spacer
line += d3.format(".1%")(item[Object.keys(output[0])[1]]); // Add the formatted probability
line += "\n"; // Add newline character
body += line; // Add the line we just created to the body
}
// Then glue the header to the body and return it in markdown format.
return md`${header + body}`
}
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more