-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathclassify-camera.js
More file actions
120 lines (102 loc) · 4.44 KB
/
classify-camera.js
File metadata and controls
120 lines (102 loc) · 4.44 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
const { ImageClassifier, LinuxImpulseRunner, Ffmpeg, Imagesnap, RunnerHelloHasAnomaly } = require('../../build/library');
(async () => {
try {
// Required arguments:
// arg 2: Path to the model file. e.g. /tmp/model.eim
// arg 3: Name of the camera device, see output of `gst-device-monitor-1.0`. e.g. "HD Pro Webcam C920"
// Optional arguments:
// arg 4: desired FPS. e.g. 20, default 5
// arg 5: desired capture width. e.g. 320, default 640
// arg 6: desired capture height. e.g. 200, default 480
const argModelFile = process.argv[2];
const argCamDevice = process.argv[3];
const fps = process.argv[4] ? Number(process.argv[4]) : 5;
const dimensions = (process.argv[5] && process.argv[6]) ? {
width: Number(process.argv[5]),
height: Number(process.argv[6])
} : {
width: 640,
height: 480
};
if (!argModelFile) {
console.log('Missing one argument (model file)');
process.exit(1);
}
let runner = new LinuxImpulseRunner(argModelFile);
let model = await runner.init();
const hasVisualAd = [
RunnerHelloHasAnomaly.VisualGMM,
RunnerHelloHasAnomaly.VisualPatchcore,
RunnerHelloHasAnomaly.VisualCustom
].includes(model.modelParameters.has_anomaly);
let labels = model.modelParameters.labels;
if (hasVisualAd) {
labels.push('anomaly');
}
console.log('Starting the image classifier for',
model.project.owner + ' / ' + model.project.name, '(v' + model.project.deploy_version + ')');
console.log('Parameters',
'image size', model.modelParameters.image_input_width + 'x' + model.modelParameters.image_input_height + ' px (' +
model.modelParameters.image_channel_count + ' channels)',
'classes', labels);
// select a camera... you can implement this interface for other targets :-)
let camera;
if (process.platform === 'darwin') {
camera = new Imagesnap();
}
else if (process.platform === 'linux') {
camera = new Ffmpeg(false /* verbose */);
}
else {
throw new Error('Unsupported platform "' + process.platform + '"');
}
await camera.init();
const devices = await camera.listDevices();
if (devices.length === 0) {
throw new Error('Cannot find any webcams');
}
if (devices.length > 1 && !argCamDevice) {
throw new Error('Multiple cameras found (' + devices.map(n => '"' + n + '"').join(', ') + '), add ' +
'the camera to use to this script (node classify-camera.js model.eim cameraname)');
}
let device = argCamDevice || devices[0];
console.log('Using camera', device, 'starting...');
await camera.start({
device: device,
intervalMs: 1000 / fps,
dimensions: dimensions,
inferenceDimensions: {
width: model.modelParameters.image_input_width,
height: model.modelParameters.image_input_height,
resizeMode: model.modelParameters.image_resize_mode || 'none',
},
});
camera.on('error', error => {
console.log('camera error', error);
});
console.log('Connected to camera');
let imageClassifier = new ImageClassifier(runner, camera);
await imageClassifier.start();
imageClassifier.on('result', (ev, timeMs, imgAsJpg) => {
if (ev.result.classification) {
// print the raw predicted values for this frame
// (turn into string here so the content does not jump around)
let c = ev.result.classification;
for (let k of Object.keys(c)) {
c[k] = c[k].toFixed(4);
}
console.log('classification', timeMs + 'ms.', c);
}
if (ev.result.bounding_boxes) {
console.log('boundingBoxes', timeMs + 'ms.', JSON.stringify(ev.result.bounding_boxes));
}
if (ev.result.visual_anomaly_grid) {
console.log('visual anomalies', timeMs + 'ms.', JSON.stringify(ev.result.visual_anomaly_grid));
}
});
}
catch (ex) {
console.error(ex);
process.exit(1);
}
})();