-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.js
60 lines (53 loc) · 1.42 KB
/
model.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
54
55
56
57
58
59
60
const models = require("@cloud-annotations/models-node-gpu");
const fs = require("fs");
module.exports = (RED) => {
function ModelNode(config) {
RED.nodes.createNode(this, config);
this.path = config.path;
const node = this;
node.status({ fill: "grey", shape: "ring", text: "loading..." });
let model;
models
.load(node.path)
.then((m) => {
model = m;
if (model.type === "detection") {
node.status({
fill: "green",
shape: "dot",
text: "object detection",
});
} else {
node.status({
fill: "green",
shape: "dot",
text: "classification",
});
}
})
.catch(() => {
node.status({
fill: "red",
shape: "dot",
text: "failed to load model",
});
});
node.on("input", async (msg) => {
let image = msg.payload;
// If image is a string assume it is a filepath.
if (typeof image === "string") {
image = fs.readFileSync(msg.payload);
}
if (model.type === "detection") {
const results = await model.detect(image);
msg.payload = results;
node.send(msg);
} else {
const results = await model.classify(image);
msg.payload = results;
node.send(msg);
}
});
}
RED.nodes.registerType("model-gpu", ModelNode);
};