Final Step
All that's left to do is call the initializer functions at the bottom of index.js:
1
initializeNNC();
2
initializeMBS();
Copied!
The end result should look like:
1
import lampix from '@lampix/core';
2
3
import './styles.css';
4
import handleObjectClassified from './handleObjectClassified';
5
6
const initializeNNC = () => {
7
const nncElement = document.getElementsByClassName('nnc')[0];
8
const nncBounds = nncElement.getBoundingClientRect();
9
const nncRecognizedClassElement = document.getElementsByClassName('nnc-recognized-class')[0];
10
11
// All Lampix classifiers return a list of recognized objects
12
// NNClassifier only recognizes one at a time, hence expecting
13
// an array with one element and destructuring it
14
const nncCallback = ([recognizedObject]) => {
15
nncRecognizedClassElement.textContent = `Recognized: ${recognizedObject.classTag}`;
16
17
if (Number(recognizedObject.classTag) === 1) {
18
nncElement.style.borderColor = '#FF0000';
19
} else {
20
// Go back to white if object no longer there
21
nncElement.style.borderColor = '#FFFFFF';
22
}
23
};
24
25
const nncFruitsWatcher = {
26
name: 'NeuralNetworkClassifier',
27
shape: lampix.helpers.rectangle(
28
nncBounds.left,
29
nncBounds.top,
30
nncBounds.width,
31
nncBounds.height
32
),
33
params: {
34
neural_network_name: 'fruits'
35
},
36
onClassification: nncCallback
37
};
38
39
lampix.watchers.add(nncFruitsWatcher);
40
};
41
42
const initializeMBS = () => {
43
const mbsElement = document.getElementsByClassName('mbs')[0];
44
const mbsBounds = mbsElement.getBoundingClientRect();
45
46
const onClassification = (classifiedObjects) => classifiedObjects.forEach((classifiedObject) => {
47
handleObjectClassified(classifiedObject, '#FFFFFF');
48
});
49
50
const onLocation = (locatedObjects) => {
51
// This step fires before onClassification!
52
console.log(locatedObjects);
53
};
54
55
const mbsFruitsWatcher = {
56
name: 'MovementBasedSegmenter',
57
shape: lampix.helpers.rectangle(
58
mbsBounds.left,
59
mbsBounds.top,
60
mbsBounds.width,
61
mbsBounds.height
62
),
63
params: {
64
neural_network_name: 'fruits'
65
},
66
onLocation,
67
onClassification
68
};
69
70
lampix.watchers.add(mbsFruitsWatcher);
71
};
72
73
initializeNNC();
74
initializeMBS();
Copied!
Minus the next, optional step, this is the exact application in the fruits example on our GitHub. If you're having trouble with this guide, see if the source code can help you out.
Last modified 3yr ago
Copy link