Skip to content

Commit cd97b52

Browse files
authored
Add files via upload
1 parent 4814e01 commit cd97b52

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

index.html

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<html>
2+
<head>
3+
<!-- Load the latest version of TensorFlow.js -->
4+
<script src="https://unpkg.com/@tensorflow/tfjs"></script>
5+
<script src="https://unpkg.com/@tensorflow-models/mobilenet"></script>
6+
<script src="https://unpkg.com/@tensorflow-models/knn-classifier"></script>
7+
8+
</head>
9+
<body>
10+
<div id="console"></div>
11+
<!-- Add an image that we will use to test -->
12+
<video autoplay playsinline muted id="webcam" width="224" height="224"></video>
13+
<button id="class-a">Add A</button>
14+
<button id="class-b">Add B</button>
15+
<button id="class-c">Add C</button>
16+
<!-- Load index.js after the content of the page -->
17+
<script src="index.js"></script>
18+
</body>
19+
</html>

index.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
const classifier = knnClassifier.create();
2+
3+
const webcamElement = document.getElementById('webcam');
4+
5+
let net;
6+
7+
async function app() {
8+
console.log('Loading mobilenet..');
9+
10+
// Load the model.
11+
net = await mobilenet.load();
12+
console.log('Successfully loaded model');
13+
14+
// Create an object from Tensorflow.js data API which could capture image
15+
// from the web camera as Tensor.
16+
const webcam = await tf.data.webcam(webcamElement);
17+
18+
// Reads an image from the webcam and associates it with a specific class
19+
// index.
20+
const addExample = async classId => {
21+
// Capture an image from the web camera.
22+
const img = await webcam.capture();
23+
24+
// Get the intermediate activation of MobileNet 'conv_preds' and pass that
25+
// to the KNN classifier.
26+
const activation = net.infer(img, 'conv_preds');
27+
28+
// Pass the intermediate activation to the classifier.
29+
classifier.addExample(activation, classId);
30+
31+
// Dispose the tensor to release the memory.
32+
img.dispose();
33+
};
34+
35+
// When clicking a button, add an example for that class.
36+
document.getElementById('class-a').addEventListener('click', () => addExample(0));
37+
document.getElementById('class-b').addEventListener('click', () => addExample(1));
38+
document.getElementById('class-c').addEventListener('click', () => addExample(2));
39+
40+
while (true) {
41+
if (classifier.getNumClasses() > 0) {
42+
const img = await webcam.capture();
43+
44+
// Get the activation from mobilenet from the webcam.
45+
const activation = net.infer(img, 'conv_preds');
46+
// Get the most likely class and confidences from the classifier module.
47+
const result = await classifier.predictClass(activation);
48+
49+
const classes = ['A', 'B', 'C'];
50+
document.getElementById('console').innerText = `
51+
prediction: ${classes[result.label]}\n
52+
probability: ${result.confidences[result.label]}
53+
`;
54+
55+
// Dispose the tensor to release the memory.
56+
img.dispose();
57+
}
58+
59+
await tf.nextFrame();
60+
}
61+
}
62+
63+
app();

0 commit comments

Comments
 (0)