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