Public
Edited
Nov 8, 2022
1 fork
9 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
ssdMobileNetPath = "https://tfhub.dev/tensorflow/tfjs-model/ssd_mobilenet_v2/1/default/1"
Insert cell
Insert cell
ssdMobileNet = tf.loadGraphModel(ssdMobileNetPath, {fromTFHub: true})
Insert cell
Insert cell
ssdMobileNet.inputs[0].shape
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
{
let count = 0;
cocoClasses.forEach(element => {
if (element === "-") {
count++;
}
});
return count;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
img = new Promise((resolve, reject) => {
const img = htl.html`<img/>`;
img.src = imgUrl;
img.crossOrigin = 'anonymous';
img.onload = () => resolve(img);
img.onerror = reject;
})
Insert cell
Insert cell
imageTensor = tf.browser.fromPixels(img)
Insert cell
imageTensor.shape
Insert cell
Insert cell
ssdMobileNet.predict(imageTensor.expandDims()) // Oops!
Insert cell
Insert cell
detections = ssdMobileNet.executeAsync(imageTensor.expandDims())
Insert cell
Insert cell
Insert cell
detections[0].shape
Insert cell
Insert cell
detections[1].shape
Insert cell
Insert cell
boxes = detections[1].squeeze().array()
Insert cell
Insert cell
Insert cell
Insert cell
detectionsTopClass = detections[0].topk()
Insert cell
Insert cell
topIndices = detectionsTopClass.indices.data()
Insert cell
Insert cell
topValues = detectionsTopClass.values.data()
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
nmsSelections = tf.image.nonMaxSuppressionWithScoreAsync(
detections[1].squeeze(), // box coordinates
detectionsTopClass.values.squeeze(), // box object probabilities
detectionConstraintsNms.maxBoxes,
detectionConstraintsNms.iouThreshold,
detectionConstraintsNms.confidenceThreshold,
detectionConstraintsNms.softNMS
)
Insert cell
selections = nmsSelections.selectedIndices.data()
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
webcamContainer = createWebcamContainer()
Insert cell
detectObjectsInWebcam(activateWebcam, webcamContainer, ssdMobileNet, cocoClasses, detectionConstraintsVideo)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
tf = require('@tensorflow/tfjs@4.0.0/dist/tf.min.js')
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