Insert cell
Insert cell
Insert cell
mobilenet = await tf.loadModel('https://storage.googleapis.com/tfjs-models/tfjs/mobilenet_v1_0.25_224/model.json')
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
predict = (imgElement) => tf.tidy(() => {
// tf.fromPixels() returns a Tensor from an image element.
const raw = tf.fromPixels(imgElement).toFloat();
const cropped = cropImage(raw);
const resized = tf.image.resizeBilinear(cropped, [IMAGE_SIZE, IMAGE_SIZE])

// Normalize the image from [0, 255] to [-1, 1].
const offset = tf.scalar(127);
const normalized = resized.sub(offset).div(offset);

// Reshape to a single-element batch so we can pass it to predict.
const batched = normalized.expandDims(0);

// Make a prediction through mobilenet.
return mobilenet.predict(batched).dataSync();
})
Insert cell
logits = {
if (webcam) {
while(true) {
yield Promises.delay(500, predict(input));
}
} else {
yield predict(input);
}
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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