forked from NikValdez/image-classifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
53 lines (35 loc) · 1.23 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const classifier = knnClassifier.create()
const webcamElement = document.getElementById("webcam")
let net
async function app() {
console.log("Loading mobilnet...")
net = await mobilenet.load()
console.log("Loaded model")
const webcam = await tf.data.webcam(webcamElement)
const addExample = async (classId) => {
const img = await webcam.capture()
const activation = net.infer(img, true)
classifier.addExample(activation, classId)
img.dispose()
}
document.getElementById("dog").addEventListener("click", () => addExample(0))
document.getElementById("cat").addEventListener("click", () => addExample(1))
document
.getElementById("snake")
.addEventListener("click", () => addExample(2))
while (true) {
if (classifier.getNumClasses() > 0) {
const img = await webcam.capture()
const activation = net.infer(img, "conv_preds")
const result = await classifier.predictClass(activation)
const classes = ["Dog", "Cat", "Snake"]
document.getElementById("console").innerText = `
prediction: ${classes[result.label]}\n
probabilty: ${result.confidences[result.label]}
`
img.dispose()
}
await tf.nextFrame()
}
}
app()