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

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more